UNPKG

redacted-ts

Version:
48 lines (47 loc) 2.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DefaultRedactor = exports.RedactorWithTag = exports.RedactorWithDictionary = exports.Redactor = exports.clean = exports.expand = exports.redact = exports.DEFAULT_TAG = void 0; const Dictionary_1 = require("./Dictionary"); exports.DEFAULT_TAG = '~'; const redact = (dictionary, tag, cipher, both) => (line, ...delimiters) => line.split(new RegExp('\\s' + delimiters.map(delim => delim !== ' ' && delim !== '\t' && delim !== '\n' && delim !== '\\s' ? delim : '').join(''))).map(word => both || !dictionary.isEmpty() ? dictionary.contains(word) ? cipher.encrypt(word) : tag !== '' && word.startsWith(tag) ? tag + cipher.encrypt(word.substr(tag.length)) : word : !both && dictionary.isEmpty() && tag !== '' && word.startsWith(tag) ? tag + cipher.encrypt(word.substr(tag.length)) : word).join(' '); exports.redact = redact; const expand = (dictionary, tag, cipher, both) => (line) => line.split(' ').map(word => both || !dictionary.isEmpty() ? tag !== '' && word.startsWith(tag) ? tag + cipher.decrypt(word.substr(tag.length)) : dictionary.contains(cipher.decrypt(word)) ? cipher.decrypt(word) : word : !both && dictionary.isEmpty() ? tag !== '' && word.startsWith(tag) ? tag + cipher.decrypt(word.substr(tag.length)) : word : word).join(' '); exports.expand = expand; const clean = (tag) => (str) => tag !== '' ? str.replace(new RegExp(tag, 'g'), '') : str; exports.clean = clean; const Redactor = (dictionary, tag, cipher, both) => ({ dictionary, tag, cipher, both: both || !dictionary.isEmpty() && tag !== '', redact: (0, exports.redact)(dictionary, tag, cipher, both), expand: (0, exports.expand)(dictionary, tag, cipher, both), clean: (0, exports.clean)(tag) }); exports.Redactor = Redactor; const RedactorWithDictionary = (dictionary, cipher) => (0, exports.Redactor)(dictionary, '', cipher, false); exports.RedactorWithDictionary = RedactorWithDictionary; const RedactorWithTag = (tag, cipher) => (0, exports.Redactor)((0, Dictionary_1.Dictionary)([]), tag, cipher, false); exports.RedactorWithTag = RedactorWithTag; const DefaultRedactor = (cipher) => (0, exports.Redactor)((0, Dictionary_1.Dictionary)([]), exports.DEFAULT_TAG, cipher, false); exports.DefaultRedactor = DefaultRedactor;