@orioro/util 0.8.0 → 0.10.0

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.
@@ -0,0 +1,10 @@
1
+ type AnyFn = (...args: any[]) => any;
2
+ type Hooks = Partial<{
3
+ input: AnyFn;
4
+ validate: AnyFn;
5
+ before: AnyFn;
6
+ output: AnyFn;
7
+ after: AnyFn;
8
+ }>;
9
+ export declare function hookFn(fn: AnyFn, { input, validate, before, output, after }: Hooks): (...args: any[]) => Promise<any>;
10
+ export {};
package/dist/index.d.ts CHANGED
@@ -8,3 +8,5 @@ export * from './switchValue';
8
8
  export * from './typeOf';
9
9
  export * from './validate';
10
10
  export * from './crawling';
11
+ export * from './normalizeString';
12
+ export * from './hookFn';
package/dist/index.mjs CHANGED
@@ -668,6 +668,31 @@ function parseBatchedResults(inputs, results) {
668
668
  });
669
669
  }
670
670
 
671
+ function makeDeferred() {
672
+ var resolve = function resolve() {};
673
+ var reject = function reject() {};
674
+ var settled = false;
675
+ var promise = new Promise(function (_resolve, _reject) {
676
+ resolve = function resolve(value) {
677
+ if (!settled) {
678
+ settled = true;
679
+ _resolve(value);
680
+ }
681
+ };
682
+ reject = function reject(reason) {
683
+ if (!settled) {
684
+ settled = true;
685
+ _reject(reason);
686
+ }
687
+ };
688
+ });
689
+ return {
690
+ promise: promise,
691
+ resolve: resolve,
692
+ reject: reject
693
+ };
694
+ }
695
+
671
696
  function testCriteria(criteria, input) {
672
697
  switch (_typeof(criteria)) {
673
698
  case 'function':
@@ -1619,4 +1644,86 @@ function fetchAllPages(_a) {
1619
1644
  });
1620
1645
  }
1621
1646
 
1622
- export { PromiseLikeEventEmitter, SKIPPED, TimeoutError, ValidationError, batchFn, debugFn, deepFreeze, fetchAllPages, inMemoryDataStore, interpolate, makeTypeOf, maybeFn, paginatedHttpFetch, parseBatchedResults, pickPaths, promiseReduce, resolveNestedPromises, switchExec, switchValue, typeMap, typeOf, validate, validateAsync, wait, withTimeout };
1647
+ function normalizeString(str) {
1648
+ return str.normalize('NFD') // Decompose accented characters
1649
+ .replace(/[\u0300-\u036f]/g, '') // Remove diacritical marks
1650
+ .toLowerCase() // Convert to lowercase
1651
+ .replace(/\s+/g, '-') // Replace multiple spaces with a single space
1652
+ .trim(); // Trim leading and trailing spaces
1653
+ }
1654
+
1655
+ //
1656
+ // Hook module
1657
+ //
1658
+ function hookFn(fn, _a) {
1659
+ var input = _a.input,
1660
+ validate = _a.validate,
1661
+ before = _a.before,
1662
+ output = _a.output,
1663
+ after = _a.after;
1664
+ return function () {
1665
+ var args = [];
1666
+ for (var _i = 0; _i < arguments.length; _i++) {
1667
+ args[_i] = arguments[_i];
1668
+ }
1669
+ return __awaiter(this, void 0, void 0, function () {
1670
+ var _input, _a, validationResult, error, rawResult, result, _b;
1671
+ return __generator(this, function (_c) {
1672
+ switch (_c.label) {
1673
+ case 0:
1674
+ if (!(typeof input === 'function')) return [3 /*break*/, 2];
1675
+ return [4 /*yield*/, input.apply(void 0, args)];
1676
+ case 1:
1677
+ _a = _c.sent();
1678
+ return [3 /*break*/, 3];
1679
+ case 2:
1680
+ _a = args;
1681
+ _c.label = 3;
1682
+ case 3:
1683
+ _input = _a;
1684
+ if (!Array.isArray(_input)) {
1685
+ throw new TypeError('Input hook must resolve an array of args');
1686
+ }
1687
+ if (!(typeof validate === 'function')) return [3 /*break*/, 5];
1688
+ return [4 /*yield*/, validate.apply(void 0, _input)];
1689
+ case 4:
1690
+ validationResult = _c.sent();
1691
+ if (validationResult !== true) {
1692
+ error = typeof validationResult === 'string' ? new Error(validationResult) : validationResult instanceof Error ? validationResult : new Error('Invalid input');
1693
+ throw error;
1694
+ }
1695
+ _c.label = 5;
1696
+ case 5:
1697
+ if (!(typeof before === 'function')) return [3 /*break*/, 7];
1698
+ return [4 /*yield*/, before.apply(void 0, _input)];
1699
+ case 6:
1700
+ _c.sent();
1701
+ _c.label = 7;
1702
+ case 7:
1703
+ return [4 /*yield*/, fn.apply(void 0, _input)];
1704
+ case 8:
1705
+ rawResult = _c.sent();
1706
+ if (!(typeof output === 'function')) return [3 /*break*/, 10];
1707
+ return [4 /*yield*/, output(rawResult)];
1708
+ case 9:
1709
+ _b = _c.sent();
1710
+ return [3 /*break*/, 11];
1711
+ case 10:
1712
+ _b = rawResult;
1713
+ _c.label = 11;
1714
+ case 11:
1715
+ result = _b;
1716
+ if (!(typeof after === 'function')) return [3 /*break*/, 13];
1717
+ return [4 /*yield*/, after.apply(void 0, __spreadArray([result], _input, false))];
1718
+ case 12:
1719
+ _c.sent();
1720
+ _c.label = 13;
1721
+ case 13:
1722
+ return [2 /*return*/, result];
1723
+ }
1724
+ });
1725
+ });
1726
+ };
1727
+ }
1728
+
1729
+ export { PromiseLikeEventEmitter, SKIPPED, TimeoutError, ValidationError, batchFn, debugFn, deepFreeze, fetchAllPages, hookFn, inMemoryDataStore, interpolate, makeDeferred, makeTypeOf, maybeFn, normalizeString, paginatedHttpFetch, parseBatchedResults, pickPaths, promiseReduce, resolveNestedPromises, switchExec, switchValue, typeMap, typeOf, validate, validateAsync, wait, withTimeout };
@@ -0,0 +1 @@
1
+ export declare function normalizeString(str: string): string;
@@ -2,3 +2,4 @@ export * from './promiseReduce';
2
2
  export * from './resolveNestedPromises';
3
3
  export * from './batchFn';
4
4
  export * from './withTimeout';
5
+ export * from './makeDeferred';
@@ -0,0 +1,6 @@
1
+ export interface Deferred<T> {
2
+ promise: Promise<T>;
3
+ resolve: (value: T | PromiseLike<T>) => void;
4
+ reject: (reason?: any) => void;
5
+ }
6
+ export declare function makeDeferred<T>(): Deferred<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orioro/util",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "packageManager": "yarn@4.0.2",
5
5
  "type": "module",
6
6
  "main": "dist/index.mjs",