UNPKG

json-schema-describes-subset

Version:

Tools for static JSON schema analysis, including functions to determine if one schema describes a subset of another or if a schema describes the empty set or to convert a schema to its disjunctive normal form (DNF).

51 lines 1.81 kB
import findIndex from 'lodash/findIndex.js'; export function createSameElementsArray(length, elementValue) { return Array.from({ length }).map(() => elementValue); } export function splitArray(array, predicate, options = {}) { let currentGroup = []; const result = [currentGroup]; for (const element of array) { if (predicate(element)) { if (options.includeSplitterElement === 'suffix') { currentGroup.push(element); } currentGroup = []; if (options.includeSplitterElement === 'prefix') { currentGroup.push(element); } result.push(currentGroup); } else { currentGroup.push(element); } } return result; } export function getElementsBetween(array, startPredicate, endPredicate) { const startIndex = findIndex(array, startPredicate) + 1; if (startIndex === 0) { return []; } const endIndex = findIndex(array, endPredicate, startIndex + 1); if (endIndex < 0) { return [array.slice(startIndex)]; } return [ array.slice(startIndex, endIndex), ...getElementsBetween(array.slice(endIndex + 1), startPredicate, endPredicate), ]; } export function forEachInRange(array, { start = 0, end = array.length, step = 1, }, callbackFn) { for (let i = Math.max(0, start); i < Math.min(array.length, end); i += step) { callbackFn(array[i], i, array); } } export function forEachElementCombination(array, callbackFn) { forEachInRange(array, { start: 1 }, (elementA, indexA) => { forEachInRange(array, { end: indexA }, (elementB, indexB) => { callbackFn(elementA, elementB, indexA, indexB, array); }); }); } //# sourceMappingURL=array.js.map