UNPKG

@allmaps/stdlib

Version:

Allmaps Standard Library

70 lines (69 loc) 2.67 kB
/* eslint-disable @typescript-eslint/no-explicit-any */ export function mergeOptions(baseOptions, ...additionalPartialOptions) { // A specific function for merging options: // - Assure the output extends the type of the first element (e.g. default options) // - Speedup: spreading is a little expensive, so it checks if necessary first // (e.g. it was previously executed for every point in transform, // where this was a 50% speed increase when transforming a lot of points // - Allow additionalOptions to be undefined, which simplifies handing over a simple spread return { ...baseOptions, ...mergePartialOptions(...additionalPartialOptions) }; } export function mergePartialOptions(...partialOptions) { const definedPartialOptionsArray = partialOptions.filter((partialOptions) => partialOptions !== undefined && partialOptions !== null); if (definedPartialOptionsArray.length === 0) { return {}; } else if (definedPartialOptionsArray.length === 1) { return definedPartialOptionsArray[0]; } else { // This spreads out eacht of the partialOptions return Object.assign({}, ...definedPartialOptionsArray); } } export function removeUndefinedOptions(...optionsArray) { const mergedOptions = {}; for (const options of optionsArray) { if (!options) { continue; } for (const key in options) { if (Object.prototype.hasOwnProperty.call(options, key)) { const value = options[key]; if (value !== undefined) { ; mergedOptions[key] = value; } } } } return mergedOptions; } // Note: not using Exclude here like we do in removeUndefinedOptions, // since TypeScript often inferes too strict types export function mergeOptionsUnlessUndefined(baseOptions, ...additionalOptions) { return { ...baseOptions, ...removeUndefinedOptions(...additionalOptions) }; } export function optionKeysToUndefinedOptions(optionKeys) { if (optionKeys === undefined) { return undefined; } return optionKeys.reduce((acc, curr) => ((acc[curr] = undefined), acc), {}); } export function optionKeysByMapIdToUndefinedOptionsByMapId(optionKeysByMapId) { if (optionKeysByMapId === undefined) { return undefined; } const optionsByMapId = new Map(); for (const mapId of optionKeysByMapId.keys()) { const optionKeys = optionKeysByMapId.get(mapId); optionsByMapId.set(mapId, optionKeysToUndefinedOptions(optionKeys)); } return optionsByMapId; }