@hairy/utils 1.26.0 → 1.28.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.
package/dist/index.cjs CHANGED
@@ -9,13 +9,13 @@ var __export = (target, all) => {
9
9
  for (var name in all)
10
10
  __defProp(target, name, { get: all[name], enumerable: true });
11
11
  };
12
- var __copyProps = (to, from, except, desc) => {
12
+ var __copyProps = (to2, from, except, desc) => {
13
13
  if (from && typeof from === "object" || typeof from === "function") {
14
14
  for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ if (!__hasOwnProp.call(to2, key) && key !== except)
16
+ __defProp(to2, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
17
  }
18
- return to;
18
+ return to2;
19
19
  };
20
20
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
21
  // If the importer is in node compatibility mode or this is not an ESM
@@ -116,6 +116,7 @@ __export(index_exports, {
116
116
  riposte: () => riposte,
117
117
  sentenceCase: () => sentenceCase,
118
118
  snakeCase: () => snakeCase,
119
+ to: () => to,
119
120
  trainCase: () => trainCase,
120
121
  truncate: () => truncate_default,
121
122
  uniq: () => uniq_default,
@@ -3235,6 +3236,30 @@ function jsonTryParse(text) {
3235
3236
  }
3236
3237
  }
3237
3238
 
3239
+ // src/util/loop.ts
3240
+ function loop_return(fn) {
3241
+ async function next(ms) {
3242
+ await delay(ms);
3243
+ return fn(next);
3244
+ }
3245
+ return fn(next);
3246
+ }
3247
+ function loop(fn) {
3248
+ let looping = true;
3249
+ async function next(ms) {
3250
+ if (!looping)
3251
+ return;
3252
+ await delay(ms);
3253
+ return fn(next);
3254
+ }
3255
+ function cancel() {
3256
+ looping = false;
3257
+ }
3258
+ fn(next);
3259
+ return cancel;
3260
+ }
3261
+ loop.return = loop_return;
3262
+
3238
3263
  // src/util/pipe-promise.ts
3239
3264
  function pPipe(...fns) {
3240
3265
  if (fns.length === 0)
@@ -3261,6 +3286,17 @@ function randomArray(array) {
3261
3286
  return array[Math.floor(Math.random() * array.length)];
3262
3287
  }
3263
3288
 
3289
+ // src/util/to.ts
3290
+ async function to(promise, error) {
3291
+ return (isFunction_default(promise) ? promise() : promise).then((data) => [null, data]).catch((err) => {
3292
+ if (error) {
3293
+ const parsedError = Object.assign({}, err, error);
3294
+ return [parsedError, void 0];
3295
+ }
3296
+ return [err, void 0];
3297
+ });
3298
+ }
3299
+
3264
3300
  // src/util/util.ts
3265
3301
  function arange(x1, x2, stp = 1, z = [], z0 = z.length) {
3266
3302
  if (!x2)
@@ -3269,13 +3305,6 @@ function arange(x1, x2, stp = 1, z = [], z0 = z.length) {
3269
3305
  z[z0++] = x1;
3270
3306
  return z;
3271
3307
  }
3272
- function loop(fn) {
3273
- async function next(ms) {
3274
- await delay(ms);
3275
- return fn(next);
3276
- }
3277
- return fn(next);
3278
- }
3279
3308
  function riposte(...args) {
3280
3309
  for (const [cond, value] of args) {
3281
3310
  if (cond)
@@ -3377,6 +3406,7 @@ function whenever(value, callback) {
3377
3406
  riposte,
3378
3407
  sentenceCase,
3379
3408
  snakeCase,
3409
+ to,
3380
3410
  trainCase,
3381
3411
  truncate,
3382
3412
  uniq,
package/dist/index.d.ts CHANGED
@@ -116,12 +116,15 @@ type DeepMerge<F, S> = MergeInsertions<{
116
116
 
117
117
  type Awaitable<T> = T | PromiseLike<T>;
118
118
  type Arrayable<T> = T | T[];
119
- type Nullable<T> = T | null | undefined;
119
+ type Promisify<T> = Promise<Awaited<T>>;
120
120
  type ElementOf<T> = T extends (infer E)[] ? E : never;
121
+ type Nullable<T> = T | null | undefined;
121
122
  type Fn$1<T = void> = () => T;
122
123
  type AnyFn = (...args: any[]) => any;
123
124
  type PromiseFn = (...args: any[]) => Promise<any>;
124
- type Constructor<T = void> = new (...args: any[]) => T;
125
+ type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N;
126
+ type IsAny<T> = IfAny<T, true, false>;
127
+ type ConstructorType<T = void> = new (...args: any[]) => T;
125
128
  type ArgumentsType<T> = T extends ((...args: infer A) => any) ? A : never;
126
129
  declare type PromiseType<P extends PromiseLike<any>> = P extends PromiseLike<infer T> ? T : never;
127
130
  type Option<L extends Key = 'label', V extends Key = 'value', C extends Key = 'children'> = {
@@ -369,6 +372,14 @@ declare const isTruthy: <T>(value: T) => value is NonNullable<T>;
369
372
 
370
373
  declare function jsonTryParse(text: string | undefined | null): any;
371
374
 
375
+ type Looper<T = void> = (next: (ms: number) => Promise<T>) => Promise<T>;
376
+ declare function loop_return<T = void>(fn: Looper<T>): Promise<T>;
377
+ declare function loop(fn: Looper<void>): Fn$1;
378
+ declare namespace loop {
379
+ var _a: typeof loop_return;
380
+ export { _a as return };
381
+ }
382
+
372
383
  declare const noop: (...args: any) => any;
373
384
 
374
385
  type UF<VT, RT> = (value: VT) => RT | PromiseLike<RT>;
@@ -451,10 +462,11 @@ declare namespace pipe {
451
462
  declare function randomNumer(min: number, max: number): number;
452
463
  declare function randomArray<T>(array: T[]): T;
453
464
 
465
+ declare function to<T, U = Error>(promise: Promise<T> | (() => Promise<T>), error?: object): Promise<[U, undefined] | [null, T]>;
466
+
454
467
  declare function arange(x1: number, x2?: number, stp?: number, z?: number[], z0?: number): number[];
455
- declare function loop<T = void>(fn: (next: (ms: number) => Promise<T>) => Promise<T>): Promise<T>;
456
468
  declare function riposte<T>(...args: [cond: boolean, value: T][]): T;
457
469
  declare function unwrap<T extends object>(value: T | (() => T)): T;
458
470
  declare function whenever<T, C extends (value: Exclude<T, null | undefined>) => any>(value: T, callback: C): ReturnType<C> | undefined;
459
471
 
460
- export { type AnyFn, type ArgumentsType, type Arrayable, type Awaitable, BIG_INTS, BigNum, type BooleanLike, type Constructor, type DecimalOptions, type DeepKeyof, type DeepMerge, type DeepPartial, type DeepReadonly, type DeepReplace, type DeepRequired, Deferred, type Delimiter, type Dimension, type DynamicObject, type ElementOf, type Fn$1 as Fn, type FormatGroupOptions, type FormatNumericOptions, type Key, type MergeInsertions, type Noop, type Nullable, type NumberObject, type Numberish, type Numeric, type NumericObject, type Option, type PromiseFn, type PromiseType, type StringObject, type SymbolObject, type Typeof, arange, average, camelCase, capitalCase, compose, constantCase, cover, decimal, delay, dotCase, formToObject, formatNumeric, formatSize, formatUnit, getTypeof, gt, gte, integer, isAndroid, isBrowser, isChrome, isEdge, isFF, isFormData, isIE, isIE11, isIE9, isIOS, isMobile, isPhantomJS, isTruthy, isTypeof, isWeex, isWindow, jsonTryParse, kebabCase, loop, lt, lte, noCase, noop, numerfix, objectToForm, parseNumeric, pascalCase, pascalSnakeCase, pathCase, percentage, pipe, plus, randomArray, randomNumer, riposte, sentenceCase, snakeCase, trainCase, unwrap, whenever, zerofill, zeromove };
472
+ export { type AnyFn, type ArgumentsType, type Arrayable, type Awaitable, BIG_INTS, BigNum, type BooleanLike, type ConstructorType, type DecimalOptions, type DeepKeyof, type DeepMerge, type DeepPartial, type DeepReadonly, type DeepReplace, type DeepRequired, Deferred, type Delimiter, type Dimension, type DynamicObject, type ElementOf, type Fn$1 as Fn, type FormatGroupOptions, type FormatNumericOptions, type IfAny, type IsAny, type Key, type Looper, type MergeInsertions, type Noop, type Nullable, type NumberObject, type Numberish, type Numeric, type NumericObject, type Option, type PromiseFn, type PromiseType, type Promisify, type StringObject, type SymbolObject, type Typeof, arange, average, camelCase, capitalCase, compose, constantCase, cover, decimal, delay, dotCase, formToObject, formatNumeric, formatSize, formatUnit, getTypeof, gt, gte, integer, isAndroid, isBrowser, isChrome, isEdge, isFF, isFormData, isIE, isIE11, isIE9, isIOS, isMobile, isPhantomJS, isTruthy, isTypeof, isWeex, isWindow, jsonTryParse, kebabCase, loop, lt, lte, noCase, noop, numerfix, objectToForm, parseNumeric, pascalCase, pascalSnakeCase, pathCase, percentage, pipe, plus, randomArray, randomNumer, riposte, sentenceCase, snakeCase, to, trainCase, unwrap, whenever, zerofill, zeromove };
@@ -4449,6 +4449,30 @@
4449
4449
  }
4450
4450
  }
4451
4451
 
4452
+ // src/util/loop.ts
4453
+ function loop_return(fn) {
4454
+ async function next(ms) {
4455
+ await delay(ms);
4456
+ return fn(next);
4457
+ }
4458
+ return fn(next);
4459
+ }
4460
+ function loop(fn) {
4461
+ let looping = true;
4462
+ async function next(ms) {
4463
+ if (!looping)
4464
+ return;
4465
+ await delay(ms);
4466
+ return fn(next);
4467
+ }
4468
+ function cancel() {
4469
+ looping = false;
4470
+ }
4471
+ fn(next);
4472
+ return cancel;
4473
+ }
4474
+ loop.return = loop_return;
4475
+
4452
4476
  // src/util/pipe-promise.ts
4453
4477
  function pPipe(...fns) {
4454
4478
  if (fns.length === 0)
@@ -4475,6 +4499,17 @@
4475
4499
  return array[Math.floor(Math.random() * array.length)];
4476
4500
  }
4477
4501
 
4502
+ // src/util/to.ts
4503
+ async function to(promise, error) {
4504
+ return (isFunction_default(promise) ? promise() : promise).then((data) => [null, data]).catch((err) => {
4505
+ if (error) {
4506
+ const parsedError = Object.assign({}, err, error);
4507
+ return [parsedError, void 0];
4508
+ }
4509
+ return [err, void 0];
4510
+ });
4511
+ }
4512
+
4478
4513
  // src/util/util.ts
4479
4514
  function arange(x1, x2, stp = 1, z = [], z0 = z.length) {
4480
4515
  if (!x2)
@@ -4483,13 +4518,6 @@
4483
4518
  z[z0++] = x1;
4484
4519
  return z;
4485
4520
  }
4486
- function loop(fn) {
4487
- async function next(ms) {
4488
- await delay(ms);
4489
- return fn(next);
4490
- }
4491
- return fn(next);
4492
- }
4493
4521
  function riposte(...args) {
4494
4522
  for (const [cond, value] of args) {
4495
4523
  if (cond)
package/dist/index.js CHANGED
@@ -3104,6 +3104,30 @@ function jsonTryParse(text) {
3104
3104
  }
3105
3105
  }
3106
3106
 
3107
+ // src/util/loop.ts
3108
+ function loop_return(fn) {
3109
+ async function next(ms) {
3110
+ await delay(ms);
3111
+ return fn(next);
3112
+ }
3113
+ return fn(next);
3114
+ }
3115
+ function loop(fn) {
3116
+ let looping = true;
3117
+ async function next(ms) {
3118
+ if (!looping)
3119
+ return;
3120
+ await delay(ms);
3121
+ return fn(next);
3122
+ }
3123
+ function cancel() {
3124
+ looping = false;
3125
+ }
3126
+ fn(next);
3127
+ return cancel;
3128
+ }
3129
+ loop.return = loop_return;
3130
+
3107
3131
  // src/util/pipe-promise.ts
3108
3132
  function pPipe(...fns) {
3109
3133
  if (fns.length === 0)
@@ -3130,6 +3154,17 @@ function randomArray(array) {
3130
3154
  return array[Math.floor(Math.random() * array.length)];
3131
3155
  }
3132
3156
 
3157
+ // src/util/to.ts
3158
+ async function to(promise, error) {
3159
+ return (isFunction_default(promise) ? promise() : promise).then((data) => [null, data]).catch((err) => {
3160
+ if (error) {
3161
+ const parsedError = Object.assign({}, err, error);
3162
+ return [parsedError, void 0];
3163
+ }
3164
+ return [err, void 0];
3165
+ });
3166
+ }
3167
+
3133
3168
  // src/util/util.ts
3134
3169
  function arange(x1, x2, stp = 1, z = [], z0 = z.length) {
3135
3170
  if (!x2)
@@ -3138,13 +3173,6 @@ function arange(x1, x2, stp = 1, z = [], z0 = z.length) {
3138
3173
  z[z0++] = x1;
3139
3174
  return z;
3140
3175
  }
3141
- function loop(fn) {
3142
- async function next(ms) {
3143
- await delay(ms);
3144
- return fn(next);
3145
- }
3146
- return fn(next);
3147
- }
3148
3176
  function riposte(...args) {
3149
3177
  for (const [cond, value] of args) {
3150
3178
  if (cond)
@@ -3245,6 +3273,7 @@ export {
3245
3273
  riposte,
3246
3274
  sentenceCase,
3247
3275
  snakeCase,
3276
+ to,
3248
3277
  trainCase,
3249
3278
  truncate_default as truncate,
3250
3279
  uniq_default as uniq,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hairy/utils",
3
3
  "type": "module",
4
- "version": "1.26.0",
4
+ "version": "1.28.0",
5
5
  "description": "Library for anywhere",
6
6
  "author": "Hairyf <wwu710632@gmail.com>",
7
7
  "license": "MIT",