UNPKG

ts-data-forge

Version:

[![npm version](https://img.shields.io/npm/v/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![npm downloads](https://img.shields.io/npm/dm/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![License](https://img.shields.

155 lines (152 loc) 5.79 kB
import { none } from '../../functional/optional/impl/optional-none.mjs'; import { some } from '../../functional/optional/impl/optional-some.mjs'; import { pipe } from '../../functional/pipe.mjs'; import '@sindresorhus/is'; import '../../number/branded-types/finite-number.mjs'; import '../../number/branded-types/int.mjs'; import '../../number/branded-types/int16.mjs'; import '../../number/branded-types/int32.mjs'; import '../../number/branded-types/non-negative-finite-number.mjs'; import '../../number/branded-types/non-negative-int16.mjs'; import '../../number/branded-types/non-negative-int32.mjs'; import '../../number/branded-types/non-zero-finite-number.mjs'; import '../../number/branded-types/non-zero-int.mjs'; import '../../number/branded-types/non-zero-int16.mjs'; import '../../number/branded-types/non-zero-int32.mjs'; import '../../number/branded-types/non-zero-safe-int.mjs'; import '../../number/branded-types/non-zero-uint16.mjs'; import '../../number/branded-types/non-zero-uint32.mjs'; import '../../number/branded-types/positive-finite-number.mjs'; import '../../number/branded-types/positive-int.mjs'; import '../../number/branded-types/positive-int16.mjs'; import '../../number/branded-types/positive-int32.mjs'; import '../../number/branded-types/positive-safe-int.mjs'; import '../../number/branded-types/positive-uint16.mjs'; import '../../number/branded-types/positive-uint32.mjs'; import '../../number/branded-types/safe-int.mjs'; import '../../number/branded-types/safe-uint.mjs'; import '../../number/branded-types/uint.mjs'; import '../../number/branded-types/uint16.mjs'; import { asUint32 } from '../../number/branded-types/uint32.mjs'; import '../../number/enum/int8.mjs'; import '../../number/enum/uint8.mjs'; import '../../number/num.mjs'; import '../../number/refined-number-utils.mjs'; function find(...args) { switch (args.length) { case 2: { const [array, predicate] = args; const foundIndex = array.findIndex( // eslint-disable-next-line total-functions/no-unsafe-type-assertion predicate); return foundIndex === -1 ? none : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion some(array[foundIndex]); } case 1: { const [predicate] = args; return (array) => find(array, predicate); } } } function findLast(...args) { switch (args.length) { case 2: { const [array, predicate] = args; const foundIndex = array.findLastIndex( // eslint-disable-next-line total-functions/no-unsafe-type-assertion predicate); return foundIndex === -1 ? none : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion some(array[foundIndex]); } case 1: { const [predicate] = args; return (array) => findLast(array, predicate); } } } function findIndex(...args) { switch (args.length) { case 2: { const [array, predicate] = args; return pipe(array.findIndex( // eslint-disable-next-line total-functions/no-unsafe-type-assertion predicate)).map((idx) => (idx >= 0 ? asUint32(idx) : -1)).value; } case 1: { const [predicate] = args; return (array) => findIndex(array, predicate); } } } function findLastIndex(...args) { switch (args.length) { case 2: { const [array, predicate] = args; return pipe(array.findLastIndex( // eslint-disable-next-line total-functions/no-unsafe-type-assertion predicate)).map((idx) => (idx >= 0 ? asUint32(idx) : -1)).value; } case 1: { const [predicate] = args; return (array) => findLastIndex(array, predicate); } } } function indexOf(...args) { switch (args.length) { case 2: { const [array, searchElement] = args; const index = array.indexOf(searchElement); return index !== -1 ? asUint32(index) : -1; } case 1: { const [searchElement] = args; return (array) => indexOf(array, searchElement); } } } function indexOfFrom(...args) { switch (args.length) { case 3: { const [array, searchElement, fromIndex] = args; const index = array.indexOf(searchElement, fromIndex); return index !== -1 ? asUint32(index) : -1; } case 2: { const [searchElement, fromIndex] = args; return (array) => indexOfFrom(array, searchElement, fromIndex); } } } function lastIndexOf(...args) { switch (args.length) { case 2: { const [array, searchElement] = args; const index = array.lastIndexOf(searchElement); return index !== -1 ? asUint32(index) : -1; } case 1: { const [searchElement] = args; return (array) => lastIndexOf(array, searchElement); } } } function lastIndexOfFrom(...args) { switch (args.length) { case 3: { const [array, searchElement, fromIndex] = args; const index = array.lastIndexOf(searchElement, fromIndex); return index !== -1 ? asUint32(index) : -1; } case 2: { const [searchElement, fromIndex] = args; return (array) => lastIndexOfFrom(array, searchElement, fromIndex); } } } export { find, findIndex, findLast, findLastIndex, indexOf, indexOfFrom, lastIndexOf, lastIndexOfFrom }; //# sourceMappingURL=array-utils-search.mjs.map