Skip to content

Commit

Permalink
breaking: ESM only 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
mesqueeb committed Jun 1, 2024
1 parent 751442a commit 7dea06f
Show file tree
Hide file tree
Showing 15 changed files with 6,592 additions and 9,365 deletions.
12 changes: 12 additions & 0 deletions .github/SPONSORS.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# These are supported funding model platforms

github: mesqueeb
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
37 changes: 37 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Test
on:
push:
branches: main
paths:
- src/**
- test/**
- '*.js'
- '*.ts'
- '*.json'
- .github/workflows/test.yml
pull_request:
branches: main
paths:
- src/**
- test/**
- '*.js'
- '*.ts'
- '*.json'
- .github/workflows/test.yml
concurrency:
group: test-${{ github.ref }}
cancel-in-progress: true
jobs:
test:
strategy:
matrix:
node-version: ['18', '20']
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: npm
- run: npm ci
- run: npm test
9 changes: 0 additions & 9 deletions .prettierrc

This file was deleted.

3 changes: 3 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import prettier from "@cycraft/eslint/prettier"

export default prettier
38 changes: 0 additions & 38 deletions dist/cjs/index.cjs

This file was deleted.

14 changes: 0 additions & 14 deletions dist/cjs/index.d.cts

This file was deleted.

6 changes: 2 additions & 4 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Options = {
export type Options = {
props?: (string | symbol)[];
nonenumerable?: boolean;
};
Expand All @@ -9,6 +9,4 @@ type Options = {
* @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 };
export declare function copy<T>(target: T, options?: Options): T;
68 changes: 37 additions & 31 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
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));
}
if (!isPlainObject(target)) {
return target;
}
const val = target[key];
const newVal = copy(val, options);
assignProp(carry, key, newVal, target, options.nonenumerable);
return carry;
}, {});
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 };
10 changes: 10 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import config from '@cycraft/eslint/config'

export default [
...config,
{
rules: {
'@typescript-eslint/no-explicit-any': 'warn',
},
},
]
Loading

0 comments on commit 7dea06f

Please sign in to comment.