source: trip-planner-front/node_modules/shallow-clone/index.js@ 6fe77af

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

initial commit

  • Property mode set to 100644
File size: 1.8 KB
Line 
1/*!
2 * shallow-clone <https://github.com/jonschlinkert/shallow-clone>
3 *
4 * Copyright (c) 2015-present, Jon Schlinkert.
5 * Released under the MIT License.
6 */
7
8'use strict';
9
10const valueOf = Symbol.prototype.valueOf;
11const typeOf = require('kind-of');
12
13function clone(val, deep) {
14 switch (typeOf(val)) {
15 case 'array':
16 return val.slice();
17 case 'object':
18 return Object.assign({}, val);
19 case 'date':
20 return new val.constructor(Number(val));
21 case 'map':
22 return new Map(val);
23 case 'set':
24 return new Set(val);
25 case 'buffer':
26 return cloneBuffer(val);
27 case 'symbol':
28 return cloneSymbol(val);
29 case 'arraybuffer':
30 return cloneArrayBuffer(val);
31 case 'float32array':
32 case 'float64array':
33 case 'int16array':
34 case 'int32array':
35 case 'int8array':
36 case 'uint16array':
37 case 'uint32array':
38 case 'uint8clampedarray':
39 case 'uint8array':
40 return cloneTypedArray(val);
41 case 'regexp':
42 return cloneRegExp(val);
43 case 'error':
44 return Object.create(val);
45 default: {
46 return val;
47 }
48 }
49}
50
51function cloneRegExp(val) {
52 const flags = val.flags !== void 0 ? val.flags : (/\w+$/.exec(val) || void 0);
53 const re = new val.constructor(val.source, flags);
54 re.lastIndex = val.lastIndex;
55 return re;
56}
57
58function cloneArrayBuffer(val) {
59 const res = new val.constructor(val.byteLength);
60 new Uint8Array(res).set(new Uint8Array(val));
61 return res;
62}
63
64function cloneTypedArray(val, deep) {
65 return new val.constructor(val.buffer, val.byteOffset, val.length);
66}
67
68function cloneBuffer(val) {
69 const len = val.length;
70 const buf = Buffer.allocUnsafe ? Buffer.allocUnsafe(len) : Buffer.from(len);
71 val.copy(buf);
72 return buf;
73}
74
75function cloneSymbol(val) {
76 return valueOf ? Object(valueOf.call(val)) : {};
77}
78
79/**
80 * Expose `clone`
81 */
82
83module.exports = clone;
Note: See TracBrowser for help on using the repository browser.