index.cjs 23 KB

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