|
1 | | -'use strict'; |
| 1 | +'use strict' |
2 | 2 |
|
3 | | -const moment = require('moment'); |
| 3 | +const moment = require('moment') |
4 | 4 |
|
5 | 5 | const _ = { |
6 | 6 | transform: require('lodash.transform'), |
7 | 7 | isObjectLike: require('lodash.isobjectlike'), |
8 | 8 | isArray: require('lodash.isarraylike'), |
9 | 9 | isDate: require('lodash.isdate'), |
10 | | - defaults: require('lodash.defaults'), |
11 | | -}; |
12 | | - |
| 10 | + defaults: require('lodash.defaults') |
| 11 | +} |
13 | 12 |
|
14 | | -const ignore = Symbol('ignore'); |
| 13 | +const ignore = Symbol('ignore') |
15 | 14 |
|
16 | 15 | const DEFAULT_OPTIONS = { |
17 | 16 | extract: 'only-add-change', |
18 | 17 | dateCheck: true, |
19 | 18 | dateFormatIn: 'YYYY-MM-DDTHH:mm:ss.sssZ', |
20 | | - dateFormatOut: 'YYYY-MM-DDTHH:mm:ss.sssZ', |
21 | | -}; |
| 19 | + dateFormatOut: 'YYYY-MM-DDTHH:mm:ss.sssZ' |
| 20 | +} |
22 | 21 |
|
23 | | -function evaluateDate(value, verifyFormat, formatIn, formatOut) { |
| 22 | +function evaluateDate (value, verifyFormat, formatIn, formatOut) { |
24 | 23 | // TODO: add date comparison only for some keys? |
25 | 24 | if (_.isDate(value)) { |
26 | | - value = value.toJSON(); |
| 25 | + value = value.toJSON() |
27 | 26 | } |
28 | 27 |
|
29 | 28 | if (verifyFormat === true) { |
30 | | - const analize = moment.utc(value, formatIn, true); |
| 29 | + const analize = moment.utc(value, formatIn, true) |
31 | 30 | if (analize.isValid()) { |
32 | | - return analize.format(formatOut); |
| 31 | + return analize.format(formatOut) |
33 | 32 | } |
34 | 33 | } |
35 | | - return value; |
| 34 | + return value |
36 | 35 | } |
37 | 36 |
|
38 | | -function mustAssignValue(value, checkValue, opts) { |
39 | | - value = evaluateDate(value, opts.dateCheck, opts.dateFormatIn, opts.dateFormatOut); |
40 | | - checkValue = evaluateDate(checkValue, opts.dateCheck, opts.dateFormatIn, opts.dateFormatOut); |
| 37 | +function mustAssignValue (value, checkValue, opts) { |
| 38 | + value = evaluateDate(value, opts.dateCheck, opts.dateFormatIn, opts.dateFormatOut) |
| 39 | + checkValue = evaluateDate(checkValue, opts.dateCheck, opts.dateFormatIn, opts.dateFormatOut) |
41 | 40 |
|
42 | 41 | switch (opts.extract) { |
43 | 42 | case 'only-add': |
44 | | - return checkValue === undefined; |
| 43 | + return checkValue === undefined |
45 | 44 | case 'only-remove': |
46 | | - return checkValue === undefined && value !== undefined; |
| 45 | + return checkValue === undefined && value !== undefined |
47 | 46 | case 'only-changed': |
48 | | - return value !== undefined && checkValue !== undefined && value !== checkValue; |
| 47 | + return value !== undefined && checkValue !== undefined && value !== checkValue |
49 | 48 | case 'only-add-change': |
50 | 49 | default: |
51 | | - return value !== checkValue; |
| 50 | + return value !== checkValue |
52 | 51 | } |
53 | 52 | } |
54 | 53 |
|
55 | | -function mustAssignObject(object) { |
56 | | - return Object.keys(object).length > 0; |
| 54 | +function mustAssignObject (object) { |
| 55 | + return Object.keys(object).length > 0 |
57 | 56 | } |
58 | 57 |
|
59 | | -function mustAssignArray(array) { |
60 | | - return array.length > 0; |
| 58 | +function mustAssignArray (array) { |
| 59 | + return array.length > 0 |
61 | 60 | } |
62 | 61 |
|
63 | | -function compare(base, comparison, opts) { |
| 62 | +function compare (base, comparison, opts) { |
64 | 63 | if (_.isObjectLike(base) && !_.isDate(base)) { |
65 | 64 | if (_.isArray(base)) { |
66 | | - const comparisonArray = comparison || []; |
| 65 | + const comparisonArray = comparison || [] |
67 | 66 | const newArray = base |
68 | 67 | .map((e, i) => compare(e, comparisonArray[i], opts)) |
69 | | - .filter(e => e !== ignore); |
| 68 | + .filter(e => e !== ignore) |
70 | 69 | if (mustAssignArray(newArray)) { |
71 | | - return newArray; |
| 70 | + return newArray |
72 | 71 | } |
73 | 72 | } else { |
74 | | - const object = transformObject(base, comparison, opts); |
| 73 | + const object = transformObject(base, comparison, opts) |
75 | 74 | if (mustAssignObject(object)) { |
76 | | - return object; |
| 75 | + return object |
77 | 76 | } |
78 | 77 | } |
79 | 78 | } else if (mustAssignValue(base, comparison, opts)) { |
80 | | - return base; |
| 79 | + return base |
81 | 80 | } |
82 | | - return ignore; |
| 81 | + return ignore |
83 | 82 | } |
84 | 83 |
|
85 | | -function transformObject(newJson, oldJson, opts) { |
| 84 | +function transformObject (newJson, oldJson, opts) { |
86 | 85 | return _.transform(newJson, (result, value, key) => { |
87 | | - const compareVal = oldJson !== undefined ? oldJson[key] : undefined; |
88 | | - const out = compare(value, compareVal, opts); |
| 86 | + const compareVal = oldJson !== undefined ? oldJson[key] : undefined |
| 87 | + const out = compare(value, compareVal, opts) |
89 | 88 | if (out !== ignore) { |
90 | 89 | // Object.defineProperty(result, key, { |
91 | 90 | // value: out, |
92 | 91 | // writable: true, |
93 | 92 | // enumerable: true, |
94 | 93 | // configurable: false, |
95 | 94 | // }); |
96 | | - result[key] = out; |
| 95 | + result[key] = out |
97 | 96 | } |
98 | | - }, {}); |
| 97 | + }, {}) |
99 | 98 | } |
100 | 99 |
|
101 | | -module.exports = function differenceValues(newJson, oldJson, opts = {}) { |
102 | | - let out; |
103 | | - _.defaults(opts, DEFAULT_OPTIONS); |
| 100 | +module.exports = function differenceValues (newJson, oldJson, opts = {}) { |
| 101 | + let out |
| 102 | + _.defaults(opts, DEFAULT_OPTIONS) |
104 | 103 | if (opts.extract === 'only-remove') { |
105 | | - out = transformObject(oldJson, newJson, opts); |
| 104 | + out = transformObject(oldJson, newJson, opts) |
106 | 105 | } else { |
107 | | - out = transformObject(newJson, oldJson, opts); |
| 106 | + out = transformObject(newJson, oldJson, opts) |
108 | 107 | } |
109 | | - return out; |
110 | | -}; |
| 108 | + return out |
| 109 | +} |
0 commit comments