1.0.0 • Published 4 years ago
nested-mask v1.0.0
The easiest library to use for masking or deleting a nested attribute from a Javascript object
Table of Contents
Install
npm install nested-mask
Usage
Example of hidding attributes:
import {
maskAttribute
} from 'nested-mask';
// The object that has the attribute(s) to hide or mask
const testObject = {
name: "nested-mask",
features: {
id: "nm123456",
date: "2021-10-01T14:36:06.265Z",
isEnabled: true,
author: {
country: "Spain",
name: "Rodrigo",
}
}
}
const testObjectMask = maskAttribute(testObject, ["name", "isEnabled"], {
action: MaskActions.MASK,
});
// RESULT
/*
{
name: "*****",
features: {
id: "nm123456",
date: "2021-10-01T14:36:06.265Z",
isEnabled: "****",
author: {
country: "Spain",
name: "*****",
}
}
}
*/
Example of masking attributes:
import {
maskAttribute
} from 'nested-mask';
// The object that has the attribute(s) to hide or mask
const testObject = {
name: "nested-mask",
features: {
id: "nm123456",
date: "2021-10-01T14:36:06.265Z",
isEnabled: true,
author: {
country: "Spain",
name: "Rodrigo",
}
}
}
const testObjectHide = maskAttribute(testObject, ["name", "isEnabled"], {
action: MaskActions.HIDE,
});
// or
/*
const testObjectHide = maskAttribute(testObject, ["name", "isEnabled"], {
action: MaskActions.HIDE,
});
*/
// RESULT
/*
{
features: {
id: "nm123456",
date: "2021-10-01T14:36:06.265Z",
author: {
country: "Spain",
}
}
}
*/
Things to watch out for
If the attribute is contained in the object but it is of type "object" then it will not work. Admitted data types:
- string
- number
- boolean
array
Methods
maskAttribute(object, attributes, options);
object(any): The input object that you want to get the attribute(s) from. Any Javascript object is valid.
attributes(string[]): Array of attributes from which you want to mask or hide.
options(Options): There are different options:
OPTIONS | TYPE | DESCRIPTION |
---|---|---|
action | MaskAction (HIDE/MASK) | Action to execute when the attribute is found. |
substituteChar | string (optional) | Default is "*". Char or string to substitute current value. It is only applied if the action is MaskActions.MASK |
useSameLength | boolean (optional) | Default is false. Indicates the length of the string that will replace the value of the attribute. By default the length used is 5. It is only applied if the action is MaskActions.MASK |