@hairy/utils 1.39.0 → 1.39.1

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.js CHANGED
@@ -3139,190 +3139,6 @@ function splitPrefixSuffix(input, options = {}) {
3139
3139
 
3140
3140
  // src/number/index.ts
3141
3141
  import Bignumber from "bignumber.js";
3142
- var BIG_INTS = {
3143
- t: { v: 10 ** 12, d: 13, n: "t" },
3144
- b: { v: 10 ** 9, d: 10, n: "b" },
3145
- m: { v: 10 ** 6, d: 7, n: "m" },
3146
- k: { v: 10 ** 3, d: 4, n: "k" }
3147
- };
3148
- function bignum(n = "0") {
3149
- return new Bignumber(numfix(n));
3150
- }
3151
- function numfix(value) {
3152
- return Number.isNaN(Number(value)) || value.toString() === "NaN" ? "0" : String(value);
3153
- }
3154
- function gte(a, b) {
3155
- return bignum(a).gte(bignum(b));
3156
- }
3157
- function gt(a, b) {
3158
- return bignum(a).gt(bignum(b));
3159
- }
3160
- function lte(a, b) {
3161
- return bignum(a).lte(bignum(b));
3162
- }
3163
- function lt(a, b) {
3164
- return bignum(a).lt(bignum(b));
3165
- }
3166
- function plus(array, options) {
3167
- const rounding = options?.r || Bignumber.ROUND_DOWN;
3168
- const decimal2 = options?.d || 0;
3169
- return array.filter((v) => bignum(v).gt(0)).reduce((t, v) => t.plus(bignum(v)), bignum(0)).toFixed(decimal2, rounding);
3170
- }
3171
- function average(array, options) {
3172
- const rounding = options?.r || Bignumber.ROUND_DOWN;
3173
- const decimal2 = options?.d || 0;
3174
- if (array.length === 0)
3175
- return "0";
3176
- return bignum(plus(array)).div(array.length).toFixed(decimal2, rounding);
3177
- }
3178
- function percentage(total, count, options) {
3179
- options ??= { d: 3, r: Bignumber.ROUND_DOWN };
3180
- const rounding = options?.r || Bignumber.ROUND_DOWN;
3181
- const decimal2 = options?.d || 3;
3182
- if (bignum(total).lte(0) || bignum(count).lte(0))
3183
- return "0";
3184
- return bignum(count).div(bignum(total)).times(100).toFixed(decimal2, rounding);
3185
- }
3186
- function zerofill(value, n = 2, type = "positive") {
3187
- const _value = integer(value);
3188
- if (_value.length >= n)
3189
- return value;
3190
- const zero = "0".repeat(n - _value.length);
3191
- if (type === "positive")
3192
- return zero + value;
3193
- if (type === "reverse")
3194
- return zero + value;
3195
- return "";
3196
- }
3197
- function zeromove(value) {
3198
- return value.toString().replace(/\.?0+$/, "");
3199
- }
3200
- function integer(value) {
3201
- return new Bignumber(numfix(value)).toFixed(0);
3202
- }
3203
- function decimal(value, n = 2) {
3204
- let [integer2, decimal2] = numfix(value).split(".");
3205
- if (n <= 0)
3206
- return integer2;
3207
- if (!decimal2)
3208
- decimal2 = "0";
3209
- decimal2 = decimal2.slice(0, n);
3210
- decimal2 = decimal2 + "0".repeat(n - decimal2.length);
3211
- return `${integer2}.${decimal2}`;
3212
- }
3213
- function parseNumeric(num, delimiters = ["t", "b", "m"]) {
3214
- const mappings = [
3215
- delimiters.includes("t") && ((n) => gte(n, BIG_INTS.t.v) && BIG_INTS.t),
3216
- delimiters.includes("b") && ((n) => gte(n, BIG_INTS.b.v) && lt(n, BIG_INTS.t.v) && BIG_INTS.b),
3217
- delimiters.includes("m") && ((n) => gte(n, BIG_INTS.m.v) && lt(n, BIG_INTS.b.v) && BIG_INTS.m),
3218
- delimiters.includes("k") && ((n) => gte(n, BIG_INTS.k.v) && lt(n, BIG_INTS.m.v) && BIG_INTS.k)
3219
- ];
3220
- let options;
3221
- for (const analy of mappings) {
3222
- const opts = analy && analy(bignum(num).toFixed(0));
3223
- opts && (options = opts);
3224
- }
3225
- return options || { v: 1, d: 0, n: "" };
3226
- }
3227
- function formatNumeric(value = "0", options) {
3228
- if (options?.default && bignum(value).isZero())
3229
- return options?.default;
3230
- const {
3231
- rounding = Bignumber.ROUND_DOWN,
3232
- delimiters,
3233
- format,
3234
- decimals = 2
3235
- } = options || {};
3236
- const config = parseNumeric(value, delimiters || []);
3237
- let number = bignum(value).div(config.v).toFormat(decimals, rounding, {
3238
- decimalSeparator: ".",
3239
- groupSeparator: ",",
3240
- groupSize: 3,
3241
- secondaryGroupSize: 0,
3242
- fractionGroupSeparator: " ",
3243
- fractionGroupSize: 0,
3244
- ...format
3245
- });
3246
- number = options?.zeromove ? zeromove(number) : number;
3247
- return `${number}${config.n}`;
3248
- }
3249
-
3250
- // src/size/index.ts
3251
- function formatUnit(value, unit = "px") {
3252
- if (!(isString_default(value) || isNumber_default(value)))
3253
- return "";
3254
- value = String(value);
3255
- return /\D/.test(value) ? value : value + unit;
3256
- }
3257
- function formatSize(dimension, unit) {
3258
- const _formatUnit = (value) => formatUnit(value, unit);
3259
- if (typeof dimension === "string" || typeof dimension === "number")
3260
- return { width: _formatUnit(dimension), height: _formatUnit(dimension) };
3261
- if (Array.isArray(dimension))
3262
- return { width: _formatUnit(dimension[0]), height: _formatUnit(dimension[1]) };
3263
- if (typeof dimension === "object")
3264
- return { width: _formatUnit(dimension.width), height: _formatUnit(dimension.height) };
3265
- return { width: "", height: "" };
3266
- }
3267
-
3268
- // src/string/index.ts
3269
- function cover(value, mode, symbol = "*") {
3270
- return value.slice(0, mode[0]) + symbol.repeat(mode[1]) + value.slice(-mode[2]);
3271
- }
3272
- function slash(str) {
3273
- return str.replace(/\\/g, "/");
3274
- }
3275
- function ensurePrefix(prefix, str) {
3276
- if (!str.startsWith(prefix))
3277
- return prefix + str;
3278
- return str;
3279
- }
3280
- function ensureSuffix(suffix, str) {
3281
- if (!str.endsWith(suffix))
3282
- return str + suffix;
3283
- return str;
3284
- }
3285
- function template(str, ...args) {
3286
- const [firstArg, fallback] = args;
3287
- if (isObject_default(firstArg)) {
3288
- const vars = firstArg;
3289
- return str.replace(/\{(\w+)\}/g, (_, key) => vars[key] || ((typeof fallback === "function" ? fallback(key) : fallback) ?? key));
3290
- } else {
3291
- return str.replace(/\{(\d+)\}/g, (_, key) => {
3292
- const index = Number(key);
3293
- if (Number.isNaN(index))
3294
- return key;
3295
- return args[index];
3296
- });
3297
- }
3298
- }
3299
- var _reFullWs = /^\s*$/;
3300
- function unindent(str) {
3301
- const lines = (typeof str === "string" ? str : str[0]).split("\n");
3302
- const whitespaceLines = lines.map((line) => _reFullWs.test(line));
3303
- const commonIndent = lines.reduce((min, line, idx) => {
3304
- if (whitespaceLines[idx])
3305
- return min;
3306
- const indent = line.match(/^\s*/)?.[0].length;
3307
- return indent === void 0 ? min : Math.min(min, indent);
3308
- }, Number.POSITIVE_INFINITY);
3309
- let emptyLinesHead = 0;
3310
- while (emptyLinesHead < lines.length && whitespaceLines[emptyLinesHead])
3311
- emptyLinesHead++;
3312
- let emptyLinesTail = 0;
3313
- while (emptyLinesTail < lines.length && whitespaceLines[lines.length - emptyLinesTail - 1])
3314
- emptyLinesTail++;
3315
- return lines.slice(emptyLinesHead, lines.length - emptyLinesTail).map((line) => line.slice(commonIndent)).join("\n");
3316
- }
3317
-
3318
- // src/typeof/index.ts
3319
- function getTypeof(target) {
3320
- const value = Object.prototype.toString.call(target).slice(8, -1).toLocaleLowerCase();
3321
- return value;
3322
- }
3323
- function isTypeof(target, type) {
3324
- return getTypeof(target) === type;
3325
- }
3326
3142
 
3327
3143
  // src/util/compose-promise.ts
3328
3144
  function pCompose(...fns) {
@@ -3344,13 +3160,30 @@ function compose(...fns) {
3344
3160
  compose.promise = pCompose;
3345
3161
 
3346
3162
  // src/util/converts.ts
3347
- function formToObject(formData) {
3163
+ function objectFromFormdata(formData) {
3348
3164
  return Object.fromEntries(formData.entries());
3349
3165
  }
3350
- function objectToForm(object) {
3351
- const formData = new FormData();
3352
- for (const [key, value] of Object.entries(object)) formData.append(key, value);
3353
- return formData;
3166
+ function formdataFromObject(object) {
3167
+ const formdata = new FormData();
3168
+ for (const [key, value] of Object.entries(object))
3169
+ formdata.set(key, value);
3170
+ return formdata;
3171
+ }
3172
+ function nonnanable(value) {
3173
+ return Number.isNaN(Number(value)) ? void 0 : value;
3174
+ }
3175
+ function numberify(value) {
3176
+ return Number.isNaN(Number(value)) ? 0 : Number(value);
3177
+ }
3178
+ function stringify(value) {
3179
+ return String(value);
3180
+ }
3181
+ function numberish(value) {
3182
+ if (value === void 0 || value === null)
3183
+ return "0";
3184
+ if (Number.isNaN(Number(value)))
3185
+ return "0";
3186
+ return value.toString();
3354
3187
  }
3355
3188
 
3356
3189
  // src/util/noop.ts
@@ -3493,6 +3326,13 @@ async function to(promise, error) {
3493
3326
  });
3494
3327
  }
3495
3328
 
3329
+ // src/util/to-array.ts
3330
+ function toArray(value) {
3331
+ if (!value)
3332
+ return void 0;
3333
+ return Array.isArray(value) ? value : [value];
3334
+ }
3335
+
3496
3336
  // src/util/util.ts
3497
3337
  function arange(x1, x2, stp = 1, z = [], z0 = z.length) {
3498
3338
  if (!x2)
@@ -3517,13 +3357,209 @@ function whenever(value, callback) {
3517
3357
  function call(fn, ...args) {
3518
3358
  return fn(...args);
3519
3359
  }
3360
+
3361
+ // src/number/index.ts
3362
+ var DEFAULT_BIGNUM_CONFIG = {
3363
+ ROUNDING_MODE: Bignumber.ROUND_UP,
3364
+ DECIMAL_PLACES: 6
3365
+ };
3366
+ var BignumberCLONE = Bignumber.clone(DEFAULT_BIGNUM_CONFIG);
3367
+ var BIG_INTS = {
3368
+ t: { v: 10 ** 12, d: 13, n: "t" },
3369
+ b: { v: 10 ** 9, d: 10, n: "b" },
3370
+ m: { v: 10 ** 6, d: 7, n: "m" },
3371
+ k: { v: 10 ** 3, d: 4, n: "k" }
3372
+ };
3373
+ function bignumber(n = "0", base) {
3374
+ return new BignumberCLONE(numberish(n), base);
3375
+ }
3376
+ bignumber.clone = function(config) {
3377
+ return Bignumber.clone({ ...DEFAULT_BIGNUM_CONFIG, ...config });
3378
+ };
3379
+ function gte(a, b) {
3380
+ return bignumber(a).gte(bignumber(b));
3381
+ }
3382
+ function gt(a, b) {
3383
+ return bignumber(a).gt(bignumber(b));
3384
+ }
3385
+ function lte(a, b) {
3386
+ return bignumber(a).lte(bignumber(b));
3387
+ }
3388
+ function lt(a, b) {
3389
+ return bignumber(a).lt(bignumber(b));
3390
+ }
3391
+ function plus(array, options) {
3392
+ const rounding = options?.r || Bignumber.ROUND_DOWN;
3393
+ const decimal2 = options?.d || 0;
3394
+ return array.filter((v) => bignumber(v).gt(0)).reduce((t, v) => t.plus(bignumber(v)), bignumber(0)).toFixed(decimal2, rounding);
3395
+ }
3396
+ function divs(array, options) {
3397
+ const rounding = options?.r || Bignumber.ROUND_DOWN;
3398
+ const decimal2 = options?.d || 0;
3399
+ return array.reduce((t, v) => t.div(bignumber(v)), bignumber(1)).toFixed(decimal2, rounding);
3400
+ }
3401
+ function average(array, options) {
3402
+ const rounding = options?.r || Bignumber.ROUND_DOWN;
3403
+ const decimal2 = options?.d || 0;
3404
+ if (array.length === 0)
3405
+ return "0";
3406
+ return bignumber(plus(array)).div(array.length).toFixed(decimal2, rounding);
3407
+ }
3408
+ function percentage(total, count, options) {
3409
+ options ??= { d: 3, r: Bignumber.ROUND_DOWN };
3410
+ const rounding = options?.r || Bignumber.ROUND_DOWN;
3411
+ const decimal2 = options?.d || 3;
3412
+ if (bignumber(total).lte(0) || bignumber(count).lte(0))
3413
+ return "0";
3414
+ return bignumber(count).div(bignumber(total)).times(100).toFixed(decimal2, rounding);
3415
+ }
3416
+ function zerofill(value, n = 2, type = "positive") {
3417
+ const _value = integer(value);
3418
+ if (_value.length >= n)
3419
+ return value;
3420
+ const zero = "0".repeat(n - _value.length);
3421
+ if (type === "positive")
3422
+ return zero + value;
3423
+ if (type === "reverse")
3424
+ return zero + value;
3425
+ return "";
3426
+ }
3427
+ function zeromove(value) {
3428
+ return numberish(value).toString().replace(/\.?0+$/, "");
3429
+ }
3430
+ function integer(value) {
3431
+ return new Bignumber(numberish(value)).toFixed(0);
3432
+ }
3433
+ function decimal(value, n = 2) {
3434
+ let [integer2, decimal2] = numberish(value).split(".");
3435
+ if (n <= 0)
3436
+ return integer2;
3437
+ if (!decimal2)
3438
+ decimal2 = "0";
3439
+ decimal2 = decimal2.slice(0, n);
3440
+ decimal2 = decimal2 + "0".repeat(n - decimal2.length);
3441
+ return `${integer2}.${decimal2}`;
3442
+ }
3443
+ function parseNumeric(num, delimiters = ["t", "b", "m"]) {
3444
+ const mappings = [
3445
+ delimiters.includes("t") && ((n) => gte(n, BIG_INTS.t.v) && BIG_INTS.t),
3446
+ delimiters.includes("b") && ((n) => gte(n, BIG_INTS.b.v) && lt(n, BIG_INTS.t.v) && BIG_INTS.b),
3447
+ delimiters.includes("m") && ((n) => gte(n, BIG_INTS.m.v) && lt(n, BIG_INTS.b.v) && BIG_INTS.m),
3448
+ delimiters.includes("k") && ((n) => gte(n, BIG_INTS.k.v) && lt(n, BIG_INTS.m.v) && BIG_INTS.k)
3449
+ ];
3450
+ let options;
3451
+ for (const analy of mappings) {
3452
+ const opts = analy && analy(bignumber(num).toFixed(0));
3453
+ opts && (options = opts);
3454
+ }
3455
+ return options || { v: 1, d: 0, n: "" };
3456
+ }
3457
+ function formatNumeric(value = "0", options) {
3458
+ if (options?.default && bignumber(value).isZero())
3459
+ return options?.default;
3460
+ const {
3461
+ rounding = Bignumber.ROUND_DOWN,
3462
+ delimiters,
3463
+ format,
3464
+ decimals = 2
3465
+ } = options || {};
3466
+ const config = parseNumeric(value, delimiters || []);
3467
+ let number = bignumber(value).div(config.v).toFormat(decimals, rounding, {
3468
+ decimalSeparator: ".",
3469
+ groupSeparator: ",",
3470
+ groupSize: 3,
3471
+ secondaryGroupSize: 0,
3472
+ fractionGroupSeparator: " ",
3473
+ fractionGroupSize: 0,
3474
+ ...format
3475
+ });
3476
+ number = options?.zeromove ? zeromove(number) : number;
3477
+ return `${number}${config.n}`;
3478
+ }
3479
+
3480
+ // src/size/index.ts
3481
+ function formatUnit(value, unit = "px") {
3482
+ if (!(isString_default(value) || isNumber_default(value)))
3483
+ return "";
3484
+ value = String(value);
3485
+ return /\D/.test(value) ? value : value + unit;
3486
+ }
3487
+ function formatSize(dimension, unit) {
3488
+ const _formatUnit = (value) => formatUnit(value, unit);
3489
+ if (typeof dimension === "string" || typeof dimension === "number")
3490
+ return { width: _formatUnit(dimension), height: _formatUnit(dimension) };
3491
+ if (Array.isArray(dimension))
3492
+ return { width: _formatUnit(dimension[0]), height: _formatUnit(dimension[1]) };
3493
+ if (typeof dimension === "object")
3494
+ return { width: _formatUnit(dimension.width), height: _formatUnit(dimension.height) };
3495
+ return { width: "", height: "" };
3496
+ }
3497
+
3498
+ // src/string/index.ts
3499
+ function cover(value, mode, symbol = "*") {
3500
+ return value.slice(0, mode[0]) + symbol.repeat(mode[1]) + value.slice(-mode[2]);
3501
+ }
3502
+ function slash(str) {
3503
+ return str.replace(/\\/g, "/");
3504
+ }
3505
+ function ensurePrefix(prefix, str) {
3506
+ if (!str.startsWith(prefix))
3507
+ return prefix + str;
3508
+ return str;
3509
+ }
3510
+ function ensureSuffix(suffix, str) {
3511
+ if (!str.endsWith(suffix))
3512
+ return str + suffix;
3513
+ return str;
3514
+ }
3515
+ function template(str, ...args) {
3516
+ const [firstArg, fallback] = args;
3517
+ if (isObject_default(firstArg)) {
3518
+ const vars = firstArg;
3519
+ return str.replace(/\{(\w+)\}/g, (_, key) => vars[key] || ((typeof fallback === "function" ? fallback(key) : fallback) ?? key));
3520
+ } else {
3521
+ return str.replace(/\{(\d+)\}/g, (_, key) => {
3522
+ const index = Number(key);
3523
+ if (Number.isNaN(index))
3524
+ return key;
3525
+ return args[index];
3526
+ });
3527
+ }
3528
+ }
3529
+ var _reFullWs = /^\s*$/;
3530
+ function unindent(str) {
3531
+ const lines = (typeof str === "string" ? str : str[0]).split("\n");
3532
+ const whitespaceLines = lines.map((line) => _reFullWs.test(line));
3533
+ const commonIndent = lines.reduce((min, line, idx) => {
3534
+ if (whitespaceLines[idx])
3535
+ return min;
3536
+ const indent = line.match(/^\s*/)?.[0].length;
3537
+ return indent === void 0 ? min : Math.min(min, indent);
3538
+ }, Number.POSITIVE_INFINITY);
3539
+ let emptyLinesHead = 0;
3540
+ while (emptyLinesHead < lines.length && whitespaceLines[emptyLinesHead])
3541
+ emptyLinesHead++;
3542
+ let emptyLinesTail = 0;
3543
+ while (emptyLinesTail < lines.length && whitespaceLines[lines.length - emptyLinesTail - 1])
3544
+ emptyLinesTail++;
3545
+ return lines.slice(emptyLinesHead, lines.length - emptyLinesTail).map((line) => line.slice(commonIndent)).join("\n");
3546
+ }
3547
+
3548
+ // src/typeof/index.ts
3549
+ function getTypeof(target) {
3550
+ const value = Object.prototype.toString.call(target).slice(8, -1).toLocaleLowerCase();
3551
+ return value;
3552
+ }
3553
+ function isTypeof(target, type) {
3554
+ return getTypeof(target) === type;
3555
+ }
3520
3556
  export {
3521
3557
  BIG_INTS,
3522
- Bignumber,
3558
+ DEFAULT_BIGNUM_CONFIG,
3523
3559
  Deferred,
3524
3560
  arange,
3525
3561
  average,
3526
- bignum,
3562
+ bignumber,
3527
3563
  call,
3528
3564
  camelCase,
3529
3565
  capitalCase,
@@ -3539,16 +3575,18 @@ export {
3539
3575
  decimal,
3540
3576
  delay,
3541
3577
  dialsPhone,
3578
+ divs,
3542
3579
  dotCase,
3543
3580
  downloadBlobFile,
3544
3581
  downloadNetworkFile,
3545
3582
  ensurePrefix,
3546
3583
  ensureSuffix,
3547
3584
  find_default as find,
3548
- formToObject,
3549
3585
  formatNumeric,
3550
3586
  formatSize,
3551
3587
  formatUnit,
3588
+ formdataFromObject,
3589
+ get_default as get,
3552
3590
  getTypeof,
3553
3591
  groupBy_default as groupBy,
3554
3592
  gt,
@@ -3613,9 +3651,11 @@ export {
3613
3651
  merge_default as merge,
3614
3652
  mergeWith_default as mergeWith,
3615
3653
  noCase,
3654
+ nonnanable,
3616
3655
  noop2 as noop,
3617
- numfix,
3618
- objectToForm,
3656
+ numberify,
3657
+ numberish,
3658
+ objectFromFormdata,
3619
3659
  off,
3620
3660
  on,
3621
3661
  parseNumeric,
@@ -3639,8 +3679,10 @@ export {
3639
3679
  showOpenImagePicker,
3640
3680
  slash,
3641
3681
  snakeCase,
3682
+ stringify,
3642
3683
  template,
3643
3684
  to,
3685
+ toArray,
3644
3686
  trainCase,
3645
3687
  truncate_default as truncate,
3646
3688
  unindent,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hairy/utils",
3
3
  "type": "module",
4
- "version": "1.39.0",
4
+ "version": "1.39.1",
5
5
  "description": "Library for anywhere",
6
6
  "author": "Hairyf <wwu710632@gmail.com>",
7
7
  "license": "MIT",