1- const _ = require ( 'lodash' ) ;
21const https = require ( 'https' ) ;
32
43/**
@@ -9,159 +8,6 @@ const https = require('https');
98 */
109module . exports . sleep = ( ms ) => new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
1110
12- /**
13- * Make `deepDiff` exportable
14- * return {object}
15- */
16- module . exports . deepDiff = deepDiff ;
17-
18- /**
19- * Make `deepMerge` exportable
20- * return {object}
21- */
22- module . exports . deepMerge = deepMerge ;
23-
24- /**
25- * Recursively scans two variables and returns another object with diffs
26- *
27- * @param {object|Array } source - source object
28- * @param {object|Array } target - target object
29- * @returns {object }
30- */
31- function deepDiff ( source , target ) {
32- const sourceType = typeOf ( source ) ;
33- const targetType = typeOf ( target ) ;
34-
35- /**
36- * If we'll compare NOTHING with SOMETHING, the diff will be SOMETHING
37- */
38- if ( source === undefined ) {
39- return target ;
40- }
41-
42- /**
43- * If we'll compare SOMETHING with NOTHING, the diff will be NOTHING
44- */
45- if ( targetType === undefined ) {
46- return undefined ;
47- }
48-
49- /**
50- * We CAN'T compare apples with dogs
51- */
52- if ( sourceType !== targetType ) {
53- return undefined ;
54- }
55-
56- if ( targetType === 'array' ) {
57- return arrayDiff ( source , target ) ;
58- } else if ( targetType === 'object' ) {
59- return objectDiff ( source , target ) ;
60- } else if ( source !== target ) {
61- return target ;
62- } else {
63- return source ;
64- }
65- }
66-
67- /**
68- * Returns two arrays difference as an new array
69- *
70- * @param {Array } source - source object
71- * @param {Array } target - target object
72- * @returns {Array }
73- */
74- function arrayDiff ( source , target ) {
75- const diffArray = [ ] ;
76-
77- for ( let i = 0 ; i < target . length ; i ++ ) {
78- diffArray [ i ] = deepDiff ( source [ i ] , target [ i ] ) ;
79- }
80-
81- return diffArray ;
82- }
83-
84- /**
85- * Returns two objects difference as new object
86- *
87- * @param {object } objectA - first object for comparing
88- * @param {object } objectB - second object for comparing
89- *
90- * @returns {object }
91- */
92- function objectDiff ( objectA , objectB ) {
93- const diffObject = { } ;
94-
95- /**
96- * objectA is a subject,
97- * we compare objectB patches
98- *
99- * For that we enumerate objectB props and assume that
100- * target object has any changes
101- *
102- * But target object might have additional patches that might not be in subject
103- * This corner case says us that whole property is a patch
104- */
105- if ( ! objectA ) {
106- return objectB ;
107- }
108-
109- Object . keys ( objectB ) . forEach ( ( prop ) => {
110- const objectAItem = objectA [ prop ] ;
111- const objectBItem = objectB [ prop ] ;
112-
113- if ( objectAItem === undefined ) {
114- diffObject [ prop ] = objectBItem ;
115-
116- return ;
117- }
118-
119- if ( objectAItem === objectBItem ) {
120- return ;
121- }
122-
123- diffObject [ prop ] = deepDiff ( objectAItem , objectBItem ) ;
124- } ) ;
125-
126- return diffObject ;
127- }
128-
129- /**
130- * Merge to objects recursively
131- *
132- * @param {object } target - target object
133- * @param {object[] } sources - sources for mering
134- * @returns {object }
135- */
136- function deepMerge ( target , ...sources ) {
137- const isObject = ( item ) => item && typeOf ( item ) === 'object' ;
138-
139- return _ . mergeWith ( { } , target , ...sources , function ( _subject , _target ) {
140- if ( _ . isArray ( _subject ) && _ . isArray ( _target ) ) {
141- const biggerArray = _subject . length > _target . length ? _subject : _target ;
142- const lesser = _subject . length > _target . length ? _target : _subject ;
143-
144- return biggerArray . map ( ( el , i ) => {
145- if ( isObject ( el ) && isObject ( lesser [ i ] ) ) {
146- return _ . mergeWith ( { } , el , lesser [ i ] ) ;
147- } else {
148- return el ;
149- }
150- } ) ;
151- }
152- } ) ;
153- }
154-
155- /**
156- * Returns real type of passed variable
157- *
158- * @param {* } obj - value to check
159- * @returns {string }
160- */
161- function typeOf ( obj ) {
162- return Object . prototype . toString . call ( obj ) . match ( / \s ( [ a - z A - Z ] + ) / ) [ 1 ] . toLowerCase ( ) ;
163- }
164-
16511/**
16612 * Sends alert to the Slack/Telegram
16713 *
0 commit comments