Skip to content

Library: cleanRegExp()

Eugene Lazutkin edited this page Jul 3, 2018 · 1 revision

cleanRegExp() module is a function, which recursively removes properties fitting a regular expression.

cleanRegExp() works slower than clean() but more flexible. If you want to remove just one property consider clean().

Usage

In a module-enabled environment (like Babel) it can be accessed like that:

import cleanRegExp from '@researchnow/reno/src/utils/cleanRegExp';

In global-based environments (like a browser) it is frequently mapped to Reno.utils.cleanRegExp.

cleanRegExp(object, pattern=/^_/')

The function takes the following arguments:

  • object is an object, which will be recursively traversed and checked for fitting properties. If found, that properties will be deleted.
  • pattern is a regular expression to look for. The default: /^_/.

It returns object.

Examples

Using the default:

const object = [
  {_id: 1, _meta: {account: 42}, value: 'x'},
  {_id: 2, _meta: {account: 66}, value: 'y'}
];
cleanRegExp(object);
assert(isEqual(object,
  [
    {value: 'x'},
    {value: 'y'}
  ]
));

Custom removal:

const object = {
  '@audit': 'mnvkbvoiqwe',
  name: {
    '@audit': 'sdspodpsdnn',
    first: 'Jill',
    last: 'Smith',
    meta$: 'db56'
  },
  meta$: 'db42'
};
cleanRegExp(object, /^@|\$$/);
assert(isEqual(object,
  {
    name: {
      first: 'Jill',
      last: 'Smith'
    }
  }
));

Clone this wiki locally