source: trip-planner-front/node_modules/rfdc/index.js@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.5 KB
Line 
1'use strict'
2module.exports = rfdc
3
4function copyBuffer (cur) {
5 if (cur instanceof Buffer) {
6 return Buffer.from(cur)
7 }
8
9 return new cur.constructor(cur.buffer.slice(), cur.byteOffset, cur.length)
10}
11
12function rfdc (opts) {
13 opts = opts || {}
14
15 if (opts.circles) return rfdcCircles(opts)
16 return opts.proto ? cloneProto : clone
17
18 function cloneArray (a, fn) {
19 var keys = Object.keys(a)
20 var a2 = new Array(keys.length)
21 for (var i = 0; i < keys.length; i++) {
22 var k = keys[i]
23 var cur = a[k]
24 if (typeof cur !== 'object' || cur === null) {
25 a2[k] = cur
26 } else if (cur instanceof Date) {
27 a2[k] = new Date(cur)
28 } else if (ArrayBuffer.isView(cur)) {
29 a2[k] = copyBuffer(cur)
30 } else {
31 a2[k] = fn(cur)
32 }
33 }
34 return a2
35 }
36
37 function clone (o) {
38 if (typeof o !== 'object' || o === null) return o
39 if (o instanceof Date) return new Date(o)
40 if (Array.isArray(o)) return cloneArray(o, clone)
41 if (o instanceof Map) return new Map(cloneArray(Array.from(o), clone))
42 if (o instanceof Set) return new Set(cloneArray(Array.from(o), clone))
43 var o2 = {}
44 for (var k in o) {
45 if (Object.hasOwnProperty.call(o, k) === false) continue
46 var cur = o[k]
47 if (typeof cur !== 'object' || cur === null) {
48 o2[k] = cur
49 } else if (cur instanceof Date) {
50 o2[k] = new Date(cur)
51 } else if (cur instanceof Map) {
52 o2[k] = new Map(cloneArray(Array.from(cur), clone))
53 } else if (cur instanceof Set) {
54 o2[k] = new Set(cloneArray(Array.from(cur), clone))
55 } else if (ArrayBuffer.isView(cur)) {
56 o2[k] = copyBuffer(cur)
57 } else {
58 o2[k] = clone(cur)
59 }
60 }
61 return o2
62 }
63
64 function cloneProto (o) {
65 if (typeof o !== 'object' || o === null) return o
66 if (o instanceof Date) return new Date(o)
67 if (Array.isArray(o)) return cloneArray(o, cloneProto)
68 if (o instanceof Map) return new Map(cloneArray(Array.from(o), cloneProto))
69 if (o instanceof Set) return new Set(cloneArray(Array.from(o), cloneProto))
70 var o2 = {}
71 for (var k in o) {
72 var cur = o[k]
73 if (typeof cur !== 'object' || cur === null) {
74 o2[k] = cur
75 } else if (cur instanceof Date) {
76 o2[k] = new Date(cur)
77 } else if (cur instanceof Map) {
78 o2[k] = new Map(cloneArray(Array.from(cur), cloneProto))
79 } else if (cur instanceof Set) {
80 o2[k] = new Set(cloneArray(Array.from(cur), cloneProto))
81 } else if (ArrayBuffer.isView(cur)) {
82 o2[k] = copyBuffer(cur)
83 } else {
84 o2[k] = cloneProto(cur)
85 }
86 }
87 return o2
88 }
89}
90
91function rfdcCircles (opts) {
92 var refs = []
93 var refsNew = []
94
95 return opts.proto ? cloneProto : clone
96
97 function cloneArray (a, fn) {
98 var keys = Object.keys(a)
99 var a2 = new Array(keys.length)
100 for (var i = 0; i < keys.length; i++) {
101 var k = keys[i]
102 var cur = a[k]
103 if (typeof cur !== 'object' || cur === null) {
104 a2[k] = cur
105 } else if (cur instanceof Date) {
106 a2[k] = new Date(cur)
107 } else if (ArrayBuffer.isView(cur)) {
108 a2[k] = copyBuffer(cur)
109 } else {
110 var index = refs.indexOf(cur)
111 if (index !== -1) {
112 a2[k] = refsNew[index]
113 } else {
114 a2[k] = fn(cur)
115 }
116 }
117 }
118 return a2
119 }
120
121 function clone (o) {
122 if (typeof o !== 'object' || o === null) return o
123 if (o instanceof Date) return new Date(o)
124 if (Array.isArray(o)) return cloneArray(o, clone)
125 if (o instanceof Map) return new Map(cloneArray(Array.from(o), clone))
126 if (o instanceof Set) return new Set(cloneArray(Array.from(o), clone))
127 var o2 = {}
128 refs.push(o)
129 refsNew.push(o2)
130 for (var k in o) {
131 if (Object.hasOwnProperty.call(o, k) === false) continue
132 var cur = o[k]
133 if (typeof cur !== 'object' || cur === null) {
134 o2[k] = cur
135 } else if (cur instanceof Date) {
136 o2[k] = new Date(cur)
137 } else if (cur instanceof Map) {
138 o2[k] = new Map(cloneArray(Array.from(cur), clone))
139 } else if (cur instanceof Set) {
140 o2[k] = new Set(cloneArray(Array.from(cur), clone))
141 } else if (ArrayBuffer.isView(cur)) {
142 o2[k] = copyBuffer(cur)
143 } else {
144 var i = refs.indexOf(cur)
145 if (i !== -1) {
146 o2[k] = refsNew[i]
147 } else {
148 o2[k] = clone(cur)
149 }
150 }
151 }
152 refs.pop()
153 refsNew.pop()
154 return o2
155 }
156
157 function cloneProto (o) {
158 if (typeof o !== 'object' || o === null) return o
159 if (o instanceof Date) return new Date(o)
160 if (Array.isArray(o)) return cloneArray(o, cloneProto)
161 if (o instanceof Map) return new Map(cloneArray(Array.from(o), cloneProto))
162 if (o instanceof Set) return new Set(cloneArray(Array.from(o), cloneProto))
163 var o2 = {}
164 refs.push(o)
165 refsNew.push(o2)
166 for (var k in o) {
167 var cur = o[k]
168 if (typeof cur !== 'object' || cur === null) {
169 o2[k] = cur
170 } else if (cur instanceof Date) {
171 o2[k] = new Date(cur)
172 } else if (cur instanceof Map) {
173 o2[k] = new Map(cloneArray(Array.from(cur), cloneProto))
174 } else if (cur instanceof Set) {
175 o2[k] = new Set(cloneArray(Array.from(cur), cloneProto))
176 } else if (ArrayBuffer.isView(cur)) {
177 o2[k] = copyBuffer(cur)
178 } else {
179 var i = refs.indexOf(cur)
180 if (i !== -1) {
181 o2[k] = refsNew[i]
182 } else {
183 o2[k] = cloneProto(cur)
184 }
185 }
186 }
187 refs.pop()
188 refsNew.pop()
189 return o2
190 }
191}
Note: See TracBrowser for help on using the repository browser.