Change mergeObject to have option to keep references
Showing
1 changed file
with
27 additions
and
2 deletions
... | @@ -622,15 +622,31 @@ function isPlainObject(obj) { | ... | @@ -622,15 +622,31 @@ function isPlainObject(obj) { |
622 | return (type === 'object'); | 622 | return (type === 'object'); |
623 | } | 623 | } |
624 | 624 | ||
625 | function mergeObjectsInSlimerjs(origin, add) { | 625 | /** |
626 | * Object recursive merging utility for use in the SlimerJS environment | ||
627 | * | ||
628 | * @param Object origin the origin object | ||
629 | * @param Object add the object to merge data into origin | ||
630 | * @param Object opts optional options to be passed in | ||
631 | * @return Object | ||
632 | */ | ||
633 | function mergeObjectsInSlimerjs(origin, add, opts) { | ||
626 | "use strict"; | 634 | "use strict"; |
635 | |||
636 | var options = opts || {}, | ||
637 | keepReferences = options.keepReferences; | ||
638 | |||
627 | for (var p in add) { | 639 | for (var p in add) { |
628 | if (isPlainObject(add[p])) { | 640 | if (isPlainObject(add[p])) { |
629 | if (isPlainObject(origin[p])) { | 641 | if (isPlainObject(origin[p])) { |
630 | origin[p] = mergeObjects(origin[p], add[p]); | 642 | origin[p] = mergeObjects(origin[p], add[p]); |
631 | } else { | 643 | } else { |
644 | if (keepReferences) { | ||
645 | origin[p] = add[p]; | ||
646 | } else { | ||
632 | origin[p] = clone(add[p]); | 647 | origin[p] = clone(add[p]); |
633 | } | 648 | } |
649 | } | ||
634 | } else { | 650 | } else { |
635 | origin[p] = add[p]; | 651 | origin[p] = add[p]; |
636 | } | 652 | } |
... | @@ -643,24 +659,33 @@ function mergeObjectsInSlimerjs(origin, add) { | ... | @@ -643,24 +659,33 @@ function mergeObjectsInSlimerjs(origin, add) { |
643 | * | 659 | * |
644 | * @param Object origin the origin object | 660 | * @param Object origin the origin object |
645 | * @param Object add the object to merge data into origin | 661 | * @param Object add the object to merge data into origin |
662 | * @param Object opts optional options to be passed in | ||
646 | * @return Object | 663 | * @return Object |
647 | */ | 664 | */ |
648 | function mergeObjects(origin, add) { | 665 | function mergeObjects(origin, add, opts) { |
649 | "use strict"; | 666 | "use strict"; |
650 | 667 | ||
668 | var options = opts || {}, | ||
669 | keepReferences = options.keepReferences; | ||
670 | |||
651 | if (phantom.casperEngine === 'slimerjs') { | 671 | if (phantom.casperEngine === 'slimerjs') { |
652 | // Because of an issue in the module system of slimerjs (security membranes?) | 672 | // Because of an issue in the module system of slimerjs (security membranes?) |
653 | // constructor is undefined. | 673 | // constructor is undefined. |
654 | // let's use an other algorithm | 674 | // let's use an other algorithm |
655 | return mergeObjectsInSlimerjs(origin, add); | 675 | return mergeObjectsInSlimerjs(origin, add); |
656 | } | 676 | } |
677 | |||
657 | for (var p in add) { | 678 | for (var p in add) { |
658 | if (add[p] && add[p].constructor === Object) { | 679 | if (add[p] && add[p].constructor === Object) { |
659 | if (origin[p] && origin[p].constructor === Object) { | 680 | if (origin[p] && origin[p].constructor === Object) { |
660 | origin[p] = mergeObjects(origin[p], add[p]); | 681 | origin[p] = mergeObjects(origin[p], add[p]); |
661 | } else { | 682 | } else { |
683 | if (keepReferences) { | ||
684 | origin[p] = add[p]; | ||
685 | } else { | ||
662 | origin[p] = clone(add[p]); | 686 | origin[p] = clone(add[p]); |
663 | } | 687 | } |
688 | } | ||
664 | } else { | 689 | } else { |
665 | origin[p] = add[p]; | 690 | origin[p] = add[p]; |
666 | } | 691 | } | ... | ... |
-
Please register or sign in to post a comment