string-values-to-numbers

1.2.0 • Public • Published

string-values-to-numbers

This package will convert your string values/numbers from your request body to numbers either int, float, double. you dont need specifically to convert/parse to int float each time.

This package uses recursive calling mechanism which can convert Numbers values in string in your nested objects, arrays at upto any level.

It handles most Cases like:

String Numbers => "4.5"/"6" => 4.5/6

Boolean String:  "true"/"false"  => true/false

Null String => "null" => null

Undefined String => "undefined" => "undefined" will be consider as string

USAGE:

const YourName = require("string-values-to-numbers")

console.log( YourName(req.body) ) //will filter your request object and return object back

Here are some screenshots of request body before and OUTPUT.

Request Body Screenshot:

request_body

Output Screenshot:

output_body

REQUEST BODY:

{
   "a" : "true",
   "b": {
       "c": "4",
       "d": 4.5
   },
   "e": [
       {
           "f": "4.1",
           "g": {
               "h": [
                   {
                       "i": "3.43"
                   },
                   {
                       "j": "false"
                   }

               ]
           }

       },
       {
           "k": "4"
       },
       4,
       "Shahkar",
       "Rizwan",
       "5.4",
       null,
       "undefined",
       [],
       {},
       false

   ],
   "l": [],
   "m": { },
   "n": true,
   "o": "null",
   "p": "undefined"
}

OUTPUT BODY:

{
    "a": true,
    "b": {
        "c": 4,
        "d": 4.5
    },
    "e": [
        {
            "f": 4.1,
            "g": {
                "h": [
                    {
                        "i": 3.43
                    },
                    {
                        "j": false
                    }
                ]
            }
        },
        {
            "k": 4
        },
        4,
        "Shahkar",
        "Rizwan",
        5.4,
        null,
        "undefined",
        [],
        {},
        false
    ],
    "l": [],
    "m": {},
    "n": false,
    "o": null,
    "p": "undefined"
}
```

For any suggestions & bugs reporting please comment issues. or reach me at shahkarkhan02@gmail.com

Package Sidebar

Install

npm i string-values-to-numbers

Weekly Downloads

1

Version

1.2.0

License

ISC

Unpacked Size

6.88 kB

Total Files

5

Last publish

Collaborators

  • rizwanshaikh
  • shahkarkhan02