42 lines
740 B
JavaScript
42 lines
740 B
JavaScript
|
'use strict';
|
||
|
|
||
|
const internals = {};
|
||
|
|
||
|
|
||
|
module.exports = function (input) {
|
||
|
|
||
|
if (!input) {
|
||
|
return '';
|
||
|
}
|
||
|
|
||
|
const lessThan = 0x3C;
|
||
|
const greaterThan = 0x3E;
|
||
|
const andSymbol = 0x26;
|
||
|
const lineSeperator = 0x2028;
|
||
|
|
||
|
// replace method
|
||
|
let charCode;
|
||
|
return input.replace(/[<>&\u2028\u2029]/g, (match) => {
|
||
|
|
||
|
charCode = match.charCodeAt(0);
|
||
|
|
||
|
if (charCode === lessThan) {
|
||
|
return '\\u003c';
|
||
|
}
|
||
|
|
||
|
if (charCode === greaterThan) {
|
||
|
return '\\u003e';
|
||
|
}
|
||
|
|
||
|
if (charCode === andSymbol) {
|
||
|
return '\\u0026';
|
||
|
}
|
||
|
|
||
|
if (charCode === lineSeperator) {
|
||
|
return '\\u2028';
|
||
|
}
|
||
|
|
||
|
return '\\u2029';
|
||
|
});
|
||
|
};
|