-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
340 additions
and
325 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
const isWhat = require('is-what'); | ||
|
||
function assignProp(carry, key, newVal, originalObject, includeNonenumerable) { | ||
const propType = {}.propertyIsEnumerable.call(originalObject, key) ? "enumerable" : "nonenumerable"; | ||
if (propType === "enumerable") | ||
carry[key] = newVal; | ||
if (includeNonenumerable && propType === "nonenumerable") { | ||
Object.defineProperty(carry, key, { | ||
value: newVal, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
}); | ||
} | ||
} | ||
function copy(target, options = {}) { | ||
if (isWhat.isArray(target)) { | ||
return target.map((item) => copy(item, options)); | ||
} | ||
if (!isWhat.isPlainObject(target)) { | ||
return target; | ||
} | ||
const props = Object.getOwnPropertyNames(target); | ||
const symbols = Object.getOwnPropertySymbols(target); | ||
return [...props, ...symbols].reduce((carry, key) => { | ||
if (isWhat.isArray(options.props) && !options.props.includes(key)) { | ||
return carry; | ||
} | ||
const val = target[key]; | ||
const newVal = copy(val, options); | ||
assignProp(carry, key, newVal, target, options.nonenumerable); | ||
return carry; | ||
}, {}); | ||
} | ||
|
||
exports.copy = copy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
type Options = { | ||
props?: (string | symbol)[]; | ||
nonenumerable?: boolean; | ||
}; | ||
/** | ||
* 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. | ||
* | ||
* @param target Target can be anything | ||
* @param [options = {}] Options can be `props` or `nonenumerable` | ||
* @returns the target with replaced values | ||
*/ | ||
declare function copy<T>(target: T, options?: Options): T; | ||
|
||
export { Options, copy }; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
export type Options = { | ||
props?: (string | symbol)[]; | ||
nonenumerable?: boolean; | ||
}; | ||
/** | ||
* 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. | ||
* | ||
* @param target Target can be anything | ||
* @param [options = {}] Options can be `props` or `nonenumerable` | ||
* @returns the target with replaced values | ||
*/ | ||
export declare function copy<T>(target: T, options?: Options): T; | ||
type Options = { | ||
props?: (string | symbol)[]; | ||
nonenumerable?: boolean; | ||
}; | ||
/** | ||
* 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. | ||
* | ||
* @param target Target can be anything | ||
* @param [options = {}] Options can be `props` or `nonenumerable` | ||
* @returns the target with replaced values | ||
*/ | ||
declare function copy<T>(target: T, options?: Options): T; | ||
|
||
export { Options, copy }; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { isArray, isPlainObject } from 'is-what'; | ||
|
||
function assignProp(carry, key, newVal, originalObject, includeNonenumerable) { | ||
const propType = {}.propertyIsEnumerable.call(originalObject, key) ? "enumerable" : "nonenumerable"; | ||
if (propType === "enumerable") | ||
carry[key] = newVal; | ||
if (includeNonenumerable && propType === "nonenumerable") { | ||
Object.defineProperty(carry, key, { | ||
value: newVal, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
}); | ||
} | ||
} | ||
function copy(target, options = {}) { | ||
if (isArray(target)) { | ||
return target.map((item) => copy(item, options)); | ||
} | ||
if (!isPlainObject(target)) { | ||
return target; | ||
} | ||
const props = Object.getOwnPropertyNames(target); | ||
const symbols = Object.getOwnPropertySymbols(target); | ||
return [...props, ...symbols].reduce((carry, key) => { | ||
if (isArray(options.props) && !options.props.includes(key)) { | ||
return carry; | ||
} | ||
const val = target[key]; | ||
const newVal = copy(val, options); | ||
assignProp(carry, key, newVal, target, options.nonenumerable); | ||
return carry; | ||
}, {}); | ||
} | ||
|
||
export { copy }; |
Oops, something went wrong.