@jiakun-zhao/utils 1.3.2 → 1.3.3

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
@@ -1,6 +1,11 @@
1
1
  'use strict';
2
2
 
3
3
  const utils = require('@antfu/utils');
4
+ const naturalCompare = require('natural-compare');
5
+
6
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
7
+
8
+ const naturalCompare__default = /*#__PURE__*/_interopDefaultCompat(naturalCompare);
4
9
 
5
10
  const isArray = Array.isArray;
6
11
  function singleOrNull(arr) {
@@ -10,19 +15,6 @@ function createArray(length, mapFn) {
10
15
  return Array.from({ length }, (_, index) => mapFn?.(index) ?? index);
11
16
  }
12
17
 
13
- function safe(fn) {
14
- try {
15
- const res = fn();
16
- return res instanceof Promise ? res.catch(() => null) : res;
17
- } catch {
18
- return null;
19
- }
20
- }
21
-
22
- function random(max, min = 0) {
23
- return Math.floor(Math.random() * max) + min;
24
- }
25
-
26
18
  function cleanText(str, searchValue) {
27
19
  return str.replace(searchValue, "");
28
20
  }
@@ -30,13 +22,19 @@ function cleanText(str, searchValue) {
30
22
  function getValueOrUndefined(condition, value) {
31
23
  return condition ? value : void 0;
32
24
  }
25
+ function from(value) {
26
+ return {
27
+ to: (fn) => from(fn(value)),
28
+ get: () => value
29
+ };
30
+ }
33
31
 
32
+ exports.naturalCompare = naturalCompare__default;
34
33
  exports.cleanText = cleanText;
35
34
  exports.createArray = createArray;
35
+ exports.from = from;
36
36
  exports.getValueOrUndefined = getValueOrUndefined;
37
37
  exports.isArray = isArray;
38
- exports.random = random;
39
- exports.safe = safe;
40
38
  exports.singleOrNull = singleOrNull;
41
39
  Object.prototype.hasOwnProperty.call(utils, '__proto__') &&
42
40
  !Object.prototype.hasOwnProperty.call(exports, '__proto__') &&
package/dist/index.d.cts CHANGED
@@ -1,18 +1,46 @@
1
1
  export * from '@antfu/utils';
2
+ export { default as naturalCompare } from 'natural-compare';
2
3
 
3
4
  declare const isArray: (arg: any) => arg is any[];
4
5
  declare function singleOrNull<T>(arr: T[]): T | null;
5
6
  declare function createArray<T = number>(length: number, mapFn?: (index: number) => T): T[];
6
7
 
7
- declare function safe<R>(fn: () => R): typeof fn extends () => Promise<infer T> ? Promise<T | null> : (R | null);
8
-
9
- /**
10
- * @description min <= result < max
11
- */
12
- declare function random(max: number, min?: number): number;
13
-
14
8
  declare function cleanText(str: string, searchValue: RegExp | string): string;
15
9
 
16
10
  declare function getValueOrUndefined<T>(condition: any, value: T): T | undefined;
11
+ declare function from<T>(value: T): {
12
+ to: <R>(fn: (value: T) => R) => {
13
+ to: <R_1>(fn: (value: R) => R_1) => {
14
+ to: <R_2>(fn: (value: R_1) => R_2) => {
15
+ to: <R_3>(fn: (value: R_2) => R_3) => {
16
+ to: <R_4>(fn: (value: R_3) => R_4) => {
17
+ to: <R_5>(fn: (value: R_4) => R_5) => {
18
+ to: <R_6>(fn: (value: R_5) => R_6) => {
19
+ to: <R_7>(fn: (value: R_6) => R_7) => {
20
+ to: <R_8>(fn: (value: R_7) => R_8) => {
21
+ to: <R_9>(fn: (value: R_8) => R_9) => {
22
+ to: <R_10>(fn: (value: R_9) => R_10) => /*elided*/ any;
23
+ get: () => R_9;
24
+ };
25
+ get: () => R_8;
26
+ };
27
+ get: () => R_7;
28
+ };
29
+ get: () => R_6;
30
+ };
31
+ get: () => R_5;
32
+ };
33
+ get: () => R_4;
34
+ };
35
+ get: () => R_3;
36
+ };
37
+ get: () => R_2;
38
+ };
39
+ get: () => R_1;
40
+ };
41
+ get: () => R;
42
+ };
43
+ get: () => T;
44
+ };
17
45
 
18
- export { cleanText, createArray, getValueOrUndefined, isArray, random, safe, singleOrNull };
46
+ export { cleanText, createArray, from, getValueOrUndefined, isArray, singleOrNull };
package/dist/index.d.mts CHANGED
@@ -1,18 +1,46 @@
1
1
  export * from '@antfu/utils';
2
+ export { default as naturalCompare } from 'natural-compare';
2
3
 
3
4
  declare const isArray: (arg: any) => arg is any[];
4
5
  declare function singleOrNull<T>(arr: T[]): T | null;
5
6
  declare function createArray<T = number>(length: number, mapFn?: (index: number) => T): T[];
6
7
 
7
- declare function safe<R>(fn: () => R): typeof fn extends () => Promise<infer T> ? Promise<T | null> : (R | null);
8
-
9
- /**
10
- * @description min <= result < max
11
- */
12
- declare function random(max: number, min?: number): number;
13
-
14
8
  declare function cleanText(str: string, searchValue: RegExp | string): string;
15
9
 
16
10
  declare function getValueOrUndefined<T>(condition: any, value: T): T | undefined;
11
+ declare function from<T>(value: T): {
12
+ to: <R>(fn: (value: T) => R) => {
13
+ to: <R_1>(fn: (value: R) => R_1) => {
14
+ to: <R_2>(fn: (value: R_1) => R_2) => {
15
+ to: <R_3>(fn: (value: R_2) => R_3) => {
16
+ to: <R_4>(fn: (value: R_3) => R_4) => {
17
+ to: <R_5>(fn: (value: R_4) => R_5) => {
18
+ to: <R_6>(fn: (value: R_5) => R_6) => {
19
+ to: <R_7>(fn: (value: R_6) => R_7) => {
20
+ to: <R_8>(fn: (value: R_7) => R_8) => {
21
+ to: <R_9>(fn: (value: R_8) => R_9) => {
22
+ to: <R_10>(fn: (value: R_9) => R_10) => /*elided*/ any;
23
+ get: () => R_9;
24
+ };
25
+ get: () => R_8;
26
+ };
27
+ get: () => R_7;
28
+ };
29
+ get: () => R_6;
30
+ };
31
+ get: () => R_5;
32
+ };
33
+ get: () => R_4;
34
+ };
35
+ get: () => R_3;
36
+ };
37
+ get: () => R_2;
38
+ };
39
+ get: () => R_1;
40
+ };
41
+ get: () => R;
42
+ };
43
+ get: () => T;
44
+ };
17
45
 
18
- export { cleanText, createArray, getValueOrUndefined, isArray, random, safe, singleOrNull };
46
+ export { cleanText, createArray, from, getValueOrUndefined, isArray, singleOrNull };
package/dist/index.d.ts CHANGED
@@ -1,18 +1,46 @@
1
1
  export * from '@antfu/utils';
2
+ export { default as naturalCompare } from 'natural-compare';
2
3
 
3
4
  declare const isArray: (arg: any) => arg is any[];
4
5
  declare function singleOrNull<T>(arr: T[]): T | null;
5
6
  declare function createArray<T = number>(length: number, mapFn?: (index: number) => T): T[];
6
7
 
7
- declare function safe<R>(fn: () => R): typeof fn extends () => Promise<infer T> ? Promise<T | null> : (R | null);
8
-
9
- /**
10
- * @description min <= result < max
11
- */
12
- declare function random(max: number, min?: number): number;
13
-
14
8
  declare function cleanText(str: string, searchValue: RegExp | string): string;
15
9
 
16
10
  declare function getValueOrUndefined<T>(condition: any, value: T): T | undefined;
11
+ declare function from<T>(value: T): {
12
+ to: <R>(fn: (value: T) => R) => {
13
+ to: <R_1>(fn: (value: R) => R_1) => {
14
+ to: <R_2>(fn: (value: R_1) => R_2) => {
15
+ to: <R_3>(fn: (value: R_2) => R_3) => {
16
+ to: <R_4>(fn: (value: R_3) => R_4) => {
17
+ to: <R_5>(fn: (value: R_4) => R_5) => {
18
+ to: <R_6>(fn: (value: R_5) => R_6) => {
19
+ to: <R_7>(fn: (value: R_6) => R_7) => {
20
+ to: <R_8>(fn: (value: R_7) => R_8) => {
21
+ to: <R_9>(fn: (value: R_8) => R_9) => {
22
+ to: <R_10>(fn: (value: R_9) => R_10) => /*elided*/ any;
23
+ get: () => R_9;
24
+ };
25
+ get: () => R_8;
26
+ };
27
+ get: () => R_7;
28
+ };
29
+ get: () => R_6;
30
+ };
31
+ get: () => R_5;
32
+ };
33
+ get: () => R_4;
34
+ };
35
+ get: () => R_3;
36
+ };
37
+ get: () => R_2;
38
+ };
39
+ get: () => R_1;
40
+ };
41
+ get: () => R;
42
+ };
43
+ get: () => T;
44
+ };
17
45
 
18
- export { cleanText, createArray, getValueOrUndefined, isArray, random, safe, singleOrNull };
46
+ export { cleanText, createArray, from, getValueOrUndefined, isArray, singleOrNull };
package/dist/index.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from '@antfu/utils';
2
+ export { default as naturalCompare } from 'natural-compare';
2
3
 
3
4
  const isArray = Array.isArray;
4
5
  function singleOrNull(arr) {
@@ -8,19 +9,6 @@ function createArray(length, mapFn) {
8
9
  return Array.from({ length }, (_, index) => mapFn?.(index) ?? index);
9
10
  }
10
11
 
11
- function safe(fn) {
12
- try {
13
- const res = fn();
14
- return res instanceof Promise ? res.catch(() => null) : res;
15
- } catch {
16
- return null;
17
- }
18
- }
19
-
20
- function random(max, min = 0) {
21
- return Math.floor(Math.random() * max) + min;
22
- }
23
-
24
12
  function cleanText(str, searchValue) {
25
13
  return str.replace(searchValue, "");
26
14
  }
@@ -28,5 +16,11 @@ function cleanText(str, searchValue) {
28
16
  function getValueOrUndefined(condition, value) {
29
17
  return condition ? value : void 0;
30
18
  }
19
+ function from(value) {
20
+ return {
21
+ to: (fn) => from(fn(value)),
22
+ get: () => value
23
+ };
24
+ }
31
25
 
32
- export { cleanText, createArray, getValueOrUndefined, isArray, random, safe, singleOrNull };
26
+ export { cleanText, createArray, from, getValueOrUndefined, isArray, singleOrNull };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jiakun-zhao/utils",
3
3
  "type": "module",
4
- "version": "1.3.2",
4
+ "version": "1.3.3",
5
5
  "description": "Utils.",
6
6
  "author": "Jiakun Zhao <hi@zhaojiakun.com>",
7
7
  "license": "MIT",
@@ -15,9 +15,9 @@
15
15
  },
16
16
  "exports": {
17
17
  ".": {
18
- "types": "./dist/index.d.ts",
19
18
  "import": "./dist/index.mjs",
20
- "require": "./dist/index.cjs"
19
+ "require": "./dist/index.cjs",
20
+ "types": "./dist/index.d.ts"
21
21
  }
22
22
  },
23
23
  "main": "./dist/index.cjs",
@@ -27,19 +27,23 @@
27
27
  "dist"
28
28
  ],
29
29
  "dependencies": {
30
- "@antfu/utils": "^9.2.0"
30
+ "@antfu/utils": "^9.2.0",
31
+ "natural-compare": "^1.4.0"
31
32
  },
32
33
  "devDependencies": {
33
- "@antfu/eslint-config": "^4.12.0",
34
- "@types/node": "^22.15.3",
35
- "bumpp": "^10.1.0",
36
- "eslint": "^9.25.1",
34
+ "@jiakun-zhao/eslint-config": "^4.1.8",
35
+ "@types/natural-compare": "^1.4.3",
36
+ "@types/node": "^24.0.3",
37
+ "bumpp": "^10.2.0",
38
+ "eslint": "^9.29.0",
37
39
  "typescript": "^5.8.3",
38
- "unbuild": "^3.5.0"
40
+ "unbuild": "^3.5.0",
41
+ "vitest": "^3.2.4"
39
42
  },
40
43
  "scripts": {
41
44
  "build": "unbuild",
45
+ "release": "bumpp && pnpm publish && npx cnpm sync @jiakun-zhao/utils",
42
46
  "stub": "unbuild --stub",
43
- "release": "bumpp && pnpm publish && npx cnpm sync @jiakun-zhao/utils"
47
+ "test": "vitest"
44
48
  }
45
49
  }