UNPKG

json-property-filter

Version:

JavaScript library and application to filter a JSON object by including and excluding properties.

83 lines (82 loc) 3.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.filter = exports.extract = void 0; var util = require("./util"); var debug_1 = require("debug"); var log = debug_1.default("json-property-filter:inclusion"); function extract(filters) { debug_1.default("Extract filters"); var extractDebug = log.extend("extract"); var inclusionFilters = []; for (var i = filters.length; i--;) { var value = filters[i]; var firstCharacter = value[0]; if (value.length && firstCharacter !== "-") { extractDebug("Extracting filter: '%s'", value); var filterWithoutSymbol = firstCharacter === "+" ? value.substring(1) : value; inclusionFilters.push(filterWithoutSymbol); } } return inclusionFilters; } exports.extract = extract; function filter(source, filters) { log("Apply filters: %o", filters); var destination = Array.isArray(source) ? [] : {}; apply(source, filters, destination, util.EMPTY_CONTEXT); return destination; } exports.filter = filter; function apply(source, filters, destination, context) { var applyDebug = log.extend(context.absolutePath); for (var propertyName in source) { if (source.hasOwnProperty(propertyName)) { applyDebug("Read property: %s", propertyName); var propertyValue = source[propertyName]; var allPropertiesFilter = context.relativePath ? context.relativePath + ".**" : "**"; var rootPropertiesFilter = context.relativePath ? context.relativePath + ".*" : "*"; if (typeof propertyValue !== "object" && filters.includes(rootPropertiesFilter)) { destination[propertyName] = propertyValue; } else if (filters.includes(allPropertiesFilter)) { destination[propertyName] = propertyValue; } else if (filters.includes(context.absolutePath) || filters.includes(context.relativePath)) { destination[propertyName] = propertyValue; } else if (typeof propertyValue === "object") { var nestedDestination = Array.isArray(propertyValue) ? [] : {}; var nestedContext = util.createContext(context, source, propertyName); apply(propertyValue, filters, nestedDestination, nestedContext); if (Object.keys(nestedDestination).length) { applyDebug("Include property: %s", propertyName); var index = +propertyName; if (Array.isArray(destination) && !isNaN(index)) { destination.splice(index, 0, nestedDestination); } else { destination[propertyName] = nestedDestination; } } } else { var segments = context.segments ? context.segments.concat(propertyName) : [propertyName]; var absolutePath = segments.join("."); var relativePath = context.relativePath ? context.relativePath + "." + propertyName : propertyName; if (filters.includes(absolutePath) || filters.includes(relativePath) || filters.includes(context.absolutePath) || filters.includes(context.relativePath)) { applyDebug("Include property: %s", propertyName); var index = +propertyName; if (Array.isArray(destination) && !isNaN(index)) { destination.splice(index, 0, propertyValue); } else { destination[propertyName] = propertyValue; } } } } } }