├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── gulpfile.js ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | end_of_line = lf 6 | charset = utf-8 7 | indent_size = 2 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [{**/{actual,fixtures,expected,templates}/**,*.md}] 12 | trim_trailing_whitespace = false 13 | insert_final_newline = false -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "ecmaFeatures": { 3 | "modules": true, 4 | "experimentalObjectRestSpread": true 5 | }, 6 | 7 | "env": { 8 | "browser": false, 9 | "es6": true, 10 | "node": true, 11 | "mocha": true 12 | }, 13 | 14 | "globals": { 15 | "document": false, 16 | "navigator": false, 17 | "window": false 18 | }, 19 | 20 | "rules": { 21 | "accessor-pairs": 2, 22 | "arrow-spacing": [2, { "before": true, "after": true }], 23 | "block-spacing": [2, "always"], 24 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 25 | "comma-dangle": [2, "never"], 26 | "comma-spacing": [2, { "before": false, "after": true }], 27 | "comma-style": [2, "last"], 28 | "constructor-super": 2, 29 | "curly": [2, "multi-line"], 30 | "dot-location": [2, "property"], 31 | "eol-last": 2, 32 | "eqeqeq": [2, "allow-null"], 33 | "generator-star-spacing": [2, { "before": true, "after": true }], 34 | "handle-callback-err": [2, "^(err|error)$" ], 35 | "indent": [2, 2, { "SwitchCase": 1 }], 36 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 37 | "keyword-spacing": [2, { "before": true, "after": true }], 38 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 39 | "new-parens": 2, 40 | "no-array-constructor": 2, 41 | "no-caller": 2, 42 | "no-class-assign": 2, 43 | "no-cond-assign": 2, 44 | "no-const-assign": 2, 45 | "no-control-regex": 2, 46 | "no-debugger": 2, 47 | "no-delete-var": 2, 48 | "no-dupe-args": 2, 49 | "no-dupe-class-members": 2, 50 | "no-dupe-keys": 2, 51 | "no-duplicate-case": 2, 52 | "no-empty-character-class": 2, 53 | "no-eval": 2, 54 | "no-ex-assign": 2, 55 | "no-extend-native": 2, 56 | "no-extra-bind": 2, 57 | "no-extra-boolean-cast": 2, 58 | "no-extra-parens": [2, "functions"], 59 | "no-fallthrough": 2, 60 | "no-floating-decimal": 2, 61 | "no-func-assign": 2, 62 | "no-implied-eval": 2, 63 | "no-inner-declarations": [2, "functions"], 64 | "no-invalid-regexp": 2, 65 | "no-irregular-whitespace": 2, 66 | "no-iterator": 2, 67 | "no-label-var": 2, 68 | "no-labels": 2, 69 | "no-lone-blocks": 2, 70 | "no-mixed-spaces-and-tabs": 2, 71 | "no-multi-spaces": 2, 72 | "no-multi-str": 2, 73 | "no-multiple-empty-lines": [2, { "max": 1 }], 74 | "no-native-reassign": 0, 75 | "no-negated-in-lhs": 2, 76 | "no-new": 2, 77 | "no-new-func": 2, 78 | "no-new-object": 2, 79 | "no-new-require": 2, 80 | "no-new-wrappers": 2, 81 | "no-obj-calls": 2, 82 | "no-octal": 2, 83 | "no-octal-escape": 2, 84 | "no-proto": 0, 85 | "no-redeclare": 2, 86 | "no-regex-spaces": 2, 87 | "no-return-assign": 2, 88 | "no-self-compare": 2, 89 | "no-sequences": 2, 90 | "no-shadow-restricted-names": 2, 91 | "no-spaced-func": 2, 92 | "no-sparse-arrays": 2, 93 | "no-this-before-super": 2, 94 | "no-throw-literal": 2, 95 | "no-trailing-spaces": 0, 96 | "no-undef": 2, 97 | "no-undef-init": 2, 98 | "no-unexpected-multiline": 2, 99 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 100 | "no-unreachable": 2, 101 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 102 | "no-useless-call": 0, 103 | "no-with": 2, 104 | "one-var": [0, { "initialized": "never" }], 105 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 106 | "padded-blocks": [0, "never"], 107 | "quotes": [2, "single", "avoid-escape"], 108 | "radix": 2, 109 | "semi": [2, "always"], 110 | "semi-spacing": [2, { "before": false, "after": true }], 111 | "space-before-blocks": [2, "always"], 112 | "space-before-function-paren": [2, "never"], 113 | "space-in-parens": [2, "never"], 114 | "space-infix-ops": 2, 115 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 116 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 117 | "use-isnan": 2, 118 | "valid-typeof": 2, 119 | "wrap-iife": [2, "any"], 120 | "yoda": [2, "never"] 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | *.sublime-* 4 | 5 | # test related, or directories generated by tests 6 | test/actual 7 | actual 8 | coverage 9 | .nyc* 10 | 11 | # npm 12 | node_modules 13 | npm-debug.log 14 | 15 | # yarn 16 | yarn.lock 17 | yarn-error.log 18 | 19 | # misc 20 | _gh_pages 21 | _draft 22 | _drafts 23 | bower_components 24 | vendor 25 | temp 26 | tmp 27 | TODO.md 28 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | os: 3 | - linux 4 | - osx 5 | language: node_js 6 | node_js: 7 | - node 8 | - '7' 9 | - '6' 10 | - '5' 11 | - '4' 12 | - '0.12' 13 | - '0.10' 14 | matrix: 15 | allow_failures: [] 16 | fast_finish: true 17 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | 3 | ```js 4 | var visit = require('{%= name %}'); 5 | 6 | var ctx = { 7 | data: {}, 8 | set: function (key, value) { 9 | if (typeof key === 'object') { 10 | visit(ctx, 'set', key); 11 | } else { 12 | ctx.data[key] = value; 13 | } 14 | } 15 | }; 16 | 17 | ctx.set('a', 'a'); 18 | ctx.set('b', 'b'); 19 | ctx.set('c', 'c'); 20 | ctx.set({d: {e: 'f'}}); 21 | 22 | console.log(ctx.data); 23 | //=> {a: 'a', b: 'b', c: 'c', d: { e: 'f' }}; 24 | ``` 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2016, Jon Schlinkert 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # collection-visit [![NPM version](https://img.shields.io/npm/v/collection-visit.svg?style=flat)](https://www.npmjs.com/package/collection-visit) [![NPM monthly downloads](https://img.shields.io/npm/dm/collection-visit.svg?style=flat)](https://npmjs.org/package/collection-visit) [![NPM total downloads](https://img.shields.io/npm/dt/collection-visit.svg?style=flat)](https://npmjs.org/package/collection-visit) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/collection-visit.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/collection-visit) 2 | 3 | > Visit a method over the items in an object, or map visit over the objects in an array. 4 | 5 | ## Install 6 | 7 | Install with [npm](https://www.npmjs.com/): 8 | 9 | ```sh 10 | $ npm install --save collection-visit 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | var visit = require('collection-visit'); 17 | 18 | var ctx = { 19 | data: {}, 20 | set: function (key, value) { 21 | if (typeof key === 'object') { 22 | visit(ctx, 'set', key); 23 | } else { 24 | ctx.data[key] = value; 25 | } 26 | } 27 | }; 28 | 29 | ctx.set('a', 'a'); 30 | ctx.set('b', 'b'); 31 | ctx.set('c', 'c'); 32 | ctx.set({d: {e: 'f'}}); 33 | 34 | console.log(ctx.data); 35 | //=> {a: 'a', b: 'b', c: 'c', d: { e: 'f' }}; 36 | ``` 37 | 38 | ## About 39 | 40 | ### Related projects 41 | 42 | * [base-methods](https://www.npmjs.com/package/base-methods): base-methods is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting… [more](https://github.com/jonschlinkert/base-methods) | [homepage](https://github.com/jonschlinkert/base-methods "base-methods is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting with a handful of common methods, like `set`, `get`, `del` and `use`.") 43 | * [map-visit](https://www.npmjs.com/package/map-visit): Map `visit` over an array of objects. | [homepage](https://github.com/jonschlinkert/map-visit "Map `visit` over an array of objects.") 44 | * [object-visit](https://www.npmjs.com/package/object-visit): Call a specified method on each value in the given object. | [homepage](https://github.com/jonschlinkert/object-visit "Call a specified method on each value in the given object.") 45 | 46 | ### Contributing 47 | 48 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 49 | 50 | ### Contributors 51 | 52 | | **Commits** | **Contributor** | 53 | | --- | --- | 54 | | 13 | [jonschlinkert](https://github.com/jonschlinkert) | 55 | | 9 | [doowb](https://github.com/doowb) | 56 | 57 | ### Building docs 58 | 59 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 60 | 61 | To generate the readme, run the following command: 62 | 63 | ```sh 64 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 65 | ``` 66 | 67 | ### Running tests 68 | 69 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: 70 | 71 | ```sh 72 | $ npm install && npm test 73 | ``` 74 | 75 | ### Author 76 | 77 | **Jon Schlinkert** 78 | 79 | * [github/jonschlinkert](https://github.com/jonschlinkert) 80 | * [twitter/jonschlinkert](https://twitter.com/jonschlinkert) 81 | 82 | ### License 83 | 84 | Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). 85 | Released under the [MIT License](LICENSE). 86 | 87 | *** 88 | 89 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.5.0, on April 09, 2017._ -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var istanbul = require('gulp-istanbul'); 4 | var eslint = require('gulp-eslint'); 5 | var mocha = require('gulp-mocha'); 6 | var gulp = require('gulp'); 7 | 8 | var lint = ['index.js', 'utils.js']; 9 | 10 | gulp.task('lint', function() { 11 | return gulp.src(lint.concat(['gulpfile.js', 'test.js'])) 12 | .pipe(eslint()) 13 | .pipe(eslint.format()); 14 | }); 15 | 16 | gulp.task('coverage', function() { 17 | return gulp.src(lint) 18 | .pipe(istanbul()) 19 | .pipe(istanbul.hookRequire()); 20 | }); 21 | 22 | gulp.task('test', ['coverage'], function() { 23 | return gulp.src('test.js') 24 | .pipe(mocha({reporter: 'spec'})) 25 | .pipe(istanbul.writeReports()) 26 | .pipe(istanbul.writeReports({ 27 | reporters: [ 'text' ], 28 | reportOpts: {dir: 'coverage', file: 'summary.txt'} 29 | })); 30 | }); 31 | 32 | gulp.task('default', ['lint', 'test']); 33 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * collection-visit 3 | * 4 | * Copyright (c) 2015, 2017, Jon Schlinkert. 5 | * Released under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | var visit = require('object-visit'); 11 | var mapVisit = require('map-visit'); 12 | 13 | module.exports = function(collection, method, val) { 14 | var result; 15 | 16 | if (typeof val === 'string' && (method in collection)) { 17 | var args = [].slice.call(arguments, 2); 18 | result = collection[method].apply(collection, args); 19 | } else if (Array.isArray(val)) { 20 | result = mapVisit.apply(null, arguments); 21 | } else { 22 | result = visit.apply(null, arguments); 23 | } 24 | 25 | if (typeof result !== 'undefined') { 26 | return result; 27 | } 28 | 29 | return collection; 30 | }; 31 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "collection-visit", 3 | "description": "Visit a method over the items in an object, or map visit over the objects in an array.", 4 | "version": "1.0.0", 5 | "homepage": "https://github.com/jonschlinkert/collection-visit", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "contributors": [ 8 | "Brian Woodward (https://twitter.com/doowb)", 9 | "Jon Schlinkert (http://twitter.com/jonschlinkert)" 10 | ], 11 | "repository": "jonschlinkert/collection-visit", 12 | "bugs": { 13 | "url": "https://github.com/jonschlinkert/collection-visit/issues" 14 | }, 15 | "license": "MIT", 16 | "files": [ 17 | "index.js" 18 | ], 19 | "main": "index.js", 20 | "engines": { 21 | "node": ">=0.10.0" 22 | }, 23 | "scripts": { 24 | "test": "mocha" 25 | }, 26 | "keywords": [ 27 | "array", 28 | "arrays", 29 | "collection", 30 | "context", 31 | "function", 32 | "helper", 33 | "invoke", 34 | "key", 35 | "map", 36 | "method", 37 | "object", 38 | "objects", 39 | "value", 40 | "visit", 41 | "visitor" 42 | ], 43 | "verb": { 44 | "related": { 45 | "list": [ 46 | "base-methods", 47 | "map-visit", 48 | "object-visit" 49 | ] 50 | }, 51 | "toc": false, 52 | "layout": "default", 53 | "tasks": [ 54 | "readme" 55 | ], 56 | "plugins": [ 57 | "gulp-format-md" 58 | ], 59 | "lint": { 60 | "reflinks": true 61 | }, 62 | "reflinks": [ 63 | "verb", 64 | "verb-generate-readme" 65 | ] 66 | }, 67 | "dependencies": { 68 | "map-visit": "^1.0.0", 69 | "object-visit": "^1.0.0" 70 | }, 71 | "devDependencies": { 72 | "clone-deep": "^0.2.4", 73 | "gulp": "^3.9.1", 74 | "gulp-eslint": "^3.0.1", 75 | "gulp-format-md": "^0.1.12", 76 | "gulp-istanbul": "^1.1.1", 77 | "gulp-mocha": "^3.0.0", 78 | "mocha": "^3.2.0" 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | var assert = require('assert'); 5 | var clone = require('clone-deep'); 6 | var visit = require('./'); 7 | var ctx; 8 | 9 | describe('collection-visit', function() { 10 | var fixture = { 11 | options: {}, 12 | data: {}, 13 | set: function(key, value) { 14 | if (typeof key !== 'string') { 15 | visit(this, 'set', key, value); 16 | } else { 17 | this.data[key] = value; 18 | } 19 | } 20 | }; 21 | 22 | beforeEach(function() { 23 | ctx = clone(fixture); 24 | }); 25 | 26 | describe('arrays', function() { 27 | describe('visit', function() { 28 | it('should call visit on every value in the given object:', function() { 29 | ctx.set('a', 'a'); 30 | ctx.set([{b: 'b'}, {c: 'c'}]); 31 | ctx.set({d: {e: 'f'}}); 32 | assert.deepEqual(ctx.data, { 33 | a: 'a', 34 | b: 'b', 35 | c: 'c', 36 | d: { e: 'f' } 37 | }); 38 | }); 39 | 40 | it('should pass the second argument to set:', function() { 41 | ctx.set(['a', 'b'], function() {}); 42 | assert.equal(typeof ctx.data.a, 'function'); 43 | assert.equal(typeof ctx.data.b, 'function'); 44 | }); 45 | 46 | it('should work when the value is a string:', function() { 47 | ctx.setName = function(str) { 48 | this.name = str; 49 | }; 50 | 51 | visit(ctx, 'setName', 'foo'); 52 | assert.equal(ctx.name, 'foo'); 53 | }); 54 | }); 55 | }); 56 | 57 | describe('objects', function() { 58 | describe('visit', function() { 59 | it('should call visit on every value in the given object:', function() { 60 | ctx.set('a', 'a'); 61 | ctx.set('b', 'b'); 62 | ctx.set('c', 'c'); 63 | ctx.set({d: {e: 'f'}}); 64 | 65 | assert.deepEqual(ctx.data, { 66 | a: 'a', 67 | b: 'b', 68 | c: 'c', 69 | d: { e: 'f' } 70 | }); 71 | }); 72 | }); 73 | }); 74 | 75 | describe('errors', function() { 76 | it('should throw an error when invalid args are passed:', function() { 77 | assert.throws(function() { 78 | visit(); 79 | }); 80 | 81 | assert.throws(function() { 82 | visit('foo', 'bar'); 83 | }); 84 | 85 | assert.throws(function() { 86 | visit({}, {}, {}); 87 | }); 88 | }); 89 | }); 90 | }); 91 | --------------------------------------------------------------------------------