@sapphire/iterator-utilities 1.0.3-next.fc35dd86 → 2.0.0-next.1e7d2dec
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +217 -0
- package/dist/cjs/index.d.cts +32 -9
- package/dist/cjs/lib/all.cjs +12 -0
- package/dist/cjs/lib/all.cjs.map +1 -0
- package/dist/cjs/lib/all.d.cts +2 -0
- package/dist/cjs/lib/any.cjs +12 -0
- package/dist/cjs/lib/any.cjs.map +1 -0
- package/dist/cjs/lib/any.d.cts +2 -0
- package/dist/cjs/lib/collect.cjs +12 -0
- package/dist/cjs/lib/collect.cjs.map +1 -0
- package/dist/cjs/lib/collect.d.cts +2 -0
- package/dist/cjs/lib/collectInto.cjs +17 -0
- package/dist/cjs/lib/collectInto.cjs.map +1 -0
- package/dist/cjs/lib/collectInto.d.cts +29 -0
- package/dist/cjs/lib/compare.cjs +15 -0
- package/dist/cjs/lib/compare.cjs.map +1 -0
- package/dist/cjs/lib/compare.d.cts +32 -0
- package/dist/cjs/lib/compareBy.cjs +26 -0
- package/dist/cjs/lib/compareBy.cjs.map +1 -0
- package/dist/cjs/lib/compareBy.d.cts +31 -0
- package/dist/cjs/lib/drop.cjs +0 -1
- package/dist/cjs/lib/drop.cjs.map +1 -1
- package/dist/cjs/lib/drop.d.cts +1 -1
- package/dist/cjs/lib/dropLast.cjs +0 -1
- package/dist/cjs/lib/dropLast.cjs.map +1 -1
- package/dist/cjs/lib/dropLast.d.cts +1 -1
- package/dist/cjs/lib/dropWhile.cjs +0 -1
- package/dist/cjs/lib/dropWhile.cjs.map +1 -1
- package/dist/cjs/lib/dropWhile.d.cts +1 -1
- package/dist/cjs/lib/equal.cjs +15 -0
- package/dist/cjs/lib/equal.cjs.map +1 -0
- package/dist/cjs/lib/equal.d.cts +26 -0
- package/dist/cjs/lib/equalBy.cjs +23 -0
- package/dist/cjs/lib/equalBy.cjs.map +1 -0
- package/dist/cjs/lib/equalBy.d.cts +27 -0
- package/dist/cjs/lib/filter.cjs +0 -1
- package/dist/cjs/lib/filter.cjs.map +1 -1
- package/dist/cjs/lib/filter.d.cts +1 -1
- package/dist/cjs/lib/fuse.cjs +26 -0
- package/dist/cjs/lib/fuse.cjs.map +1 -0
- package/dist/cjs/lib/fuse.d.cts +47 -0
- package/dist/cjs/lib/greaterOrEqualThan.cjs +17 -0
- package/dist/cjs/lib/greaterOrEqualThan.cjs.map +1 -0
- package/dist/cjs/lib/greaterOrEqualThan.d.cts +32 -0
- package/dist/cjs/lib/greaterThan.cjs +17 -0
- package/dist/cjs/lib/greaterThan.cjs.map +1 -0
- package/dist/cjs/lib/greaterThan.d.cts +32 -0
- package/dist/cjs/lib/indexOf.cjs +0 -1
- package/dist/cjs/lib/indexOf.cjs.map +1 -1
- package/dist/cjs/lib/indexOf.d.cts +1 -1
- package/dist/cjs/lib/inspect.cjs +25 -0
- package/dist/cjs/lib/inspect.cjs.map +1 -0
- package/dist/cjs/lib/inspect.d.cts +39 -0
- package/dist/cjs/lib/intersperse.cjs +35 -0
- package/dist/cjs/lib/intersperse.cjs.map +1 -0
- package/dist/cjs/lib/intersperse.d.cts +30 -0
- package/dist/cjs/lib/isSorted.cjs +15 -0
- package/dist/cjs/lib/isSorted.cjs.map +1 -0
- package/dist/cjs/lib/isSorted.d.cts +35 -0
- package/dist/cjs/lib/isSortedBy.cjs +26 -0
- package/dist/cjs/lib/isSortedBy.cjs.map +1 -0
- package/dist/cjs/lib/isSortedBy.d.cts +32 -0
- package/dist/cjs/lib/isSortedByKey.cjs +29 -0
- package/dist/cjs/lib/isSortedByKey.cjs.map +1 -0
- package/dist/cjs/lib/isSortedByKey.d.cts +30 -0
- package/dist/cjs/lib/lessOrEqualThan.cjs +17 -0
- package/dist/cjs/lib/lessOrEqualThan.cjs.map +1 -0
- package/dist/cjs/lib/lessOrEqualThan.d.cts +32 -0
- package/dist/cjs/lib/lessThan.cjs +17 -0
- package/dist/cjs/lib/lessThan.cjs.map +1 -0
- package/dist/cjs/lib/lessThan.d.cts +32 -0
- package/dist/cjs/lib/max.cjs +3 -7
- package/dist/cjs/lib/max.cjs.map +1 -1
- package/dist/cjs/lib/max.d.cts +8 -3
- package/dist/cjs/lib/maxBy.cjs +27 -0
- package/dist/cjs/lib/maxBy.cjs.map +1 -0
- package/dist/cjs/lib/maxBy.d.cts +31 -0
- package/dist/cjs/lib/maxByKey.cjs +32 -0
- package/dist/cjs/lib/maxByKey.cjs.map +1 -0
- package/dist/cjs/lib/maxByKey.d.cts +32 -0
- package/dist/cjs/lib/min.cjs +3 -7
- package/dist/cjs/lib/min.cjs.map +1 -1
- package/dist/cjs/lib/min.d.cts +8 -3
- package/dist/cjs/lib/minBy.cjs +27 -0
- package/dist/cjs/lib/minBy.cjs.map +1 -0
- package/dist/cjs/lib/minBy.d.cts +31 -0
- package/dist/cjs/lib/minByKey.cjs +32 -0
- package/dist/cjs/lib/minByKey.cjs.map +1 -0
- package/dist/cjs/lib/minByKey.d.cts +31 -0
- package/dist/cjs/lib/notEqual.cjs +15 -0
- package/dist/cjs/lib/notEqual.cjs.map +1 -0
- package/dist/cjs/lib/notEqual.d.cts +26 -0
- package/dist/cjs/lib/nth.cjs +12 -0
- package/dist/cjs/lib/nth.cjs.map +1 -0
- package/dist/cjs/lib/nth.d.cts +2 -0
- package/dist/cjs/lib/position.cjs +12 -0
- package/dist/cjs/lib/position.cjs.map +1 -0
- package/dist/cjs/lib/position.d.cts +2 -0
- package/dist/cjs/lib/reverse.cjs +2 -5
- package/dist/cjs/lib/reverse.cjs.map +1 -1
- package/dist/cjs/lib/shared/_compare.cjs +36 -0
- package/dist/cjs/lib/shared/_compare.cjs.map +1 -0
- package/dist/cjs/lib/shared/_compare.d.cts +20 -0
- package/dist/cjs/lib/shared/comparators.cjs +28 -0
- package/dist/cjs/lib/shared/comparators.cjs.map +1 -0
- package/dist/cjs/lib/shared/comparators.d.cts +34 -0
- package/dist/cjs/lib/skip.cjs +12 -0
- package/dist/cjs/lib/skip.cjs.map +1 -0
- package/dist/cjs/lib/skip.d.cts +2 -0
- package/dist/cjs/lib/skipLast.cjs +12 -0
- package/dist/cjs/lib/skipLast.cjs.map +1 -0
- package/dist/cjs/lib/skipLast.d.cts +2 -0
- package/dist/cjs/lib/skipWhile.cjs +12 -0
- package/dist/cjs/lib/skipWhile.cjs.map +1 -0
- package/dist/cjs/lib/skipWhile.d.cts +2 -0
- package/dist/cjs/lib/stepBy.cjs +29 -0
- package/dist/cjs/lib/stepBy.cjs.map +1 -0
- package/dist/cjs/lib/stepBy.d.cts +24 -0
- package/dist/cjs/lib/takeWhile.cjs +12 -0
- package/dist/cjs/lib/takeWhile.cjs.map +1 -0
- package/dist/cjs/lib/takeWhile.d.cts +2 -0
- package/dist/esm/index.d.mts +32 -9
- package/dist/esm/index.mjs +31 -0
- package/dist/esm/lib/all.d.mts +2 -0
- package/dist/esm/lib/all.mjs +4 -0
- package/dist/esm/lib/all.mjs.map +1 -0
- package/dist/esm/lib/any.d.mts +2 -0
- package/dist/esm/lib/any.mjs +4 -0
- package/dist/esm/lib/any.mjs.map +1 -0
- package/dist/esm/lib/collect.d.mts +2 -0
- package/dist/esm/lib/collect.mjs +4 -0
- package/dist/esm/lib/collect.mjs.map +1 -0
- package/dist/esm/lib/collectInto.d.mts +29 -0
- package/dist/esm/lib/collectInto.mjs +14 -0
- package/dist/esm/lib/collectInto.mjs.map +1 -0
- package/dist/esm/lib/compare.d.mts +32 -0
- package/dist/esm/lib/compare.mjs +12 -0
- package/dist/esm/lib/compare.mjs.map +1 -0
- package/dist/esm/lib/compareBy.d.mts +31 -0
- package/dist/esm/lib/compareBy.mjs +23 -0
- package/dist/esm/lib/compareBy.mjs.map +1 -0
- package/dist/esm/lib/drop.d.mts +1 -1
- package/dist/esm/lib/drop.mjs +1 -1
- package/dist/esm/lib/drop.mjs.map +1 -1
- package/dist/esm/lib/dropLast.d.mts +1 -1
- package/dist/esm/lib/dropLast.mjs +1 -1
- package/dist/esm/lib/dropLast.mjs.map +1 -1
- package/dist/esm/lib/dropWhile.d.mts +1 -1
- package/dist/esm/lib/dropWhile.mjs +1 -1
- package/dist/esm/lib/dropWhile.mjs.map +1 -1
- package/dist/esm/lib/equal.d.mts +26 -0
- package/dist/esm/lib/equal.mjs +11 -0
- package/dist/esm/lib/equal.mjs.map +1 -0
- package/dist/esm/lib/equalBy.d.mts +27 -0
- package/dist/esm/lib/equalBy.mjs +19 -0
- package/dist/esm/lib/equalBy.mjs.map +1 -0
- package/dist/esm/lib/filter.d.mts +1 -1
- package/dist/esm/lib/filter.mjs +1 -1
- package/dist/esm/lib/filter.mjs.map +1 -1
- package/dist/esm/lib/fuse.d.mts +47 -0
- package/dist/esm/lib/fuse.mjs +23 -0
- package/dist/esm/lib/fuse.mjs.map +1 -0
- package/dist/esm/lib/greaterOrEqualThan.d.mts +32 -0
- package/dist/esm/lib/greaterOrEqualThan.mjs +13 -0
- package/dist/esm/lib/greaterOrEqualThan.mjs.map +1 -0
- package/dist/esm/lib/greaterThan.d.mts +32 -0
- package/dist/esm/lib/greaterThan.mjs +13 -0
- package/dist/esm/lib/greaterThan.mjs.map +1 -0
- package/dist/esm/lib/indexOf.d.mts +1 -1
- package/dist/esm/lib/indexOf.mjs +1 -1
- package/dist/esm/lib/indexOf.mjs.map +1 -1
- package/dist/esm/lib/inspect.d.mts +39 -0
- package/dist/esm/lib/inspect.mjs +22 -0
- package/dist/esm/lib/inspect.mjs.map +1 -0
- package/dist/esm/lib/intersperse.d.mts +30 -0
- package/dist/esm/lib/intersperse.mjs +32 -0
- package/dist/esm/lib/intersperse.mjs.map +1 -0
- package/dist/esm/lib/isSorted.d.mts +35 -0
- package/dist/esm/lib/isSorted.mjs +12 -0
- package/dist/esm/lib/isSorted.mjs.map +1 -0
- package/dist/esm/lib/isSortedBy.d.mts +32 -0
- package/dist/esm/lib/isSortedBy.mjs +23 -0
- package/dist/esm/lib/isSortedBy.mjs.map +1 -0
- package/dist/esm/lib/isSortedByKey.d.mts +30 -0
- package/dist/esm/lib/isSortedByKey.mjs +26 -0
- package/dist/esm/lib/isSortedByKey.mjs.map +1 -0
- package/dist/esm/lib/lessOrEqualThan.d.mts +32 -0
- package/dist/esm/lib/lessOrEqualThan.mjs +13 -0
- package/dist/esm/lib/lessOrEqualThan.mjs.map +1 -0
- package/dist/esm/lib/lessThan.d.mts +32 -0
- package/dist/esm/lib/lessThan.mjs +13 -0
- package/dist/esm/lib/lessThan.mjs.map +1 -0
- package/dist/esm/lib/max.d.mts +8 -3
- package/dist/esm/lib/max.mjs +3 -7
- package/dist/esm/lib/max.mjs.map +1 -1
- package/dist/esm/lib/maxBy.d.mts +31 -0
- package/dist/esm/lib/maxBy.mjs +24 -0
- package/dist/esm/lib/maxBy.mjs.map +1 -0
- package/dist/esm/lib/maxByKey.d.mts +32 -0
- package/dist/esm/lib/maxByKey.mjs +29 -0
- package/dist/esm/lib/maxByKey.mjs.map +1 -0
- package/dist/esm/lib/min.d.mts +8 -3
- package/dist/esm/lib/min.mjs +3 -7
- package/dist/esm/lib/min.mjs.map +1 -1
- package/dist/esm/lib/minBy.d.mts +31 -0
- package/dist/esm/lib/minBy.mjs +24 -0
- package/dist/esm/lib/minBy.mjs.map +1 -0
- package/dist/esm/lib/minByKey.d.mts +31 -0
- package/dist/esm/lib/minByKey.mjs +29 -0
- package/dist/esm/lib/minByKey.mjs.map +1 -0
- package/dist/esm/lib/notEqual.d.mts +26 -0
- package/dist/esm/lib/notEqual.mjs +11 -0
- package/dist/esm/lib/notEqual.mjs.map +1 -0
- package/dist/esm/lib/nth.d.mts +2 -0
- package/dist/esm/lib/nth.mjs +4 -0
- package/dist/esm/lib/nth.mjs.map +1 -0
- package/dist/esm/lib/position.d.mts +2 -0
- package/dist/esm/lib/position.mjs +4 -0
- package/dist/esm/lib/position.mjs.map +1 -0
- package/dist/esm/lib/reverse.mjs +2 -5
- package/dist/esm/lib/reverse.mjs.map +1 -1
- package/dist/esm/lib/shared/_compare.d.mts +20 -0
- package/dist/esm/lib/shared/_compare.mjs +30 -0
- package/dist/esm/lib/shared/_compare.mjs.map +1 -0
- package/dist/esm/lib/shared/comparators.d.mts +34 -0
- package/dist/esm/lib/shared/comparators.mjs +21 -0
- package/dist/esm/lib/shared/comparators.mjs.map +1 -0
- package/dist/esm/lib/skip.d.mts +2 -0
- package/dist/esm/lib/skip.mjs +4 -0
- package/dist/esm/lib/skip.mjs.map +1 -0
- package/dist/esm/lib/skipLast.d.mts +2 -0
- package/dist/esm/lib/skipLast.mjs +4 -0
- package/dist/esm/lib/skipLast.mjs.map +1 -0
- package/dist/esm/lib/skipWhile.d.mts +2 -0
- package/dist/esm/lib/skipWhile.mjs +4 -0
- package/dist/esm/lib/skipWhile.mjs.map +1 -0
- package/dist/esm/lib/stepBy.d.mts +24 -0
- package/dist/esm/lib/stepBy.mjs +26 -0
- package/dist/esm/lib/stepBy.mjs.map +1 -0
- package/dist/esm/lib/takeWhile.d.mts +2 -0
- package/dist/esm/lib/takeWhile.mjs +4 -0
- package/dist/esm/lib/takeWhile.mjs.map +1 -0
- package/dist/iife/index.global.js +391 -49
- package/dist/iife/index.global.js.map +1 -1
- package/package.json +292 -22
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { __name } from '../chunk-PAWJFY3S.mjs';
|
|
2
|
+
import { assertFunction } from './shared/_assertFunction.mjs';
|
|
3
|
+
import { compareIteratorElements, orderingIsLess } from './shared/_compare.mjs';
|
|
4
|
+
import { toIterableIterator } from './toIterableIterator.mjs';
|
|
5
|
+
|
|
6
|
+
function maxBy(iterable, comparator) {
|
|
7
|
+
comparator = assertFunction(comparator);
|
|
8
|
+
const iterator = toIterableIterator(iterable);
|
|
9
|
+
const first = iterator.next();
|
|
10
|
+
if (first.done) return null;
|
|
11
|
+
let maximum = first.value;
|
|
12
|
+
for (const value of iterator) {
|
|
13
|
+
const comparison = compareIteratorElements(value, maximum, comparator);
|
|
14
|
+
if (!orderingIsLess(comparison)) {
|
|
15
|
+
maximum = value;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return maximum;
|
|
19
|
+
}
|
|
20
|
+
__name(maxBy, "maxBy");
|
|
21
|
+
|
|
22
|
+
export { maxBy };
|
|
23
|
+
//# sourceMappingURL=maxBy.mjs.map
|
|
24
|
+
//# sourceMappingURL=maxBy.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/maxBy.ts"],"names":[],"mappings":";;;;;AAgCO,SAAS,KAAA,CACf,UACA,UACqB,EAAA;AACrB,EAAA,UAAA,GAAa,eAAe,UAAU,CAAA,CAAA;AAEtC,EAAM,MAAA,QAAA,GAAW,mBAAmB,QAAQ,CAAA,CAAA;AAC5C,EAAM,MAAA,KAAA,GAAQ,SAAS,IAAK,EAAA,CAAA;AAC5B,EAAI,IAAA,KAAA,CAAM,MAAa,OAAA,IAAA,CAAA;AAEvB,EAAA,IAAI,UAAU,KAAM,CAAA,KAAA,CAAA;AACpB,EAAA,KAAA,MAAW,SAAS,QAAU,EAAA;AAC7B,IAAA,MAAM,UAAa,GAAA,uBAAA,CAAqC,KAAO,EAAA,OAAA,EAAS,UAAU,CAAA,CAAA;AAClF,IAAI,IAAA,CAAC,cAAe,CAAA,UAAU,CAAG,EAAA;AAChC,MAAU,OAAA,GAAA,KAAA,CAAA;AAAA,KACX;AAAA,GACD;AAEA,EAAO,OAAA,OAAA,CAAA;AACR,CAAA;AAnBgB,MAAA,CAAA,KAAA,EAAA,OAAA,CAAA","file":"maxBy.mjs","sourcesContent":["import type { IterableResolvable } from './from';\nimport type { max } from './max';\nimport type { maxByKey } from './maxByKey';\nimport { assertFunction } from './shared/_assertFunction';\nimport { compareIteratorElements, orderingIsLess, type CompareByComparator } from './shared/_compare';\nimport { toIterableIterator } from './toIterableIterator';\n\n/**\n * Returns the element that gives the maximum value with respect to the specified comparison function.\n *\n * If several elements are equally maximum, the last element is returned. If the iterator is empty, `null` is returned.\n *\n * @seealso {@link max} for a version that uses the default comparator.\n * @seealso {@link maxByKey} for a version that allows custom key extractors.\n *\n * @param iterable An iterator of number values to determine the maximum value of.\n * @param comparator A function to execute for each element produced by the iterator. It should return a number value.\n * @returns The element that gives the maximum value from the specified function, or `null` if the iterator is empty.\n *\n * @example\n * ```typescript\n * import { ascNumber, maxBy } from '@sapphire/iterator-utilities';\n *\n * const iterable = [-3, 0, 1, 5, -10];\n * console.log(maxBy(iterable, ascNumber));\n * // Output: 5\n * ```\n *\n * @remarks\n *\n * This function consumes the entire iterator.\n */\nexport function maxBy<const ElementType>(\n\titerable: IterableResolvable<ElementType>,\n\tcomparator: CompareByComparator<ElementType>\n): ElementType | null {\n\tcomparator = assertFunction(comparator);\n\n\tconst iterator = toIterableIterator(iterable);\n\tconst first = iterator.next();\n\tif (first.done) return null;\n\n\tlet maximum = first.value;\n\tfor (const value of iterator) {\n\t\tconst comparison = compareIteratorElements<ElementType>(value, maximum, comparator);\n\t\tif (!orderingIsLess(comparison)) {\n\t\t\tmaximum = value;\n\t\t}\n\t}\n\n\treturn maximum;\n}\n"]}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { IterableResolvable } from './from.mjs';
|
|
2
|
+
import { CompareByComparator } from './shared/_compare.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Returns the element that gives the maximum value from the specified function.
|
|
6
|
+
*
|
|
7
|
+
* If several elements are equally maximum, the last element is returned. If the iterator is empty, `null` is returned.
|
|
8
|
+
*
|
|
9
|
+
* @seealso {@link max} for a version that uses the default comparator.
|
|
10
|
+
* @seealso {@link maxBy} for a version that allows custom comparators.
|
|
11
|
+
*
|
|
12
|
+
* @param iterable An iterator of number values to determine the maximum value of.
|
|
13
|
+
* @param callbackFn A function to execute for each element produced by the iterator, producing a key to compare with.
|
|
14
|
+
* @param comparator A function to execute for each element produced by the iterator. It should return a number value.
|
|
15
|
+
* @returns The element that gives the maximum value from the specified function, or `null` if the iterator is empty.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { maxByKey } from '@sapphire/iterator-utilities';
|
|
20
|
+
*
|
|
21
|
+
* const iterable = [-3, 0, 1, 5, -10];
|
|
22
|
+
* console.log(maxByKey(iterable, (value) => Math.abs(value)));
|
|
23
|
+
* // Output: -10
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @remarks
|
|
27
|
+
*
|
|
28
|
+
* This function consumes the entire iterator.
|
|
29
|
+
*/
|
|
30
|
+
declare function maxByKey<const ElementType, const MappedType>(iterable: IterableResolvable<ElementType>, callbackFn: (element: ElementType, index: number) => MappedType, comparator?: CompareByComparator<MappedType>): ElementType | null;
|
|
31
|
+
|
|
32
|
+
export { maxByKey };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { __name } from '../chunk-PAWJFY3S.mjs';
|
|
2
|
+
import { assertFunction } from './shared/_assertFunction.mjs';
|
|
3
|
+
import { compareIteratorElements, orderingIsGreater } from './shared/_compare.mjs';
|
|
4
|
+
import { defaultCompare } from './shared/comparators.mjs';
|
|
5
|
+
import { toIterableIterator } from './toIterableIterator.mjs';
|
|
6
|
+
|
|
7
|
+
function maxByKey(iterable, callbackFn, comparator = defaultCompare) {
|
|
8
|
+
callbackFn = assertFunction(callbackFn);
|
|
9
|
+
const iterator = toIterableIterator(iterable);
|
|
10
|
+
const first = iterator.next();
|
|
11
|
+
if (first.done) return null;
|
|
12
|
+
let maximum = first.value;
|
|
13
|
+
let maximumKey = callbackFn(maximum, 0);
|
|
14
|
+
let index = 1;
|
|
15
|
+
for (const value of iterator) {
|
|
16
|
+
const key = callbackFn(value, index++);
|
|
17
|
+
const comparison = compareIteratorElements(maximumKey, key, comparator);
|
|
18
|
+
if (!orderingIsGreater(comparison)) {
|
|
19
|
+
maximum = value;
|
|
20
|
+
maximumKey = key;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return maximum;
|
|
24
|
+
}
|
|
25
|
+
__name(maxByKey, "maxByKey");
|
|
26
|
+
|
|
27
|
+
export { maxByKey };
|
|
28
|
+
//# sourceMappingURL=maxByKey.mjs.map
|
|
29
|
+
//# sourceMappingURL=maxByKey.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/maxByKey.ts"],"names":[],"mappings":";;;;;;AAkCO,SAAS,QACf,CAAA,QAAA,EACA,UACA,EAAA,UAAA,GAA8C,cACzB,EAAA;AACrB,EAAA,UAAA,GAAa,eAAe,UAAU,CAAA,CAAA;AAEtC,EAAM,MAAA,QAAA,GAAW,mBAAmB,QAAQ,CAAA,CAAA;AAC5C,EAAM,MAAA,KAAA,GAAQ,SAAS,IAAK,EAAA,CAAA;AAC5B,EAAI,IAAA,KAAA,CAAM,MAAa,OAAA,IAAA,CAAA;AAEvB,EAAA,IAAI,UAAU,KAAM,CAAA,KAAA,CAAA;AACpB,EAAI,IAAA,UAAA,GAAa,UAAW,CAAA,OAAA,EAAS,CAAC,CAAA,CAAA;AACtC,EAAA,IAAI,KAAQ,GAAA,CAAA,CAAA;AACZ,EAAA,KAAA,MAAW,SAAS,QAAU,EAAA;AAC7B,IAAM,MAAA,GAAA,GAAM,UAAW,CAAA,KAAA,EAAO,KAAO,EAAA,CAAA,CAAA;AACrC,IAAA,MAAM,UAAa,GAAA,uBAAA,CAAoC,UAAY,EAAA,GAAA,EAAK,UAAU,CAAA,CAAA;AAElF,IAAI,IAAA,CAAC,iBAAkB,CAAA,UAAU,CAAG,EAAA;AACnC,MAAU,OAAA,GAAA,KAAA,CAAA;AACV,MAAa,UAAA,GAAA,GAAA,CAAA;AAAA,KACd;AAAA,GACD;AAEA,EAAO,OAAA,OAAA,CAAA;AACR,CAAA;AAzBgB,MAAA,CAAA,QAAA,EAAA,UAAA,CAAA","file":"maxByKey.mjs","sourcesContent":["import type { IterableResolvable } from './from';\nimport type { max } from './max';\nimport type { maxBy } from './maxBy';\nimport { assertFunction } from './shared/_assertFunction';\nimport { compareIteratorElements, orderingIsGreater, type CompareByComparator } from './shared/_compare';\nimport { defaultCompare } from './shared/comparators';\nimport { toIterableIterator } from './toIterableIterator';\n\n/**\n * Returns the element that gives the maximum value from the specified function.\n *\n * If several elements are equally maximum, the last element is returned. If the iterator is empty, `null` is returned.\n *\n * @seealso {@link max} for a version that uses the default comparator.\n * @seealso {@link maxBy} for a version that allows custom comparators.\n *\n * @param iterable An iterator of number values to determine the maximum value of.\n * @param callbackFn A function to execute for each element produced by the iterator, producing a key to compare with.\n * @param comparator A function to execute for each element produced by the iterator. It should return a number value.\n * @returns The element that gives the maximum value from the specified function, or `null` if the iterator is empty.\n *\n * @example\n * ```typescript\n * import { maxByKey } from '@sapphire/iterator-utilities';\n *\n * const iterable = [-3, 0, 1, 5, -10];\n * console.log(maxByKey(iterable, (value) => Math.abs(value)));\n * // Output: -10\n * ```\n *\n * @remarks\n *\n * This function consumes the entire iterator.\n */\nexport function maxByKey<const ElementType, const MappedType>(\n\titerable: IterableResolvable<ElementType>,\n\tcallbackFn: (element: ElementType, index: number) => MappedType,\n\tcomparator: CompareByComparator<MappedType> = defaultCompare\n): ElementType | null {\n\tcallbackFn = assertFunction(callbackFn);\n\n\tconst iterator = toIterableIterator(iterable);\n\tconst first = iterator.next();\n\tif (first.done) return null;\n\n\tlet maximum = first.value;\n\tlet maximumKey = callbackFn(maximum, 0);\n\tlet index = 1;\n\tfor (const value of iterator) {\n\t\tconst key = callbackFn(value, index++);\n\t\tconst comparison = compareIteratorElements<MappedType>(maximumKey, key, comparator);\n\n\t\tif (!orderingIsGreater(comparison)) {\n\t\t\tmaximum = value;\n\t\t\tmaximumKey = key;\n\t\t}\n\t}\n\n\treturn maximum;\n}\n"]}
|
package/dist/esm/lib/min.d.mts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import { IterableResolvable } from './from.mjs';
|
|
2
|
-
import { NumberResolvable } from './shared/_toNumberOrThrow.mjs';
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
|
-
* Consumes the iterable and returns the lowest number element. If the iterable is empty,
|
|
4
|
+
* Consumes the iterable and returns the lowest number element. If the iterable is empty, it returns `null`.
|
|
5
|
+
*
|
|
6
|
+
* This function uses the default comparator (lexicographically), which means it will compare the elements as strings.
|
|
7
|
+
* If this is undesirable, use {@link minBy} instead.
|
|
8
|
+
*
|
|
9
|
+
* @seealso {@link minBy} for a version that allows custom comparators.
|
|
10
|
+
* @seealso {@link minByKey} for a version that allows custom key extractors.
|
|
6
11
|
*
|
|
7
12
|
* @param iterable An iterator of number values to determine the minimum value of.
|
|
8
13
|
* @returns The minimum value in the input iterator, or `null` if the iterator is empty or contains only non-number values.
|
|
@@ -20,6 +25,6 @@ import { NumberResolvable } from './shared/_toNumberOrThrow.mjs';
|
|
|
20
25
|
*
|
|
21
26
|
* This function consumes the entire iterator.
|
|
22
27
|
*/
|
|
23
|
-
declare function min(iterable: IterableResolvable<
|
|
28
|
+
declare function min<const ElementType>(iterable: IterableResolvable<ElementType>): ElementType | null;
|
|
24
29
|
|
|
25
30
|
export { min };
|
package/dist/esm/lib/min.mjs
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
import { __name } from '../chunk-PAWJFY3S.mjs';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { minBy } from './minBy.mjs';
|
|
3
|
+
import { defaultCompare } from './shared/comparators.mjs';
|
|
4
4
|
|
|
5
5
|
function min(iterable) {
|
|
6
|
-
|
|
7
|
-
for (const value of map(iterable, toNumberOrThrow)) {
|
|
8
|
-
if (min2 === null || value < min2) min2 = value;
|
|
9
|
-
}
|
|
10
|
-
return min2;
|
|
6
|
+
return minBy(iterable, defaultCompare);
|
|
11
7
|
}
|
|
12
8
|
__name(min, "min");
|
|
13
9
|
|
package/dist/esm/lib/min.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/lib/min.ts"],"names":[
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/min.ts"],"names":[],"mappings":";;;;AA8BO,SAAS,IAAuB,QAA+D,EAAA;AACrG,EAAO,OAAA,KAAA,CAAM,UAAU,cAAc,CAAA,CAAA;AACtC,CAAA;AAFgB,MAAA,CAAA,GAAA,EAAA,KAAA,CAAA","file":"min.mjs","sourcesContent":["import type { IterableResolvable } from './from';\nimport { minBy } from './minBy';\nimport type { minByKey } from './minByKey';\nimport { defaultCompare } from './shared/comparators';\n\n/**\n * Consumes the iterable and returns the lowest number element. If the iterable is empty, it returns `null`.\n *\n * This function uses the default comparator (lexicographically), which means it will compare the elements as strings.\n * If this is undesirable, use {@link minBy} instead.\n *\n * @seealso {@link minBy} for a version that allows custom comparators.\n * @seealso {@link minByKey} for a version that allows custom key extractors.\n *\n * @param iterable An iterator of number values to determine the minimum value of.\n * @returns The minimum value in the input iterator, or `null` if the iterator is empty or contains only non-number values.\n *\n * @example\n * ```typescript\n * import { min } from '@sapphire/iterator-utilities';\n *\n * const iterable = [1, 2, 3, 4, 5];\n * console.log(min(iterable));\n * // Output: 1\n * ```\n *\n * @remarks\n *\n * This function consumes the entire iterator.\n */\nexport function min<const ElementType>(iterable: IterableResolvable<ElementType>): ElementType | null {\n\treturn minBy(iterable, defaultCompare);\n}\n"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { IterableResolvable } from './from.mjs';
|
|
2
|
+
import { CompareByComparator } from './shared/_compare.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Returns the element that gives the minimum value with respect to the specified comparison function.
|
|
6
|
+
*
|
|
7
|
+
* If several elements are equally minimum, the last element is returned. If the iterator is empty, `null` is returned.
|
|
8
|
+
*
|
|
9
|
+
* @seealso {@link min} for a version that uses the default comparator.
|
|
10
|
+
* @seealso {@link minByKey} for a version that allows custom key extractors.
|
|
11
|
+
*
|
|
12
|
+
* @param iterable An iterator of number values to determine the minimum value of.
|
|
13
|
+
* @param callbackFn A function to execute for each element produced by the iterator. It should return a number value.
|
|
14
|
+
* @returns The element that gives the minimum value from the specified function, or `null` if the iterator is empty.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { ascNumber, minBy } from '@sapphire/iterator-utilities';
|
|
19
|
+
*
|
|
20
|
+
* const iterable = [-3, 0, 1, 5, -10];
|
|
21
|
+
* console.log(minBy(iterable, ascNumber));
|
|
22
|
+
* // Output: -10
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
*
|
|
27
|
+
* This function consumes the entire iterator.
|
|
28
|
+
*/
|
|
29
|
+
declare function minBy<const ElementType>(iterable: IterableResolvable<ElementType>, comparator: CompareByComparator<ElementType>): ElementType | null;
|
|
30
|
+
|
|
31
|
+
export { minBy };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { __name } from '../chunk-PAWJFY3S.mjs';
|
|
2
|
+
import { assertFunction } from './shared/_assertFunction.mjs';
|
|
3
|
+
import { compareIteratorElements, orderingIsGreater } from './shared/_compare.mjs';
|
|
4
|
+
import { toIterableIterator } from './toIterableIterator.mjs';
|
|
5
|
+
|
|
6
|
+
function minBy(iterable, comparator) {
|
|
7
|
+
comparator = assertFunction(comparator);
|
|
8
|
+
const iterator = toIterableIterator(iterable);
|
|
9
|
+
const first = iterator.next();
|
|
10
|
+
if (first.done) return null;
|
|
11
|
+
let minimum = first.value;
|
|
12
|
+
for (const value of iterator) {
|
|
13
|
+
const comparison = compareIteratorElements(value, minimum, comparator);
|
|
14
|
+
if (!orderingIsGreater(comparison)) {
|
|
15
|
+
minimum = value;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return minimum;
|
|
19
|
+
}
|
|
20
|
+
__name(minBy, "minBy");
|
|
21
|
+
|
|
22
|
+
export { minBy };
|
|
23
|
+
//# sourceMappingURL=minBy.mjs.map
|
|
24
|
+
//# sourceMappingURL=minBy.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/minBy.ts"],"names":[],"mappings":";;;;;AAgCO,SAAS,KAAA,CACf,UACA,UACqB,EAAA;AACrB,EAAA,UAAA,GAAa,eAAe,UAAU,CAAA,CAAA;AAEtC,EAAM,MAAA,QAAA,GAAW,mBAAmB,QAAQ,CAAA,CAAA;AAC5C,EAAM,MAAA,KAAA,GAAQ,SAAS,IAAK,EAAA,CAAA;AAC5B,EAAI,IAAA,KAAA,CAAM,MAAa,OAAA,IAAA,CAAA;AAEvB,EAAA,IAAI,UAAU,KAAM,CAAA,KAAA,CAAA;AACpB,EAAA,KAAA,MAAW,SAAS,QAAU,EAAA;AAC7B,IAAA,MAAM,UAAa,GAAA,uBAAA,CAAqC,KAAO,EAAA,OAAA,EAAS,UAAU,CAAA,CAAA;AAElF,IAAI,IAAA,CAAC,iBAAkB,CAAA,UAAU,CAAG,EAAA;AACnC,MAAU,OAAA,GAAA,KAAA,CAAA;AAAA,KACX;AAAA,GACD;AAEA,EAAO,OAAA,OAAA,CAAA;AACR,CAAA;AApBgB,MAAA,CAAA,KAAA,EAAA,OAAA,CAAA","file":"minBy.mjs","sourcesContent":["import type { IterableResolvable } from './from';\nimport type { min } from './min';\nimport type { minByKey } from './minByKey';\nimport { assertFunction } from './shared/_assertFunction';\nimport { compareIteratorElements, orderingIsGreater, type CompareByComparator } from './shared/_compare';\nimport { toIterableIterator } from './toIterableIterator';\n\n/**\n * Returns the element that gives the minimum value with respect to the specified comparison function.\n *\n * If several elements are equally minimum, the last element is returned. If the iterator is empty, `null` is returned.\n *\n * @seealso {@link min} for a version that uses the default comparator.\n * @seealso {@link minByKey} for a version that allows custom key extractors.\n *\n * @param iterable An iterator of number values to determine the minimum value of.\n * @param callbackFn A function to execute for each element produced by the iterator. It should return a number value.\n * @returns The element that gives the minimum value from the specified function, or `null` if the iterator is empty.\n *\n * @example\n * ```typescript\n * import { ascNumber, minBy } from '@sapphire/iterator-utilities';\n *\n * const iterable = [-3, 0, 1, 5, -10];\n * console.log(minBy(iterable, ascNumber));\n * // Output: -10\n * ```\n *\n * @remarks\n *\n * This function consumes the entire iterator.\n */\nexport function minBy<const ElementType>(\n\titerable: IterableResolvable<ElementType>,\n\tcomparator: CompareByComparator<ElementType>\n): ElementType | null {\n\tcomparator = assertFunction(comparator);\n\n\tconst iterator = toIterableIterator(iterable);\n\tconst first = iterator.next();\n\tif (first.done) return null;\n\n\tlet minimum = first.value;\n\tfor (const value of iterator) {\n\t\tconst comparison = compareIteratorElements<ElementType>(value, minimum, comparator);\n\n\t\tif (!orderingIsGreater(comparison)) {\n\t\t\tminimum = value;\n\t\t}\n\t}\n\n\treturn minimum;\n}\n"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { IterableResolvable } from './from.mjs';
|
|
2
|
+
import { CompareByComparator } from './shared/_compare.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Returns the element that gives the minimum value from the specified function.
|
|
6
|
+
*
|
|
7
|
+
* If several elements are equally minimum, the last element is returned. If the iterator is empty, `null` is returned.
|
|
8
|
+
*
|
|
9
|
+
* @seealso {@link min} for a version that uses the default comparator.
|
|
10
|
+
* @seealso {@link minBy} for a version that allows custom comparators.
|
|
11
|
+
*
|
|
12
|
+
* @param iterable An iterator of number values to determine the minimum value of.
|
|
13
|
+
* @param callbackFn A function to execute for each element produced by the iterator. It should return a number value.
|
|
14
|
+
* @returns The element that gives the minimum value from the specified function, or `null` if the iterator is empty.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { minByKey } from '@sapphire/iterator-utilities';
|
|
19
|
+
*
|
|
20
|
+
* const iterable = [-3, 0, 1, 5, -10];
|
|
21
|
+
* console.log(minByKey(iterable, (value) => Math.abs(value)));
|
|
22
|
+
* // Output: 0
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
*
|
|
27
|
+
* This function consumes the entire iterator.
|
|
28
|
+
*/
|
|
29
|
+
declare function minByKey<const ElementType, const MappedType>(iterable: IterableResolvable<ElementType>, callbackFn: (element: ElementType, index: number) => MappedType, comparator?: CompareByComparator<MappedType>): ElementType | null;
|
|
30
|
+
|
|
31
|
+
export { minByKey };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { __name } from '../chunk-PAWJFY3S.mjs';
|
|
2
|
+
import { assertFunction } from './shared/_assertFunction.mjs';
|
|
3
|
+
import { compareIteratorElements, orderingIsLess } from './shared/_compare.mjs';
|
|
4
|
+
import { defaultCompare } from './shared/comparators.mjs';
|
|
5
|
+
import { toIterableIterator } from './toIterableIterator.mjs';
|
|
6
|
+
|
|
7
|
+
function minByKey(iterable, callbackFn, comparator = defaultCompare) {
|
|
8
|
+
callbackFn = assertFunction(callbackFn);
|
|
9
|
+
const iterator = toIterableIterator(iterable);
|
|
10
|
+
const first = iterator.next();
|
|
11
|
+
if (first.done) return null;
|
|
12
|
+
let minimum = first.value;
|
|
13
|
+
let minimumKey = callbackFn(minimum, 0);
|
|
14
|
+
let index = 1;
|
|
15
|
+
for (const value of iterator) {
|
|
16
|
+
const key = callbackFn(value, index++);
|
|
17
|
+
const comparison = compareIteratorElements(minimumKey, key, comparator);
|
|
18
|
+
if (!orderingIsLess(comparison)) {
|
|
19
|
+
minimum = value;
|
|
20
|
+
minimumKey = key;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return minimum;
|
|
24
|
+
}
|
|
25
|
+
__name(minByKey, "minByKey");
|
|
26
|
+
|
|
27
|
+
export { minByKey };
|
|
28
|
+
//# sourceMappingURL=minByKey.mjs.map
|
|
29
|
+
//# sourceMappingURL=minByKey.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/minByKey.ts"],"names":[],"mappings":";;;;;;AAiCO,SAAS,QACf,CAAA,QAAA,EACA,UACA,EAAA,UAAA,GAA8C,cACzB,EAAA;AACrB,EAAA,UAAA,GAAa,eAAe,UAAU,CAAA,CAAA;AAEtC,EAAM,MAAA,QAAA,GAAW,mBAAmB,QAAQ,CAAA,CAAA;AAC5C,EAAM,MAAA,KAAA,GAAQ,SAAS,IAAK,EAAA,CAAA;AAC5B,EAAI,IAAA,KAAA,CAAM,MAAa,OAAA,IAAA,CAAA;AAEvB,EAAA,IAAI,UAAU,KAAM,CAAA,KAAA,CAAA;AACpB,EAAI,IAAA,UAAA,GAAa,UAAW,CAAA,OAAA,EAAS,CAAC,CAAA,CAAA;AACtC,EAAA,IAAI,KAAQ,GAAA,CAAA,CAAA;AACZ,EAAA,KAAA,MAAW,SAAS,QAAU,EAAA;AAC7B,IAAM,MAAA,GAAA,GAAM,UAAW,CAAA,KAAA,EAAO,KAAO,EAAA,CAAA,CAAA;AACrC,IAAA,MAAM,UAAa,GAAA,uBAAA,CAAoC,UAAY,EAAA,GAAA,EAAK,UAAU,CAAA,CAAA;AAElF,IAAI,IAAA,CAAC,cAAe,CAAA,UAAU,CAAG,EAAA;AAChC,MAAU,OAAA,GAAA,KAAA,CAAA;AACV,MAAa,UAAA,GAAA,GAAA,CAAA;AAAA,KACd;AAAA,GACD;AAEA,EAAO,OAAA,OAAA,CAAA;AACR,CAAA;AAzBgB,MAAA,CAAA,QAAA,EAAA,UAAA,CAAA","file":"minByKey.mjs","sourcesContent":["import type { IterableResolvable } from './from';\nimport type { min } from './min';\nimport type { minBy } from './minBy';\nimport { assertFunction } from './shared/_assertFunction';\nimport { compareIteratorElements, orderingIsLess, type CompareByComparator } from './shared/_compare';\nimport { defaultCompare } from './shared/comparators';\nimport { toIterableIterator } from './toIterableIterator';\n\n/**\n * Returns the element that gives the minimum value from the specified function.\n *\n * If several elements are equally minimum, the last element is returned. If the iterator is empty, `null` is returned.\n *\n * @seealso {@link min} for a version that uses the default comparator.\n * @seealso {@link minBy} for a version that allows custom comparators.\n *\n * @param iterable An iterator of number values to determine the minimum value of.\n * @param callbackFn A function to execute for each element produced by the iterator. It should return a number value.\n * @returns The element that gives the minimum value from the specified function, or `null` if the iterator is empty.\n *\n * @example\n * ```typescript\n * import { minByKey } from '@sapphire/iterator-utilities';\n *\n * const iterable = [-3, 0, 1, 5, -10];\n * console.log(minByKey(iterable, (value) => Math.abs(value)));\n * // Output: 0\n * ```\n *\n * @remarks\n *\n * This function consumes the entire iterator.\n */\nexport function minByKey<const ElementType, const MappedType>(\n\titerable: IterableResolvable<ElementType>,\n\tcallbackFn: (element: ElementType, index: number) => MappedType,\n\tcomparator: CompareByComparator<MappedType> = defaultCompare\n): ElementType | null {\n\tcallbackFn = assertFunction(callbackFn);\n\n\tconst iterator = toIterableIterator(iterable);\n\tconst first = iterator.next();\n\tif (first.done) return null;\n\n\tlet minimum = first.value;\n\tlet minimumKey = callbackFn(minimum, 0);\n\tlet index = 1;\n\tfor (const value of iterator) {\n\t\tconst key = callbackFn(value, index++);\n\t\tconst comparison = compareIteratorElements<MappedType>(minimumKey, key, comparator);\n\n\t\tif (!orderingIsLess(comparison)) {\n\t\t\tminimum = value;\n\t\t\tminimumKey = key;\n\t\t}\n\t}\n\n\treturn minimum;\n}\n"]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { IterableResolvable } from './from.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Determines if the elements of both iterators are not equal.
|
|
5
|
+
*
|
|
6
|
+
* @param iterable The iterator to compare.
|
|
7
|
+
* @param other The iterator to compare against.
|
|
8
|
+
* @returns Whether the two iterators are not equal.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { notEqual } from '@sapphire/iterator-utilities';
|
|
13
|
+
*
|
|
14
|
+
* console.log(notEqual([1], [1]));
|
|
15
|
+
* // Output: false
|
|
16
|
+
* console.log(notEqual([1], [1, 2]));
|
|
17
|
+
* // Output: true
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @remarks
|
|
21
|
+
*
|
|
22
|
+
* This function consumes the entire iterator.
|
|
23
|
+
*/
|
|
24
|
+
declare function notEqual<const ElementType>(iterable: IterableResolvable<ElementType>, other: IterableResolvable<ElementType>): boolean;
|
|
25
|
+
|
|
26
|
+
export { notEqual as ne, notEqual };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { __name } from '../chunk-PAWJFY3S.mjs';
|
|
2
|
+
import { equal } from './equal.mjs';
|
|
3
|
+
|
|
4
|
+
function notEqual(iterable, other) {
|
|
5
|
+
return !equal(iterable, other);
|
|
6
|
+
}
|
|
7
|
+
__name(notEqual, "notEqual");
|
|
8
|
+
|
|
9
|
+
export { notEqual as ne, notEqual };
|
|
10
|
+
//# sourceMappingURL=notEqual.mjs.map
|
|
11
|
+
//# sourceMappingURL=notEqual.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/notEqual.ts"],"names":[],"mappings":";;;AAwBO,SAAS,QAAA,CAA4B,UAA2C,KAAiD,EAAA;AACvI,EAAO,OAAA,CAAC,KAAM,CAAA,QAAA,EAAU,KAAK,CAAA,CAAA;AAC9B,CAAA;AAFgB,MAAA,CAAA,QAAA,EAAA,UAAA,CAAA","file":"notEqual.mjs","sourcesContent":["import { equal } from './equal';\nimport type { IterableResolvable } from './from';\n\n/**\n * Determines if the elements of both iterators are not equal.\n *\n * @param iterable The iterator to compare.\n * @param other The iterator to compare against.\n * @returns Whether the two iterators are not equal.\n *\n * @example\n * ```typescript\n * import { notEqual } from '@sapphire/iterator-utilities';\n *\n * console.log(notEqual([1], [1]));\n * // Output: false\n * console.log(notEqual([1], [1, 2]));\n * // Output: true\n * ```\n *\n * @remarks\n *\n * This function consumes the entire iterator.\n */\nexport function notEqual<const ElementType>(iterable: IterableResolvable<ElementType>, other: IterableResolvable<ElementType>): boolean {\n\treturn !equal(iterable, other);\n}\n\nexport { notEqual as ne };\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"nth.mjs","sourcesContent":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"position.mjs","sourcesContent":[]}
|
package/dist/esm/lib/reverse.mjs
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import { __name } from '../chunk-PAWJFY3S.mjs';
|
|
2
|
-
import {
|
|
2
|
+
import { collect } from './collect.mjs';
|
|
3
3
|
|
|
4
4
|
function* reverse(iterable) {
|
|
5
|
-
const items =
|
|
6
|
-
for (const item of toIterableIterator(iterable)) {
|
|
7
|
-
items.push(item);
|
|
8
|
-
}
|
|
5
|
+
const items = collect(iterable);
|
|
9
6
|
for (let i = items.length - 1; i >= 0; i--) {
|
|
10
7
|
yield items[i];
|
|
11
8
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/lib/reverse.ts"],"names":[],"mappings":";;;AAwBO,UAAU,QAA2B,QAA0E,EAAA;AACrH,
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/reverse.ts"],"names":[],"mappings":";;;AAwBO,UAAU,QAA2B,QAA0E,EAAA;AACrH,EAAM,MAAA,KAAA,GAAQ,QAAQ,QAAQ,CAAA,CAAA;AAC9B,EAAA,KAAA,IAAS,IAAI,KAAM,CAAA,MAAA,GAAS,CAAG,EAAA,CAAA,IAAK,GAAG,CAAK,EAAA,EAAA;AAC3C,IAAA,MAAM,MAAM,CAAC,CAAA,CAAA;AAAA,GACd;AACD,CAAA;AALiB,MAAA,CAAA,OAAA,EAAA,SAAA,CAAA","file":"reverse.mjs","sourcesContent":["import { collect } from './collect';\nimport type { IterableResolvable } from './from';\n\n/**\n * Consumes the iterable and returns a new iterable with the elements in reverse order.\n *\n * @param iterable The iterator to reverse.\n * @returns An iterator whose element correspond to the elements of the provided iterator in reverse order.\n *\n * @example\n * ```typescript\n * import { reverse } from '@sapphire/iterator-utilities';\n *\n * console.log([...reverse([1, 2, 3, 4, 5])]);\n * // Output: [5, 4, 3, 2, 1]\n *\n * console.log([...reverse('hello')]);\n * // Output: ['o', 'l', 'l', 'e', 'h']\n * ```\n *\n * @remarks\n *\n * This function collects all elements of the provided iterator into an array before yielding them in reverse order.\n */\nexport function* reverse<const ElementType>(iterable: IterableResolvable<ElementType>): IterableIterator<ElementType> {\n\tconst items = collect(iterable);\n\tfor (let i = items.length - 1; i >= 0; i--) {\n\t\tyield items[i];\n\t}\n}\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the result of a lexicographic comparison.
|
|
3
|
+
*
|
|
4
|
+
* The operations are as follows:
|
|
5
|
+
*
|
|
6
|
+
* - Two iterators are compared element by element.
|
|
7
|
+
* - The first pair of unequal elements determines the result.
|
|
8
|
+
* - If one sequence is a prefix of the other, the shorter sequence is lexicographically less than the other.
|
|
9
|
+
* - If two sequences have the same elements in the same order, they are lexicographically equal.
|
|
10
|
+
* - An empty sequence is lexicographically less than any non-empty sequence.
|
|
11
|
+
* - Two empty sequences are lexicographically equal.
|
|
12
|
+
*/
|
|
13
|
+
type LexicographicComparison = -1 | 0 | 1;
|
|
14
|
+
type CompareByComparator<ElementType> = (x: Exclude<ElementType, undefined>, y: Exclude<ElementType, undefined>) => number;
|
|
15
|
+
declare function compareIteratorElements<const ElementType>(x: ElementType | undefined, y: ElementType | undefined, comparator: CompareByComparator<ElementType>): number;
|
|
16
|
+
declare function orderingIsLess(ordering: number): boolean;
|
|
17
|
+
declare function orderingIsEqual(ordering: number): boolean;
|
|
18
|
+
declare function orderingIsGreater(ordering: number): boolean;
|
|
19
|
+
|
|
20
|
+
export { type CompareByComparator, type LexicographicComparison, compareIteratorElements, orderingIsEqual, orderingIsGreater, orderingIsLess };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { __name } from '../../chunk-PAWJFY3S.mjs';
|
|
2
|
+
|
|
3
|
+
// src/lib/shared/_compare.ts
|
|
4
|
+
function compareIteratorElements(x, y, comparator) {
|
|
5
|
+
if (typeof x === "undefined") {
|
|
6
|
+
if (typeof y === "undefined") return 0;
|
|
7
|
+
return 1;
|
|
8
|
+
}
|
|
9
|
+
if (typeof y === "undefined") {
|
|
10
|
+
return -1;
|
|
11
|
+
}
|
|
12
|
+
return comparator(x, y);
|
|
13
|
+
}
|
|
14
|
+
__name(compareIteratorElements, "compareIteratorElements");
|
|
15
|
+
function orderingIsLess(ordering) {
|
|
16
|
+
return ordering < 0;
|
|
17
|
+
}
|
|
18
|
+
__name(orderingIsLess, "orderingIsLess");
|
|
19
|
+
function orderingIsEqual(ordering) {
|
|
20
|
+
return ordering === 0;
|
|
21
|
+
}
|
|
22
|
+
__name(orderingIsEqual, "orderingIsEqual");
|
|
23
|
+
function orderingIsGreater(ordering) {
|
|
24
|
+
return ordering > 0;
|
|
25
|
+
}
|
|
26
|
+
__name(orderingIsGreater, "orderingIsGreater");
|
|
27
|
+
|
|
28
|
+
export { compareIteratorElements, orderingIsEqual, orderingIsGreater, orderingIsLess };
|
|
29
|
+
//# sourceMappingURL=_compare.mjs.map
|
|
30
|
+
//# sourceMappingURL=_compare.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/lib/shared/_compare.ts"],"names":[],"mappings":";;;AAgBO,SAAS,uBAAA,CACf,CACA,EAAA,CAAA,EACA,UACS,EAAA;AAET,EAAI,IAAA,OAAO,MAAM,WAAa,EAAA;AAE7B,IAAI,IAAA,OAAO,CAAM,KAAA,WAAA,EAAoB,OAAA,CAAA,CAAA;AAErC,IAAO,OAAA,CAAA,CAAA;AAAA,GACR;AAEA,EAAI,IAAA,OAAO,MAAM,WAAa,EAAA;AAE7B,IAAO,OAAA,CAAA,CAAA,CAAA;AAAA,GACR;AAEA,EAAO,OAAA,UAAA,CAAW,GAAsC,CAAoC,CAAA,CAAA;AAC7F,CAAA;AAnBgB,MAAA,CAAA,uBAAA,EAAA,yBAAA,CAAA,CAAA;AAqBT,SAAS,eAAe,QAAkB,EAAA;AAChD,EAAA,OAAO,QAAW,GAAA,CAAA,CAAA;AACnB,CAAA;AAFgB,MAAA,CAAA,cAAA,EAAA,gBAAA,CAAA,CAAA;AAIT,SAAS,gBAAgB,QAAkB,EAAA;AACjD,EAAA,OAAO,QAAa,KAAA,CAAA,CAAA;AACrB,CAAA;AAFgB,MAAA,CAAA,eAAA,EAAA,iBAAA,CAAA,CAAA;AAIT,SAAS,kBAAkB,QAAkB,EAAA;AACnD,EAAA,OAAO,QAAW,GAAA,CAAA,CAAA;AACnB,CAAA;AAFgB,MAAA,CAAA,iBAAA,EAAA,mBAAA,CAAA","file":"_compare.mjs","sourcesContent":["/**\n * Represents the result of a lexicographic comparison.\n *\n * The operations are as follows:\n *\n * - Two iterators are compared element by element.\n * - The first pair of unequal elements determines the result.\n * - If one sequence is a prefix of the other, the shorter sequence is lexicographically less than the other.\n * - If two sequences have the same elements in the same order, they are lexicographically equal.\n * - An empty sequence is lexicographically less than any non-empty sequence.\n * - Two empty sequences are lexicographically equal.\n */\nexport type LexicographicComparison = -1 | 0 | 1;\n\nexport type CompareByComparator<ElementType> = (x: Exclude<ElementType, undefined>, y: Exclude<ElementType, undefined>) => number;\n\nexport function compareIteratorElements<const ElementType>(\n\tx: ElementType | undefined,\n\ty: ElementType | undefined,\n\tcomparator: CompareByComparator<ElementType>\n): number {\n\t// 23.1.3.30.2 CompareArrayElements (https://tc39.es/ecma262/#sec-comparearrayelements)\n\tif (typeof x === 'undefined') {\n\t\t// 1. If x and y are both `undefined`, return +0.\n\t\tif (typeof y === 'undefined') return 0;\n\t\t// 2. If x is `undefined`, return +1.\n\t\treturn 1;\n\t}\n\n\tif (typeof y === 'undefined') {\n\t\t// 3. If y is `undefined`, return -1.\n\t\treturn -1;\n\t}\n\n\treturn comparator(x as Exclude<ElementType, undefined>, y as Exclude<ElementType, undefined>);\n}\n\nexport function orderingIsLess(ordering: number) {\n\treturn ordering < 0;\n}\n\nexport function orderingIsEqual(ordering: number) {\n\treturn ordering === 0;\n}\n\nexport function orderingIsGreater(ordering: number) {\n\treturn ordering > 0;\n}\n"]}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { LexicographicComparison, CompareByComparator } from './_compare.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Compares two elements lexicographically using the default comparison algorithm.
|
|
5
|
+
* @seealso 23.1.3.30.2 CompareArrayElements ({@link https://tc39.es/ecma262/#sec-comparearrayelements})
|
|
6
|
+
*
|
|
7
|
+
* @param x The first element to compare.
|
|
8
|
+
* @param y The second element to compare.
|
|
9
|
+
* @returns The lexicographic comparison of the two elements.
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
*
|
|
13
|
+
* This function is used as the comparison algorithm for {@link compareBy}.
|
|
14
|
+
*/
|
|
15
|
+
declare function defaultCompare<const ElementType>(x: ElementType, y: ElementType): LexicographicComparison;
|
|
16
|
+
|
|
17
|
+
declare const descString: <const ElementType>(x: Exclude<ElementType, undefined>, y: Exclude<ElementType, undefined>) => number;
|
|
18
|
+
/**
|
|
19
|
+
* Compares two elements using the default comparison algorithm.
|
|
20
|
+
*
|
|
21
|
+
* @param x The first element to compare.
|
|
22
|
+
* @param y The second element to compare.
|
|
23
|
+
* @returns The numeric comparison of the two elements.
|
|
24
|
+
*
|
|
25
|
+
* @privateRemarks
|
|
26
|
+
*
|
|
27
|
+
* The implementation of this function is based on the optimized version of the
|
|
28
|
+
* starship operator in Rust. The subtraction is intentional and is used to
|
|
29
|
+
* determine the ordering of the two elements.
|
|
30
|
+
*/
|
|
31
|
+
declare function ascNumber(x: number | bigint | string, y: number | bigint | string): LexicographicComparison;
|
|
32
|
+
declare const descNumber: CompareByComparator<string | number | bigint>;
|
|
33
|
+
|
|
34
|
+
export { ascNumber, defaultCompare as ascString, defaultCompare, descNumber, descString };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { __name } from '../../chunk-PAWJFY3S.mjs';
|
|
2
|
+
|
|
3
|
+
// src/lib/shared/comparators.ts
|
|
4
|
+
function swap(cb) {
|
|
5
|
+
return (x, y) => 0 - cb(x, y);
|
|
6
|
+
}
|
|
7
|
+
__name(swap, "swap");
|
|
8
|
+
function defaultCompare(x, y) {
|
|
9
|
+
return ascNumber(String(x), String(y));
|
|
10
|
+
}
|
|
11
|
+
__name(defaultCompare, "defaultCompare");
|
|
12
|
+
var descString = swap(defaultCompare);
|
|
13
|
+
function ascNumber(x, y) {
|
|
14
|
+
return (x > y) - (x < y);
|
|
15
|
+
}
|
|
16
|
+
__name(ascNumber, "ascNumber");
|
|
17
|
+
var descNumber = swap(ascNumber);
|
|
18
|
+
|
|
19
|
+
export { ascNumber, defaultCompare as ascString, defaultCompare, descNumber, descString };
|
|
20
|
+
//# sourceMappingURL=comparators.mjs.map
|
|
21
|
+
//# sourceMappingURL=comparators.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/lib/shared/comparators.ts"],"names":[],"mappings":";;;AAEA,SAAS,KAAwB,EAAwE,EAAA;AACxG,EAAA,OAAO,CAAC,CAAG,EAAA,CAAA,KAAO,CAAI,GAAA,EAAA,CAAG,GAAG,CAAC,CAAA,CAAA;AAC9B,CAAA;AAFS,MAAA,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;AAgBF,SAAS,cAAA,CAAkC,GAAgB,CAAyC,EAAA;AAI1G,EAAA,OAAO,UAAU,MAAO,CAAA,CAAC,CAAG,EAAA,MAAA,CAAO,CAAC,CAAC,CAAA,CAAA;AACtC,CAAA;AALgB,MAAA,CAAA,cAAA,EAAA,gBAAA,CAAA,CAAA;AAQH,IAAA,UAAA,GAAa,KAAK,cAAc,EAAA;AAetC,SAAS,SAAA,CAAU,GAA6B,CAAsD,EAAA;AAE5G,EAAS,OAAA,CAAA,CAAA,GAAI,MAAM,CAAI,GAAA,CAAA,CAAA,CAAA;AACxB,CAAA;AAHgB,MAAA,CAAA,SAAA,EAAA,WAAA,CAAA,CAAA;AAIH,IAAA,UAAA,GAAa,KAAK,SAAS","file":"comparators.mjs","sourcesContent":["import type { CompareByComparator, LexicographicComparison } from './_compare';\n\nfunction swap<const ElementType>(cb: CompareByComparator<ElementType>): CompareByComparator<ElementType> {\n\treturn (x, y) => (0 - cb(x, y)) as LexicographicComparison;\n}\n\n/**\n * Compares two elements lexicographically using the default comparison algorithm.\n * @seealso 23.1.3.30.2 CompareArrayElements ({@link https://tc39.es/ecma262/#sec-comparearrayelements})\n *\n * @param x The first element to compare.\n * @param y The second element to compare.\n * @returns The lexicographic comparison of the two elements.\n *\n * @remarks\n *\n * This function is used as the comparison algorithm for {@link compareBy}.\n */\nexport function defaultCompare<const ElementType>(x: ElementType, y: ElementType): LexicographicComparison {\n\t// 5. Let xString be ? ToString(x).\n\t// 6. Let yString be ? ToString(y).\n\t// Steps 7-11 are optimized away by the usage of `ascNumber`.\n\treturn ascNumber(String(x), String(y));\n}\n\nexport { defaultCompare as ascString };\nexport const descString = swap(defaultCompare);\n\n/**\n * Compares two elements using the default comparison algorithm.\n *\n * @param x The first element to compare.\n * @param y The second element to compare.\n * @returns The numeric comparison of the two elements.\n *\n * @privateRemarks\n *\n * The implementation of this function is based on the optimized version of the\n * starship operator in Rust. The subtraction is intentional and is used to\n * determine the ordering of the two elements.\n */\nexport function ascNumber(x: number | bigint | string, y: number | bigint | string): LexicographicComparison {\n\t// @ts-expect-error: The subtraction is intentional\n\treturn ((x > y) - (x < y)) as LexicographicComparison;\n}\nexport const descNumber = swap(ascNumber);\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"skip.mjs","sourcesContent":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"skipLast.mjs","sourcesContent":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"skipWhile.mjs","sourcesContent":[]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { IterableResolvable } from './from.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates an iterator starting at the same point, but stepping by the given amount at each iteration.
|
|
5
|
+
*
|
|
6
|
+
* @param iterable An iterator to map over.
|
|
7
|
+
* @param step A positive integer representing the step to take at each iteration.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { stepBy } from '@sapphire/iterator-utilities';
|
|
12
|
+
*
|
|
13
|
+
* const iterable = [0, 1, 2, 3, 4, 5];
|
|
14
|
+
* console.log([...stepBy(iterable, 2)]);
|
|
15
|
+
* // Output: [0, 2, 4]
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
*
|
|
20
|
+
* The first element of the iterator will always be returned, regardless of the step given.
|
|
21
|
+
*/
|
|
22
|
+
declare function stepBy<const ElementType>(iterable: IterableResolvable<ElementType>, step: number): IterableIterator<ElementType>;
|
|
23
|
+
|
|
24
|
+
export { stepBy };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { __name } from '../chunk-PAWJFY3S.mjs';
|
|
2
|
+
import { from } from './from.mjs';
|
|
3
|
+
import { assertPositive } from './shared/_assertPositive.mjs';
|
|
4
|
+
import { makeIterableIterator } from './shared/_makeIterableIterator.mjs';
|
|
5
|
+
import { toIntegerOrInfinityOrThrow } from './shared/_toIntegerOrInfinityOrThrow.mjs';
|
|
6
|
+
|
|
7
|
+
function stepBy(iterable, step) {
|
|
8
|
+
step = assertPositive(toIntegerOrInfinityOrThrow(step), step);
|
|
9
|
+
const iterator = from(iterable);
|
|
10
|
+
return makeIterableIterator(() => {
|
|
11
|
+
const result = iterator.next();
|
|
12
|
+
if (result.done) {
|
|
13
|
+
return { done: true, value: void 0 };
|
|
14
|
+
}
|
|
15
|
+
for (let i = 0; i < step - 1; i++) {
|
|
16
|
+
const result2 = iterator.next();
|
|
17
|
+
if (result2.done) break;
|
|
18
|
+
}
|
|
19
|
+
return { done: false, value: result.value };
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
__name(stepBy, "stepBy");
|
|
23
|
+
|
|
24
|
+
export { stepBy };
|
|
25
|
+
//# sourceMappingURL=stepBy.mjs.map
|
|
26
|
+
//# sourceMappingURL=stepBy.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/stepBy.ts"],"names":["result"],"mappings":";;;;;;AAwBO,SAAS,MAAA,CAA0B,UAA2C,IAA6C,EAAA;AACjI,EAAA,IAAA,GAAO,cAAe,CAAA,0BAAA,CAA2B,IAAI,CAAA,EAAG,IAAI,CAAA,CAAA;AAE5D,EAAM,MAAA,QAAA,GAAW,KAAK,QAAQ,CAAA,CAAA;AAC9B,EAAA,OAAO,qBAAkC,MAAM;AAC9C,IAAM,MAAA,MAAA,GAAS,SAAS,IAAK,EAAA,CAAA;AAC7B,IAAA,IAAI,OAAO,IAAM,EAAA;AAChB,MAAA,OAAO,EAAE,IAAA,EAAM,IAAM,EAAA,KAAA,EAAO,KAAU,CAAA,EAAA,CAAA;AAAA,KACvC;AAEA,IAAA,KAAA,IAAS,CAAI,GAAA,CAAA,EAAG,CAAI,GAAA,IAAA,GAAO,GAAG,CAAK,EAAA,EAAA;AAClC,MAAMA,MAAAA,OAAAA,GAAS,SAAS,IAAK,EAAA,CAAA;AAC7B,MAAA,IAAIA,QAAO,IAAM,EAAA,MAAA;AAAA,KAClB;AAEA,IAAA,OAAO,EAAE,IAAA,EAAM,KAAO,EAAA,KAAA,EAAO,OAAO,KAAM,EAAA,CAAA;AAAA,GAC1C,CAAA,CAAA;AACF,CAAA;AAjBgB,MAAA,CAAA,MAAA,EAAA,QAAA,CAAA","file":"stepBy.mjs","sourcesContent":["import { from, type IterableResolvable } from './from';\nimport { assertPositive } from './shared/_assertPositive';\nimport { makeIterableIterator } from './shared/_makeIterableIterator';\nimport { toIntegerOrInfinityOrThrow } from './shared/_toIntegerOrInfinityOrThrow';\n\n/**\n * Creates an iterator starting at the same point, but stepping by the given amount at each iteration.\n *\n * @param iterable An iterator to map over.\n * @param step A positive integer representing the step to take at each iteration.\n *\n * @example\n * ```typescript\n * import { stepBy } from '@sapphire/iterator-utilities';\n *\n * const iterable = [0, 1, 2, 3, 4, 5];\n * console.log([...stepBy(iterable, 2)]);\n * // Output: [0, 2, 4]\n * ```\n *\n * @remarks\n *\n * The first element of the iterator will always be returned, regardless of the step given.\n */\nexport function stepBy<const ElementType>(iterable: IterableResolvable<ElementType>, step: number): IterableIterator<ElementType> {\n\tstep = assertPositive(toIntegerOrInfinityOrThrow(step), step);\n\n\tconst iterator = from(iterable);\n\treturn makeIterableIterator<ElementType>(() => {\n\t\tconst result = iterator.next();\n\t\tif (result.done) {\n\t\t\treturn { done: true, value: undefined };\n\t\t}\n\n\t\tfor (let i = 0; i < step - 1; i++) {\n\t\t\tconst result = iterator.next();\n\t\t\tif (result.done) break;\n\t\t}\n\n\t\treturn { done: false, value: result.value };\n\t});\n}\n"]}
|