-
Notifications
You must be signed in to change notification settings - Fork 0
/
npm.comparisons.js
1001 lines (834 loc) · 28.8 KB
/
npm.comparisons.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
var comparisons = (
function () {
var typeOf,
isEmpty,
each,
every,
some,
has,
merge,
pathToObject,
mapHandler,
methods = {};
////////////////////////////////////////////////////////////////////////////////
// The genius work of Douglas Crockford
/**
* A function for getting a more better typeof value than the built-in one
*
* @method typeOf
* @param value {Variable} Any value to be tested
* @return {String} A string containing the type of the supplied variable
**/
methods.typeOf = function ( value ) {
var s = typeof value;
if ( s === 'object' ) {
if ( value ) {
if ( typeof value.length === 'number' && !( value.propertyIsEnumerable( 'length' ) ) && typeof value.splice === 'function' ) {
s = 'array';
}
} else {
s = 'null';
}
}
return s;
};
////////////////////////////////////////////////////////////////////////////////
// The genius work of Douglas Crockford
/**
* A function for testing if a variable is empty
*
* @method isEmpty
* @param o {Object} Any object to be tested
* @return {Boolean} True if the object contains enumerable members or False if not
**/
methods.isEmpty = function ( o ) {
var i,
v;
if ( methods.typeOf( o ) === 'object' ) {
for ( i in o ) {
v = o[i];
if ( v !== undefined && methods.typeOf( v ) !== 'function' ) {
return false;
}
}
}
return true;
};
////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////
// Iterate through an array or object
methods.each = function ( object, callback, withInherited, withFunctions ) {
var returnObject = {},
count = 0,
key;
for ( key in object ) {
if ( object.hasOwnProperty( key ) || withInherited ) {
if ( typeof object[ key ] !== 'function' || ( typeof object[ key ] === 'function' && withFunctions ) ) {
returnObject[ key ] = callback.call( object, key, object[ key ], count );
count++;
}
}
}
return returnObject;
};
////////////////////////////////////////////////////////////////////////////////
// Iterate through an array or object
methods.every = function ( object, callback, withInherited, withFunctions ) {
var returnValue = true,
count = 0,
key;
for ( key in object ) {
if ( object.hasOwnProperty( key ) || withInherited ) {
if ( typeof object[ key ] !== 'function' || ( typeof object[ key ] === 'function' && withFunctions ) ) {
returnValue = callback.call( object, key, object[ key ], count );
if ( !returnValue ) {
return false;
}
}
}
}
return true;
};
////////////////////////////////////////////////////////////////////////////////
// Iterate through an array or object
methods.some = function ( object, callback, withInherited, withFunctions ) {
var returnValue = false,
count = 0,
key;
for ( key in object ) {
if ( object.hasOwnProperty( key ) || withInherited ) {
if ( typeof object[ key ] !== 'function' || ( typeof object[ key ] === 'function' && withFunctions ) ) {
returnValue = callback.call( object, key, object[ key ], count );
if ( returnValue ) {
return true;
}
}
}
}
return false;
};
////////////////////////////////////////////////////////////////////////////////
// Check if an object has a property (or array key)
methods.has = function ( object, string, withInherited, withFunctions ) {
var key;
for ( key in object ) {
if ( object.hasOwnProperty( key ) || withInherited ) {
if ( string instanceof RegExp ) {
if ( ( typeof object[ key ] !== 'function' || ( typeof object[ key ] !== 'function' && withFunctions ) ) && string.test( key ) ) {
return true;
}
}
else {
if ( ( typeof object[ key ] !== 'function' || ( typeof object[ key ] !== 'function' && withFunctions ) ) && key === string ) {
return true;
}
}
}
}
return false;
};
////////////////////////////////////////////////////////////////////////////////
// Combine objects
methods.merge = function () {
var returnObject = {},
object = {},
i,
key,
deep = false;
var argLen = arguments.length;
if ( argLen > 1 && typeof arguments[ argLen - 1 ] === "boolean" && arguments[ argLen - 1 ] ) {
deep = true;
}
returnObject = ( typeof arguments[ 0 ] === "object" ) ? arguments[ 0 ] : returnObject;
for ( i = 1; i < argLen; i++ ) {
object = arguments[ i ];
if ( object && typeof object === "object" ) {
for ( key in object ) {
if ( object.hasOwnProperty( key ) ) {
if ( deep && ( typeof object[ key ] === "object" ) && ( object[ key ] !== null ) && !( typeof object[ key ] instanceof RegExp ) ) {
if ( object[ key ] instanceof Array ) {
returnObject[ key ] = object[ key ].concat();
}
else if ( ( returnObject[ key ] !== undefined ) && ( typeof returnObject[ key ] === "object" ) && ( returnObject[ key ] !== null ) && !( returnObject[ key ] instanceof Array ) ) {
returnObject[ key ] = methods.merge( returnObject[ key ], object[ key ], deep );
}
else {
returnObject[ key ] = methods.merge( {}, object[ key ], deep );
}
}
else {
returnObject[ key ] = object[ key ];
}
}
}
}
}
return returnObject;
};
////////////////////////////////////////////////////////////////////////////////
methods.makeMap = function () {
var map = {},
object,
skipTrue = false;
object = arguments[ 0 ];
if ( arguments.length > 1 && methods.typeOf( arguments[ arguments.length - 1 ] ) === "boolean" ) {
skipTrue = arguments[ arguments.length - 1 ];
}
if ( arguments.length > 2 && methods.typeOf( arguments[ arguments.length - 2 ] ) === "boolean" ) {
skipTrue = arguments[ arguments.length - 2 ];
map = arguments[ arguments.length - 1 ];
}
if ( methods.typeOf( object ) === "array" ) {
map.$type = "array";
map.$unid = false;
map.$sort = false;
object.forEach(
function ( element ) {
if ( map.$child === undefined || map.$child === true ) {
if ( methods.typeOf( element ) === "array" || methods.typeOf( element ) === "object" ) {
map.$child = methods.makeMap( element, skipTrue );
}
}
else {
if ( map.$child === undefined && !skipTrue ) {
map[ key ] = true;
}
}
}
);
}
else if ( methods.typeOf( object ) === "object" ) {
map.$type = "object";
methods.each(
object,
function ( key, value, i ) {
if ( methods.typeOf( value ) === "array" || methods.typeOf( value ) === "object" ) {
map[ key ] = methods.makeMap( value, skipTrue );
}
else {
if ( !skipTrue ) {
map[ key ] = true;
}
}
}
);
}
if ( arguments.length > 1 ) {
Array.prototype.slice.call(
arguments,
function ( element, i ) {
if ( i > 0 ) {
var extraMap = methods.makeMap( element, skipTrue );
if ( extraMap.$type && extraMap.$type === map.$type ) {
map = methods.merge( extraMap, map );
}
}
}
);
}
return map;
};
////////////////////////////////////////////////////////////////////////////////
pathToObject = function ( object, path, value ) {
var current,
length;
value = ( value === undefined ) ? false : value;
current = object;
path = path.split( "." );
length = path.length;
path.some(
function ( prop, iter ) {
if ( ( iter + 1 ) < length ) {
if ( current[ prop ] !== false && current[ prop ] !== null ) {
if ( current[ prop ] === undefined ) {
current[ prop ] = {};
}
current = current[ prop ];
}
else {
return true;
}
}
else {
current[ prop ] = value;
return true;
}
}
);
return object;
};
mapHandler = function ( map ) {
if ( typeof map === "string" ) {
map = [ map ];
}
if ( map instanceof Array ) {
map = map.reduce(
function ( result, element, iter ) {
if ( typeof element === "object" ) {
Object.keys( element ).forEach(
function ( key ) {
var value;
value = element[ key ];
result = pathToObject( result, key, value );
}
);
}
else if ( typeof element === "string" ) {
result = pathToObject( result, element, false );
}
return result;
},
{}
);
}
return map;
};
////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// equals
// Identify if two objects are different
//
// Object A is assumed to be 'new', and Object B is assumed to be 'old'
//
methods.equals = function ( objectA, objectB, map ) {
var result = false,
keysA,
keysB,
keysBDifference,
keysMap;
////////////////////////////////////////
map = mapHandler( map );
////////////////////////////////////////
// Check if they are pointing at the same variable. If they are, no need to test further.
if ( objectA === objectB ) {
return true;
}
// Check if they are the same type. If they are not, no need to test further.
if ( methods.typeOf( objectA ) !== methods.typeOf( objectB ) ) {
return false;
}
// Check what kind of variables they are to see what sort of comparison we should make.
if ( typeof objectA === "object" ) {
// Check if they have the same constructor, so that we are comparing apples with apples.
if ( objectA.constructor === objectA.constructor ) {
// If we are working with Arrays...
if ( methods.typeOf( objectA ) === "array" ) {
// Check the arrays are the same length. If not, they cannot be the same.
if ( objectA.length === objectB.length ) {
// Compare each element. They must be identical. If not, the comparison stops immediately and returns false.
if ( map && map.$sort ) {
(function () {
var objectASorted,
objectBSorted,
sortByID;
if ( typeof map.$sort === true && map.$unid ) {
sortByID = function ( a, b ) {
var af = ( a[ map.$unid ] !== undefined ) ? a[ map.$unid ] : 0,
bf = ( b[ map.$unid ] !== undefined ) ? b[ map.$unid ] : 0,
r = 0;
if ( af !== bf ) {
r = ( af < bf ) ? -1 : 1;
}
return r;
};
objectASorted = objectA.concat().sort( sortByID );
objectBSorted = objectB.concat().sort( sortByID );
}
else if ( typeof map.$sort === "function" ) {
objectASorted = objectA.concat().sort( map.$sort );
objectBSorted = objectB.concat().sort( map.$sort );
}
else {
objectASorted = objectA.concat().sort();
objectBSorted = objectB.concat().sort();
}
return objectASorted.every(
function ( element, i ) {
return methods.equals( element, objectBSorted[ i ], ( map.$child ) ? map.$child : null );
}
);
})();
}
else {
return objectA.every(
function ( element, i ) {
return methods.equals( element, objectB[ i ], ( map.$child ) ? map.$child : null );
}
);
}
}
// They are not the same length, and so are not identical.
else {
return false;
}
}
// If we are working with RegExps...
else if ( objectA instanceof RegExp ) {
// Return the results of a string comparison of the expression.
return ( objectA.toString() === objectB.toString() );
}
// Else we are working with other types of objects...
else {
// Get the keys as arrays from both objects. This uses Object.keys, so no old browsers here.
keysA = Object.keys( objectA );
keysB = Object.keys( objectB );
/*
// We don't need two check keys from both objects if they have the same number of keys
keysBDifference = keysB.filter(
function ( element ) {
if ( keysA.indexOf( element ) === -1 ) {
return true;
}
}
);
*/
if ( map ) {
keysA = keysA.filter(
function ( element ) {
if ( map[ element ] !== false && map[ element ] !== null ) {
return true;
}
}
);
keysB = keysB.filter(
function ( element ) {
if ( map[ element ] !== false && map[ element ] !== null ) {
return true;
}
}
);
}
// Check the key arrays are the same length. If not, they cannot be the same.
if ( keysA.length === keysB.length ) {
// Compare each property. They must be identical. If not, the comparison stops immediately and returns false.
return keysA.every(
function ( element ) {
if ( map ) {
return methods.equals( objectA[ element ], objectB[ element ], map[ element ] );
}
else {
return methods.equals( objectA[ element ], objectB[ element ] );
}
}
);
}
// They do not have the same number of keys, and so are not identical.
else {
return false;
}
}
}
// They don't have the same constructor.
else {
return false;
}
}
// If they are both functions, let us do a string comparison.
else if ( typeof objectA === "function" ) {
return ( objectA.toString() === objectB.toString() );
}
// If a simple variable type, compare directly without coercion.
else {
return ( objectA === objectB );
}
// Return a default if nothing has already been returned.
return result;
};
////////////////////////////////////////
//
// differences
// Identify and notify for all differences between two objects
// callback( path, status, valueA, valueB )
//
// Object A is assumed to be 'new', and Object B is assumed to be 'old'
//
methods.differences = function ( objectA, objectB, options ) {
var result = true,
opts = {map: null, path: "", callback: null, mode: "all"}, // map, sorts, path, callback
callback,
map,
pathParent = "",
keysA,
keysB,
keysBDifference,
keysMap,
callbackTest,
elementsResult = true;
////////////////////////////////////////
opts = methods.merge( opts, options );
callback = opts.callback || null;
////////////////////////////////////////
if ( !opts.path ) {
opts.path = "";
}
else {
if ( opts.path && opts.path.length ) {
pathParent = opts.path + ".";
}
}
////////////////////////////////////////
callbackTest = function ( result, status, valueA, valueB, path ) {
if ( !path ) {
path = opts.path;
}
if ( ( !result || opts.mode === "all" ) && path && callback && typeof callback === "function" ) {
if ( opts.mode === "all" || ( status !== "equal" && status !== "array.equal" ) ) {
callback( path, status, valueA, valueB );
}
}
};
////////////////////////////////////////
opts.map = mapHandler( opts.map );
map = opts.map;
////////////////////////////////////////
// Check if they are pointing at the same variable. If they are, no need to test further.
if ( objectA === objectB ) {
result = true;
callbackTest( result, "equal", objectA, objectB );
return true;
}
////////////////////////////////////////
// Check if they are the same type. If they are not, no need to test further.
if ( methods.typeOf( objectA ) !== methods.typeOf( objectB ) ) {
result = false;
callbackTest( result, "type.mismatch", objectA, objectB );
return result;
}
////////////////////////////////////////
// Check what kind of variables they are to see what sort of comparison we should make.
if ( typeof objectA === "object" ) {
// Check if they have the same constructor, so that we are comparing apples with apples.
if ( objectA.constructor === objectB.constructor ) {
// If we are working with Arrays...
if ( methods.typeOf( objectA ) === "array" ) {
// Check the arrays are the same length. If not, they cannot be the same.
// $compare: "simple"
// $compare: "basic"
// Sort arrays
// Iterate over array A
// if match, store match iter
// if none
// array.added
// array.changed
// array.removed
// array.noid
// array.mismatch
(function () {
var objectASorted,
objectBSorted,
sortByID,
objectALength,
objectBLength,
startB = 0,
totalCounter = 0,
uniqueCounter = 0,
foundAny = false,
mapChild = null;
objectALength = objectA.length;
objectBLength = objectB.length;
if ( ( map && map.$compare && map.$compare === "simple" ) || !map ) {
if ( objectA.length !== objectB.length ) {
result = false;
callbackTest( result, "array.mismatch", objectA, objectB );
return result;
}
}
if ( map && map.$sort ) {
if ( map.$sort === true && map.$unid ) {
sortByID = function ( a, b ) {
var af = ( a[ map.$unid ] !== undefined ) ? a[ map.$unid ] : 0,
bf = ( b[ map.$unid ] !== undefined ) ? b[ map.$unid ] : 0,
r = 0;
if ( af !== bf ) {
r = ( af < bf ) ? -1 : 1;
}
return r;
};
objectASorted = objectA.concat().sort( sortByID );
objectBSorted = objectB.concat().sort( sortByID );
}
else if ( typeof map.$sort === "function" ) {
objectASorted = objectA.concat().sort( map.$sort );
objectBSorted = objectB.concat().sort( map.$sort );
}
else {
objectASorted = objectA.concat().sort();
objectBSorted = objectB.concat().sort();
}
}
else {
objectASorted = objectA;
objectBSorted = objectB;
}
if ( map && map.$child ) {
mapChild = map.$child;
}
// Should we do a simple comparison of the array?
// If the arrays are not the same length, it fails
if ( map && map.$compare && map.$compare === "simple" ) {
if ( objectA.length === objectB.length ) {
// Compare each element. They must be identical. If not, the comparison stops immediately and returns false.
objectASorted.forEach(
function ( element, i ) {
elementsResult = methods.differences( element, objectBSorted[ i ], {map: mapChild, path: pathParent + i, callback: callback, mode: opts.mode} );
if ( elementsResult ) {
callbackTest( true, "array.equal", element, objectBSorted[ j ], pathParent + i );
}
else {
callbackTest( false, "array.changed", element, objectBSorted[ j ], pathParent + i );
result = false;
}
}
);
}
// They are not the same length, and so are not identical.
else {
result = false;
callbackTest( result, "array.mismatch", objectA, objectB, path );
return result;
}
}
else {
// array.added
// array.changed
// array.removed
// array.noid
objectASorted.forEach(
function ( element, i ) {
var uniqueAIDValue,
uniqueBIDValue,
originalStartB = startB,
j = startB,
k = 0,
objectBItem,
found = false,
first = false;
// If there's a unique ID, we should expect elements to have them for a safe comparison
if ( map ) {
// A unique ID property has been defined that we should look for.
// If we don't have one, we do naive comparison, one to one.
if ( element[ map.$unid ] !== undefined ) {
// This element has a unique ID
uniqueAIDValue = element[ map.$unid ];
}
else {
// console.warn( "No unique id for this element." )
// It doesn't have the unique ID property
// Compare to the first one that doesn't have the unique ID property, and so on.
}
for ( ; j < objectBLength; j++ ) {
objectBItem = objectBSorted[ j ];
if ( map.$unid ) {
if ( element[ map.$unid ] !== undefined ) {
// && objectBItem[ map.$unid ] !== undefined
// Element B also has to have the unique ID
if ( element[ map.$unid ] === objectBItem[ map.$unid ] ) {
startB = j + 1;
found = true;
if ( !foundAny ) {
foundAny = true;
first = true;
}
// It's the same item
elementsResult = methods.differences( element, objectBSorted[ j ], {map: mapChild, path: pathParent + i, callback: callback, mode: opts.mode} );
if ( elementsResult ) {
callbackTest( true, "array.equal", element, objectBSorted[ j ], pathParent + i );
}
else {
callbackTest( false, "array.changed", element, objectBSorted[ j ], pathParent + i );
}
break;
}
else {
}
}
else {
// It doesn't have the unique ID property
// Compare to the first one that doesn't have the unique ID property, and so on.
}
}
else {
if ( element === objectBItem ) {
// It's the same item
startB = j + 1;
found = true;
if ( !foundAny ) {
foundAny = true;
first = true;
}
totalCounter++;
callbackTest( true, "array.matched", element, objectBItem, pathParent + i );
break;
}
else {
// No match, ignore it and move on
// console.log( "No match between A Element " + i + ": " + element[ opts.map.$unid ] + " and B Element " + j + ": " + objectBItem[ opts.map.$unid ] + ";" )
}
}
}
// If this is the first matched item
if ( first ) {
// if the found index is not the first and less than the array length (it should be, if an item was found)
if ( j > 0 && j < objectBLength ) {
for ( k = 0; k < j; k++ ) {
// For each k
totalCounter++;
callbackTest( false, "array.removed", null, objectBSorted[ k ], pathParent + k );
}
}
}
// Or if there was a gap in matches
else if ( j > originalStartB && j < objectBLength ) {
for ( k = originalStartB; k < j; k++ ) {
// For each k
totalCounter++;
callbackTest( false, "array.removed", null, objectBSorted[ k ], pathParent + k );
}
}
// If no matches were found for this item
if ( !found ) {
// The element is new
totalCounter++;
callbackTest( false, "array.added", element, null, pathParent + i );
}
}
// There no unique ID. We must assume a 1-to-1 mapping for testing.
else {
totalCounter++;
elementsResult = methods.differences( element, objectBSorted[ i ], {map: mapChild, path: pathParent + i, callback: callback, mode: opts.mode} );
if ( elementsResult ) {
callbackTest( true, "array.equal", element, objectBSorted[ j ], pathParent + i );
}
else {
callbackTest( false, "array.changed", element, objectBSorted[ j ], pathParent + i );
}
}
// Check if this is the last one
if ( i === ( objectALength - 1 ) ) {
// We're still starting the loop from 0
// It means we found no matches
if ( startB < objectBLength ) {
for ( k = startB; k < objectBLength; k++ ) {
// For each k
totalCounter++;
callbackTest( false, "array.removed", null, objectBSorted[ k ], pathParent + k );
}
}
}
}
);
if ( !elementsResult ) {
result = false;
}
}
})();
}
// If we are working with RegExps...
else if ( objectA instanceof RegExp ) {
// Return the results of a string comparison of the expression.
result = ( objectA.toString() === objectB.toString() );
callbackTest( result, ( result ? "equal" : "regex.mismatch" ), objectA, objectB );
return result;
}
else if ( objectA instanceof Date ) {
// Return the results of a string comparison of the expression.
result = ( objectA.toString() === objectB.toString() );
callbackTest( result, ( result ? "equal" : "date.mismatch" ), objectA, objectB );
return result;
}
// Else we are working with other types of objects...
else {
// Get the keys as arrays from both objects. This uses Object.keys, so no old browsers here.
keysA = Object.keys( objectA );
keysB = Object.keys( objectB );
keysBDifference = keysB.filter(
function ( element ) {
if ( keysA.indexOf( element ) === -1 ) {
return true;
}
}
);
if ( map ) {
keysA = keysA.filter(
function ( element ) {
if ( map[ element ] !== false && map[ element ] !== null ) {
return true;
}
}
);
/*
keysB = keysB.filter(
function ( element ) {
if ( map[ element ] !== false && map[ element ] !== null ) {
return true;
}
}
);
*/
keysBDifference = keysBDifference.filter(
function ( element ) {
if ( map[ element ] !== false && map[ element ] !== null ) {
return true;
}
}
);
}
// Compare each property. They must be identical. If not, the comparison stops immediately and returns false.
keysA.forEach(
function ( element ) {
var keysResult = true,
mapChild = null;
if ( map && map[ element ] && ( ["$sort", "$unid", "$compare", "$type"].indexOf( element ) === -1 ) ) {
mapChild = map[ element ];
}
keysResult = methods.differences( objectA[ element ], objectB[ element ], {map: mapChild, path: pathParent + element, callback: callback, mode: opts.mode} );
if ( !keysResult ) {
result = false;
}
}
);
if ( keysBDifference.length ) {
result = false;
}
// Compare each property. They must be identical. If not, the comparison stops immediately and returns false.
keysBDifference.forEach(
function ( element ) {
callbackTest( false, "property.removed", null, objectB[ element ], pathParent + element );
}
);
return result;
}
}
// They don't have the same constructor.
else {
result = false;
callbackTest( result, "constructor.mismatch", objectA, objectB );
return result;
}
}
// If they are both functions, let us do a string comparison.
else if ( typeof objectA === "function" ) {
result = ( objectA.toString() === objectB.toString() );
callbackTest( result, ( result ? "equal" : "function.mismatch" ), objectA, objectB );
return result;
}
// If a simple variable type, compare directly without coercion.
else {
result = ( objectA === objectB );
callbackTest( result, ( result ? "equal" : "value.mismatch" ), objectA, objectB );
return result;
}
////////////////////////////////////////
// Return a default if nothing has already been returned.
return result;
};
return methods;
}
)();
////////////////////////////////////////
module.exports = comparisons;