/string.prototype.safe

Make a string safe!

Primary LanguageJavaScript

string.prototype.safe

Make anything safe

A simple function to make anything safe so no one can XSS your website.

const Safe = function (input) {
    if (typeof input === "number") {
        return input;
    }
    if (typeof input === "string") {
        return input
            .split('')
            .join('')
            .replace(/</g, "&lt;")
            .replace(/>/g, "&gt;");
    }
    if (Array.isArray(input)) {
        return input.map(Safe);
    }
    if (typeof input === "object" && input !== null) {
        let safeObj = {};
        for (let key in input) {
            if (input.hasOwnProperty(key)) {
                safeObj[Safe(key)] = Safe(input[key]);
            }
        }
        return safeObj;
    }
    throw new Error("Unrecognized type");
};