[Pkg-javascript-commits] [node-map-obj] 01/03: New upstream version 2.0.0

Praveen Arimbrathodiyil praveen at moszumanska.debian.org
Tue Oct 25 12:50:39 UTC 2016


This is an automated email from the git hooks/post-receive script.

praveen pushed a commit to branch master
in repository node-map-obj.

commit f7ea2da2592d9e63cc9109845c656b2b8dd85f92
Author: Praveen Arimbrathodiyil <praveen at debian.org>
Date:   Tue Oct 25 17:50:56 2016 +0530

    New upstream version 2.0.0
---
 .editorconfig  |  3 ---
 .gitattributes |  1 +
 .jshintrc      | 12 ------------
 .travis.yml    |  5 ++---
 index.js       | 50 +++++++++++++++++++++++++++++++++++++++++---------
 package.json   | 19 ++++++++++++++-----
 readme.md      | 50 +++++++++++++++++++++++++++++++++++++++++++-------
 test.js        | 48 ++++++++++++++++++++++++++++++++++--------------
 8 files changed, 135 insertions(+), 53 deletions(-)

diff --git a/.editorconfig b/.editorconfig
index 8f9d77e..98a761d 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -10,6 +10,3 @@ insert_final_newline = true
 [{package.json,*.yml}]
 indent_style = space
 indent_size = 2
-
-[*.md]
-trim_trailing_whitespace = false
diff --git a/.gitattributes b/.gitattributes
index 176a458..391f0a4 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1 +1,2 @@
 * text=auto
+*.js text eol=lf
diff --git a/.jshintrc b/.jshintrc
deleted file mode 100644
index 9fe1be2..0000000
--- a/.jshintrc
+++ /dev/null
@@ -1,12 +0,0 @@
-{
-	"node": true,
-	"esnext": true,
-	"bitwise": true,
-	"curly": true,
-	"immed": true,
-	"newcap": true,
-	"noarg": true,
-	"undef": true,
-	"unused": "vars",
-	"strict": true
-}
diff --git a/.travis.yml b/.travis.yml
index dedfc07..b18bae5 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,6 +1,5 @@
 sudo: false
 language: node_js
 node_js:
-  - 'iojs'
-  - '0.12'
-  - '0.10'
+  - '6'
+  - '4'
diff --git a/index.js b/index.js
index 8b7b4d6..2a19cde 100644
--- a/index.js
+++ b/index.js
@@ -1,13 +1,45 @@
 'use strict';
-module.exports = function (obj, cb) {
-	var ret = {};
-	var keys = Object.keys(obj);
-
-	for (var i = 0; i < keys.length; i++) {
-		var key = keys[i];
-		var res = cb(key, obj[key], obj);
-		ret[res[0]] = res[1];
+
+// customized for this use-case
+const isObject = x =>
+	typeof x === 'object' &&
+	x !== null &&
+	!(x instanceof RegExp) &&
+	!(x instanceof Error) &&
+	!(x instanceof Date);
+
+module.exports = function mapObj(obj, fn, opts, seen) {
+	opts = Object.assign({
+		deep: false,
+		target: {}
+	}, opts);
+
+	seen = seen || new WeakMap();
+
+	if (seen.has(obj)) {
+		return seen.get(obj);
+	}
+
+	seen.set(obj, opts.target);
+
+	const target = opts.target;
+	delete opts.target;
+
+	for (const key of Object.keys(obj)) {
+		const val = obj[key];
+		const res = fn(key, val, obj);
+		let newVal = res[1];
+
+		if (opts.deep && isObject(newVal)) {
+			if (Array.isArray(newVal)) {
+				newVal = newVal.map(x => isObject(x) ? mapObj(x, fn, opts, seen) : x);
+			} else {
+				newVal = mapObj(newVal, fn, opts, seen);
+			}
+		}
+
+		target[res[0]] = newVal;
 	}
 
-	return ret;
+	return target;
 };
diff --git a/package.json b/package.json
index d178305..4c8e9da 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "map-obj",
-  "version": "1.0.1",
+  "version": "2.0.0",
   "description": "Map object keys and values into a new object",
   "license": "MIT",
   "repository": "sindresorhus/map-obj",
@@ -10,10 +10,10 @@
     "url": "sindresorhus.com"
   },
   "engines": {
-    "node": ">=0.10.0"
+    "node": ">=4"
   },
   "scripts": {
-    "test": "node test.js"
+    "test": "xo && ava"
   },
   "files": [
     "index.js"
@@ -28,9 +28,18 @@
     "values",
     "val",
     "iterate",
-    "iterator"
+    "iterator",
+    "rename",
+    "modify",
+    "deep",
+    "recurse",
+    "recursive"
   ],
   "devDependencies": {
-    "ava": "0.0.4"
+    "ava": "*",
+    "xo": "*"
+  },
+  "xo": {
+    "esnext": true
   }
 }
