strman.htmlencode
Version:
The [strman](https://github.com/dleitee/strman) method `htmlencode` exported as a [Node.js](https://nodejs.org/) module.
41 lines (39 loc) • 977 B
JavaScript
var entitiesEncode = require('strman.entitiesencode')
var replace = require('strman.replace')
Object.defineProperty(exports, "__esModule", {
value: true
});
/**
* @module htmlEncode
* @description
* Convert all applicable characters to HTML entities.
* ## Install
* Install all functions of strman
* ```sh
* yarn add strman
* ```
* or just the htmlEncode function
* ```sh
* yarn add strman.htmlencode
* ```
* ## Usage
* ```javascript
* import { htmlEncode } from 'strman'
* // OR
* import htmlEncode from 'strman.htmlencode'
* ```
* @param {String} value value to encode.
* @example
* htmlEncode('<div>')
* // => '<div>'
* @returns { String } The encoded data.
*/
exports.default = function (value) {
return replace(value, '[\\u00A0-\\u9999<>\\&]', function (match) {
if (typeof entitiesEncode[match] !== 'undefined') {
return entitiesEncode[match];
}
return match;
}, true, true);
};
module.exports = exports['default'];