Comparing version 1.3.2 to 1.4.0
export interface MapFn { | ||
(value: any, key: string | number): any; | ||
} | ||
export interface Options { | ||
export interface Opts { | ||
thisArg?: any; | ||
inPlace?: boolean; | ||
} | ||
export declare function deepMap<T>(object: T, mapFn: MapFn, options?: Options): T; | ||
export declare function deepMap<T>(object: any, mapFn: MapFn, options?: Options): T; | ||
export declare class DeepMap { | ||
private mapFn; | ||
private opts; | ||
private cache; | ||
constructor(mapFn: MapFn, opts: Opts); | ||
map(value: any, key?: string | number): any; | ||
private mapArray(arr, key); | ||
private mapObject(obj, key); | ||
} |
"use strict"; | ||
var lang_1 = require('./lang'); | ||
function deepMap(object, mapFn, options) { | ||
options = lang_1.isVoid(options) ? {} : options; | ||
if (!mapFn) { | ||
throw new Error('mapFn is required'); | ||
var WeakMap = require('es6-weak-map'); | ||
var lodash_1 = require('lodash'); | ||
var DeepMap = (function () { | ||
function DeepMap(mapFn, opts) { | ||
this.mapFn = mapFn; | ||
this.opts = opts; | ||
this.cache = new WeakMap(); | ||
} | ||
else if (!lang_1.isFunction(mapFn)) { | ||
throw new TypeError('mapFn must be a function'); | ||
} | ||
else if (!lang_1.isObject(options)) { | ||
throw new TypeError('options must be an object'); | ||
} | ||
return map(object, mapFn, options); | ||
} | ||
exports.deepMap = deepMap; | ||
function map(value, fn, opts, key) { | ||
return lang_1.isArray(value) ? mapArray(value, fn, opts) : | ||
lang_1.isObject(value) ? mapObject(value, fn, opts) : | ||
fn.call(opts.thisArg, value, key); | ||
} | ||
function mapArray(arr, fn, opts) { | ||
var result = opts.inPlace ? arr : []; | ||
var len = arr.length; | ||
for (var i = 0; i < len; i++) { | ||
result[i] = map(arr[i], fn, opts, i); | ||
} | ||
return result; | ||
} | ||
function mapObject(obj, fn, opts) { | ||
var result = opts.inPlace ? obj : {}; | ||
for (var key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
result[key] = map(obj[key], fn, opts, key); | ||
DeepMap.prototype.map = function (value, key) { | ||
return lodash_1.isArray(value) ? this.mapArray(value, key) : | ||
lodash_1.isObject(value) ? this.mapObject(value, key) : | ||
this.mapFn.call(this.opts.thisArg, value, key); | ||
}; | ||
DeepMap.prototype.mapArray = function (arr, key) { | ||
if (this.cache.has(arr)) { | ||
return this.cache.get(arr); | ||
} | ||
} | ||
return result; | ||
} | ||
var length = arr.length; | ||
var result = this.opts.inPlace ? arr : []; | ||
this.cache.set(arr, result); | ||
for (var i = 0; i < length; i++) { | ||
result[i] = this.map(arr[i], i); | ||
} | ||
return result; | ||
}; | ||
DeepMap.prototype.mapObject = function (obj, key) { | ||
if (this.cache.has(obj)) { | ||
return this.cache.get(obj); | ||
} | ||
var result = this.opts.inPlace ? obj : {}; | ||
this.cache.set(obj, result); | ||
for (var key_1 in obj) { | ||
if (obj.hasOwnProperty(key_1)) { | ||
result[key_1] = this.map(obj[key_1], key_1); | ||
} | ||
} | ||
return result; | ||
}; | ||
return DeepMap; | ||
}()); | ||
exports.DeepMap = DeepMap; | ||
//# sourceMappingURL=deep-map.js.map |
@@ -1,2 +0,4 @@ | ||
import { deepMap } from './deep-map'; | ||
import { MapFn, Opts } from './deep-map'; | ||
declare function deepMap<T>(object: T, mapFn: MapFn, options?: Opts): T; | ||
declare function deepMap<T>(object: any, mapFn: MapFn, options?: Opts): T; | ||
export = deepMap; |
"use strict"; | ||
var lodash_1 = require('lodash'); | ||
var deep_map_1 = require('./deep-map'); | ||
module.exports = deep_map_1.deepMap; | ||
function deepMap(object, mapFn, options) { | ||
options = lodash_1.isNil(options) ? {} : options; | ||
if (!mapFn) { | ||
throw new Error('mapFn is required'); | ||
} | ||
else if (!lodash_1.isFunction(mapFn)) { | ||
throw new TypeError('mapFn must be a function'); | ||
} | ||
else if (!lodash_1.isObject(options)) { | ||
throw new TypeError('options must be an object'); | ||
} | ||
return new deep_map_1.DeepMap(mapFn, options).map(object); | ||
} | ||
module.exports = deepMap; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "deep-map", | ||
"version": "1.3.2", | ||
"version": "1.4.0", | ||
"description": "Transforms nested values of complex objects", | ||
@@ -8,7 +8,7 @@ "main": "lib/index.js", | ||
"scripts": { | ||
"build:compile": "tsc -p tsbuild.json", | ||
"build:compile": "tsc", | ||
"build:remove": "rimraf lib", | ||
"build": "npm run build:remove && npm run build:compile", | ||
"test:lint": "tslint 'src/**/*.ts'", | ||
"test:unit": "istanbul cover -e .ts -x '*.test.ts' _mocha -- 'src/**/*.test.ts' --compilers ts:ts-node/register", | ||
"test:unit": "istanbul cover -e .ts -x '*.test.ts' _mocha -- src --opts mocha.opts", | ||
"test:report": "npm test && open coverage/lcov-report/index.html", | ||
@@ -33,2 +33,3 @@ "test": "npm run test:lint && npm run test:unit", | ||
"array", | ||
"circular", | ||
"json", | ||
@@ -57,3 +58,7 @@ "primitive", | ||
"typings": "^1.1.0" | ||
}, | ||
"dependencies": { | ||
"es6-weak-map": "^2.0.1", | ||
"lodash": "^4.13.1" | ||
} | ||
} |
@@ -13,5 +13,9 @@ # Deep Map | ||
– including nested values – according to some function. Essentially, | ||
it works like [`Array.prototype.map()`][array-prototype-map], except that it | ||
works on all objects rather than just on Arrays. | ||
it's a deep version of [`Array.prototype.map()`][array-prototype-map] that | ||
works on all objects rather than just on Arrays. Circular references are | ||
supported. | ||
To transform the *keys* of an object rather than its values, use | ||
[Deep Map Keys][deep-map-keys]. | ||
## Install | ||
@@ -100,5 +104,4 @@ | ||
<code>Array</code></a>, and may contain nested arrays whose values will | ||
be deeply transformed in the same way. In the unlikely use-case in which | ||
a primitive value is passed, `mapFn()` will be applied once and the | ||
result returned. | ||
be deeply transformed in the same way. The object may contain circular | ||
references. | ||
</td> | ||
@@ -115,7 +118,9 @@ </tr> | ||
<strong>value</strong> <<code>any</code>> | ||
The value being transformed | ||
The value being transformed. | ||
</li> | ||
<li> | ||
<strong>key</strong> <<code>string | number</code>> | ||
The key name or numerical index of the value being transformed | ||
The key or index of the value being transformed. In the case | ||
of plain objects, this will be a string; in the case of arrays, | ||
this will be a number. | ||
</li> | ||
@@ -134,11 +139,11 @@ </ul> | ||
<li> | ||
<strong>inPlace</strong> <<code>boolean = false</code>> | ||
Transform <code>object</code> in-place rather than constructing | ||
a new object | ||
<strong>inPlace</strong> <<code>boolean=false</code>> | ||
Mutate <code>object</code> rather than constructing a new | ||
object. Nested objects will also be mutated. | ||
</li> | ||
<li> | ||
<strong>thisArg</strong> <<code>any = undefined</code>> | ||
<strong>thisArg</strong> <<code>any=undefined</code>> | ||
Sets the value of | ||
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this"><code>this</code></a> | ||
within <code>mapFn()</code> | ||
within <code>mapFn()</code>. | ||
</li> | ||
@@ -154,3 +159,3 @@ </ul> | ||
Returns a new object with the same keys as `object`. If `options.inPlace` is set | ||
to `true`, the original object is returned mutated. | ||
to `true`, the original object is returned, mutated. | ||
@@ -196,3 +201,4 @@ ## TypeScript | ||
[gemnasium]: https://gemnasium.com/akim-mcmath/deep-map | ||
[deep-map-keys]: https://github.com/akim-mcmath/deep-map-keys | ||
[typescript]: http://www.typescriptlang.org/ | ||
[array-prototype-map]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map |
@@ -9,4 +9,5 @@ { | ||
"sinon": "registry:npm/sinon#1.16.0+20160427193336", | ||
"sinon-chai": "registry:npm/sinon-chai#2.8.0+20160310030142" | ||
"sinon-chai": "registry:npm/sinon-chai#2.8.0+20160310030142", | ||
"lodash": "registry:npm/lodash#4.0.0+20160416211519" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14180
91
199
2
11
+ Addedes6-weak-map@^2.0.1
+ Addedlodash@^4.13.1
+ Addedd@1.0.2(transitive)
+ Addedes5-ext@0.10.64(transitive)
+ Addedes6-iterator@2.0.3(transitive)
+ Addedes6-symbol@3.1.4(transitive)
+ Addedes6-weak-map@2.0.3(transitive)
+ Addedesniff@2.0.1(transitive)
+ Addedevent-emitter@0.3.5(transitive)
+ Addedext@1.7.0(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addednext-tick@1.1.0(transitive)
+ Addedtype@2.7.3(transitive)