1.0.5 • Published 2 years ago

handle-json-payload v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

handle-json-payload

Library to handle the object that will be sent to an API service

GitHub top language GitHub code size in bytes GitHub issues GitHub closed issues GitHub contributors GitHub npm Website

handle-json-payload I'm making this library to solve a problem with payload objects, the purpose of this library is to intercept the final object, clean, fix, remove null properties, empty arrays, undefined. I hope to be able to contribute to the community! Welcome to add more features. Thanks :)

Characteristics

  • Treat an object with empty, null, array, empty and etc properties

How to use

npm.io

Installation

NPM:

npm i handle-json-payload --save

1 - Simple mode, using object handling

How to use without intercepting the data

const payload = require('handle-json-payload');

const yourObject = {
    "teste1": null,
    "teste2": "Some text to represent string",
    "teste3": [],
    "teste4": [
        {
            "teste5": null
        },
        {
            "teste6": "Teste 6"
        },
        {
            "teste7": [],
            "teste8": "Teste 8",
            "teste9": [
                {
                    "teste10": null,
                    "teste11": []
                }
            ]
        }
    ],
    "teste12": {
        "teste13": null,
        "teste14": {},
        "teste15": false
    },
    "teste16": {},
    "teste17": false,
    "teste18": 18,
    "teste19": [
        {},
        {
            "teste20": []
        }
    ]
}

const cleanObject = payload.treatObjectToSend(yourObject);
console.log(cleanObject);

2 - Now how to intercept and handle the traversed values

If you want to intercept the values ​​and treat you can follow this second example

const payload = require('handle-json-payload');

const yourObject = {
    "teste1": null,
    "teste2": "Some text to represent string",
    "teste3": [],
    "teste4": [
        {
            "teste5": null
        },
        {
            "teste6": "Teste 6"
        },
        {
            "teste7": [],
            "teste8": "Teste 8",
            "teste9": [
                {
                    "teste10": null,
                    "teste11": []
                }
            ]
        }
    ],
    "teste12": {
        "teste13": null,
        "teste14": {},
        "teste15": false
    },
    "teste16": {},
    "teste17": false,
    "teste18": 18,
    "teste19": [
        {},
        {
            "teste20": []
        }
    ]
}

This function (callback) will be called on all values

const callback = (data) => {
    console.log('Data->', data);
    return data;
}

const cleanObject = payload.treatObjectToSend(yourObject, callback);
console.log(cleanObject);

FINAL RESULT

  {
    teste2: "Some text to represent string",
    teste4: [
        {
            teste6: "Teste 6"
        },
        {
            teste8: "Teste 8"
        }
    ],
    teste12: {
        teste15: false
    },
    teste17: false,
    teste18: 18
  }

Solutions and Errors

1 - For those with TypeScript getting error TS2304: can't find the name 'requires'

2 - TypeScript getting error, cannot find name 'require' - TypeScript 1.x

Help improve

Found a bug or a problem? Open a new issue GitHub.

Contributing to this project

GitHub

Autor

@JeanPaul – jeanpaulwebb@gmail.com.br

GitHub contributors

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago