index.mjs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. function clamp(n, min, max) {
  2. return Math.min(max, Math.max(min, n));
  3. }
  4. function sum(...args) {
  5. return flattenArrayable(args).reduce((a, b) => a + b, 0);
  6. }
  7. function lerp(min, max, t) {
  8. const interpolation = clamp(t, 0, 1);
  9. return min + (max - min) * interpolation;
  10. }
  11. function remap(n, inMin, inMax, outMin, outMax) {
  12. const interpolation = (n - inMin) / (inMax - inMin);
  13. return lerp(outMin, outMax, interpolation);
  14. }
  15. function toArray(array) {
  16. array = array ?? [];
  17. return Array.isArray(array) ? array : [array];
  18. }
  19. function flattenArrayable(array) {
  20. return toArray(array).flat(1);
  21. }
  22. function mergeArrayable(...args) {
  23. return args.flatMap((i) => toArray(i));
  24. }
  25. function partition(array, ...filters) {
  26. const result = Array.from({ length: filters.length + 1 }).fill(null).map(() => []);
  27. array.forEach((e, idx, arr) => {
  28. let i = 0;
  29. for (const filter of filters) {
  30. if (filter(e, idx, arr)) {
  31. result[i].push(e);
  32. return;
  33. }
  34. i += 1;
  35. }
  36. result[i].push(e);
  37. });
  38. return result;
  39. }
  40. function uniq(array) {
  41. return Array.from(new Set(array));
  42. }
  43. function uniqueBy(array, equalFn) {
  44. return array.reduce((acc, cur) => {
  45. const index = acc.findIndex((item) => equalFn(cur, item));
  46. if (index === -1)
  47. acc.push(cur);
  48. return acc;
  49. }, []);
  50. }
  51. function last(array) {
  52. return at(array, -1);
  53. }
  54. function remove(array, value) {
  55. if (!array)
  56. return false;
  57. const index = array.indexOf(value);
  58. if (index >= 0) {
  59. array.splice(index, 1);
  60. return true;
  61. }
  62. return false;
  63. }
  64. function at(array, index) {
  65. const len = array.length;
  66. if (!len)
  67. return void 0;
  68. if (index < 0)
  69. index += len;
  70. return array[index];
  71. }
  72. function range(...args) {
  73. let start, stop, step;
  74. if (args.length === 1) {
  75. start = 0;
  76. step = 1;
  77. [stop] = args;
  78. } else {
  79. [start, stop, step = 1] = args;
  80. }
  81. const arr = [];
  82. let current = start;
  83. while (current < stop) {
  84. arr.push(current);
  85. current += step || 1;
  86. }
  87. return arr;
  88. }
  89. function move(arr, from, to) {
  90. arr.splice(to, 0, arr.splice(from, 1)[0]);
  91. return arr;
  92. }
  93. function clampArrayRange(n, arr) {
  94. return clamp(n, 0, arr.length - 1);
  95. }
  96. function sample(arr, quantity) {
  97. return Array.from({ length: quantity }, (_) => arr[Math.round(Math.random() * (arr.length - 1))]);
  98. }
  99. function shuffle(array) {
  100. for (let i = array.length - 1; i > 0; i--) {
  101. const j = Math.floor(Math.random() * (i + 1));
  102. [array[i], array[j]] = [array[j], array[i]];
  103. }
  104. return array;
  105. }
  106. function assert(condition, message) {
  107. if (!condition)
  108. throw new Error(message);
  109. }
  110. const toString = (v) => Object.prototype.toString.call(v);
  111. function getTypeName(v) {
  112. if (v === null)
  113. return "null";
  114. const type = toString(v).slice(8, -1).toLowerCase();
  115. return typeof v === "object" || typeof v === "function" ? type : typeof v;
  116. }
  117. function noop() {
  118. }
  119. function isDeepEqual(value1, value2) {
  120. const type1 = getTypeName(value1);
  121. const type2 = getTypeName(value2);
  122. if (type1 !== type2)
  123. return false;
  124. if (type1 === "array") {
  125. if (value1.length !== value2.length)
  126. return false;
  127. return value1.every((item, i) => {
  128. return isDeepEqual(item, value2[i]);
  129. });
  130. }
  131. if (type1 === "object") {
  132. const keyArr = Object.keys(value1);
  133. if (keyArr.length !== Object.keys(value2).length)
  134. return false;
  135. return keyArr.every((key) => {
  136. return isDeepEqual(value1[key], value2[key]);
  137. });
  138. }
  139. return Object.is(value1, value2);
  140. }
  141. function notNullish(v) {
  142. return v != null;
  143. }
  144. function noNull(v) {
  145. return v !== null;
  146. }
  147. function notUndefined(v) {
  148. return v !== void 0;
  149. }
  150. function isTruthy(v) {
  151. return Boolean(v);
  152. }
  153. const isDef = (val) => typeof val !== "undefined";
  154. const isBoolean = (val) => typeof val === "boolean";
  155. const isFunction = (val) => typeof val === "function";
  156. const isNumber = (val) => typeof val === "number";
  157. const isString = (val) => typeof val === "string";
  158. const isObject = (val) => toString(val) === "[object Object]";
  159. const isUndefined = (val) => toString(val) === "[object Undefined]";
  160. const isNull = (val) => toString(val) === "[object Null]";
  161. const isRegExp = (val) => toString(val) === "[object RegExp]";
  162. const isDate = (val) => toString(val) === "[object Date]";
  163. const isWindow = (val) => typeof window !== "undefined" && toString(val) === "[object Window]";
  164. const isBrowser = typeof window !== "undefined";
  165. function slash(str) {
  166. return str.replace(/\\/g, "/");
  167. }
  168. function ensurePrefix(prefix, str) {
  169. if (!str.startsWith(prefix))
  170. return prefix + str;
  171. return str;
  172. }
  173. function ensureSuffix(suffix, str) {
  174. if (!str.endsWith(suffix))
  175. return str + suffix;
  176. return str;
  177. }
  178. function template(str, ...args) {
  179. const [firstArg, fallback] = args;
  180. if (isObject(firstArg)) {
  181. const vars = firstArg;
  182. return str.replace(/{([\w\d]+)}/g, (_, key) => vars[key] || ((typeof fallback === "function" ? fallback(key) : fallback) ?? key));
  183. } else {
  184. return str.replace(/{(\d+)}/g, (_, key) => {
  185. const index = Number(key);
  186. if (Number.isNaN(index))
  187. return key;
  188. return args[index];
  189. });
  190. }
  191. }
  192. const urlAlphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
  193. function randomStr(size = 16, dict = urlAlphabet) {
  194. let id = "";
  195. let i = size;
  196. const len = dict.length;
  197. while (i--)
  198. id += dict[Math.random() * len | 0];
  199. return id;
  200. }
  201. function capitalize(str) {
  202. return str[0].toUpperCase() + str.slice(1).toLowerCase();
  203. }
  204. const _reFullWs = /^\s*$/;
  205. function unindent(str) {
  206. const lines = (typeof str === "string" ? str : str[0]).split("\n");
  207. const whitespaceLines = lines.map((line) => _reFullWs.test(line));
  208. const commonIndent = lines.reduce((min, line, idx) => {
  209. var _a;
  210. if (whitespaceLines[idx])
  211. return min;
  212. const indent = (_a = line.match(/^\s*/)) == null ? void 0 : _a[0].length;
  213. return indent === void 0 ? min : Math.min(min, indent);
  214. }, Number.POSITIVE_INFINITY);
  215. let emptyLinesHead = 0;
  216. while (emptyLinesHead < lines.length && whitespaceLines[emptyLinesHead])
  217. emptyLinesHead++;
  218. let emptyLinesTail = 0;
  219. while (emptyLinesTail < lines.length && whitespaceLines[lines.length - emptyLinesTail - 1])
  220. emptyLinesTail++;
  221. return lines.slice(emptyLinesHead, lines.length - emptyLinesTail).map((line) => line.slice(commonIndent)).join("\n");
  222. }
  223. const timestamp = () => +Date.now();
  224. function batchInvoke(functions) {
  225. functions.forEach((fn) => fn && fn());
  226. }
  227. function invoke(fn) {
  228. return fn();
  229. }
  230. function tap(value, callback) {
  231. callback(value);
  232. return value;
  233. }
  234. function objectMap(obj, fn) {
  235. return Object.fromEntries(
  236. Object.entries(obj).map(([k, v]) => fn(k, v)).filter(notNullish)
  237. );
  238. }
  239. function isKeyOf(obj, k) {
  240. return k in obj;
  241. }
  242. function objectKeys(obj) {
  243. return Object.keys(obj);
  244. }
  245. function objectEntries(obj) {
  246. return Object.entries(obj);
  247. }
  248. function deepMerge(target, ...sources) {
  249. if (!sources.length)
  250. return target;
  251. const source = sources.shift();
  252. if (source === void 0)
  253. return target;
  254. if (isMergableObject(target) && isMergableObject(source)) {
  255. objectKeys(source).forEach((key) => {
  256. if (key === "__proto__" || key === "constructor" || key === "prototype")
  257. return;
  258. if (isMergableObject(source[key])) {
  259. if (!target[key])
  260. target[key] = {};
  261. if (isMergableObject(target[key])) {
  262. deepMerge(target[key], source[key]);
  263. } else {
  264. target[key] = source[key];
  265. }
  266. } else {
  267. target[key] = source[key];
  268. }
  269. });
  270. }
  271. return deepMerge(target, ...sources);
  272. }
  273. function deepMergeWithArray(target, ...sources) {
  274. if (!sources.length)
  275. return target;
  276. const source = sources.shift();
  277. if (source === void 0)
  278. return target;
  279. if (Array.isArray(target) && Array.isArray(source))
  280. target.push(...source);
  281. if (isMergableObject(target) && isMergableObject(source)) {
  282. objectKeys(source).forEach((key) => {
  283. if (key === "__proto__" || key === "constructor" || key === "prototype")
  284. return;
  285. if (Array.isArray(source[key])) {
  286. if (!target[key])
  287. target[key] = [];
  288. deepMergeWithArray(target[key], source[key]);
  289. } else if (isMergableObject(source[key])) {
  290. if (!target[key])
  291. target[key] = {};
  292. deepMergeWithArray(target[key], source[key]);
  293. } else {
  294. target[key] = source[key];
  295. }
  296. });
  297. }
  298. return deepMergeWithArray(target, ...sources);
  299. }
  300. function isMergableObject(item) {
  301. return isObject(item) && !Array.isArray(item);
  302. }
  303. function objectPick(obj, keys, omitUndefined = false) {
  304. return keys.reduce((n, k) => {
  305. if (k in obj) {
  306. if (!omitUndefined || obj[k] !== void 0)
  307. n[k] = obj[k];
  308. }
  309. return n;
  310. }, {});
  311. }
  312. function clearUndefined(obj) {
  313. Object.keys(obj).forEach((key) => obj[key] === void 0 ? delete obj[key] : {});
  314. return obj;
  315. }
  316. function hasOwnProperty(obj, v) {
  317. if (obj == null)
  318. return false;
  319. return Object.prototype.hasOwnProperty.call(obj, v);
  320. }
  321. function createSingletonPromise(fn) {
  322. let _promise;
  323. function wrapper() {
  324. if (!_promise)
  325. _promise = fn();
  326. return _promise;
  327. }
  328. wrapper.reset = async () => {
  329. const _prev = _promise;
  330. _promise = void 0;
  331. if (_prev)
  332. await _prev;
  333. };
  334. return wrapper;
  335. }
  336. function sleep(ms, callback) {
  337. return new Promise(
  338. (resolve) => setTimeout(async () => {
  339. await (callback == null ? void 0 : callback());
  340. resolve();
  341. }, ms)
  342. );
  343. }
  344. function createPromiseLock() {
  345. const locks = [];
  346. return {
  347. async run(fn) {
  348. const p = fn();
  349. locks.push(p);
  350. try {
  351. return await p;
  352. } finally {
  353. remove(locks, p);
  354. }
  355. },
  356. async wait() {
  357. await Promise.allSettled(locks);
  358. },
  359. isWaiting() {
  360. return Boolean(locks.length);
  361. },
  362. clear() {
  363. locks.length = 0;
  364. }
  365. };
  366. }
  367. function createControlledPromise() {
  368. let resolve, reject;
  369. const promise = new Promise((_resolve, _reject) => {
  370. resolve = _resolve;
  371. reject = _reject;
  372. });
  373. promise.resolve = resolve;
  374. promise.reject = reject;
  375. return promise;
  376. }
  377. /* eslint-disable no-undefined,no-param-reassign,no-shadow */
  378. /**
  379. * Throttle execution of a function. Especially useful for rate limiting
  380. * execution of handlers on events like resize and scroll.
  381. *
  382. * @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher)
  383. * are most useful.
  384. * @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through,
  385. * as-is, to `callback` when the throttled-function is executed.
  386. * @param {object} [options] - An object to configure options.
  387. * @param {boolean} [options.noTrailing] - Optional, defaults to false. If noTrailing is true, callback will only execute every `delay` milliseconds
  388. * while the throttled-function is being called. If noTrailing is false or unspecified, callback will be executed
  389. * one final time after the last throttled-function call. (After the throttled-function has not been called for
  390. * `delay` milliseconds, the internal counter is reset).
  391. * @param {boolean} [options.noLeading] - Optional, defaults to false. If noLeading is false, the first throttled-function call will execute callback
  392. * immediately. If noLeading is true, the first the callback execution will be skipped. It should be noted that
  393. * callback will never executed if both noLeading = true and noTrailing = true.
  394. * @param {boolean} [options.debounceMode] - If `debounceMode` is true (at begin), schedule `clear` to execute after `delay` ms. If `debounceMode` is
  395. * false (at end), schedule `callback` to execute after `delay` ms.
  396. *
  397. * @returns {Function} A new, throttled, function.
  398. */
  399. function throttle (delay, callback, options) {
  400. var _ref = options || {},
  401. _ref$noTrailing = _ref.noTrailing,
  402. noTrailing = _ref$noTrailing === void 0 ? false : _ref$noTrailing,
  403. _ref$noLeading = _ref.noLeading,
  404. noLeading = _ref$noLeading === void 0 ? false : _ref$noLeading,
  405. _ref$debounceMode = _ref.debounceMode,
  406. debounceMode = _ref$debounceMode === void 0 ? undefined : _ref$debounceMode;
  407. /*
  408. * After wrapper has stopped being called, this timeout ensures that
  409. * `callback` is executed at the proper times in `throttle` and `end`
  410. * debounce modes.
  411. */
  412. var timeoutID;
  413. var cancelled = false; // Keep track of the last time `callback` was executed.
  414. var lastExec = 0; // Function to clear existing timeout
  415. function clearExistingTimeout() {
  416. if (timeoutID) {
  417. clearTimeout(timeoutID);
  418. }
  419. } // Function to cancel next exec
  420. function cancel(options) {
  421. var _ref2 = options || {},
  422. _ref2$upcomingOnly = _ref2.upcomingOnly,
  423. upcomingOnly = _ref2$upcomingOnly === void 0 ? false : _ref2$upcomingOnly;
  424. clearExistingTimeout();
  425. cancelled = !upcomingOnly;
  426. }
  427. /*
  428. * The `wrapper` function encapsulates all of the throttling / debouncing
  429. * functionality and when executed will limit the rate at which `callback`
  430. * is executed.
  431. */
  432. function wrapper() {
  433. for (var _len = arguments.length, arguments_ = new Array(_len), _key = 0; _key < _len; _key++) {
  434. arguments_[_key] = arguments[_key];
  435. }
  436. var self = this;
  437. var elapsed = Date.now() - lastExec;
  438. if (cancelled) {
  439. return;
  440. } // Execute `callback` and update the `lastExec` timestamp.
  441. function exec() {
  442. lastExec = Date.now();
  443. callback.apply(self, arguments_);
  444. }
  445. /*
  446. * If `debounceMode` is true (at begin) this is used to clear the flag
  447. * to allow future `callback` executions.
  448. */
  449. function clear() {
  450. timeoutID = undefined;
  451. }
  452. if (!noLeading && debounceMode && !timeoutID) {
  453. /*
  454. * Since `wrapper` is being called for the first time and
  455. * `debounceMode` is true (at begin), execute `callback`
  456. * and noLeading != true.
  457. */
  458. exec();
  459. }
  460. clearExistingTimeout();
  461. if (debounceMode === undefined && elapsed > delay) {
  462. if (noLeading) {
  463. /*
  464. * In throttle mode with noLeading, if `delay` time has
  465. * been exceeded, update `lastExec` and schedule `callback`
  466. * to execute after `delay` ms.
  467. */
  468. lastExec = Date.now();
  469. if (!noTrailing) {
  470. timeoutID = setTimeout(debounceMode ? clear : exec, delay);
  471. }
  472. } else {
  473. /*
  474. * In throttle mode without noLeading, if `delay` time has been exceeded, execute
  475. * `callback`.
  476. */
  477. exec();
  478. }
  479. } else if (noTrailing !== true) {
  480. /*
  481. * In trailing throttle mode, since `delay` time has not been
  482. * exceeded, schedule `callback` to execute `delay` ms after most
  483. * recent execution.
  484. *
  485. * If `debounceMode` is true (at begin), schedule `clear` to execute
  486. * after `delay` ms.
  487. *
  488. * If `debounceMode` is false (at end), schedule `callback` to
  489. * execute after `delay` ms.
  490. */
  491. timeoutID = setTimeout(debounceMode ? clear : exec, debounceMode === undefined ? delay - elapsed : delay);
  492. }
  493. }
  494. wrapper.cancel = cancel; // Return the wrapper function.
  495. return wrapper;
  496. }
  497. /* eslint-disable no-undefined */
  498. /**
  499. * Debounce execution of a function. Debouncing, unlike throttling,
  500. * guarantees that a function is only executed a single time, either at the
  501. * very beginning of a series of calls, or at the very end.
  502. *
  503. * @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
  504. * @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,
  505. * to `callback` when the debounced-function is executed.
  506. * @param {object} [options] - An object to configure options.
  507. * @param {boolean} [options.atBegin] - Optional, defaults to false. If atBegin is false or unspecified, callback will only be executed `delay` milliseconds
  508. * after the last debounced-function call. If atBegin is true, callback will be executed only at the first debounced-function call.
  509. * (After the throttled-function has not been called for `delay` milliseconds, the internal counter is reset).
  510. *
  511. * @returns {Function} A new, debounced function.
  512. */
  513. function debounce (delay, callback, options) {
  514. var _ref = options || {},
  515. _ref$atBegin = _ref.atBegin,
  516. atBegin = _ref$atBegin === void 0 ? false : _ref$atBegin;
  517. return throttle(delay, callback, {
  518. debounceMode: atBegin !== false
  519. });
  520. }
  521. /*
  522. How it works:
  523. `this.#head` is an instance of `Node` which keeps track of its current value and nests another instance of `Node` that keeps the value that comes after it. When a value is provided to `.enqueue()`, the code needs to iterate through `this.#head`, going deeper and deeper to find the last value. However, iterating through every single item is slow. This problem is solved by saving a reference to the last value as `this.#tail` so that it can reference it to add a new value.
  524. */
  525. class Node {
  526. value;
  527. next;
  528. constructor(value) {
  529. this.value = value;
  530. }
  531. }
  532. class Queue {
  533. #head;
  534. #tail;
  535. #size;
  536. constructor() {
  537. this.clear();
  538. }
  539. enqueue(value) {
  540. const node = new Node(value);
  541. if (this.#head) {
  542. this.#tail.next = node;
  543. this.#tail = node;
  544. } else {
  545. this.#head = node;
  546. this.#tail = node;
  547. }
  548. this.#size++;
  549. }
  550. dequeue() {
  551. const current = this.#head;
  552. if (!current) {
  553. return;
  554. }
  555. this.#head = this.#head.next;
  556. this.#size--;
  557. return current.value;
  558. }
  559. clear() {
  560. this.#head = undefined;
  561. this.#tail = undefined;
  562. this.#size = 0;
  563. }
  564. get size() {
  565. return this.#size;
  566. }
  567. * [Symbol.iterator]() {
  568. let current = this.#head;
  569. while (current) {
  570. yield current.value;
  571. current = current.next;
  572. }
  573. }
  574. }
  575. const AsyncResource = {
  576. bind(fn, _type, thisArg) {
  577. return fn.bind(thisArg);
  578. },
  579. };
  580. function pLimit(concurrency) {
  581. if (!((Number.isInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency > 0)) {
  582. throw new TypeError('Expected `concurrency` to be a number from 1 and up');
  583. }
  584. const queue = new Queue();
  585. let activeCount = 0;
  586. const next = () => {
  587. activeCount--;
  588. if (queue.size > 0) {
  589. queue.dequeue()();
  590. }
  591. };
  592. const run = async (function_, resolve, arguments_) => {
  593. activeCount++;
  594. const result = (async () => function_(...arguments_))();
  595. resolve(result);
  596. try {
  597. await result;
  598. } catch {}
  599. next();
  600. };
  601. const enqueue = (function_, resolve, arguments_) => {
  602. queue.enqueue(
  603. AsyncResource.bind(run.bind(undefined, function_, resolve, arguments_)),
  604. );
  605. (async () => {
  606. // This function needs to wait until the next microtask before comparing
  607. // `activeCount` to `concurrency`, because `activeCount` is updated asynchronously
  608. // when the run function is dequeued and called. The comparison in the if-statement
  609. // needs to happen asynchronously as well to get an up-to-date value for `activeCount`.
  610. await Promise.resolve();
  611. if (activeCount < concurrency && queue.size > 0) {
  612. queue.dequeue()();
  613. }
  614. })();
  615. };
  616. const generator = (function_, ...arguments_) => new Promise(resolve => {
  617. enqueue(function_, resolve, arguments_);
  618. });
  619. Object.defineProperties(generator, {
  620. activeCount: {
  621. get: () => activeCount,
  622. },
  623. pendingCount: {
  624. get: () => queue.size,
  625. },
  626. clearQueue: {
  627. value() {
  628. queue.clear();
  629. },
  630. },
  631. });
  632. return generator;
  633. }
  634. const VOID = Symbol("p-void");
  635. class PInstance extends Promise {
  636. constructor(items = [], options) {
  637. super(() => {
  638. });
  639. this.items = items;
  640. this.options = options;
  641. this.promises = /* @__PURE__ */ new Set();
  642. }
  643. get promise() {
  644. var _a;
  645. let batch;
  646. const items = [...Array.from(this.items), ...Array.from(this.promises)];
  647. if ((_a = this.options) == null ? void 0 : _a.concurrency) {
  648. const limit = pLimit(this.options.concurrency);
  649. batch = Promise.all(items.map((p2) => limit(() => p2)));
  650. } else {
  651. batch = Promise.all(items);
  652. }
  653. return batch.then((l) => l.filter((i) => i !== VOID));
  654. }
  655. add(...args) {
  656. args.forEach((i) => {
  657. this.promises.add(i);
  658. });
  659. }
  660. map(fn) {
  661. return new PInstance(
  662. Array.from(this.items).map(async (i, idx) => {
  663. const v = await i;
  664. if (v === VOID)
  665. return VOID;
  666. return fn(v, idx);
  667. }),
  668. this.options
  669. );
  670. }
  671. filter(fn) {
  672. return new PInstance(
  673. Array.from(this.items).map(async (i, idx) => {
  674. const v = await i;
  675. const r = await fn(v, idx);
  676. if (!r)
  677. return VOID;
  678. return v;
  679. }),
  680. this.options
  681. );
  682. }
  683. forEach(fn) {
  684. return this.map(fn).then();
  685. }
  686. reduce(fn, initialValue) {
  687. return this.promise.then((array) => array.reduce(fn, initialValue));
  688. }
  689. clear() {
  690. this.promises.clear();
  691. }
  692. then(fn) {
  693. const p2 = this.promise;
  694. if (fn)
  695. return p2.then(fn);
  696. else
  697. return p2;
  698. }
  699. catch(fn) {
  700. return this.promise.catch(fn);
  701. }
  702. finally(fn) {
  703. return this.promise.finally(fn);
  704. }
  705. }
  706. function p(items, options) {
  707. return new PInstance(items, options);
  708. }
  709. export { assert, at, batchInvoke, capitalize, clamp, clampArrayRange, clearUndefined, createControlledPromise, createPromiseLock, createSingletonPromise, debounce, deepMerge, deepMergeWithArray, ensurePrefix, ensureSuffix, flattenArrayable, getTypeName, hasOwnProperty, invoke, isBoolean, isBrowser, isDate, isDeepEqual, isDef, isFunction, isKeyOf, isNull, isNumber, isObject, isRegExp, isString, isTruthy, isUndefined, isWindow, last, lerp, mergeArrayable, move, noNull, noop, notNullish, notUndefined, objectEntries, objectKeys, objectMap, objectPick, p, partition, randomStr, range, remap, remove, sample, shuffle, slash, sleep, sum, tap, template, throttle, timestamp, toArray, toString, unindent, uniq, uniqueBy };