1.1.1 • Published 3 years ago

nested-mask-attributes v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

npm version

The easiest library to use for masking or deleting one or multiple nested attributes from a Javascript object.

Table of Contents

Install

npm install nested-mask-attributes

Usage

Example of masking attributes:

import {
  MaskActions,
  maskAttribute,
} from 'nested-mask-attributes';

// The object that has the attribute(s) to hide or mask
const testObject = {
  name: "nested-mask-attributes",
  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 hiding attributes:

import {
  MaskActions,
  maskAttribute,
} from 'nested-mask-attributes';

// The object that has the attribute(s) to hide or mask
const testObject = {
  name: "nested-mask-attributes",
  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",
      }
    }
  }
*/

Notes

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:

OPTIONSTYPEDESCRIPTION
actionMaskAction (HIDE/MASK)Action to execute when the attribute is found.
substituteCharstring (optional)Default is "*". Char or string to substitute current value. It is only applied if the action is MaskActions.MASK
useSameLengthboolean (optional)Default is false. Flag to indicate if the length of the string that will replace the original value of the attribute will keep the same length. If set to false, by default the length used is 5. It is only applied if the action is MaskActions.MASK

License

Author