Skip to content

Latest commit

 

History

History
118 lines (102 loc) · 2.2 KB

README.md

File metadata and controls

118 lines (102 loc) · 2.2 KB

Circle CI

mangler

data manipulation utility

install

npm install mangler --save

usage

importing module

var mangler = require("mangler")

mangler.mapify (arrayOfObject, key)

converts an array of objects into a map with key present in those objects

example

var input = [
    {id: 1, val:20},
    {id: 1, val:30},
    {id: 2, val:40}
]

mangler.mapify(input, "id") 

output

{
    1: {id: 1, val:30}, 
    2: {id: 2, val:40}
}

mangler.arrayMapify (arrayOfObject, key)

clusters objects with the same keys into arrays

example

var input = [
    {id: 1, val:20},
    {id: 1, val:30},
    {id: 2, val:40}
]

mangler.arrayMapify(input, "id") 

output

{
    1: [
        {id: 1, val:20}, 
        {id: 1, val:30}
       ],
    2: [{id: 2, val:40}]
}

mangler.fixed(number)

removes floating point rounding error

example

console.log(0.2 + 0.1)                      // 0.30000000000000004
console.log(mangler.fixed(0.2 + 0.1))       // 0.3

mangler.delProps(arrayOfObjects, arrayOfPropertiesToBeRemoved)

removes properties from an array of objects

example

var input = [
            {
              "object": "value",
              "killme": true,
              "zapme": "yes"
            },
            {
              "object": "value",
              "killme": true
            }
          ]
var result = mangler.delProps(input, ["killme", "zapme"])

output

[
    {"object": "value"},
    {"object": "value"}
]

mangler.sanitizeString(string)

returns sanitized string

example

var result = mangler.sanitizeString("<script>alert(' c&tch n\"')</script>");

output

"&lt;script&gt;alert(&#x27; c&amp;tch n&quot;&#x27;)&lt;&#x2F;script&gt;"

mangler.unsanitizeString(string)

returns unsanitized string

example

var result = mangler.unsanitizeString("&lt;script&gt;alert(&#x27; c&amp;tch n&quot;&#x27;)&lt;&#x2F;script&gt;");

output

"<script>alert(' c&tch n\"')</script>"