Comparing version 1.0.3 to 1.0.4
65
index.js
@@ -1,53 +0,28 @@ | ||
const he = require("he"); | ||
const DOMPurify = require("dompurify"); | ||
const sanitizer = require("./sanitizer"); | ||
module.exports = value => { | ||
function sanitizeStr(str) { | ||
return str ? sanitized(str).trim() : ""; | ||
} | ||
function sanitizeArr(arr) { | ||
var clone = [].concat(arr); | ||
clone.forEach((item, index) => { | ||
clone[index] = module.exports(item); | ||
}); | ||
return clone; | ||
} | ||
function sanitizeObj(obj) { | ||
var clone = JSON.parse(JSON.stringify(obj)); | ||
for (const prop in clone) { | ||
clone[prop] = module.exports(clone[prop]); | ||
} | ||
return clone; | ||
} | ||
module.exports = (value) => { | ||
const handlers = { | ||
string: (str) => (str ? sanitizer(str).trim() : ""), | ||
array: (arr) => { | ||
var clone = [].concat(arr); | ||
clone.forEach((item, index) => (clone[index] = module.exports(item))); | ||
return clone; | ||
}, | ||
object: (obj) => { | ||
var clone = JSON.parse(JSON.stringify(obj)); | ||
Object.keys(clone).forEach( | ||
(key) => (clone[key] = module.exports(clone[key])) | ||
); | ||
return clone; | ||
}, | ||
}; | ||
return value | ||
? value.constructor === String | ||
? sanitizeStr(value) | ||
? handlers.string(value) | ||
: value.constructor === Array | ||
? sanitizeArr(value) | ||
? handlers.array(value) | ||
: value.constructor === Object | ||
? sanitizeObj(value) | ||
? handlers.object(value) | ||
: value | ||
: value; | ||
}; | ||
function sanitized(value) { | ||
return DOMPurify.sanitize | ||
? he.decode(DOMPurify.sanitize(value)) | ||
: jsdom({ createDOMPurify: DOMPurify, value }); | ||
} | ||
function jsdom({ createDOMPurify, value }) { | ||
const { JSDOM } = require("jsdom"); | ||
const window = new JSDOM("").window; | ||
const DOMPurify = createDOMPurify(window); | ||
return he.decode(DOMPurify.sanitize(value)); | ||
} |
{ | ||
"name": "sanitized", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Recursive function that'll sanitize a string or ALL values in an object or array.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2747
4
40