avancement planning
This commit is contained in:
+12
-8
@@ -26,16 +26,20 @@ I was looking for:
|
||||
This last one is crucial! So many libraries use custom classes that create objects with special prototypes, and such objects all break when trying to copy them inproperly. So we gotta be careful!
|
||||
|
||||
copy-anything will copy objects and nested properties, but only as long as they're "plain objects". As soon as a sub-prop is not a "plain object" and has a special prototype, it will copy that instance over "as is". ♻️
|
||||
## Meet the family (more tiny utils with TS support)
|
||||
|
||||
## Meet the family
|
||||
|
||||
- [copy-anything 🎭](https://github.com/mesqueeb/copy-anything)
|
||||
- [merge-anything 🥡](https://github.com/mesqueeb/merge-anything)
|
||||
- [filter-anything ⚔️](https://github.com/mesqueeb/filter-anything)
|
||||
- [find-and-replace-anything 🎣](https://github.com/mesqueeb/find-and-replace-anything)
|
||||
- [compare-anything 🛰](https://github.com/mesqueeb/compare-anything)
|
||||
- [flatten-anything 🏏](https://github.com/mesqueeb/flatten-anything)
|
||||
- [is-what 🙉](https://github.com/mesqueeb/is-what)
|
||||
- [is-where 🙈](https://github.com/mesqueeb/is-where)
|
||||
- [merge-anything 🥡](https://github.com/mesqueeb/merge-anything)
|
||||
- [check-anything 👁](https://github.com/mesqueeb/check-anything)
|
||||
- [remove-anything ✂️](https://github.com/mesqueeb/remove-anything)
|
||||
- [getorset-anything 🐊](https://github.com/mesqueeb/getorset-anything)
|
||||
- [map-anything 🗺](https://github.com/mesqueeb/map-anything)
|
||||
- [filter-anything ⚔️](https://github.com/mesqueeb/filter-anything)
|
||||
- [copy-anything 🎭](https://github.com/mesqueeb/copy-anything)
|
||||
- [case-anything 🐫](https://github.com/mesqueeb/case-anything)
|
||||
- [flatten-anything 🏏](https://github.com/mesqueeb/flatten-anything)
|
||||
- [nestify-anything 🧅](https://github.com/mesqueeb/nestify-anything)
|
||||
|
||||
## Usage
|
||||
|
||||
|
||||
-52
@@ -1,52 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
var 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,
|
||||
});
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @export
|
||||
* @template T
|
||||
* @param {T} target Target can be anything
|
||||
* @param {Options} [options = {}] Options can be `props` or `nonenumerable`
|
||||
* @returns {T} the target with replaced values
|
||||
* @export
|
||||
*/
|
||||
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;
|
||||
-48
@@ -1,48 +0,0 @@
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @export
|
||||
* @template T
|
||||
* @param {T} target Target can be anything
|
||||
* @param {Options} [options = {}] Options can be `props` or `nonenumerable`
|
||||
* @returns {T} the target with replaced values
|
||||
* @export
|
||||
*/
|
||||
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 };
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
export declare 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.
|
||||
*
|
||||
* @export
|
||||
* @template T
|
||||
* @param {T} target Target can be anything
|
||||
* @param {Options} [options = {}] Options can be `props` or `nonenumerable`
|
||||
* @returns {T} the target with replaced values
|
||||
* @export
|
||||
*/
|
||||
export declare function copy<T>(target: T, options?: Options): T;
|
||||
+32
-22
@@ -1,43 +1,53 @@
|
||||
{
|
||||
"name": "copy-anything",
|
||||
"sideEffects": false,
|
||||
"version": "2.0.6",
|
||||
"version": "3.0.5",
|
||||
"description": "An optimised way to copy'ing an object. A small and simple integration",
|
||||
"module": "./dist/index.es.js",
|
||||
"main": "./dist/index.cjs",
|
||||
"types": "./dist/types/index.d.ts",
|
||||
"type": "module",
|
||||
"sideEffects": false,
|
||||
"types": "./dist/index.d.ts",
|
||||
"module": "./dist/index.js",
|
||||
"main": "./dist/index.js",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/index.es.js",
|
||||
"require": "./dist/index.cjs",
|
||||
"types": "./dist/types/index.d.ts"
|
||||
"require": {
|
||||
"types": "./dist/cjs/index.d.cts",
|
||||
"default": "./dist/cjs/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"default": "./dist/index.js"
|
||||
}
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=12.13"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "vitest run",
|
||||
"lint": "tsc --noEmit && eslint ./src --ext .ts",
|
||||
"build": "rollup -c ./scripts/build.js",
|
||||
"build": "rollup -c ./rollup.config.js",
|
||||
"release": "npm run lint && del dist && npm run build && np"
|
||||
},
|
||||
"dependencies": {
|
||||
"is-what": "^3.14.1"
|
||||
"is-what": "^4.1.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@typescript-eslint/eslint-plugin": "^5.10.1",
|
||||
"@typescript-eslint/parser": "^5.10.1",
|
||||
"del-cli": "^4.0.1",
|
||||
"eslint": "^8.7.0",
|
||||
"eslint-config-prettier": "^8.3.0",
|
||||
"@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.6.0",
|
||||
"prettier": "^2.5.1",
|
||||
"rollup": "^2.66.0",
|
||||
"rollup-plugin-typescript2": "^0.31.1",
|
||||
"typescript": "^4.5.5",
|
||||
"vitest": "^0.2.1"
|
||||
"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"
|
||||
},
|
||||
"keywords": [
|
||||
"copy",
|
||||
@@ -66,7 +76,7 @@
|
||||
},
|
||||
"np": {
|
||||
"yarn": false,
|
||||
"branch": "legacy"
|
||||
"branch": "production"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"ignorePatterns": [
|
||||
|
||||
Reference in New Issue
Block a user