copy-anything
Advanced tools
Comparing version 3.0.5 to 4.0.0
@@ -1,2 +0,2 @@ | ||
type Options = { | ||
export type Options = { | ||
props?: (string | symbol)[]; | ||
@@ -12,4 +12,2 @@ nonenumerable?: boolean; | ||
*/ | ||
declare function copy<T>(target: T, options?: Options): T; | ||
export { Options, copy }; | ||
export declare function copy<T>(target: T, options?: Options): T; |
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 | ||
}); | ||
} | ||
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; | ||
/** | ||
* 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 function copy(target, options = {}) { | ||
if (isArray(target)) { | ||
return target.map((item) => copy(item, options)); | ||
} | ||
const val = target[key]; | ||
const newVal = copy(val, options); | ||
assignProp(carry, key, newVal, target, options.nonenumerable); | ||
return carry; | ||
}, {}); | ||
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 }; |
{ | ||
"name": "copy-anything", | ||
"version": "3.0.5", | ||
"version": "4.0.0", | ||
"description": "An optimised way to copy'ing an object. A small and simple integration", | ||
"type": "module", | ||
"sideEffects": false, | ||
"types": "./dist/index.d.ts", | ||
"module": "./dist/index.js", | ||
"main": "./dist/index.js", | ||
"exports": { | ||
@@ -22,13 +19,10 @@ ".": { | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"engines": { | ||
"node": ">=12.13" | ||
"node": ">=18" | ||
}, | ||
"scripts": { | ||
"test": "vitest run", | ||
"lint": "tsc --noEmit && eslint ./src --ext .ts", | ||
"build": "rollup -c ./rollup.config.js", | ||
"release": "npm run lint && del dist && npm run build && np" | ||
"lint": "tsc --noEmit && eslint ./src", | ||
"build": "del-cli dist && tsc", | ||
"release": "npm run lint && npm run build && np" | ||
}, | ||
@@ -39,16 +33,11 @@ "dependencies": { | ||
"devDependencies": { | ||
"@typescript-eslint/eslint-plugin": "^5.59.2", | ||
"@typescript-eslint/parser": "^5.59.2", | ||
"del-cli": "^5.0.0", | ||
"eslint": "^8.40.0", | ||
"eslint-config-prettier": "^8.8.0", | ||
"eslint-plugin-tree-shaking": "^1.10.0", | ||
"np": "^7.7.0", | ||
"prettier": "^2.8.8", | ||
"rollup": "^3.23.0", | ||
"rollup-plugin-dts": "^5.3.0", | ||
"rollup-plugin-esbuild": "^5.0.0", | ||
"typescript": "^4.9.5", | ||
"vitest": "^0.31.0" | ||
"del-cli": "^5.1.0", | ||
"np": "^10.0.5", | ||
"vitest": "^1.6.0", | ||
"@cycraft/eslint": "^0.3.0", | ||
"@cycraft/tsconfig": "^0.1.2" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"keywords": [ | ||
@@ -68,44 +57,11 @@ "copy", | ||
], | ||
"author": "Luca Ban - Mesqueeb", | ||
"author": "Luca Ban - Mesqueeb (https://cycraft.co)", | ||
"funding": "https://github.com/sponsors/mesqueeb", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mesqueeb/copy-anything/issues" | ||
}, | ||
"homepage": "https://github.com/mesqueeb/copy-anything#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/mesqueeb/copy-anything.git" | ||
"url": "https://github.com/mesqueeb/copy-anything.git" | ||
}, | ||
"np": { | ||
"yarn": false, | ||
"branch": "production" | ||
}, | ||
"eslintConfig": { | ||
"ignorePatterns": [ | ||
"node_modules", | ||
"dist", | ||
"scripts", | ||
"test" | ||
], | ||
"root": true, | ||
"parser": "@typescript-eslint/parser", | ||
"plugins": [ | ||
"@typescript-eslint", | ||
"tree-shaking" | ||
], | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/eslint-recommended", | ||
"plugin:@typescript-eslint/recommended", | ||
"prettier" | ||
], | ||
"rules": { | ||
"@typescript-eslint/no-empty-function": "off", | ||
"@typescript-eslint/no-explicit-any": "off", | ||
"@typescript-eslint/ban-ts-ignore": "off", | ||
"tree-shaking/no-side-effects-in-initialization": "error", | ||
"@typescript-eslint/ban-ts-comment": "off" | ||
} | ||
} | ||
"homepage": "https://github.com/mesqueeb/copy-anything#readme", | ||
"bugs": "https://github.com/mesqueeb/copy-anything/issues" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
5
0
9285
5
54
1