|
1 | | -import {isArray, isFunction, isObject, isVoid} from './lang'; |
| 1 | +import WeakMap = require('es6-weak-map'); |
| 2 | +import {isArray, isObject} from 'lodash'; |
| 3 | + |
| 4 | +interface NonPrimitive extends Object { |
| 5 | + [key: string]: any; |
| 6 | + [index: number]: any; |
| 7 | +} |
2 | 8 |
|
3 | 9 | export interface MapFn { |
4 | 10 | (key: string, value: any): string; |
5 | 11 | } |
6 | 12 |
|
7 | | -export interface Options { |
| 13 | +export interface Opts { |
8 | 14 | thisArg?: any; |
9 | 15 | } |
10 | 16 |
|
11 | | -export function deepMapKeys<T>(object: any, mapFn: MapFn, options?: Options): T { |
12 | | - options = isVoid(options) ? {} : options; |
| 17 | +export class DeepMapKeys { |
| 18 | + |
| 19 | + private cache = new WeakMap<NonPrimitive, any>(); |
13 | 20 |
|
14 | | - if (!mapFn) { |
15 | | - throw new Error('mapFn is required'); |
16 | | - } else if (!isFunction(mapFn)) { |
17 | | - throw new TypeError('mapFn must be a function'); |
18 | | - } else if (!isObject(options)) { |
19 | | - throw new TypeError('options must be an object'); |
| 21 | + constructor( |
| 22 | + private mapFn: MapFn, |
| 23 | + private opts: Opts |
| 24 | + ) { } |
| 25 | + |
| 26 | + public map(value: any): any { |
| 27 | + return isArray(value) ? this.mapArray(value) : |
| 28 | + isObject(value) ? this.mapObject(value) : |
| 29 | + value; |
20 | 30 | } |
21 | 31 |
|
22 | | - return map(object, mapFn, options); |
23 | | -} |
| 32 | + private mapArray(arr: any[]): any[] { |
| 33 | + if (this.cache.has(arr)) { |
| 34 | + return this.cache.get(arr); |
| 35 | + } |
24 | 36 |
|
25 | | -function map(value: any, fn: MapFn, opts: Options): any { |
26 | | - return isArray(value) ? mapArray(value, fn, opts) : |
27 | | - isObject(value) ? mapObject(value, fn, opts) : |
28 | | - value; |
29 | | -} |
| 37 | + let length = arr.length; |
| 38 | + let result: any[] = []; |
| 39 | + this.cache.set(arr, result); |
30 | 40 |
|
31 | | -function mapArray(arr: any[], fn: MapFn, opts: Options): any[] { |
32 | | - let result: any[] = []; |
33 | | - let len = arr.length; |
| 41 | + for (let i = 0; i < length; i++) { |
| 42 | + result.push(this.map(arr[i])); |
| 43 | + } |
34 | 44 |
|
35 | | - for (let i = 0; i < len; i++) { |
36 | | - result.push(map(arr[i], fn, opts)); |
| 45 | + return result; |
37 | 46 | } |
38 | 47 |
|
39 | | - return result; |
40 | | -} |
| 48 | + private mapObject(obj: NonPrimitive): NonPrimitive { |
| 49 | + if (this.cache.has(obj)) { |
| 50 | + return this.cache.get(obj); |
| 51 | + } |
41 | 52 |
|
42 | | -function mapObject(obj: {[key: string]: any}, fn: MapFn, opts: Options): {[key: string]: any} { |
43 | | - let result: {[key: string]: any} = {}; |
| 53 | + let {mapFn, opts: {thisArg}} = this; |
| 54 | + let result: NonPrimitive = {}; |
| 55 | + this.cache.set(obj, result); |
44 | 56 |
|
45 | | - for (let key in obj) { |
46 | | - if (obj.hasOwnProperty(key)) { |
47 | | - let value = obj[key]; |
48 | | - result[fn.call(opts.thisArg, key, value)] = map(value, fn, opts); |
| 57 | + for (let key in obj) { |
| 58 | + if (obj.hasOwnProperty(key)) { |
| 59 | + result[mapFn.call(thisArg, key, obj[key])] = this.map(obj[key]); |
| 60 | + } |
49 | 61 | } |
50 | | - } |
51 | 62 |
|
52 | | - return result; |
| 63 | + return result; |
| 64 | + } |
53 | 65 | } |
0 commit comments