Skip to content

Commit

Permalink
chore: build
Browse files Browse the repository at this point in the history
  • Loading branch information
mesqueeb committed Jan 25, 2022
1 parent 394d210 commit 3e707b4
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 25 deletions.
64 changes: 64 additions & 0 deletions dist/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var isWhat = require('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} [config={onlyPlainObjects: false, checkArrayValues: false}]
* @returns {*} the target with replaced values
*/
function findAndReplace(target, find, replaceWith, config = { onlyPlainObjects: false, checkArrayValues: false }) {
const _target = target;
// arrays
if (config.checkArrayValues && isWhat.isArray(_target) && !isWhat.isAnyObject(_target)) {
return _target.map(value => findAndReplace(value, find, replaceWith, config));
}
// non-objects
if ((!config.onlyPlainObjects && !isWhat.isAnyObject(_target)) ||
(config.onlyPlainObjects === true && !isWhat.isPlainObject(_target))) {
if (_target === find || (isWhat.isNaNValue(_target) && isWhat.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 && isWhat.isArray(_target) && !isWhat.isAnyObject(_target)) {
return _target.map(value => _findAndReplaceIf(value, checkFn, undefined, config));
}
if (!isWhat.isPlainObject(_target))
return _target;
return Object.entries(_target).reduce((carry, [key, val]) => {
carry[key] = _findAndReplaceIf(val, checkFn, key, config);
return carry;
}, {});
}
/**
* 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
*/
function findAndReplaceIf(target, checkFn, config = { onlyPlainObjects: true, checkArrayValues: false }) {
return _findAndReplaceIf(target, checkFn, undefined, config);
}

exports._findAndReplaceIf = _findAndReplaceIf;
exports.findAndReplace = findAndReplace;
exports.findAndReplaceIf = findAndReplaceIf;
58 changes: 58 additions & 0 deletions dist/index.es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { isArray, isAnyObject, isPlainObject, isNaNValue } 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} [config={onlyPlainObjects: false, checkArrayValues: false}]
* @returns {*} the target with replaced values
*/
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));
}
// 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, undefined, config));
}
if (!isPlainObject(_target))
return _target;
return Object.entries(_target).reduce((carry, [key, val]) => {
carry[key] = _findAndReplaceIf(val, checkFn, key, config);
return carry;
}, {});
}
/**
* 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
*/
function findAndReplaceIf(target, checkFn, config = { onlyPlainObjects: true, checkArrayValues: false }) {
return _findAndReplaceIf(target, checkFn, undefined, config);
}

