1 |
|
2 |
|
3 |
|
4 | !function(exports) {
|
5 | var pointerCache = {}
|
6 | , hasOwn = pointerCache.hasOwnProperty
|
7 |
|
8 | exports.clone = clone
|
9 | exports.merge = merge
|
10 | exports.mergePatch = mergePatch
|
11 | exports.isObject = isObject
|
12 |
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 | function mergePatch(target, patch, changed, previous, pointer) {
|
19 | var undef, key, oldVal, val, len, nextPointer
|
20 | if (isObject(patch)) {
|
21 | if (!pointer) {
|
22 | pointer = ""
|
23 | }
|
24 | if (!isObject(target)) {
|
25 | target = {}
|
26 | }
|
27 | for (key in patch) if (
|
28 | undef !== (oldVal = target[key], val = patch[key]) &&
|
29 | hasOwn.call(patch, key) &&
|
30 | (
|
31 | undef == val ?
|
32 | undef !== oldVal && delete target[key] :
|
33 | target[key] !== val
|
34 | )
|
35 | ) {
|
36 | nextPointer = pointer + "/" + key.replace(/~/g, "~0").replace(/\//g, "~1")
|
37 | len = changed && isObject(target[key]) && changed.length
|
38 | if (undef != val) {
|
39 | target[key] = mergePatch(target[key], val, changed, previous, nextPointer)
|
40 | }
|
41 | if (len === false || changed && len != changed.length) {
|
42 | changed.push(nextPointer)
|
43 | if (previous && !isObject(oldVal)) {
|
44 | previous[nextPointer] = oldVal
|
45 | }
|
46 | }
|
47 | }
|
48 | } else {
|
49 | if (changed && isObject(target)) {
|
50 | val = {}
|
51 | for (key in target) if (hasOwn.call(target, key)) {
|
52 | val[key] = null
|
53 | }
|
54 | mergePatch(target, val, changed, previous, pointer)
|
55 | }
|
56 | target = patch
|
57 | }
|
58 | return target
|
59 | }
|
60 |
|
61 | function merge(target) {
|
62 | for (var key, source, a = arguments, i = 1, len = a.length; i < len; ) {
|
63 | if (source = a[i++]) for (key in source) if (hasOwn.call(source, key)) {
|
64 | target[key] = source[key]
|
65 | }
|
66 | }
|
67 | return target
|
68 | }
|
69 |
|
70 | function clone(obj) {
|
71 | var temp, key
|
72 | if (obj && typeof obj == "object") {
|
73 |
|
74 | temp = obj instanceof Date ? new Date(+obj) :
|
75 | obj instanceof RegExp ? RegExp(obj.source, (""+obj).split("/").pop()) :
|
76 | obj.constructor()
|
77 | for (key in obj) if (hasOwn.call(obj, key)) {
|
78 | temp[key] = clone(obj[key])
|
79 | }
|
80 | obj = temp
|
81 | }
|
82 | return obj
|
83 | }
|
84 |
|
85 | function isObject(obj) {
|
86 | return !!obj && obj.constructor === Object
|
87 | }
|
88 |
|
89 |
|
90 | }(this.JSON || this)
|
91 |
|
92 |
|