-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
385 additions
and
230 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
/coverage/ | ||
/lib/ | ||
/node_modules/ | ||
/typings/ | ||
/npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
/coverage/ | ||
/test/ | ||
/typings/ | ||
/src/ | ||
/lib/*.test.* | ||
/.editorconfig | ||
/.eslintrc.yml | ||
/.gitignore | ||
/.travis.yml | ||
/tsconfig.json | ||
/tslint.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
{ | ||
"name": "deep-map-keys", | ||
"version": "1.0.0", | ||
"description": "Transforms keys of a JSON-like object", | ||
"description": "Transforms nested keys of complex objects", | ||
"main": "lib/index.js", | ||
"typings": "lib/index.d.ts", | ||
"scripts": { | ||
"test:lint": "eslint lib", | ||
"test:unit": "istanbul cover _mocha", | ||
"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:report": "npm test && open coverage/lcov-report/index.html", | ||
"test": "npm run test:lint && npm run test:unit", | ||
"coveralls": "cat coverage/lcov.info | coveralls" | ||
"ci:typings": "typings install", | ||
"ci:coveralls": "cat coverage/lcov.info | coveralls" | ||
}, | ||
"engines": { | ||
"node": ">=0.10" | ||
|
@@ -20,19 +26,24 @@ | |
"nested", | ||
"object", | ||
"array", | ||
"json" | ||
"json", | ||
"typescript", | ||
"typings" | ||
], | ||
"author": "Akim McMath <[email protected]>", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"coffee-script": "^1.10.0", | ||
"coveralls": "^2.11.9", | ||
"eslint": "^2.12.0", | ||
"istanbul": "^0.4.3", | ||
"istanbul": "1.0.0-alpha.2", | ||
"mocha": "^2.5.3", | ||
"rimraf": "^2.5.2", | ||
"sinon": "^1.17.4", | ||
"sinon-chai": "^2.8.0" | ||
"sinon-chai": "^2.8.0", | ||
"ts-node": "^0.9.1", | ||
"tslint": "^3.11.0", | ||
"typescript": "^1.8.10", | ||
"typings": "^1.1.0" | ||
}, | ||
"dependencies": {}, | ||
"repository": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {isArray, isFunction, isObject, isVoid} from './lang'; | ||
|
||
export interface MapFn { | ||
(key: string, value: any): string; | ||
} | ||
|
||
export interface Options { | ||
thisArg?: any; | ||
} | ||
|
||
export function deepMapKeys<T>(object: any, mapFn: MapFn, options?: Options): T { | ||
options = isVoid(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 map(object, mapFn, options); | ||
} | ||
|
||
function map(value: any, fn: MapFn, opts: Options): any { | ||
return isArray(value) ? mapArray(value, fn, opts) : | ||
isObject(value) ? mapObject(value, fn, opts) : | ||
value; | ||
} | ||
|
||
function mapArray(arr: any[], fn: MapFn, opts: Options): any[] { | ||
let result: any[] = []; | ||
let len = arr.length; | ||
|
||
for (let i = 0; i < len; i++) { | ||
result.push(map(arr[i], fn, opts)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
function mapObject(obj: {[key: string]: any}, fn: MapFn, opts: Options): {[key: string]: any} { | ||
let result: {[key: string]: any} = {}; | ||
|
||
for (let key in obj) { | ||
let value = obj[key]; | ||
result[fn.call(opts.thisArg, key, value)] = map(value, fn, opts); | ||
} | ||
|
||
return result; | ||
} |
Oops, something went wrong.