[Pkg-javascript-commits] [node-map-visit] 01/07: New upstream version 1.0.0
Praveen Arimbrathodiyil
praveen at moszumanska.debian.org
Thu Jan 11 11:01:46 UTC 2018
This is an automated email from the git hooks/post-receive script.
praveen pushed a commit to branch master
in repository node-map-visit.
commit fca5beb38d19108ce06e8a966dc0a6a36b192bd5
Author: Pirate Praveen <praveen at debian.org>
Date: Wed Jan 10 20:15:33 2018 +0530
New upstream version 1.0.0
---
.gitignore | 28 ++++++++++++++++++++--------
.travis.yml | 10 ++++++----
.verb.md | 18 ++++++++++++------
README.md | 46 ++++++++++++++++++++++++++++++----------------
bower.json | 31 +++++++++++++++++++++++++++----
index.js | 30 +++++++++++++++++++++---------
package.json | 33 ++++++++++++++++++---------------
test.js | 54 ++++++++++++++++++++++++++++++++++--------------------
utils.js | 14 --------------
9 files changed, 168 insertions(+), 96 deletions(-)
diff --git a/.gitignore b/.gitignore
index 80a228c..0a16ee9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,15 +1,27 @@
+# always ignore files
*.DS_Store
*.sublime-*
-_gh_pages
-bower_components
+
+# test related, or directories generated by tests
+test/actual
+actual
+coverage
+.nyc*
+
+# npm
node_modules
npm-debug.log
-actual
-test/actual
+
+# yarn
+yarn.lock
+yarn-error.log
+
+# misc
+_gh_pages
+_draft
+_drafts
+bower_components
+vendor
temp
tmp
TODO.md
-vendor
-.idea
-benchmark
-coverage
diff --git a/.travis.yml b/.travis.yml
index 3932eaa..3b07618 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,14 +1,16 @@
sudo: false
+os:
+ - linux
+ - osx
language: node_js
node_js:
+ - node
+ - '7'
- '6'
- '5'
- '4'
- '0.12'
- '0.10'
matrix:
+ allow_failures: []
fast_finish: true
- allow_failures:
- - node_js: '4'
- - node_js: '0.10'
- - node_js: '0.12'
diff --git a/.verb.md b/.verb.md
index f37fdbe..367ab26 100644
--- a/.verb.md
+++ b/.verb.md
@@ -1,11 +1,12 @@
-{{#block "logo"}}{{/block}}
## Usage
```js
var mapVisit = require('{%= name %}');
```
-## Assign or Merge vs. Visit
+## What does this do?
+
+**Assign/Merge/Extend vs. Visit**
Let's say you want to add a `set` method to your application that will:
@@ -15,7 +16,7 @@ Let's say you want to add a `set` method to your application that will:
**Example using `extend`**
-Here is one way to accomplish this using Lo-Dash's `extend`:
+Here is one way to accomplish this using Lo-Dash's `extend` (comparable to `Object.assign`):
```js
var _ = require('lodash');
@@ -41,11 +42,16 @@ console.log(obj.data);
//=> {a: 'a', b: 'b', c: 'c', d: { e: 'f' }}
```
-The above approach works fine for most use cases. But **if you also want to emit an event** each time a property is added to the `data` object. A better approach would be to use `visit`.
+The above approach works fine for most use cases. However, **if you also want to emit an event** each time a property is added to the `data` object, or you want more control over what happens as the object is extended, a better approach would be to use `visit`.
**Example using `visit`**
-In this approach, when an array is passed to `set`, `mapVisit` calls `set` on each object in the array. When an object is passed, `visit` calls `set` on each property in the object. As a result, the `data` event will be emitted every time a property is added to `data`.
+In this approach:
+
+- when an array is passed to `set`, the `mapVisit` library calls the `set` method on each object in the array.
+- when an object is passed, `visit` calls `set` on each property in the object.
+
+As a result, the `data` event will be emitted every time a property is added to `data` (events are just an example, you can use this approach to perform any necessary logic every time the method is called).
```js
var mapVisit = require('{%= name %}');
@@ -59,7 +65,7 @@ var obj = {
} else if (typeof key === 'object') {
visit(obj, 'set', key);
} else {
- // some event-emitter
+ // simulate an event-emitter
console.log('emit', key, value);
obj.data[key] = value;
}
diff --git a/README.md b/README.md
index c4b7598..5ab02d4 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
-# map-visit [![NPM version](https://img.shields.io/npm/v/map-visit.svg?style=flat)](https://www.npmjs.com/package/map-visit) [![NPM downloads](https://img.shields.io/npm/dm/map-visit.svg?style=flat)](https://npmjs.org/package/map-visit) [![Build Status](https://img.shields.io/travis/jonschlinkert/map-visit.svg?style=flat)](https://travis-ci.org/jonschlinkert/map-visit)
+# map-visit [![NPM version](https://img.shields.io/npm/v/map-visit.svg?style=flat)](https://www.npmjs.com/package/map-visit) [![NPM monthly downloads](https://img.shields.io/npm/dm/map-visit.svg?style=flat)](https://npmjs.org/package/map-visit) [![NPM total downloads](https://img.shields.io/npm/dt/map-visit.svg?style=flat)](https://npmjs.org/package/map-visit) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/map-visit.svg?style=flat&label=Travis)](https://travis-ci.org [...]
-Map `visit` over an array of objects.
+> Map `visit` over an array of objects.
## Install
@@ -16,7 +16,9 @@ $ npm install --save map-visit
var mapVisit = require('map-visit');
```
-## Assign or Merge vs. Visit
+## What does this do?
+
+**Assign/Merge/Extend vs. Visit**
Let's say you want to add a `set` method to your application that will:
@@ -26,7 +28,7 @@ Let's say you want to add a `set` method to your application that will:
**Example using `extend`**
-Here is one way to accomplish this using Lo-Dash's `extend`:
+Here is one way to accomplish this using Lo-Dash's `extend` (comparable to `Object.assign`):
```js
var _ = require('lodash');
@@ -52,11 +54,16 @@ console.log(obj.data);
//=> {a: 'a', b: 'b', c: 'c', d: { e: 'f' }}
```
-The above approach works fine for most use cases. But **if you also want to emit an event** each time a property is added to the `data` object. A better approach would be to use `visit`.
+The above approach works fine for most use cases. However, **if you also want to emit an event** each time a property is added to the `data` object, or you want more control over what happens as the object is extended, a better approach would be to use `visit`.
**Example using `visit`**
-In this approach, when an array is passed to `set`, `mapVisit` calls `set` on each object in the array. When an object is passed, `visit` calls `set` on each property in the object. As a result, the `data` event will be emitted every time a property is added to `data`.
+In this approach:
+
+* when an array is passed to `set`, the `mapVisit` library calls the `set` method on each object in the array.
+* when an object is passed, `visit` calls `set` on each property in the object.
+
+As a result, the `data` event will be emitted every time a property is added to `data` (events are just an example, you can use this approach to perform any necessary logic every time the method is called).
```js
var mapVisit = require('map-visit');
@@ -70,7 +77,7 @@ var obj = {
} else if (typeof key === 'object') {
visit(obj, 'set', key);
} else {
- // some event-emitter
+ // simulate an event-emitter
console.log('emit', key, value);
obj.data[key] = value;
}
@@ -106,22 +113,29 @@ console.log(obj.data);
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
+### Contributors
+
+| **Commits** | **Contributor** |
+| --- | --- |
+| 15 | [jonschlinkert](https://github.com/jonschlinkert) |
+| 7 | [doowb](https://github.com/doowb) |
+
### Building docs
-_(This document was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme) (a [verb](https://github.com/verbose/verb) generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_
+_(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.)_
-To generate the readme and API documentation with [verb](https://github.com/verbose/verb):
+To generate the readme, run the following command:
```sh
-$ npm install -g verb verb-generate-readme && verb
+$ npm install -g verbose/verb#dev verb-generate-readme && verb
```
### Running tests
-Install dev dependencies:
+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:
```sh
-$ npm install -d && npm test
+$ npm install && npm test
```
### Author
@@ -129,13 +143,13 @@ $ npm install -d && npm test
**Jon Schlinkert**
* [github/jonschlinkert](https://github.com/jonschlinkert)
-* [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
+* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
### License
-Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert).
-Released under the [MIT license](https://github.com/jonschlinkert/map-visit/blob/master/LICENSE).
+Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
+Released under the [MIT License](LICENSE).
***
-_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on August 05, 2016._
\ No newline at end of file
+_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.5.0, on April 09, 2017._
\ No newline at end of file
diff --git a/bower.json b/bower.json
index b22bc98..bf6042c 100644
--- a/bower.json
+++ b/bower.json
@@ -11,12 +11,14 @@
"index.js"
],
"dependencies": {
- "lazy-cache": "^0.2.4",
- "object-visit": "^0.3.1"
+ "object-visit": "^1.0.0"
},
"devDependencies": {
- "mocha": "*",
- "should": "*"
+ "clone-deep": "^0.2.4",
+ "extend-shallow": "^2.0.1",
+ "gulp-format-md": "^0.1.12",
+ "lodash": "^4.17.4",
+ "mocha": "^3.2.0"
},
"keywords": [
"array",
@@ -32,5 +34,26 @@
"value",
"visit",
"visitor"
+ ],
+ "version": "0.1.5",
+ "bugs": {
+ "url": "https://github.com/jonschlinkert/map-visit/issues"
+ },
+ "files": [
+ "index.js"
+ ],
+ "ignore": [
+ "actual",
+ "bower_components",
+ "fixtures",
+ "node_modules",
+ "temp",
+ "test",
+ "test.js",
+ "tmp"
+ ],
+ "contributors": [
+ "Brian Woodward <brian.woodward at gmail.com> (https://twitter.com/doowb)",
+ "Jon Schlinkert <jon.schlinkert at sellside.com> (http://twitter.com/jonschlinkert)"
]
}
\ No newline at end of file
diff --git a/index.js b/index.js
index 426735d..bc54ccc 100644
--- a/index.js
+++ b/index.js
@@ -1,6 +1,7 @@
'use strict';
-var utils = require('./utils');
+var util = require('util');
+var visit = require('object-visit');
/**
* Map `visit` over an array of objects.
@@ -10,16 +11,27 @@ var utils = require('./utils');
* @param {Object} `arr` Array of objects.
*/
-module.exports = function mapVisit(collection, method, arr) {
- if (!Array.isArray(arr)) {
- throw new TypeError('expected an array');
+module.exports = function mapVisit(collection, method, val) {
+ if (isObject(val)) {
+ return visit.apply(null, arguments);
}
- arr.forEach(function(val) {
- if (typeof val === 'string') {
- collection[method](val);
+ if (!Array.isArray(val)) {
+ throw new TypeError('expected an array: ' + util.inspect(val));
+ }
+
+ var args = [].slice.call(arguments, 3);
+
+ for (var i = 0; i < val.length; i++) {
+ var ele = val[i];
+ if (isObject(ele)) {
+ visit.apply(null, [collection, method, ele].concat(args));
} else {
- utils.visit(collection, method, val);
+ collection[method].apply(collection, [ele].concat(args));
}
- });
+ }
};
+
+function isObject(val) {
+ return val && (typeof val === 'function' || (!Array.isArray(val) && typeof val === 'object'));
+}
diff --git a/package.json b/package.json
index d848c51..e8d0f41 100644
--- a/package.json
+++ b/package.json
@@ -1,17 +1,20 @@
{
"name": "map-visit",
"description": "Map `visit` over an array of objects.",
- "version": "0.1.5",
+ "version": "1.0.0",
"homepage": "https://github.com/jonschlinkert/map-visit",
"author": "Jon Schlinkert (https://github.com/jonschlinkert)",
+ "contributors": [
+ "Brian Woodward <brian.woodward at gmail.com> (https://twitter.com/doowb)",
+ "Jon Schlinkert <jon.schlinkert at sellside.com> (http://twitter.com/jonschlinkert)"
+ ],
"repository": "jonschlinkert/map-visit",
"bugs": {
"url": "https://github.com/jonschlinkert/map-visit/issues"
},
"license": "MIT",
"files": [
- "index.js",
- "utils.js"
+ "index.js"
],
"main": "index.js",
"engines": {
@@ -21,14 +24,14 @@
"test": "mocha"
},
"dependencies": {
- "lazy-cache": "^2.0.1",
- "object-visit": "^0.3.4"
+ "object-visit": "^1.0.0"
},
"devDependencies": {
- "gulp-format-md": "^0.1.10",
- "lodash": "^4.14.1",
- "mocha": "^3.0.1",
- "should": "^10.0.0"
+ "clone-deep": "^0.2.4",
+ "extend-shallow": "^2.0.1",
+ "gulp-format-md": "^0.1.12",
+ "lodash": "^4.17.4",
+ "mocha": "^3.2.0"
},
"keywords": [
"array",
@@ -46,12 +49,6 @@
"visitor"
],
"verb": {
- "related": {
- "list": [
- "collection-visit",
- "object-visit"
- ]
- },
"toc": false,
"layout": "default",
"tasks": [
@@ -63,6 +60,12 @@
"lint": {
"reflinks": true
},
+ "related": {
+ "list": [
+ "collection-visit",
+ "object-visit"
+ ]
+ },
"reflinks": [
"verb",
"verb-generate-readme"
diff --git a/test.js b/test.js
index 61dafd7..9d52d10 100644
--- a/test.js
+++ b/test.js
@@ -1,42 +1,42 @@
'use strict';
require('mocha');
-require('should');
+var clone = require('clone-deep');
var assert = require('assert');
+var extend = require('extend-shallow');
var visit = require('object-visit');
var mapVisit = require('./');
+var ctx;
-var ctx = {
+var fixture = {
data: {},
+ options: {},
set: function(key, value) {
- if (Array.isArray(key)) {
- mapVisit(ctx, 'set', key);
- } else if (typeof key === 'object') {
- visit(ctx, 'set', key);
+ if (typeof key !== 'string') {
+ if (value) extend(this.options, value);
+ mapVisit(this, 'set', key, value);
} else {
- ctx.data[key] = value;
+ this.data[key] = value;
}
}
};
describe('visit', function() {
- it('should throw an error when value is not an array.', function(done) {
- try {
+ beforeEach(function() {
+ ctx = clone(fixture);
+ });
+
+ it('should throw an error when value is not an array', function() {
+ assert.throws(function() {
mapVisit({}, 'foo', 'bar');
- done(new Error('expected an error'));
- } catch (err) {
- assert(err);
- assert(err.message);
- assert(err.message === 'expected an array');
- done();
- }
+ });
});
- it('should call visit on every value in the given object:', function() {
+ it('should call visit on every value in the given object', function() {
ctx.set('a', 'a');
ctx.set([{b: 'b'}, {c: 'c'}]);
ctx.set({d: {e: 'f'}});
- ctx.data.should.eql({
+ assert.deepEqual(ctx.data, {
a: 'a',
b: 'b',
c: 'c',
@@ -44,13 +44,13 @@ describe('visit', function() {
});
});
- it('should call visit on every value in the given object:', function() {
+ it('should call visit on every element of any array', function() {
ctx.set('a', 'a');
ctx.set(['x', 'y']);
ctx.set([{b: 'b'}, {c: 'c'}]);
ctx.set({d: {e: 'f'}});
- ctx.data.should.eql({
+ assert.deepEqual(ctx.data, {
a: 'a',
b: 'b',
c: 'c',
@@ -59,4 +59,18 @@ describe('visit', function() {
y: undefined
});
});
+
+ it('should set the second arg on every element', function() {
+ ctx.set(['foo', 'bar'], function() {});
+
+ assert.equal(typeof ctx.data.foo, 'function');
+ assert.equal(typeof ctx.data.bar, 'function');
+ });
+
+ it('should set the second arg on every object', function() {
+ ctx.set({a: 'aaa', b: 'bbb'}, {cwd: process.cwd()});
+
+ assert.equal(ctx.options.cwd, process.cwd());
+ assert.equal(ctx.options.cwd, process.cwd());
+ });
});
diff --git a/utils.js b/utils.js
deleted file mode 100644
index 4707585..0000000
--- a/utils.js
+++ /dev/null
@@ -1,14 +0,0 @@
-'use strict';
-
-var utils = require('lazy-cache')(require);
-var fn = require;
-
-require = utils; // trick browserify
-require('object-visit', 'visit');
-require = fn;
-
-/**
- * Expose utils
- */
-
-module.exports = utils;
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-map-visit.git
More information about the Pkg-javascript-commits
mailing list