Clean JavaScript Objects and Arrays, recursively. Lightweight and Fast.
Cleana efficiently sanitize both JavaScript Objects and Arrays by removing unwanted values such as undefined
, NaN
, empty objects {}
and empty arrays []
.
Install package:
npm install cleana
import { cleana } from "cleana"
const input = {
array: [],
object: {},
string: "",
nan: Number.NaN,
und: undefined,
value: "123",
nested: {
arr: ["", [], {}],
obj: { key: {}, key2: [] },
nestedValue: "456"
}
}
const output = cleana(input)
/*
{
value: "123",
nested: {
nestedValue: "456"
}
}
*/
Cleana takes argument object of type CleanaOptions
:
Option | Type | Description | Default |
---|---|---|---|
cleanArray |
boolean |
Remove empty arrays, e.g., [] |
true |
cleanObject |
boolean |
Remove empty objects, e.g., {} |
true |
cleanNull |
boolean |
Remove null values | true |
cleanUndefined |
boolean |
Remove undefined values | true |
cleanString |
boolean |
Remove empty strings, e.g., "" |
true |
cleanNaN |
boolean |
Remove NaN values | true |
removeKeys |
string[] |
Forcefully remove keys, e.g., ["key1", "key2"] |
[] |
removeValues |
any[] |
Forcefully remove values, ie: ["someString", 123, false, {key: "123"} ], using the fast-deep-equal to check equality. |
[] |
inPlace |
boolean |
Create a new object that is cleaned or mutate the object passed to it & clean in place | false |
Cleana is built on the work of fast-clean
, clean-deep
. deep-cleaner
and obj-clean
.
Faster and More Efficient
Cleana is designed to be quicker than its predecessors.
Benchmark code is located here.
All-in-One Features
Cleana combines the best functionalities from the libraries mentioned above. This means you get a set of tools for cleaning JavaScript objects all in one place.
Lightweight and fully tested
Cleana has no external dependencies. Its lightweight design helps keep your application running without unnecessary bloat. Cleana is around 1 KB
when Gzipped, and has 100%
test coverage.
This benchmark runs against the available test cases, It is recommended to benchmark against your data using the benchmark source code.
Do not Clean null Values
const options: CleanaOptions = { cleanNull: false }
const input = { a: Number.NaN, b: null, c: "", d: 42 }
cleana(input, options)
// { d: 42, b: null }
Remove Specefic keys
const input = { a: { b: { c: 1, d: 2, e: 3 }, e: 5 } }
const options = { removeKeys: ["e", "c"] }
cleana(input, options)
// { a: { b: { d: 2 } } }
Remove Specefic values
const input = { a: { b: { c: 1, d: 2, e: 3, u: "55", g: false }, e: 5 }, v: { g: 100 } }
const options = { removeValues: [1, 3, false, "55", { g: 100 }] }
cleana(input, options)
// { a: { b: { d: 2 }, e: 5 } }
Clean in-place
const input = { a: null, b: Number.NaN, c: "", d: 42 }
const options = { inPlace: true }
cleana(input, options)
// input is equal to `{d: 42}`
Local Development
- Clone this repository
- Install latest LTS version of Node.js
- Install dependencies using
bun install
- Run interactive tests using
bun dev