Skip to content

Commit

Permalink
fix: exports
Browse files Browse the repository at this point in the history
  • Loading branch information
mesqueeb committed May 23, 2023
1 parent b715900 commit 5dec868
Show file tree
Hide file tree
Showing 10 changed files with 340 additions and 325 deletions.
38 changes: 38 additions & 0 deletions dist/cjs/index.cjs
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;
14 changes: 14 additions & 0 deletions dist/cjs/index.d.cts
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 };
47 changes: 0 additions & 47 deletions dist/index.cjs

This file was deleted.

26 changes: 14 additions & 12 deletions dist/types/index.d.ts → dist/index.d.ts
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 };
45 changes: 0 additions & 45 deletions dist/index.es.js

This file was deleted.

36 changes: 36 additions & 0 deletions dist/index.js
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 };
Loading

0 comments on commit 5dec868

Please sign in to comment.