JSPM

recursive-escape

2.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 81
  • Score
    100M100P100Q68471F
  • License ISC

Recursivly escape an object or array for use in HTML

Package Exports

  • recursive-escape

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (recursive-escape) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Recursively Escape Objects/Arrays for HTML

Install:

$ npm install --save recursive-escape

Recursivly escapes strings in objects or arrays for safe output in HTML. The most common use case for this is to pass data from a server rendered app to the front-end. See recursive-unescape for use on the front-end when loading in the escaped data.

Usage:

var escape = require('recursive-escape');

var obj = {
    foo: 'My unsafe <script>alert("You have been hacked!!");</script> string.',
    number: 1,
    arr: ['foo', '<h1>Hi!!</h1>'],
    obj: {
        nested: {
            bar: '<'
        }
    }
};

var e = escape(obj);

console.log(e.toJSON(e, '\t'));
/*
Output:

{
    "foo": "My unsafe &lt;script&gt;alert(&quot;You have been hacked!!&quot;);&lt;/script&gt; string.",
    "number": 1,
    "arr": [
        "foo",
        "&lt;h1&gt;Hi!!&lt;/h1&gt;"
    ],
    "obj": {
        "nested": {
            "bar": "&lt;"
        }
    }
}
*/