diff --git a/readme.md b/readme.md
index fee03d9..d6ed05c 100644
--- a/readme.md
+++ b/readme.md
@@ -13,17 +13,53 @@ $ npm install --save map-obj
 ## Usage
 
 ```js
-var mapObj = require('map-obj');
+const mapObj = require('map-obj');
 
-var newObject = mapObj({foo: 'bar'}, function (key, value, object) {
-	// first element is the new key and second is the new value
-	// here we reverse the order
-	return [value, key];
-});
+const newObject = mapObj({foo: 'bar'}, (key, value) => [value, key]);
 //=> {bar: 'foo'}
 ```
 
 
+## API
+
+### mapObj(source, mapper, [options])
+
+#### source
+
+Type: `Object`
+
+Source object to copy properties from.
+
+#### mapper
+
+Type: `Function`
+
+Mapping function.
+
+- It has signature `mapper(sourceKey, sourceValue, source)`.
+- It must return a two item array: `[targetKey, targetValue]`.
+
+#### deep
+
+Type: `boolean`<br>
+Default: `false`
+
+Recurse nested objects and objects in arrays.
+
+#### target
+
+Type: `Object`<br>
+Default: `{}`
+
+Target object to map properties on to.
+
+
+## Related
+
+- [filter-obj](https://github.com/sindresorhus/filter-obj) - Filter object keys and values into a new object
+- [object-assign](https://github.com/sindresorhus/object-assign) - Copy enumerable own properties from one or more source objects to a target object
+
+
 ## License
 
-MIT © [Sindre Sorhus](http://sindresorhus.com)
+MIT © [Sindre Sorhus](https://sindresorhus.com)
diff --git a/test.js b/test.js
index f8311be..56910c0 100644
--- a/test.js
+++ b/test.js
@@ -1,19 +1,39 @@
-'use strict';
-var test = require('ava');
-var mapObj = require('./');
+import test from 'ava';
+import m from './';
 
-test(function (t) {
-	t.assert(mapObj({foo: 'bar'}, function (key, val) {
-		return [key, 'unicorn'];
-	}).foo === 'unicorn');
+test('main', t => {
+	t.is(m({foo: 'bar'}, key => [key, 'unicorn']).foo, 'unicorn');
+	t.is(m({foo: 'bar'}, (key, val) => ['unicorn', val]).unicorn, 'bar');
+	t.is(m({foo: 'bar'}, (key, val) => [val, key]).bar, 'foo');
+});
+
+test('target option', t => {
+	const target = {};
+	t.is(m({foo: 'bar'}, (key, val) => [val, key], {target}), target);
+	t.is(target.bar, 'foo');
+});
+
+test('deep option', t => {
+	const obj = {one: 1, obj: {two: 2, three: 3}, arr: [{four: 4}, 5]};
+	const expected = {one: 2, obj: {two: 4, three: 6}, arr: [{four: 8}, 5]};
+	const fn = (key, val) => [key, typeof val === 'number' ? val * 2 : val];
+	const actual = m(obj, fn, {deep: true});
+	t.deepEqual(actual, expected);
+});
+
+test('handles circular references', t => {
+	const obj = {one: 1, arr: [2]};
+	obj.circular = obj;
+	obj.arr2 = obj.arr;
+	obj.arr.push(obj);
 
-	t.assert(mapObj({foo: 'bar'}, function (key, val) {
-		return ['unicorn', val];
-	}).unicorn === 'bar');
+	const fn = (key, val) => [key.toUpperCase(), val];
+	const actual = m(obj, fn, {deep: true});
 
-	t.assert(mapObj({foo: 'bar'}, function (key, val) {
-		return [val, key];
-	}).bar === 'foo');
+	const expected = {ONE: 1, ARR: [2]};
+	expected.CIRCULAR = expected;
+	expected.ARR2 = expected.ARR;
+	expected.ARR.push(expected);
 
-	t.end();
+	t.deepEqual(actual, expected);
 });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-map-obj.git



More information about the Pkg-javascript-commits mailing list