MCPcopy Create free account
hub / github.com/dfinity/orbit / compactArray

Function compactArray

apps/wallet/src/utils/helper.utils.ts:334–358  ·  view source on GitHub ↗
(
  array: (T | null | undefined)[],
  opts: {
    /** If true, also removes empty strings from the array. */
    removeEmptyStrings?: boolean;
    /** if provided, only includes items that are in the set. */
    include?: Set<unknown>;
  } = {},
)

Source from the content-addressed store, hash-verified

332 * @returns A new array with all null and undefined values removed.
333 */
334export const compactArray = <T, R = T>(
335 array: (T | null | undefined)[],
336 opts: {
337 /** If true, also removes empty strings from the array. */
338 removeEmptyStrings?: boolean;
339 /** if provided, only includes items that are in the set. */
340 include?: Set<unknown>;
341 } = {},
342): R[] => {
343 return array.filter(item => {
344 if (item === null || item === undefined) {
345 return false;
346 }
347
348 if (opts.removeEmptyStrings && item === '') {
349 return false;
350 }
351
352 if (opts.include && !opts.include.has(item)) {
353 return false;
354 }
355
356 return true;
357 }) as R[];
358};
359
360/**
361 * Parses a location query object and returns a record with string arrays.

Callers 3

buildFiltersFunction · 0.90
parseLocationQueryFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected