Skip to content
jericirenej edited this page May 27, 2022 · 9 revisions

Object filtering

Easily filter object by their properties - then get the filtered object back!

(documentation is in the process of updating for the 1.3.0 release)

  • Two types of filtering available:
    • Exclude all of the matched properties or
    • Include only the matched properties.
  • Filter by a list of property names or regex filters.
  • Recursive filtering option.
    • Recursive filtering will not inspect (filter within) arrays, sets, maps, dates, and other in-built classes.
  • Compatibility with ES6 and CommonJS imports.

Usage

  • Install the package via npm install @jericirenej/object-filter.
    • You can also clone the repo and transpile the files manually (npm install, followed by npx tsc which will transpile to dist).
  • Import or require the objectFilter function from @jericirenej/object-filter and pass it the appropriate configuration object.
// ES6 import
import objectFilter from "@jericirenej/object-filter"

// CommonJS import
const objectFilter = require("@jericirenej/object.filter").default;

/*
objectFilter signature: {
  targetObject: Record<string,any>,
  filters?: string|string[],
  regexFilters?: string|RegExp|(string|RegExp)[], --- At least one of the filter groups needs to be valid.
  filterType?: "exclude"|"include",               --- defaults to exclude.
  recursive?:boolean,                             --- defaults to true.

}
*/

const employeeInfo = {
  name: "John",
  surname: "Doe",
  personalInfo: {
    age: 30,
    sensitive1: "secret",
  },
  sensitive2: "secret",
};

const excludeSensitive = {
  targetObject: employeeInfo,
  regexFilters: /sensitive/,
  filterType: "exclude",
  recursive: true,
};

const cleanedEmployeeInfo = objectFilter(excludeSensitive);
// Will return
{
  name: "John",
  surname: "Doe",
  personalInfo: {
    age: 30,
  },
};

const includeSensitive = {
  targetObject: employeeInfo,
  regexFilters: /sensitive/,
  filterType: "include",
  recursive: true,
}

const sensitiveEmployeeInfo = objectFilter(includeSensitive);

//Will return
{
  sensitive2: "secret",
  personalInfo: {
    sensitive1: "secret,
  }
}

Things to note

  • Although the filterObject function creates a new object based on the old one, it is not guaranteed to produce a deep clone of the object. In particular, excluded types (for example arrays, sets, maps, etc.) will be shallow copied. If you need to ensure immutability, use an appropriate library (lodash, immer), to clone the result.
  • You need to supply at least one type of valid filter groups. If none of the supplied filter groups are valid, the original object will be returned. - When using the include filterType together with the recursive option please note, that if filters target a nested property, their parent has to be provided as well, as it will be filtered out otherwise in the first filter step. - That makes the include option somewhat less practical than exclude. --> Fixed.
  • If you are working with TypeScript, make sure to type cast the returned object, as its type will be Record<string,any> by default.

Planned additional features

  • Regex matching of properties. --> Implemented.
  • Improved include filterType for recursive filterings (see limitation above). Planned for the 1.3.0 release --> Implemented.
  • GitHub Page for documentation with a live code playground for testing out the package.
  • Tranpoline recursive calls.
Clone this wiki locally