A powerful tool to free your objects and arrays from unwanted content
npm install --save doff
- wipe, i.e. remove, unwanted content
- blur, i.e. mask, sensitive content
- mutate or accumulate a target
- choose start or end depth
- symbol keys support
- no external dependencies
Alongside the default instance
const doff = require('doff');
you can also create a new instance with custom options
// Options for a new instance are result of merge between provided and default options
const instance = doff.create({
mutate: true,
symbols: true
});
if needed, you can overwrite options of the default instance
// Only provided keys are overwritten
doff.use({
symbols: true,
falltrough: true,
blur: {
paths: 'top.secret.path'
}
});
Overwriting options of the default instance won't affect instances created before, but it will affect instances created later on, if options are not isolated.
Working with instance options
const target = {
some: 'value',
state: false,
big: 0,
nest: {
nothing: null
},
sequence: [17, 'is', '', undefined, {}],
unique: new Set(),
pattern: /[how]_[does]_[this]_[work]/
};
const output = doff(target);
console.log(target === output);
// => false
console.log(output);
/* =>
{
some: 'value',
state: false,
big: 0,
sequence: [17, 'is'],
pattern: /[how]_[does]_[this]_[work]/
}
*/
Working with custom options
const target = [
'bold statement',
NaN,
true,
{
now: {
lets: {
nest: {
things: undefined
}
}
}
},
null,
new Set(),
['', {}, [null]]
];
doff(target, {
mutate: true,
depth: 2
});
console.log(target);
/* =>
[
'bold statement',
true,
{
now: {
lets: {
nest: {
things: undefined
}
}
}
},
[[null]]
]
*/
Working with isolated options
const target = {
start: undefined,
with: true,
empty: {},
or: '',
simple: {
[Symbol('key')]: 17
},
pattern: /[0-9]/
};
// Using a instance with isolated options
const instance = doff.create({
isolate: true
});
let output = instance(target);
// Is equivalent of providing isolated options
output = doff(target, {
isolate: true
});
console.log(target === output);
// => false
console.log(target.empty === output.empty);
// => true
console.log(target.simple === output.simple);
// => true
console.log(output);
/* =>
{
start: undefined,
with: true,
empty: {},
or: '',
simple: {
[Symbol('key')]: 17
},
pattern: /[1-9]/
}
*/
and not providing wipe
and blur
keys, will output an accumulator which is a clone of a target. However, not
all values will be cloned, just ones with a type object
that have enumerable keys, symbol
keys are dependent on a
symbols
option.
Providing isolated options to the doff.use
is the same as providing non-isolated options.
The list of available options for doffing
isolate
- determines how provided options are used, whentrue
options are used as is, otherwise merged with options of the default instancemutate
- determines what to do with a provided target, whentrue
a target is mutated, otherwise a new one, with a same prototype, is constructeddepth
- defines how deep to go, acceptsnumber 0
- go all the waynumber > 0
- go up to the defined depthnumber < 0
- start from the defined depth
symbols
- determines which keys are taken into consideration, whentrue
symbol keys are also taken into consideration, otherwise they are ignoredfallthrough
- determines what to do with kept entries, whentrue
they will reach thewipe
, otherwise they won'tpreserve
- takes precedence over thereference
and thewipe
arrays
- determines what to do with arrays, whentrue
array values will stay intact, otherwise they will be inspectedobjects
- determines what to do with objects, whentrue
object values will stay intact, otherwise they will be inspectedpaths
- defines paths to keep, acceptsstring
- a single path to keep, e.g.some.important[0].path
Array.<string>
- a list of paths to keep, e.g.[some.important[0].path, this.one.also]
types
- defines types to keep, acceptsstring
- a single type to keep, e.g.[object Map]
function
- a single type to keep, e.g.Set
, evaluated usinginstanceof
Array.<string|function>
- a combination of previous two
reference
- an object to take as the reference, i.e. if an object has a path keep it, otherwise inspect, takes precedence over thewipe
wipe
- values to wipe from a target, acceptsstring falsy.relaxed
- falsy values excluding,false
and0
string falsy.strict
- falsy valuesstring empty.loose
- falsy values, excluding0
andfalse
, objects without own keys, arrays and likes with length of0
, maps and sets with size of0
string empty.relaxed
, aliasfalse
- falsy values, excluding0
andfalse
, objects without ownstring
enumerable keys, arrays and likes with length of0
, maps and sets with size of0
string empty.strict
, aliastrue
- falsy values, objects without ownstring
enumerable keys, arrays and likes with length of0
, maps and sets with size of0
Array
- of valuesfunction
- with the signature(value, path, target)
, returningtrue
for wiping orfalse
otherwise
blur
- paths that are not wiped can be blurred, e.g. masked. Suitable for a sensitive data, e.g. passwordspaths
- to blur, i.e. replace with a mask value, acceptsstring
- a single path to blur using value of themask
keyobject
- where key is a path to blur and value is a mask to useArray.<string|object>
- a combination of previous twofunction
- with the signature(value, path, target)
, returning a mask to use
mask
- the default mask to use for blurring
Default values of available options, i.e. options of the default instance
{
isolate: false,
mutate: false,
depth: 0,
symbols: false,
fallthrough: false,
preserve: {
arrays: false,
objects: false,
paths: undefined,
types: ['[object Date]', '[object RegExp]']
},
reference: undefined,
wipe: false,
blur: {
paths: undefined,
mask: '*******'
}
}
As mentioned above callback provided as wipe
and / or blur.paths
options should look like
/**
* @param {Object} value - the value to inspect
* @param {Object} path - the path of the value inside the target
* @param {Object} target - the provided target
*/
function (value, path, target) {
// Hm, what to do now?
}
Parameter path
passed to a callback is an object containing asString
and asArray
properties.
Take for a example the following target,
const target = {
start: [{
[Symbol('of')]: 'something'
}]
};
path
passed to a callback in a case of the 'something'
value, would look like
{
asString: 'start[0].@@of'
asArray: ['start', 0, Symbol('of')]
}
Doff unwanted entries and return the result.
Available only on the default instance!
Overwrite options of the default instance with provided ones and return the default instance.
Available only on the default instance!
Create a new instance using provided options.
Return options of a target instance.
Alias for the doff
.
This project is licensed under the terms of the MIT license.