0.1.1 • Published 6 years ago
@ganuz/clone-with v0.1.1
@ganuz/clone-with
Clone With is package from Ganuz library
Install
$ yarn add @ganuz/clone-with
Or
$ npm install --save @ganuz/clone-with
Use
Module
import {
default as cloneWith
} from '@ganuz/clone-with';
Browser
<script src="https://unpkg.com/@ganuz/clone-with/bundle.umd.min.js"></script>
let {
cloneWith
} = G;
Examples
cloneWith('foo', v => v); // throw TypeError
cloneWith({foo: 'bar'}, () => 77); // throw TypeError
cloneWith({foo: 'bar'}, () => {get: () => 'bar', writable: false}); // throw TypeError
cloneWith({}, v => v); // => {}
cloneWith({foo: 'bar'}, v => v); // => {foo: 'bar'}
cloneWith({foo: 'bar'}, () => undefined); // => {foo: 'bar'}
cloneWith([0, 1, 2, 3, 4], ({value: n}) => {value: n % 2 === 0 ? n : NaN}); // => [0, NaN, 2, NaN, 4]
cloneWith({name: 'alice'}, d => ({...d, value: 'bob'})); // => {name: 'bob'}
let proto = {data: 'data'};
let object = {foo: 'bar', get color(){ return 'blue'; }, id(v){ return v; }, set some(v){}};
Object.setPrototype(object, proto);
let cloneObject = clone(object, ({value, get}, property, object) => {
return {
value: get ? get.call(object) : value,
writable: true
};
});
Object.getPrototypeOf(cloneObject) === proto; // => true
Object.getOwnPropertyDescriptors(cloneObject);
// => {
// color: {value: 'blue', configurable: false, enumerable: false, writable: true},
// foo: {value: 'blue', configurable: false, enumerable: false, writable: true},
// id: {value(v){ return v; }, configurable: false, enumerable: false, writable: true}
// some: {value: undefined, configurable: false, enumerable: false, writable: true}
// }
License
Copyright © Yisrael Eliev, Licensed under the MIT license.