ng-anonymize
Version:
Data anonymization library for Angular
151 lines (143 loc) • 5.91 kB
JavaScript
import { __decorate } from 'tslib';
import { Pipe, ɵɵdefineInjectable, Injectable, NgModule } from '@angular/core';
/* Default settings exported for internal only
Todo: allow consumer to query or set these
*/
const AnonymizeDefaultValues = {
PhoneCcLength: 3,
MaskChar: '*',
LeftReveal: 3,
RightReveal: 4
};
/* List of implemented anonymization methods.
For more details, see documentation
*/
var AnonymizeMethod;
(function (AnonymizeMethod) {
AnonymizeMethod["Randomize"] = "randomize";
AnonymizeMethod["Shuffle"] = "shuffle";
AnonymizeMethod["First"] = "first";
AnonymizeMethod["Last"] = "last"; // Reveal only x last characters
})(AnonymizeMethod || (AnonymizeMethod = {}));
/* Method metadata for internal use only
*/
const AnonymizeMethods = [
{
name: AnonymizeMethod.Randomize,
label: 'Randomize'
},
{
name: AnonymizeMethod.Shuffle,
label: 'Shuffle'
},
{
name: AnonymizeMethod.First,
label: 'First'
},
{
name: AnonymizeMethod.Last,
label: 'Last'
},
];
var AnonymizeDataType;
(function (AnonymizeDataType) {
AnonymizeDataType["Phone"] = "phone";
})(AnonymizeDataType || (AnonymizeDataType = {}));
/* Predefined constants for text processing */
const ALPHABET = 'abcdefghijjklmnopqrstuvwyz';
const lowerAlpha = ALPHABET.split('');
const upperAlpha = ALPHABET.toUpperCase().split('');
const nums = '0123456789'.split('');
let AnonymizePipe = class AnonymizePipe {
transform(value, method = AnonymizeMethod.Randomize, options) {
if (!value)
return '';
// Extract options or default
const { type, mask = AnonymizeDefaultValues.MaskChar, bleed } = options || {};
switch (method) {
case AnonymizeMethod.First:
// Replace all but first bleed characters by mask
return value.split('').map((c, index) => index < (bleed || AnonymizeDefaultValues.LeftReveal) ? c : mask).join('');
case AnonymizeMethod.Last:
// Replace all but last bleed characters by mask
return value.split('').map((c, index) => index >= value.length - (bleed || AnonymizeDefaultValues.RightReveal) ? c : mask).join('');
case AnonymizeMethod.Randomize:
// Replace each character with random value of same character class
return value.split('').map((c, index) => {
const isLowerAlpha = lowerAlpha.indexOf(c) >= 0;
const isUpperAlpha = upperAlpha.indexOf(c) >= 0;
const isNum = nums.indexOf(c) >= 0;
const charClass = isLowerAlpha ? lowerAlpha : isUpperAlpha ? upperAlpha : isNum ? nums : null;
if (!charClass)
return c;
if (type === AnonymizeDataType.Phone) {
if (index < AnonymizeDefaultValues.PhoneCcLength)
return c;
}
return charClass[Math.floor(Math.random() * charClass.length)];
}).join('');
case AnonymizeMethod.Shuffle:
// Swap positions of all characters, preserving position of same character class
const chars = value.split('');
const shuffledAlpha = chars
.filter(c => lowerAlpha.indexOf(c) >= 0 || upperAlpha.indexOf(c) >= 0).sort(() => 0.5 - Math.random());
const shuffledNums = chars
.filter(c => nums.indexOf(c) >= 0).sort(() => 0.5 - Math.random());
return chars.map((c, index) => {
const isLowerAlpha = lowerAlpha.indexOf(c) >= 0;
const isUpperAlpha = upperAlpha.indexOf(c) >= 0;
const isNum = nums.indexOf(c) >= 0;
const charClass = isLowerAlpha ? lowerAlpha : isUpperAlpha ? upperAlpha : isNum ? nums : null;
if (!charClass)
return c;
if (type === AnonymizeDataType.Phone) {
// If type = phone, do not transform first 3 characters
if (index < AnonymizeDefaultValues.PhoneCcLength)
return c;
}
let newChar = isNum ? shuffledNums.pop() : shuffledAlpha.pop();
if (isUpperAlpha)
newChar = newChar.toUpperCase();
else if (isLowerAlpha)
newChar = newChar.toLowerCase();
return newChar;
}).join('');
default:
return value || '';
}
}
};
AnonymizePipe = __decorate([
Pipe({
name: 'anonymize',
pure: true
})
], AnonymizePipe);
/* TODO: expose services for consumer */
let NgAnonymizeService = class NgAnonymizeService {
constructor() {
}
};
NgAnonymizeService.ɵprov = ɵɵdefineInjectable({ factory: function NgAnonymizeService_Factory() { return new NgAnonymizeService(); }, token: NgAnonymizeService, providedIn: "root" });
NgAnonymizeService = __decorate([
Injectable({
providedIn: 'root'
})
], NgAnonymizeService);
let NgAnonymizeModule = class NgAnonymizeModule {
};
NgAnonymizeModule = __decorate([
NgModule({
declarations: [AnonymizePipe],
imports: [],
exports: [AnonymizePipe]
})
], NgAnonymizeModule);
/*
* Public API Surface of ng-anonymize
*/
/**
* Generated bundle index. Do not edit.
*/
export { AnonymizeDataType, AnonymizeDefaultValues, AnonymizeMethod, AnonymizeMethods, AnonymizePipe, NgAnonymizeModule, NgAnonymizeService };
//# sourceMappingURL=ng-anonymize.js.map