source: trip-planner-front/node_modules/copy-anything/dist/index.esm.js@ 84d0fbb

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

initial commit

  • Property mode set to 100644
File size: 2.7 KB
Line 
1import { isArray, isPlainObject } from 'is-what';
2
3/*! *****************************************************************************
4Copyright (c) Microsoft Corporation.
5
6Permission to use, copy, modify, and/or distribute this software for any
7purpose with or without fee is hereby granted.
8
9THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15PERFORMANCE OF THIS SOFTWARE.
16***************************************************************************** */
17
18function __spreadArrays() {
19 for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
20 for (var r = Array(s), k = 0, i = 0; i < il; i++)
21 for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
22 r[k] = a[j];
23 return r;
24}
25
26function assignProp(carry, key, newVal, originalObject, includeNonenumerable) {
27 var propType = {}.propertyIsEnumerable.call(originalObject, key)
28 ? 'enumerable'
29 : 'nonenumerable';
30 if (propType === 'enumerable')
31 carry[key] = newVal;
32 if (includeNonenumerable && propType === 'nonenumerable') {
33 Object.defineProperty(carry, key, {
34 value: newVal,
35 enumerable: false,
36 writable: true,
37 configurable: true,
38 });
39 }
40}
41/**
42 * Copy (clone) an object and all its props recursively to get rid of any prop referenced of the original object. Arrays are also cloned, however objects inside arrays are still linked.
43 *
44 * @export
45 * @template T
46 * @param {T} target Target can be anything
47 * @param {Options} [options = {}] Options can be `props` or `nonenumerable`
48 * @returns {T} the target with replaced values
49 * @export
50 */
51function copy(target, options) {
52 if (options === void 0) { options = {}; }
53 if (isArray(target))
54 return target.map(function (item) { return copy(item, options); });
55 if (!isPlainObject(target))
56 return target;
57 var props = Object.getOwnPropertyNames(target);
58 var symbols = Object.getOwnPropertySymbols(target);
59 return __spreadArrays(props, symbols).reduce(function (carry, key) {
60 if (isArray(options.props) && !options.props.includes(key)) {
61 return carry;
62 }
63 var val = target[key];
64 var newVal = copy(val, options);
65 assignProp(carry, key, newVal, target, options.nonenumerable);
66 return carry;
67 }, {});
68}
69
70export { copy };
Note: See TracBrowser for help on using the repository browser.