Skip to content

Feature: Add support for circular references #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/coverage/
/typings/
/src/
/lib/*.test.*
/lib/**/*.test.*
/.editorconfig
/.gitignore
/.travis.yml
/custom.d.ts
/mocha.opts
/tsconfig.json
/tslint.json
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
[Install](#install) | [Usage](#usage) | [API](#api) | [TypeScript](#typescript) | [License](#license)

**Deep Map Keys** recurses through an object and transforms its keys – and
the keys of any nested objects – according to some function.
the keys of any nested objects – according to some function. Circular
references are supported.

To transform the *values* of an object rather than its keys, use
[Deep Map][deep-map].
Expand Down Expand Up @@ -86,7 +87,8 @@ And the result will look like this:
a complex object containing other nested objects. This object may be an
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array">
<code>Array</code></a>, in which case the keys of any objects it
contains will be transformed.
contains will be transformed. The object may contain circular
references.
</td>
</tr>
<tr>
Expand Down
10 changes: 10 additions & 0 deletions custom.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare module 'es6-weak-map' {

export = class WeakMap<K, V> {
delete(key: K): boolean;
get(key: K): V;
has(key: K): boolean;
set(key: K, value?: V): this;
};

}
3 changes: 3 additions & 0 deletions mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--compilers ts:ts-node/register
--recursive
--reporter dot
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"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",
"test": "npm run test:lint && npm run test:unit",
"ci:typings": "typings install",
Expand All @@ -26,6 +26,7 @@
"nested",
"object",
"array",
"circular",
"json",
"typescript",
"typings"
Expand All @@ -45,7 +46,10 @@
"typescript": "^1.8.10",
"typings": "^1.1.0"
},
"dependencies": {},
"dependencies": {
"es6-weak-map": "^2.0.1",
"lodash": "^4.13.1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/akim-mcmath/deep-map-keys.git"
Expand Down
76 changes: 44 additions & 32 deletions src/deep-map-keys.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,65 @@
import {isArray, isFunction, isObject, isVoid} from './lang';
import WeakMap = require('es6-weak-map');
import {isArray, isObject} from 'lodash';

interface NonPrimitive extends Object {
[key: string]: any;
[index: number]: any;
}

export interface MapFn {
(key: string, value: any): string;
}

export interface Options {
export interface Opts {
thisArg?: any;
}

export function deepMapKeys<T>(object: any, mapFn: MapFn, options?: Options): T {
options = isVoid(options) ? {} : options;
export class DeepMapKeys {

private cache = new WeakMap<NonPrimitive, any>();

if (!mapFn) {
throw new Error('mapFn is required');
} else if (!isFunction(mapFn)) {
throw new TypeError('mapFn must be a function');
} else if (!isObject(options)) {
throw new TypeError('options must be an object');
constructor(
private mapFn: MapFn,
private opts: Opts
) { }

public map(value: any): any {
return isArray(value) ? this.mapArray(value) :
isObject(value) ? this.mapObject(value) :
value;
}

return map(object, mapFn, options);
}
private mapArray(arr: any[]): any[] {
if (this.cache.has(arr)) {
return this.cache.get(arr);
}

function map(value: any, fn: MapFn, opts: Options): any {
return isArray(value) ? mapArray(value, fn, opts) :
isObject(value) ? mapObject(value, fn, opts) :
value;
}
let length = arr.length;
let result: any[] = [];
this.cache.set(arr, result);

function mapArray(arr: any[], fn: MapFn, opts: Options): any[] {
let result: any[] = [];
let len = arr.length;
for (let i = 0; i < length; i++) {
result.push(this.map(arr[i]));
}

for (let i = 0; i < len; i++) {
result.push(map(arr[i], fn, opts));
return result;
}

return result;
}
private mapObject(obj: NonPrimitive): NonPrimitive {
if (this.cache.has(obj)) {
return this.cache.get(obj);
}

function mapObject(obj: {[key: string]: any}, fn: MapFn, opts: Options): {[key: string]: any} {
let result: {[key: string]: any} = {};
let {mapFn, opts: {thisArg}} = this;
let result: NonPrimitive = {};
this.cache.set(obj, result);

for (let key in obj) {
if (obj.hasOwnProperty(key)) {
let value = obj[key];
result[fn.call(opts.thisArg, key, value)] = map(value, fn, opts);
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
result[mapFn.call(thisArg, key, obj[key])] = this.map(obj[key]);
}
}
}

return result;
return result;
}
}
12 changes: 12 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ describe('deepMapKeys(object, mapFn, [options])', () => {
.should.deep.equal([1, {TWO: 2, THREE: 3, ARR: [4, {FIVE: 5}]}]);
});

it('transforms an object with circular references', () => {
let obj = {one: 1, arr: [2, 3], self: null as any, arr2: null as any[]};
obj.self = obj;
obj.arr2 = obj.arr;

let exp = {ONE: 1, ARR: [2, 3], SELF: null as any, ARR2: null as any[]};
exp.SELF = exp;
exp.ARR2 = exp.ARR;

deepMapKeys(obj, caps).should.deep.equal(exp);
});

});

describe('@mapFn(key: string, value: any): string', () => {
Expand Down
17 changes: 16 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
import {deepMapKeys} from './deep-map-keys';
import {isFunction, isNil, isObject} from 'lodash';
import {DeepMapKeys, MapFn, Opts} from './deep-map-keys';

function deepMapKeys<T>(object: any, mapFn: MapFn, options?: Opts): T {
options = isNil(options) ? {} : options;

if (!mapFn) {
throw new Error('mapFn is required');
} else if (!isFunction(mapFn)) {
throw new TypeError('mapFn must be a function');
} else if (!isObject(options)) {
throw new TypeError('options must be an object');
}

return new DeepMapKeys(mapFn, options).map(object);
}

export = deepMapKeys;
13 changes: 0 additions & 13 deletions src/lang.ts

This file was deleted.

3 changes: 2 additions & 1 deletion typings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"devDependencies": {
"chai": "registry:npm/chai#3.5.0+20160415060238",
"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"
}
}