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 6a36143 commit 80b055f
Show file tree
Hide file tree
Showing 14 changed files with 6,628 additions and 13,436 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
39 changes: 0 additions & 39 deletions dist/cjs/index.cjs

This file was deleted.

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

This file was deleted.

27 changes: 13 additions & 14 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@ type Config = {
* Goes through an object recursively and replaces all occurences of the `find` value with `replaceWith`. Also works no non-objects.
*
* @export
* @param {*} target Target can be anything
* @param {*} find val to find
* @param {*} replaceWith val to replace
* @param {Config} [config={onlyPlainObjects: false, checkArrayValues: false}]
* @returns {*} the target with replaced values
* @param target Target can be anything
* @param find val to find
* @param replaceWith val to replace
* @param [config={onlyPlainObjects: false, checkArrayValues: false}]
* @returns the target with replaced values
*/
declare function findAndReplace(target: any, find: any, replaceWith: any, config?: Config): any;
declare function _findAndReplaceIf(target: any, checkFn: (foundVal: any, propKey: string | undefined) => any, propKey: string | undefined, config?: Config): any;
export declare function findAndReplace(target: any, find: any, replaceWith: any, config?: Config): unknown;
export declare function _findAndReplaceIf(target: any, checkFn: (foundVal: any, propKey: string | undefined) => any, propKey: string | undefined, config?: Config): unknown;
/**
* Goes through an object recursively and replaces all props with what's is returned in the `checkFn`. Also works on non-objects. `checkFn` is triggered on every single level of any value/object.
*
* @export
* @param {*} target Target can be anything
* @param {(foundVal: any, propKey: string | undefined) => any} checkFn a function that will receive the `foundVal`
* @param {Config} [config={onlyPlainObjects: true, checkArrayValues: false}]
* @returns {*} the target with replaced values
* @param target Target can be anything
* @param checkFn a function that will receive the `foundVal`
* @param [config={onlyPlainObjects: true, checkArrayValues: false}]
* @returns the target with replaced values
*/
declare function findAndReplaceIf(target: any, checkFn: (foundVal: any, propKey: string | undefined) => any, config?: Config): any;

export { _findAndReplaceIf, findAndReplace, findAndReplaceIf };
export declare function findAndReplaceIf(target: any, checkFn: (foundVal: any, propKey: string | undefined) => any, config?: Config): unknown;
export {};
82 changes: 51 additions & 31 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,55 @@
import { isArray, isAnyObject, isPlainObject, isNaNValue } from 'is-what';

function findAndReplace(target, find, replaceWith, config = { onlyPlainObjects: false, checkArrayValues: false }) {
const _target = target;
if (config.checkArrayValues && isArray(_target) && !isAnyObject(_target)) {
return _target.map((value) => findAndReplace(value, find, replaceWith, config));
}
if (!config.onlyPlainObjects && !isAnyObject(_target) || config.onlyPlainObjects === true && !isPlainObject(_target)) {
if (_target === find || isNaNValue(_target) && isNaNValue(find)) {
return replaceWith;
import { isAnyObject, isArray, isNaNValue, isPlainObject } from 'is-what';
/**
* Goes through an object recursively and replaces all occurences of the `find` value with `replaceWith`. Also works no non-objects.
*
* @export
* @param target Target can be anything
* @param find val to find
* @param replaceWith val to replace
* @param [config={onlyPlainObjects: false, checkArrayValues: false}]
* @returns the target with replaced values
*/
export function findAndReplace(target, find, replaceWith, config = { onlyPlainObjects: false, checkArrayValues: false }) {
const _target = target;
// arrays
if (config.checkArrayValues && isArray(_target) && !isAnyObject(_target)) {
return _target.map((value) => findAndReplace(value, find, replaceWith, config));
}
return _target;
}
return Object.entries(target).reduce((carry, [key, val]) => {
carry[key] = findAndReplace(val, find, replaceWith, config);
return carry;
}, {});
// non-objects
if ((!config.onlyPlainObjects && !isAnyObject(_target)) ||
(config.onlyPlainObjects === true && !isPlainObject(_target))) {
if (_target === find || (isNaNValue(_target) && isNaNValue(find))) {
return replaceWith;
}
return _target;
}
// objects
return Object.entries(target).reduce((carry, [key, val]) => {
carry[key] = findAndReplace(val, find, replaceWith, config);
return carry;
}, {});
}
function _findAndReplaceIf(target, checkFn, propKey, config = { onlyPlainObjects: true, checkArrayValues: false }) {
const _target = checkFn(target, propKey);
if (config.checkArrayValues && isArray(_target) && !isAnyObject(_target)) {
return _target.map((value) => _findAndReplaceIf(value, checkFn, void 0, config));
}
if (!isPlainObject(_target))
return _target;
return Object.entries(_target).reduce((carry, [key, val]) => {
carry[key] = _findAndReplaceIf(val, checkFn, key, config);
return carry;
}, {});
export function _findAndReplaceIf(target, checkFn, propKey, config = { onlyPlainObjects: true, checkArrayValues: false }) {
const _target = checkFn(target, propKey);
if (config.checkArrayValues && isArray(_target) && !isAnyObject(_target)) {
return _target.map((value) => _findAndReplaceIf(value, checkFn, undefined, config));
}
if (!isPlainObject(_target))
return _target;
return Object.entries(_target).reduce((carry, [key, val]) => {
carry[key] = _findAndReplaceIf(val, checkFn, key, config);
return carry;
}, {});
}
function findAndReplaceIf(target, checkFn, config = { onlyPlainObjects: true, checkArrayValues: false }) {
return _findAndReplaceIf(target, checkFn, void 0, config);
/**
* Goes through an object recursively and replaces all props with what's is returned in the `checkFn`. Also works on non-objects. `checkFn` is triggered on every single level of any value/object.
*
* @export
* @param target Target can be anything
* @param checkFn a function that will receive the `foundVal`
* @param [config={onlyPlainObjects: true, checkArrayValues: false}]
* @returns the target with replaced values
*/
export function findAndReplaceIf(target, checkFn, config = { onlyPlainObjects: true, checkArrayValues: false }) {
return _findAndReplaceIf(target, checkFn, undefined, config);
}

export { _findAndReplaceIf, findAndReplace, findAndReplaceIf };
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 80b055f

Please sign in to comment.