@thyself/clean-empty-values
v1.1.5
Published
Fast object empty values cleaner.
Downloads
223
Maintainers
Readme
clean-empty-values, or "cev"
Fast object empty values cleaner.
Features
- Dependency-free, super small and efficient package.
- Supports the following clean-ups:
undefined
,null
,NaN
,empty-strings
,emptyObjects
,emptyArrays
. - Supports a "clean in place" strategy that reduces memory consumption, with the
cleanInPlace
option. - Supports nested-objects clean-up.
- Advanced TypeScript support for maximum flexibility on development time.
- Both CommonJS and ESM support.
Install
# npm
npm i @thyself/clean-empty-values
# yarn
yarn add @thyself/clean-empty-values
# pnpm
pnpm add @thyself/clean-empty-values
Usage
usage examples presented as jest tests for convenience
Simple usage
import { cleanEmptyValues } from '@thyself/clean-empty-values';
// value will be `{ y: null }`.
const value = cleanEmptyValues({ x: '', y: null }, { emptyStrings: true });
// value will be `{ x: '' }`.
const value = cleanEmptyValues({ x: '', y: null }, { null: true });
// value will be `{}`.
const value = cleanEmptyValues({ x: '', y: null }, { null: true, emptyStrings: true });
// value will be `{ y: { z: null, zyx: undefined } }`
const value = cleanEmptyValues({ x: '', y: { z: null, abc: '', zyx: undefined } }, { emptyStrings: true });
// value will be `{}`. Using "replaceInPlace" which optimizes memory usage.
const value = cleanEmptyValues({ x: '', y: null }, { null: true, emptyStrings: true, replaceInPlace: true });
See more examples by reviewing the unit tests.
Advanced TypeScript Support
Provides maximum flexibility and strongly-typed code.
import { cleanEmptyValues } from '@thyself/clean-empty-values';
type MyType = { y: { z: null | number } };
const myValue = cleanEmptyValues<MyType>({ x: '', y: { z: null, abc: '' } }, { emptyStrings: true });
myValue.x; // ❌ Type error, `"myValue.x` is NOT known "MyType".
myValue.y.abc; // ❌ Type error, `"myValue.y.abc` is NOT known by the "MyType"
myValue.y; // ✅ "myValue.y" is known by the Typescript compiler (tsc)
myValue.y.z; // ✅ "myValue.y.z" is known by the Typescript compiler (tsc)
myValue.y.z = 7; // ✅ "myValue.z" can be assigned to number.
myValue.y.z = '7'; // ❌ Type error, "myValue.y.z" must be assigned to `null | number`
"replaceInPlace" option
This option is considered more advanced, that's why its default value is false.
"replace in place" meaning that instead of creating new helper objects and variables building the "cleaned" object, it does the replace over the original object reference and by that save memory and improves performance.
🚧 WARNING! If you utilize the original object reference in other places in your code using this option might affect your code, so ensure you know what you're doing. 🚧