nullsweep
v0.1.3
Published
Remove undefined & null values from objects safely
Maintainers
Readme
╔═══════════════════════════════════════════════════════════╗
║ ║
║ ███╗ ██╗██╗ ██╗██╗ ██╗ ███████╗██╗ ██╗ ║
║ ████╗ ██║██║ ██║██║ ██║ ██╔════╝██║ ██║ ║
║ ██╔██╗ ██║██║ ██║██║ ██║ ███████╗██║ █╗ ██║ ║
║ ██║╚██╗██║██║ ██║██║ ██║ ╚════██║██║███╗██║ ║
║ ██║ ╚████║╚██████╔╝███████╗███████╗███████║╚███╔███╔╝ ║
║ ╚═╝ ╚═══╝ ╚═════╝ ╚══════╝╚══════╝╚══════╝ ╚══╝╚══╝ ║
║ ║
║ Remove null & undefined values safely ║
║ ║
╚═══════════════════════════════════════════════════════════╝Installation
npm install nullsweepUsage
Basic cleaning
import { cleanObject } from "nullsweep"
const dirty = {
name: "Alex",
email: undefined,
age: null,
active: false
}
const clean = cleanObject(dirty)
// { name: "Alex", active: false }Deep cleaning nested objects
import { cleanObject } from "nullsweep"
const data = {
user: {
name: "Alex",
email: null
}
}
const clean = cleanObject(data, { deep: true })
// { user: { name: "Alex" } }Remove empty values
import { cleanObject } from "nullsweep"
const data = {
name: "",
tags: [],
meta: {}
}
const clean = cleanObject(data, {
removeEmptyString: true,
removeEmptyArray: true,
removeEmptyObject: true
})
// {}API
cleanObject(obj, options?)
Parameters
objRecord<string, any>— Object to cleanoptionsCleanOptions— Optional configuration
Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| deep | boolean | false | Recursively clean nested objects |
| removeEmptyString | boolean | false | Remove empty strings "" |
| removeEmptyArray | boolean | false | Remove empty arrays [] |
| removeEmptyObject | boolean | false | Remove empty objects {} |
Returns
Partial<T> — Cleaned object with null and undefined values removed
Contributing
We welcome contributions! 🎉
See CONTRIBUTING.md for guidelines on how to contribute.
License
MIT © Suraj
