|
2 | 2 | // - lodash.clonedeep is replaced with JSON parse / stringify |
3 | 3 | // - lodash.isequal is replaced with fast-deep-equal. |
4 | 4 |
|
| 5 | +/** |
| 6 | + * External dependencies |
| 7 | + */ |
5 | 8 | import { default as isEqual } from 'fast-deep-equal/es6'; |
6 | 9 |
|
7 | | -function cloneDeep<T>(value: T): T { |
8 | | - return JSON.parse(JSON.stringify(value)) as T; |
| 10 | +function cloneDeep< T >( value: T ): T { |
| 11 | + return JSON.parse( JSON.stringify( value ) ) as T; |
9 | 12 | } |
10 | 13 |
|
11 | 14 | interface AttributeMap { |
12 | | - [key: string]: unknown; |
| 15 | + [ key: string ]: unknown; |
13 | 16 | } |
14 | 17 |
|
15 | 18 | namespace AttributeMap { |
16 | | - export function compose( |
17 | | - a: AttributeMap = {}, |
18 | | - b: AttributeMap = {}, |
19 | | - keepNull = false, |
20 | | - ): AttributeMap | undefined { |
21 | | - if (typeof a !== 'object') { |
22 | | - a = {}; |
23 | | - } |
24 | | - if (typeof b !== 'object') { |
25 | | - b = {}; |
26 | | - } |
27 | | - let attributes = cloneDeep(b); |
28 | | - if (!keepNull) { |
29 | | - attributes = Object.keys(attributes).reduce<AttributeMap>((copy, key) => { |
30 | | - if (attributes[key] != null) { |
31 | | - copy[key] = attributes[key]; |
32 | | - } |
33 | | - return copy; |
34 | | - }, {}); |
35 | | - } |
36 | | - for (const key in a) { |
37 | | - if (a[key] !== undefined && b[key] === undefined) { |
38 | | - attributes[key] = a[key]; |
39 | | - } |
40 | | - } |
41 | | - return Object.keys(attributes).length > 0 ? attributes : undefined; |
42 | | - } |
| 19 | + export function compose( |
| 20 | + a: AttributeMap = {}, |
| 21 | + b: AttributeMap = {}, |
| 22 | + keepNull = false |
| 23 | + ): AttributeMap | undefined { |
| 24 | + if ( typeof a !== 'object' ) { |
| 25 | + a = {}; |
| 26 | + } |
| 27 | + if ( typeof b !== 'object' ) { |
| 28 | + b = {}; |
| 29 | + } |
| 30 | + let attributes = cloneDeep( b ); |
| 31 | + if ( ! keepNull ) { |
| 32 | + attributes = Object.keys( attributes ).reduce< AttributeMap >( |
| 33 | + ( copy, key ) => { |
| 34 | + if ( attributes[ key ] !== null ) { |
| 35 | + copy[ key ] = attributes[ key ]; |
| 36 | + } |
| 37 | + return copy; |
| 38 | + }, |
| 39 | + {} |
| 40 | + ); |
| 41 | + } |
| 42 | + for ( const key in a ) { |
| 43 | + if ( a[ key ] !== undefined && b[ key ] === undefined ) { |
| 44 | + attributes[ key ] = a[ key ]; |
| 45 | + } |
| 46 | + } |
| 47 | + return Object.keys( attributes ).length > 0 ? attributes : undefined; |
| 48 | + } |
43 | 49 |
|
44 | | - export function diff( |
45 | | - a: AttributeMap = {}, |
46 | | - b: AttributeMap = {}, |
47 | | - ): AttributeMap | undefined { |
48 | | - if (typeof a !== 'object') { |
49 | | - a = {}; |
50 | | - } |
51 | | - if (typeof b !== 'object') { |
52 | | - b = {}; |
53 | | - } |
54 | | - const attributes = Object.keys(a) |
55 | | - .concat(Object.keys(b)) |
56 | | - .reduce<AttributeMap>((attrs, key) => { |
57 | | - if (!isEqual(a[key], b[key])) { |
58 | | - attrs[key] = b[key] === undefined ? null : b[key]; |
59 | | - } |
60 | | - return attrs; |
61 | | - }, {}); |
62 | | - return Object.keys(attributes).length > 0 ? attributes : undefined; |
63 | | - } |
| 50 | + export function diff( |
| 51 | + a: AttributeMap = {}, |
| 52 | + b: AttributeMap = {} |
| 53 | + ): AttributeMap | undefined { |
| 54 | + if ( typeof a !== 'object' ) { |
| 55 | + a = {}; |
| 56 | + } |
| 57 | + if ( typeof b !== 'object' ) { |
| 58 | + b = {}; |
| 59 | + } |
| 60 | + const attributes = Object.keys( a ) |
| 61 | + .concat( Object.keys( b ) ) |
| 62 | + .reduce< AttributeMap >( ( attrs, key ) => { |
| 63 | + if ( ! isEqual( a[ key ], b[ key ] ) ) { |
| 64 | + attrs[ key ] = b[ key ] === undefined ? null : b[ key ]; |
| 65 | + } |
| 66 | + return attrs; |
| 67 | + }, {} ); |
| 68 | + return Object.keys( attributes ).length > 0 ? attributes : undefined; |
| 69 | + } |
64 | 70 |
|
65 | | - export function invert( |
66 | | - attr: AttributeMap = {}, |
67 | | - base: AttributeMap = {}, |
68 | | - ): AttributeMap { |
69 | | - attr = attr || {}; |
70 | | - const baseInverted = Object.keys(base).reduce<AttributeMap>((memo, key) => { |
71 | | - if (base[key] !== attr[key] && attr[key] !== undefined) { |
72 | | - memo[key] = base[key]; |
73 | | - } |
74 | | - return memo; |
75 | | - }, {}); |
76 | | - return Object.keys(attr).reduce<AttributeMap>((memo, key) => { |
77 | | - if (attr[key] !== base[key] && base[key] === undefined) { |
78 | | - memo[key] = null; |
79 | | - } |
80 | | - return memo; |
81 | | - }, baseInverted); |
82 | | - } |
| 71 | + export function invert( |
| 72 | + attr: AttributeMap = {}, |
| 73 | + base: AttributeMap = {} |
| 74 | + ): AttributeMap { |
| 75 | + attr = attr || {}; |
| 76 | + const baseInverted = Object.keys( base ).reduce< AttributeMap >( |
| 77 | + ( memo, key ) => { |
| 78 | + if ( |
| 79 | + base[ key ] !== attr[ key ] && |
| 80 | + attr[ key ] !== undefined |
| 81 | + ) { |
| 82 | + memo[ key ] = base[ key ]; |
| 83 | + } |
| 84 | + return memo; |
| 85 | + }, |
| 86 | + {} |
| 87 | + ); |
| 88 | + return Object.keys( attr ).reduce< AttributeMap >( ( memo, key ) => { |
| 89 | + if ( attr[ key ] !== base[ key ] && base[ key ] === undefined ) { |
| 90 | + memo[ key ] = null; |
| 91 | + } |
| 92 | + return memo; |
| 93 | + }, baseInverted ); |
| 94 | + } |
83 | 95 |
|
84 | | - export function transform( |
85 | | - a: AttributeMap | undefined, |
86 | | - b: AttributeMap | undefined, |
87 | | - priority = false, |
88 | | - ): AttributeMap | undefined { |
89 | | - if (typeof a !== 'object') { |
90 | | - return b; |
91 | | - } |
92 | | - if (typeof b !== 'object') { |
93 | | - return undefined; |
94 | | - } |
95 | | - if (!priority) { |
96 | | - return b; // b simply overwrites us without priority |
97 | | - } |
98 | | - const attributes = Object.keys(b).reduce<AttributeMap>((attrs, key) => { |
99 | | - if (a[key] === undefined) { |
100 | | - attrs[key] = b[key]; // null is a valid value |
101 | | - } |
102 | | - return attrs; |
103 | | - }, {}); |
104 | | - return Object.keys(attributes).length > 0 ? attributes : undefined; |
105 | | - } |
| 96 | + export function transform( |
| 97 | + a: AttributeMap | undefined, |
| 98 | + b: AttributeMap | undefined, |
| 99 | + priority = false |
| 100 | + ): AttributeMap | undefined { |
| 101 | + if ( typeof a !== 'object' ) { |
| 102 | + return b; |
| 103 | + } |
| 104 | + if ( typeof b !== 'object' ) { |
| 105 | + return undefined; |
| 106 | + } |
| 107 | + if ( ! priority ) { |
| 108 | + return b; // b simply overwrites us without priority |
| 109 | + } |
| 110 | + const attributes = Object.keys( b ).reduce< AttributeMap >( |
| 111 | + ( attrs, key ) => { |
| 112 | + if ( a[ key ] === undefined ) { |
| 113 | + attrs[ key ] = b[ key ]; // null is a valid value |
| 114 | + } |
| 115 | + return attrs; |
| 116 | + }, |
| 117 | + {} |
| 118 | + ); |
| 119 | + return Object.keys( attributes ).length > 0 ? attributes : undefined; |
| 120 | + } |
106 | 121 | } |
107 | 122 |
|
108 | 123 | export default AttributeMap; |
0 commit comments