Deep Cleaner is a JavaScript utility designed to clean and sanitize JSON data structures by removing unwanted values such as null
, undefined
, empty strings, and zeros. This package is especially useful for preparing data for processing or storage, ensuring that only meaningful values are retained.
- Cleans nested objects and arrays.
- Configurable options to specify which values to remove.
- Handles JSON string input and parses it automatically.
- Throws informative errors for invalid input.
You can install the Deep Cleaner package using npm. Run the following command in your terminal:
npm install deep-cleaner-js
To use the Deep Cleaner package, you can import it in your JavaScript file using either require()
or the ES6 import
statement.
const cleanData = require('deep-cleaner-js');
import cleanData from 'deep-cleaner-js';
const jsonData = `{ "name": "Alice", "age": null, "email": "", "preferences": { "color": "blue", "food": null } }`;
const config = { cleanNull: true, cleanEmptyString: true };
const cleanedData = cleanData(jsonData, config); console.log(cleanedData);
{
"name": "Alice",
"preferences": {
"color": "blue"
}
}
The cleanData
function accepts an optional configuration object to specify which values to clean. The following options are available:
cleanNull
(Boolean): Iftrue
, removes allnull
values from the data. Default istrue
.cleanUndefined
(Boolean): Iftrue
, removes allundefined
values from the data. Default istrue
.cleanEmptyString
(Boolean): Iftrue
, removes all empty strings from the data. Default isfalse
.cleanZero
(Boolean): Iftrue
, removes all zero values from the data. Default isfalse
.
const config = {
cleanNull: true,
cleanUndefined: true,
cleanEmptyString: true,
cleanZero: true
};
const jsonData = `{ "users": [ { "name": "Charlie", "age": null }, { "name": "Dana", "age": 28, "preferences": { "hobbies": [null, "Dancing"] } }, { "name": "Eve", "age": 0 } ] }`;
const config = { cleanNull: true, cleanUndefined: true, cleanEmptyString: true, cleanZero: true };
const cleanedData = cleanData(jsonData, config); console.log(cleanedData);
{
"users": [
{ "name": "Dana", "age": 28, "preferences": { "hobbies": ["Dancing"] } }
]
}
The cleanData
function will throw errors in the following scenarios:
- If the input is an invalid JSON string, it will throw an error with the message:
"Invalid JSON string provided."
- If the input is of an unsupported type (not an object, array, or valid JSON string), it will throw an error with the message:
"Input must be an object, array, or a valid JSON string."
try {
const invalidData = cleanData('Invalid JSON', {});
} catch (error) {
console.error(error.message); // "Invalid JSON string provided."
}
Contributions are welcome! If you would like to contribute to Deep Cleaner, please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeature
). - Make your changes and commit them (
git commit -m 'Add some feature'
). - Push to the branch (
git push origin feature/YourFeature
). - Open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.