export { _findAndReplaceIf, findAndReplace, findAndReplaceIf };
27 changes: 27 additions & 0 deletions dist/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
declare type Config = {
onlyPlainObjects?: boolean;
checkArrayValues?: boolean;
};
/**
* 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
*/
export declare function findAndReplace(target: any, find: any, replaceWith: any, config?: Config): any;
export declare function _findAndReplaceIf(target: any, checkFn: (foundVal: any, propKey: string | undefined) => any, propKey: string | undefined, config?: Config): any;
/**
* 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
*/
export declare function findAndReplaceIf(target: any, checkFn: (foundVal: any, propKey: string | undefined) => any, config?: Config): any;
export {};
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function findAndReplace (
return _target
}
// objects
return Object.entries(target).reduce((carry, [key, val]) => {
return Object.entries(target).reduce<any>((carry, [key, val]) => {
carry[key] = findAndReplace(val, find, replaceWith, config)
return carry
}, {})
Expand All @@ -54,7 +54,7 @@ export function _findAndReplaceIf (
return (_target as any[]).map(value => _findAndReplaceIf(value, checkFn, undefined, config))
}
if (!isPlainObject(_target)) return _target
return Object.entries(_target).reduce((carry, [key, val]) => {
return Object.entries(_target).reduce<any>((carry, [key, val]) => {
carry[key] = _findAndReplaceIf(val, checkFn, key, config)
return carry
}, {})
Expand Down
46 changes: 23 additions & 23 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ import { isPlainObject } from 'is-what'

test('findAndReplace in arrays', () => {
const res = findAndReplace({ a: [{ b: 'c' }] }, 'c', 'd', { checkArrayValues: true })
t.deepEqual(res, {
expect(res).toEqual({
a: [{ b: 'd' }],
})
})

test('findAndReplaceIf in arrays', () => {
const replacer = foundVal => (foundVal === 'c' ? 'd' : foundVal)
t.deepEqual(findAndReplaceIf({ a: ['c'] }, replacer, { checkArrayValues: true }), {
const replacer = (foundVal: any) => (foundVal === 'c' ? 'd' : foundVal)
expect(findAndReplaceIf({ a: ['c'] }, replacer, { checkArrayValues: true })).toEqual({
a: ['d'],
})
})

test('findAndReplaceIf in arrays double nested', () => {
const replacer = foundVal => (foundVal === 'c' ? 'd' : foundVal)
t.deepEqual(findAndReplaceIf({ a: [{ b: 'c' }, 'c'] }, replacer, { checkArrayValues: true }), {
const replacer = (foundVal: any) => (foundVal === 'c' ? 'd' : foundVal)
expect(findAndReplaceIf({ a: [{ b: 'c' }, 'c'] }, replacer, { checkArrayValues: true })).toEqual({
a: [{ b: 'd' }, 'd'],
})
})

test('findAndReplace nested strings', () => {
t.deepEqual(findAndReplace({ a: { b: { c: 'a' } } }, 'a', 'b'), { a: { b: { c: 'b' } } })
expect(findAndReplace({ a: { b: { c: 'a' } } }, 'a', 'b')).toEqual({ a: { b: { c: 'b' } } })
})

test('findAndReplace strings', () => {
Expand All @@ -33,50 +33,50 @@ test('findAndReplace strings', () => {
})

test('findAndReplace undefined', () => {
t.deepEqual(findAndReplace({ undefined: undefined }, undefined, 'undefined'), {
expect(findAndReplace({ undefined: undefined }, undefined, 'undefined')).toEqual({
undefined: 'undefined',
})
})
test('findAndReplace NaN', () => {
t.deepEqual(findAndReplace({ NaN: NaN }, NaN, 'NaN'), { NaN: 'NaN' })
expect(findAndReplace({ NaN: NaN }, NaN, 'NaN')).toEqual({ NaN: 'NaN' })
})
test('findAndReplace null', () => {
t.deepEqual(findAndReplace({ null: null }, null, 'null'), { null: 'null' })
expect(findAndReplace({ null: null }, null, 'null')).toEqual({ null: 'null' })
})

test('findAndReplace does not modify objects', () => {
let res, ori
ori = { a: { b: { c: 'a' }, d: 1 } }
res = findAndReplace(ori, 'a', 'b')
t.deepEqual(res, { a: { b: { c: 'b' }, d: 1 } })
t.deepEqual(ori, { a: { b: { c: 'a' }, d: 1 } })
expect(res).toEqual({ a: { b: { c: 'b' }, d: 1 } })
expect(ori).toEqual({ a: { b: { c: 'a' }, d: 1 } })
res.a.b = 1
t.deepEqual(res, { a: { b: 1, d: 1 } })
t.deepEqual(ori, { a: { b: { c: 'a' }, d: 1 } })
expect(res).toEqual({ a: { b: 1, d: 1 } })
expect(ori).toEqual({ a: { b: { c: 'a' }, d: 1 } })
res.a.d = 2
t.deepEqual(res, { a: { b: 1, d: 2 } })
t.deepEqual(ori, { a: { b: { c: 'a' }, d: 1 } })
expect(res).toEqual({ a: { b: 1, d: 2 } })
expect(ori).toEqual({ a: { b: { c: 'a' }, d: 1 } })
ori.a.d = 3
t.deepEqual(res, { a: { b: 1, d: 2 } })
t.deepEqual(ori, { a: { b: { c: 'a' }, d: 3 } })
expect(res).toEqual({ a: { b: 1, d: 2 } })
expect(ori).toEqual({ a: { b: { c: 'a' }, d: 3 } })
})

test('findAndReplace does not work with objects', () => {
let res, ori
ori = { a: { b: { c: 'a' } } }
res = findAndReplace(ori, { c: 'a' }, { c: 'b' })
t.deepEqual(res, { a: { b: { c: 'a' } } })
t.deepEqual(ori, { a: { b: { c: 'a' } } })
expect(res).toEqual({ a: { b: { c: 'a' } } })
expect(ori).toEqual({ a: { b: { c: 'a' } } })
})

test('findAndReplaceIf', () => {
let res
function checkFn (foundVal) {
function checkFn(foundVal: any) {
if (foundVal === 'a') return 'b'
return foundVal
}
res = findAndReplaceIf({ a: { b: { c: 'a' } } }, checkFn)
t.deepEqual(res, { a: { b: { c: 'b' } } })
expect(res).toEqual({ a: { b: { c: 'b' } } })
res = findAndReplaceIf('_', checkFn)
expect(res).toEqual('_')
res = findAndReplaceIf('a', checkFn)
Expand All @@ -87,7 +87,7 @@ test('should work on classes', () => {
let res, target
class MyClass {
prop = 0
constructor () {
constructor() {
this.prop = 1
}
}
Expand All @@ -105,7 +105,7 @@ test('should prevent classes', () => {
let res, target
class MyClass {
prop = 0
constructor () {
constructor() {
this.prop = 1
}
}
Expand Down

0 comments on commit 3e707b4

Please sign in to comment.