Skip to content

MohammadRostami71/deep-cleaner-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Deep Cleaner

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.

Table of Contents

Features

  • 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.

Installation

You can install the Deep Cleaner package using npm. Run the following command in your terminal:

npm install deep-cleaner-js

Usage

To use the Deep Cleaner package, you can import it in your JavaScript file using either require() or the ES6 import statement.

Using require()

const cleanData = require('deep-cleaner-js');

Using import

import cleanData from 'deep-cleaner-js';

Basic Example

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);

Output

{
    "name": "Alice",
    "preferences": {
        "color": "blue"
    }
}

Configuration Options

The cleanData function accepts an optional configuration object to specify which values to clean. The following options are available:

  • cleanNull (Boolean): If true, removes all null values from the data. Default is true.
  • cleanUndefined (Boolean): If true, removes all undefined values from the data. Default is true.
  • cleanEmptyString (Boolean): If true, removes all empty strings from the data. Default is false.
  • cleanZero (Boolean): If true, removes all zero values from the data. Default is false.

Example Configuration

const config = {
    cleanNull: true,
    cleanUndefined: true,
    cleanEmptyString: true,
    cleanZero: true
};

Examples

Cleaning Nested Structures

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);

Output

{
    "users": [
        { "name": "Dana", "age": 28, "preferences": { "hobbies": ["Dancing"] } }
    ]
}

Error Handling

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."

Example of Error Handling

try {
    const invalidData = cleanData('Invalid JSON', {});
} catch (error) {
    console.error(error.message); // "Invalid JSON string provided."
}

Contributing

Contributions are welcome! If you would like to contribute to Deep Cleaner, please follow these steps:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/YourFeature).
  3. Make your changes and commit them (git commit -m 'Add some feature').
  4. Push to the branch (git push origin feature/YourFeature).
  5. Open a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

deep-cleaner-js

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published