diff --git a/index.js b/index.js index f05915c..481079f 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ // TODO: See "iteratorKind" in https://tc39.es/ecma262/#sec-runtime-semantics-forin-div-ofbodyevaluation-lhs-stmt-iterator-lhskind-labelset – see how it loops and validates the returned values // TODO: THERE'S ACTUALLY A "throw" method MENTION IN https://tc39.es/ecma262/#sec-generator-function-definitions-runtime-semantics-evaluation: "NOTE: Exceptions from the inner iterator throw method are propagated. Normal completions from an inner throw method are processed similarly to an inner next." THOUGH NOT SURE HOW TO TRIGGER IT IN PRACTICE, SEE yield.spec.js +import { isType, typesafeIsArray } from '@voxpelli/typed-utils'; import { findLeastTargeted } from './lib/find-least-targeted.js'; import { arrayDeleteInPlace, makeIterableAsync } from './lib/misc.js'; import { isAsyncIterable, isIterable, isPartOfArray } from './lib/type-checks.js'; @@ -51,8 +52,8 @@ export function bufferedAsyncMap (input, callback, options) { if (!input) throw new TypeError('Expected input to be provided'); if (!isAsyncIterable(asyncIterable)) throw new TypeError('Expected asyncIterable to have a Symbol.asyncIterator function'); - if (typeof callback !== 'function') throw new TypeError('Expected callback to be a function'); - if (typeof bufferSize !== 'number') throw new TypeError('Expected bufferSize to be a number'); + if (!isType(callback, 'function')) throw new TypeError('Expected callback to be a function'); + if (!isType(bufferSize, 'number')) throw new TypeError('Expected bufferSize to be a number'); /** @type {AsyncIterator} */ const asyncIterator = asyncIterable[Symbol.asyncIterator](); @@ -130,7 +131,7 @@ export function bufferedAsyncMap (input, callback, options) { err: err instanceof Error ? err : new Error('Unknown subiterator error'), })) .then(async result => { - if (typeof result !== 'object') { + if (!isType(result, 'object')) { throw new TypeError('Expected an object value'); } if ('err' in result || result.done) { @@ -155,7 +156,7 @@ export function bufferedAsyncMap (input, callback, options) { err: err instanceof Error ? err : new Error('Unknown iterator error'), })) .then(async result => { - if (typeof result !== 'object') { + if (!isType(result, 'object')) { throw new TypeError('Expected an object value'); } if ('err' in result || result.done) { diff --git a/lib/type-checks.js b/lib/type-checks.js index 70909c3..5d7ac32 100644 --- a/lib/type-checks.js +++ b/lib/type-checks.js @@ -1,3 +1,5 @@ +import { guardedArrayIncludes } from '@voxpelli/typed-utils'; + /** * @param {unknown} value * @returns {value is Iterable} @@ -16,4 +18,4 @@ export const isAsyncIterable = (value) => Boolean(value && typeof value === 'obj * @param {Values[]} list * @returns {value is Values} */ -export const isPartOfArray = (value, list) => list.includes(/** @type {Values} */ (value)); +export const isPartOfArray = (value, list) => guardedArrayIncludes(list, value); diff --git a/package.json b/package.json index 0a01f4f..7400abe 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "author": "Pelle Wessman (http://kodfabrik.se/)", "license": "MIT", "engines": { - "node": ">=18.6.0" + "node": "^20.11.0 || >=22.0.0" }, "type": "module", "exports": "./index.js", @@ -71,5 +71,8 @@ "type-coverage": "^2.29.1", "typescript": "~5.5.3", "validate-conventional-commit": "^1.0.4" + }, + "dependencies": { + "@voxpelli/typed-utils": "^2.6.0" } }