@naturalcycles/js-lib
Advanced tools
Comparing version 5.0.1 to 5.1.0
@@ -0,1 +1,8 @@ | ||
# [5.1.0](https://github.com/NaturalCycles/js-lib/compare/v5.0.1...v5.1.0) (2019-05-10) | ||
### Features | ||
* omit(), as opposite of `pick()` ([89de5ae](https://github.com/NaturalCycles/js-lib/commit/89de5ae)) | ||
## [5.0.1](https://github.com/NaturalCycles/js-lib/compare/v5.0.0...v5.0.1) (2019-05-10) | ||
@@ -2,0 +9,0 @@ |
@@ -9,3 +9,3 @@ import { logMillis } from './decorators/logMillis.decorator'; | ||
import { arrayRange, dedupeArray, flatArray } from './util/array.util'; | ||
import { by, deepCopy, deepEquals, deepFreeze, deepTrim, filterEmptyStringValues, filterFalsyValues, filterObject, filterUndefinedValues, getKeyByValue, invertMap, invertObject, isEmptyObject, isObject, mask, mergeDeep, objectNullValuesToUndefined, pick, sortObjectDeep, transformObject, unsetValue, } from './util/object.util'; | ||
import { by, deepCopy, deepEquals, deepFreeze, deepTrim, filterEmptyStringValues, filterFalsyValues, filterObject, filterUndefinedValues, getKeyByValue, invertMap, invertObject, isEmptyObject, isObject, mask, mergeDeep, objectNullValuesToUndefined, omit, pick, sortObjectDeep, transformObject, unsetValue, } from './util/object.util'; | ||
import { randomInt } from './util/random.util'; | ||
@@ -16,3 +16,3 @@ import { loadScript } from './util/script.util'; | ||
import { silentConsole } from './util/test.util'; | ||
export { memo, memoCache, memoPromise, logMillis, AppError, HttpError, silentConsole, randomInt, loadScript, capitalizeFirstLetter, lowercaseFirstLetter, removeWhitespace, resultToString, pick, filterFalsyValues, filterEmptyStringValues, filterUndefinedValues, filterObject, transformObject, objectNullValuesToUndefined, deepEquals, deepCopy, isObject, isEmptyObject, mergeDeep, deepTrim, sortObjectDeep, unsetValue, mask, getKeyByValue, invertObject, invertMap, by, deepFreeze, anyToErrorMessage, anyToErrorObject, anyToAppError, errorToErrorObject, errorObjectToAppError, errorObjectToHttpError, appErrorToErrorObject, appErrorToHttpError, arrayRange, dedupeArray, flatArray, SimpleMovingAverage, }; | ||
export { memo, memoCache, memoPromise, logMillis, AppError, HttpError, silentConsole, randomInt, loadScript, capitalizeFirstLetter, lowercaseFirstLetter, removeWhitespace, resultToString, pick, omit, filterFalsyValues, filterEmptyStringValues, filterUndefinedValues, filterObject, transformObject, objectNullValuesToUndefined, deepEquals, deepCopy, isObject, isEmptyObject, mergeDeep, deepTrim, sortObjectDeep, unsetValue, mask, getKeyByValue, invertObject, invertMap, by, deepFreeze, anyToErrorMessage, anyToErrorObject, anyToAppError, errorToErrorObject, errorObjectToAppError, errorObjectToHttpError, appErrorToErrorObject, appErrorToHttpError, arrayRange, dedupeArray, flatArray, SimpleMovingAverage, }; | ||
//# sourceMappingURL=index.js.map |
/** | ||
* Returns clone of `obj` with only `fields` preserved. | ||
* Returns clone of `obj` with only `props` preserved. | ||
* Opposite of Omit. | ||
*/ | ||
export function pick(obj, fields = [], initialObject = {}) { | ||
if (!obj || !fields || !fields.length) | ||
export function pick(obj, props = [], initialObject = {}) { | ||
if (!obj || !props || !props.length) | ||
return obj; | ||
const o = initialObject; | ||
fields.forEach(k => { | ||
if (k in obj) | ||
o[k] = obj[k]; | ||
}); | ||
return o; | ||
return props.reduce((r, prop) => { | ||
if (prop in obj) | ||
r[prop] = obj[prop]; | ||
return r; | ||
}, initialObject); | ||
} | ||
/** | ||
* Returns clone of `obj` with `props` omitted. | ||
* Opposite of Pick. | ||
*/ | ||
export function omit(obj, props = []) { | ||
if (!obj || !props || !props.length) | ||
return obj; | ||
return props.reduce((r, prop) => { | ||
delete r[prop]; | ||
return r; | ||
}, Object.assign({}, obj)); | ||
} | ||
/** | ||
* Returns object with filtered keys from `props` array. | ||
* E.g: | ||
* mask({...}, [ | ||
* 'account.id', | ||
* 'account.updated', | ||
* ]) | ||
* | ||
* Pass deepCopy if you want to protect the whole object (not just first level) from mutation. | ||
*/ | ||
export function mask(_o, props, _deepCopy = false) { | ||
return props.reduce((r, prop) => { | ||
unsetValue(r, prop); | ||
return r; | ||
}, _deepCopy ? deepCopy(_o) : Object.assign({}, _o)); | ||
} | ||
/** | ||
* Removes "falsy" values from the object. | ||
*/ | ||
export function filterFalsyValues(_obj, mutate = false) { | ||
return filterObject(_obj, (k, v) => !!v, mutate); | ||
export function filterFalsyValues(obj, mutate = false) { | ||
return filterObject(obj, (k, v) => !!v, mutate); | ||
} | ||
export function filterEmptyStringValues(_obj, mutate = false) { | ||
return filterObject(_obj, (k, v) => v !== '', mutate); | ||
export function filterEmptyStringValues(obj, mutate = false) { | ||
return filterObject(obj, (k, v) => v !== '', mutate); | ||
} | ||
export function filterUndefinedValues(_obj, mutate = false) { | ||
return filterObject(_obj, (k, v) => v !== undefined && v !== null, mutate); | ||
export function filterUndefinedValues(obj, mutate = false) { | ||
return filterObject(obj, (k, v) => v !== undefined && v !== null, mutate); | ||
} | ||
@@ -30,24 +58,21 @@ /** | ||
*/ | ||
export function filterObject(_obj, predicate, mutate = false) { | ||
if (!isObject(_obj)) | ||
return _obj; | ||
const o = mutate ? _obj : Object.assign({}, _obj); | ||
Object.keys(o).forEach(k => { | ||
const keep = predicate(k, o[k]); | ||
if (!keep) | ||
delete o[k]; | ||
}); | ||
return o; | ||
export function filterObject(obj, predicate, mutate = false) { | ||
if (!isObject(obj)) | ||
return obj; | ||
return Object.keys(obj).reduce((r, k) => { | ||
if (!predicate(k, r[k])) | ||
delete r[k]; | ||
return r; | ||
}, mutate ? obj : Object.assign({}, obj)); | ||
} | ||
export function transformObject(_obj, transformFn, mutate = false) { | ||
if (!isObject(_obj)) | ||
return _obj; | ||
const o = mutate ? _obj : Object.assign({}, _obj); | ||
Object.keys(o).forEach(k => { | ||
o[k] = transformFn(k, o[k]); | ||
}); | ||
return o; | ||
export function transformObject(obj, transformFn, mutate = false) { | ||
if (!isObject(obj)) | ||
return obj; | ||
return Object.keys(obj).reduce((r, k) => { | ||
r[k] = transformFn(k, r[k]); | ||
return r; | ||
}, mutate ? obj : Object.assign({}, obj)); | ||
} | ||
export function objectNullValuesToUndefined(_obj, mutate = false) { | ||
return transformObject(_obj, (k, v) => { | ||
export function objectNullValuesToUndefined(obj, mutate = false) { | ||
return transformObject(obj, (k, v) => { | ||
if (v === null) | ||
@@ -155,22 +180,2 @@ return undefined; | ||
} | ||
/** | ||
* Returns object with filtered keys from "exclude" array. | ||
* E.g: | ||
* mask({...}, [ | ||
* 'account.id', | ||
* 'account.updated', | ||
* ]) | ||
* | ||
* Pass deepCopy if you want to protect the whole object (not just first level) from mutation. | ||
*/ | ||
export function mask(_o, exclude, _deepCopy = false) { | ||
let o = Object.assign({}, _o); | ||
if (_deepCopy) | ||
o = deepCopy(o); | ||
exclude.forEach(e => { | ||
// eval(`delete o.${e}`) | ||
unsetValue(o, e); | ||
}); | ||
return o; | ||
} | ||
export function getKeyByValue(object, value) { | ||
@@ -177,0 +182,0 @@ if (!isObject(object)) |
@@ -11,3 +11,3 @@ import { logMillis } from './decorators/logMillis.decorator'; | ||
import { arrayRange, dedupeArray, flatArray } from './util/array.util'; | ||
import { by, deepCopy, deepEquals, deepFreeze, deepTrim, filterEmptyStringValues, filterFalsyValues, filterObject, filterUndefinedValues, getKeyByValue, invertMap, invertObject, isEmptyObject, isObject, mask, mergeDeep, objectNullValuesToUndefined, pick, sortObjectDeep, transformObject, unsetValue } from './util/object.util'; | ||
import { by, deepCopy, deepEquals, deepFreeze, deepTrim, filterEmptyStringValues, filterFalsyValues, filterObject, filterUndefinedValues, getKeyByValue, invertMap, invertObject, isEmptyObject, isObject, mask, mergeDeep, objectNullValuesToUndefined, omit, pick, sortObjectDeep, transformObject, unsetValue } from './util/object.util'; | ||
import { randomInt } from './util/random.util'; | ||
@@ -18,2 +18,2 @@ import { loadScript } from './util/script.util'; | ||
import { silentConsole } from './util/test.util'; | ||
export { memo, memoCache, memoPromise, logMillis, ErrorData, ErrorObject, HttpErrorData, HttpErrorResponse, AppError, HttpError, Admin401ErrorData, Admin403ErrorData, silentConsole, randomInt, loadScript, StringMap, PromiseMap, ClassType, DeepReadonly, ValuesOf, ValueOf, capitalizeFirstLetter, lowercaseFirstLetter, removeWhitespace, resultToString, pick, filterFalsyValues, filterEmptyStringValues, filterUndefinedValues, filterObject, transformObject, objectNullValuesToUndefined, deepEquals, deepCopy, isObject, isEmptyObject, mergeDeep, deepTrim, sortObjectDeep, unsetValue, mask, getKeyByValue, invertObject, invertMap, by, deepFreeze, anyToErrorMessage, anyToErrorObject, anyToAppError, errorToErrorObject, errorObjectToAppError, errorObjectToHttpError, appErrorToErrorObject, appErrorToHttpError, arrayRange, dedupeArray, flatArray, SimpleMovingAverage, }; | ||
export { memo, memoCache, memoPromise, logMillis, ErrorData, ErrorObject, HttpErrorData, HttpErrorResponse, AppError, HttpError, Admin401ErrorData, Admin403ErrorData, silentConsole, randomInt, loadScript, StringMap, PromiseMap, ClassType, DeepReadonly, ValuesOf, ValueOf, capitalizeFirstLetter, lowercaseFirstLetter, removeWhitespace, resultToString, pick, omit, filterFalsyValues, filterEmptyStringValues, filterUndefinedValues, filterObject, transformObject, objectNullValuesToUndefined, deepEquals, deepCopy, isObject, isEmptyObject, mergeDeep, deepTrim, sortObjectDeep, unsetValue, mask, getKeyByValue, invertObject, invertMap, by, deepFreeze, anyToErrorMessage, anyToErrorObject, anyToAppError, errorToErrorObject, errorObjectToAppError, errorObjectToHttpError, appErrorToErrorObject, appErrorToHttpError, arrayRange, dedupeArray, flatArray, SimpleMovingAverage, }; |
@@ -46,2 +46,3 @@ "use strict"; | ||
exports.objectNullValuesToUndefined = object_util_1.objectNullValuesToUndefined; | ||
exports.omit = object_util_1.omit; | ||
exports.pick = object_util_1.pick; | ||
@@ -48,0 +49,0 @@ exports.sortObjectDeep = object_util_1.sortObjectDeep; |
@@ -0,12 +1,30 @@ | ||
import { Omit } from 'type-fest'; | ||
import { StringMap } from '../types'; | ||
/** | ||
* Returns clone of `obj` with only `fields` preserved. | ||
* Returns clone of `obj` with only `props` preserved. | ||
* Opposite of Omit. | ||
*/ | ||
export declare function pick<T, K extends keyof T>(obj: T, fields?: readonly K[], initialObject?: Partial<T>): Pick<T, K>; | ||
export declare function pick<T, K extends keyof T>(obj: T, props?: readonly K[], initialObject?: Partial<T>): Pick<T, K>; | ||
/** | ||
* Returns clone of `obj` with `props` omitted. | ||
* Opposite of Pick. | ||
*/ | ||
export declare function omit<T, K extends keyof T>(obj: T, props?: readonly K[]): Omit<T, K>; | ||
/** | ||
* Returns object with filtered keys from `props` array. | ||
* E.g: | ||
* mask({...}, [ | ||
* 'account.id', | ||
* 'account.updated', | ||
* ]) | ||
* | ||
* Pass deepCopy if you want to protect the whole object (not just first level) from mutation. | ||
*/ | ||
export declare function mask<T extends object>(_o: T, props: string[], _deepCopy?: boolean): T; | ||
/** | ||
* Removes "falsy" values from the object. | ||
*/ | ||
export declare function filterFalsyValues<T>(_obj: T, mutate?: boolean): T; | ||
export declare function filterEmptyStringValues<T>(_obj: T, mutate?: boolean): T; | ||
export declare function filterUndefinedValues<T>(_obj: T, mutate?: boolean): T; | ||
export declare function filterFalsyValues<T>(obj: T, mutate?: boolean): T; | ||
export declare function filterEmptyStringValues<T>(obj: T, mutate?: boolean): T; | ||
export declare function filterUndefinedValues<T>(obj: T, mutate?: boolean): T; | ||
/** | ||
@@ -16,5 +34,5 @@ * Returns clone of `obj` without properties that does not pass `predicate`. | ||
*/ | ||
export declare function filterObject<T>(_obj: T, predicate: (key: keyof T, value: any) => boolean, mutate?: boolean): T; | ||
export declare function transformObject<T>(_obj: T, transformFn: (key: any, value: any) => any, mutate?: boolean): T; | ||
export declare function objectNullValuesToUndefined<T>(_obj: T, mutate?: boolean): T; | ||
export declare function filterObject<T>(obj: T, predicate: (key: keyof T, value: any) => boolean, mutate?: boolean): T; | ||
export declare function transformObject<T>(obj: T, transformFn: (key: any, value: any) => any, mutate?: boolean): T; | ||
export declare function objectNullValuesToUndefined<T>(obj: T, mutate?: boolean): T; | ||
export declare function deepEquals(a: object, b: object): boolean; | ||
@@ -35,13 +53,2 @@ /** | ||
export declare function unsetValue(obj: any, prop: string): void; | ||
/** | ||
* Returns object with filtered keys from "exclude" array. | ||
* E.g: | ||
* mask({...}, [ | ||
* 'account.id', | ||
* 'account.updated', | ||
* ]) | ||
* | ||
* Pass deepCopy if you want to protect the whole object (not just first level) from mutation. | ||
*/ | ||
export declare function mask<T extends object>(_o: T, exclude: string[], _deepCopy?: boolean): T; | ||
export declare function getKeyByValue<T = any>(object: any, value: any): T | undefined; | ||
@@ -48,0 +55,0 @@ export declare function invertObject<T>(o: any): T; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Returns clone of `obj` with only `fields` preserved. | ||
* Returns clone of `obj` with only `props` preserved. | ||
* Opposite of Omit. | ||
*/ | ||
function pick(obj, fields = [], initialObject = {}) { | ||
if (!obj || !fields || !fields.length) | ||
function pick(obj, props = [], initialObject = {}) { | ||
if (!obj || !props || !props.length) | ||
return obj; | ||
const o = initialObject; | ||
fields.forEach(k => { | ||
if (k in obj) | ||
o[k] = obj[k]; | ||
}); | ||
return o; | ||
return props.reduce((r, prop) => { | ||
if (prop in obj) | ||
r[prop] = obj[prop]; | ||
return r; | ||
}, initialObject); | ||
} | ||
exports.pick = pick; | ||
/** | ||
* Returns clone of `obj` with `props` omitted. | ||
* Opposite of Pick. | ||
*/ | ||
function omit(obj, props = []) { | ||
if (!obj || !props || !props.length) | ||
return obj; | ||
return props.reduce((r, prop) => { | ||
delete r[prop]; | ||
return r; | ||
}, Object.assign({}, obj)); | ||
} | ||
exports.omit = omit; | ||
/** | ||
* Returns object with filtered keys from `props` array. | ||
* E.g: | ||
* mask({...}, [ | ||
* 'account.id', | ||
* 'account.updated', | ||
* ]) | ||
* | ||
* Pass deepCopy if you want to protect the whole object (not just first level) from mutation. | ||
*/ | ||
function mask(_o, props, _deepCopy = false) { | ||
return props.reduce((r, prop) => { | ||
unsetValue(r, prop); | ||
return r; | ||
}, _deepCopy ? deepCopy(_o) : Object.assign({}, _o)); | ||
} | ||
exports.mask = mask; | ||
/** | ||
* Removes "falsy" values from the object. | ||
*/ | ||
function filterFalsyValues(_obj, mutate = false) { | ||
return filterObject(_obj, (k, v) => !!v, mutate); | ||
function filterFalsyValues(obj, mutate = false) { | ||
return filterObject(obj, (k, v) => !!v, mutate); | ||
} | ||
exports.filterFalsyValues = filterFalsyValues; | ||
function filterEmptyStringValues(_obj, mutate = false) { | ||
return filterObject(_obj, (k, v) => v !== '', mutate); | ||
function filterEmptyStringValues(obj, mutate = false) { | ||
return filterObject(obj, (k, v) => v !== '', mutate); | ||
} | ||
exports.filterEmptyStringValues = filterEmptyStringValues; | ||
function filterUndefinedValues(_obj, mutate = false) { | ||
return filterObject(_obj, (k, v) => v !== undefined && v !== null, mutate); | ||
function filterUndefinedValues(obj, mutate = false) { | ||
return filterObject(obj, (k, v) => v !== undefined && v !== null, mutate); | ||
} | ||
@@ -36,26 +66,23 @@ exports.filterUndefinedValues = filterUndefinedValues; | ||
*/ | ||
function filterObject(_obj, predicate, mutate = false) { | ||
if (!isObject(_obj)) | ||
return _obj; | ||
const o = mutate ? _obj : Object.assign({}, _obj); | ||
Object.keys(o).forEach(k => { | ||
const keep = predicate(k, o[k]); | ||
if (!keep) | ||
delete o[k]; | ||
}); | ||
return o; | ||
function filterObject(obj, predicate, mutate = false) { | ||
if (!isObject(obj)) | ||
return obj; | ||
return Object.keys(obj).reduce((r, k) => { | ||
if (!predicate(k, r[k])) | ||
delete r[k]; | ||
return r; | ||
}, mutate ? obj : Object.assign({}, obj)); | ||
} | ||
exports.filterObject = filterObject; | ||
function transformObject(_obj, transformFn, mutate = false) { | ||
if (!isObject(_obj)) | ||
return _obj; | ||
const o = mutate ? _obj : Object.assign({}, _obj); | ||
Object.keys(o).forEach(k => { | ||
o[k] = transformFn(k, o[k]); | ||
}); | ||
return o; | ||
function transformObject(obj, transformFn, mutate = false) { | ||
if (!isObject(obj)) | ||
return obj; | ||
return Object.keys(obj).reduce((r, k) => { | ||
r[k] = transformFn(k, r[k]); | ||
return r; | ||
}, mutate ? obj : Object.assign({}, obj)); | ||
} | ||
exports.transformObject = transformObject; | ||
function objectNullValuesToUndefined(_obj, mutate = false) { | ||
return transformObject(_obj, (k, v) => { | ||
function objectNullValuesToUndefined(obj, mutate = false) { | ||
return transformObject(obj, (k, v) => { | ||
if (v === null) | ||
@@ -173,23 +200,2 @@ return undefined; | ||
exports.unsetValue = unsetValue; | ||
/** | ||
* Returns object with filtered keys from "exclude" array. | ||
* E.g: | ||
* mask({...}, [ | ||
* 'account.id', | ||
* 'account.updated', | ||
* ]) | ||
* | ||
* Pass deepCopy if you want to protect the whole object (not just first level) from mutation. | ||
*/ | ||
function mask(_o, exclude, _deepCopy = false) { | ||
let o = Object.assign({}, _o); | ||
if (_deepCopy) | ||
o = deepCopy(o); | ||
exclude.forEach(e => { | ||
// eval(`delete o.${e}`) | ||
unsetValue(o, e); | ||
}); | ||
return o; | ||
} | ||
exports.mask = mask; | ||
function getKeyByValue(object, value) { | ||
@@ -196,0 +202,0 @@ if (!isObject(object)) |
{ | ||
"name": "@naturalcycles/js-lib", | ||
"version": "5.0.1", | ||
"version": "5.1.0", | ||
"scripts": { | ||
@@ -22,3 +22,4 @@ "build": "del ./dist && tsc", | ||
"benchmark": "^2.1.4", | ||
"jest": "^24.1.0" | ||
"jest": "^24.1.0", | ||
"type-fest": "^0.4.1" | ||
}, | ||
@@ -25,0 +26,0 @@ "files": [ |
@@ -45,2 +45,3 @@ import { logMillis } from './decorators/logMillis.decorator' | ||
objectNullValuesToUndefined, | ||
omit, | ||
pick, | ||
@@ -89,2 +90,3 @@ sortObjectDeep, | ||
pick, | ||
omit, | ||
filterFalsyValues, | ||
@@ -91,0 +93,0 @@ filterEmptyStringValues, |
@@ -0,35 +1,70 @@ | ||
import { Omit } from 'type-fest' | ||
import { StringMap } from '../types' | ||
/** | ||
* Returns clone of `obj` with only `fields` preserved. | ||
* Returns clone of `obj` with only `props` preserved. | ||
* Opposite of Omit. | ||
*/ | ||
export function pick<T, K extends keyof T> ( | ||
obj: T, | ||
fields: readonly K[] = [], | ||
props: readonly K[] = [], | ||
initialObject: Partial<T> = {}, | ||
): Pick<T, K> { | ||
if (!obj || !fields || !fields.length) return obj | ||
if (!obj || !props || !props.length) return obj | ||
const o = initialObject as Pick<T, K> | ||
return props.reduce((r, prop) => { | ||
if (prop in obj) r[prop] = obj[prop] | ||
return r | ||
}, initialObject) as Pick<T, K> | ||
} | ||
fields.forEach(k => { | ||
if (k in obj) o[k] = obj[k] | ||
}) | ||
/** | ||
* Returns clone of `obj` with `props` omitted. | ||
* Opposite of Pick. | ||
*/ | ||
export function omit<T, K extends keyof T> (obj: T, props: readonly K[] = []): Omit<T, K> { | ||
if (!obj || !props || !props.length) return obj | ||
return o | ||
return props.reduce( | ||
(r, prop) => { | ||
delete r[prop] | ||
return r | ||
}, | ||
{ ...obj }, | ||
) | ||
} | ||
/** | ||
* Returns object with filtered keys from `props` array. | ||
* E.g: | ||
* mask({...}, [ | ||
* 'account.id', | ||
* 'account.updated', | ||
* ]) | ||
* | ||
* Pass deepCopy if you want to protect the whole object (not just first level) from mutation. | ||
*/ | ||
export function mask<T extends object> (_o: T, props: string[], _deepCopy = false): T { | ||
return props.reduce( | ||
(r, prop) => { | ||
unsetValue(r, prop) | ||
return r | ||
}, | ||
_deepCopy ? deepCopy(_o) : { ..._o }, | ||
) | ||
} | ||
/** | ||
* Removes "falsy" values from the object. | ||
*/ | ||
export function filterFalsyValues<T> (_obj: T, mutate = false): T { | ||
return filterObject(_obj, (k, v) => !!v, mutate) | ||
export function filterFalsyValues<T> (obj: T, mutate = false): T { | ||
return filterObject(obj, (k, v) => !!v, mutate) | ||
} | ||
export function filterEmptyStringValues<T> (_obj: T, mutate = false): T { | ||
return filterObject(_obj, (k, v) => v !== '', mutate) | ||
export function filterEmptyStringValues<T> (obj: T, mutate = false): T { | ||
return filterObject(obj, (k, v) => v !== '', mutate) | ||
} | ||
export function filterUndefinedValues<T> (_obj: T, mutate = false): T { | ||
return filterObject(_obj, (k, v) => v !== undefined && v !== null, mutate) as any | ||
export function filterUndefinedValues<T> (obj: T, mutate = false): T { | ||
return filterObject(obj, (k, v) => v !== undefined && v !== null, mutate) | ||
} | ||
@@ -42,37 +77,36 @@ | ||
export function filterObject<T> ( | ||
_obj: T, | ||
obj: T, | ||
predicate: (key: keyof T, value: any) => boolean, | ||
mutate = false, | ||
): T { | ||
if (!isObject(_obj)) return _obj | ||
if (!isObject(obj)) return obj | ||
const o: T = mutate ? _obj : { ..._obj } | ||
Object.keys(o).forEach(k => { | ||
const keep = predicate(k as keyof T, o[k]) | ||
if (!keep) delete o[k] | ||
}) | ||
return o | ||
return Object.keys(obj).reduce( | ||
(r, k) => { | ||
if (!predicate(k as keyof T, r[k])) delete r[k] | ||
return r | ||
}, | ||
mutate ? obj : { ...obj }, | ||
) | ||
} | ||
export function transformObject<T> ( | ||
_obj: T, | ||
obj: T, | ||
transformFn: (key: any, value: any) => any, | ||
mutate = false, | ||
): T { | ||
if (!isObject(_obj)) return _obj | ||
if (!isObject(obj)) return obj | ||
const o: T = mutate ? _obj : { ...(_obj as any) } | ||
Object.keys(o).forEach(k => { | ||
o[k] = transformFn(k, o[k]) | ||
}) | ||
return o | ||
return Object.keys(obj).reduce( | ||
(r, k) => { | ||
r[k] = transformFn(k, r[k]) | ||
return r | ||
}, | ||
mutate ? obj : { ...obj }, | ||
) | ||
} | ||
export function objectNullValuesToUndefined<T> (_obj: T, mutate = false): T { | ||
export function objectNullValuesToUndefined<T> (obj: T, mutate = false): T { | ||
return transformObject( | ||
_obj, | ||
obj, | ||
(k, v) => { | ||
@@ -197,23 +231,2 @@ if (v === null) return undefined | ||
/** | ||
* Returns object with filtered keys from "exclude" array. | ||
* E.g: | ||
* mask({...}, [ | ||
* 'account.id', | ||
* 'account.updated', | ||
* ]) | ||
* | ||
* Pass deepCopy if you want to protect the whole object (not just first level) from mutation. | ||
*/ | ||
export function mask<T extends object> (_o: T, exclude: string[], _deepCopy = false): T { | ||
let o = { ..._o } | ||
if (_deepCopy) o = deepCopy(o) | ||
exclude.forEach(e => { | ||
// eval(`delete o.${e}`) | ||
unsetValue(o, e) | ||
}) | ||
return o as T | ||
} | ||
export function getKeyByValue<T = any> (object: any, value: any): T | undefined { | ||
@@ -220,0 +233,0 @@ if (!isObject(object)) return |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
165524
3165
8