@jiakun-zhao/utils 0.1.5 → 0.1.7
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 +10 -15
- package/dist/index.d.cts +12 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.d.ts +4 -12
- package/dist/index.mjs +8 -14
- package/package.json +6 -6
package/dist/index.cjs
CHANGED
|
@@ -1,19 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const utils = require('@antfu/utils');
|
|
4
|
-
const promises = require('node:fs/promises');
|
|
5
|
-
const node_path = require('node:path');
|
|
6
|
-
|
|
7
|
-
async function tree(dir) {
|
|
8
|
-
const arr = [];
|
|
9
|
-
const items = await promises.readdir(dir, { withFileTypes: true });
|
|
10
|
-
for (const it of items) {
|
|
11
|
-
const path = node_path.join(dir, it.name);
|
|
12
|
-
const item = it.isFile() ? { type: "file", path } : { type: "directory", path, children: await tree(path) };
|
|
13
|
-
arr.push(item);
|
|
14
|
-
}
|
|
15
|
-
return arr;
|
|
16
|
-
}
|
|
17
4
|
|
|
18
5
|
function to(value, block) {
|
|
19
6
|
return block(value);
|
|
@@ -33,13 +20,21 @@ function isNotEmpty(o) {
|
|
|
33
20
|
function singleOrNull(arr) {
|
|
34
21
|
return arr.length === 1 ? arr[0] : null;
|
|
35
22
|
}
|
|
23
|
+
function createArray(length, fn) {
|
|
24
|
+
const block = fn ? (_, idx) => fn(idx) : (_, idx) => idx;
|
|
25
|
+
return Array.from({ length }, block);
|
|
26
|
+
}
|
|
27
|
+
function random(max, min = 0) {
|
|
28
|
+
return Math.floor(Math.random() * max) + min;
|
|
29
|
+
}
|
|
36
30
|
|
|
31
|
+
exports.createArray = createArray;
|
|
37
32
|
exports.isNotEmpty = isNotEmpty;
|
|
33
|
+
exports.random = random;
|
|
38
34
|
exports.singleOrNull = singleOrNull;
|
|
39
35
|
exports.takeIf = takeIf;
|
|
40
36
|
exports.to = to;
|
|
41
|
-
exports.tree = tree;
|
|
42
37
|
exports.use = use;
|
|
43
38
|
Object.keys(utils).forEach(function (k) {
|
|
44
|
-
if (k !== 'default' && !
|
|
39
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = utils[k];
|
|
45
40
|
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from '@antfu/utils';
|
|
2
|
+
|
|
3
|
+
declare function to<T, R>(value: T, block: (it: T) => R): R;
|
|
4
|
+
declare function use<T>(value: T, block: (it: T) => void): T;
|
|
5
|
+
declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
|
|
6
|
+
declare function isNotEmpty(o: any): boolean;
|
|
7
|
+
declare function singleOrNull<T>(arr: T[]): T | null;
|
|
8
|
+
declare function createArray(length: number): number[];
|
|
9
|
+
declare function createArray<R>(length: number, fn: (index: number) => R): R[];
|
|
10
|
+
declare function random(max: number, min?: number): number;
|
|
11
|
+
|
|
12
|
+
export { createArray, isNotEmpty, random, singleOrNull, takeIf, to, use };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from '@antfu/utils';
|
|
2
|
+
|
|
3
|
+
declare function to<T, R>(value: T, block: (it: T) => R): R;
|
|
4
|
+
declare function use<T>(value: T, block: (it: T) => void): T;
|
|
5
|
+
declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
|
|
6
|
+
declare function isNotEmpty(o: any): boolean;
|
|
7
|
+
declare function singleOrNull<T>(arr: T[]): T | null;
|
|
8
|
+
declare function createArray(length: number): number[];
|
|
9
|
+
declare function createArray<R>(length: number, fn: (index: number) => R): R[];
|
|
10
|
+
declare function random(max: number, min?: number): number;
|
|
11
|
+
|
|
12
|
+
export { createArray, isNotEmpty, random, singleOrNull, takeIf, to, use };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,20 +1,12 @@
|
|
|
1
1
|
export * from '@antfu/utils';
|
|
2
2
|
|
|
3
|
-
type Tree = {
|
|
4
|
-
path: string;
|
|
5
|
-
} & ({
|
|
6
|
-
type: 'file';
|
|
7
|
-
} | {
|
|
8
|
-
type: 'directory';
|
|
9
|
-
children: Tree[];
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
declare function tree(dir: string): Promise<Tree[]>;
|
|
13
|
-
|
|
14
3
|
declare function to<T, R>(value: T, block: (it: T) => R): R;
|
|
15
4
|
declare function use<T>(value: T, block: (it: T) => void): T;
|
|
16
5
|
declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
|
|
17
6
|
declare function isNotEmpty(o: any): boolean;
|
|
18
7
|
declare function singleOrNull<T>(arr: T[]): T | null;
|
|
8
|
+
declare function createArray(length: number): number[];
|
|
9
|
+
declare function createArray<R>(length: number, fn: (index: number) => R): R[];
|
|
10
|
+
declare function random(max: number, min?: number): number;
|
|
19
11
|
|
|
20
|
-
export { isNotEmpty, singleOrNull, takeIf, to,
|
|
12
|
+
export { createArray, isNotEmpty, random, singleOrNull, takeIf, to, use };
|
package/dist/index.mjs
CHANGED
|
@@ -1,17 +1,4 @@
|
|
|
1
1
|
export * from '@antfu/utils';
|
|
2
|
-
import { readdir } from 'node:fs/promises';
|
|
3
|
-
import { join } from 'node:path';
|
|
4
|
-
|
|
5
|
-
async function tree(dir) {
|
|
6
|
-
const arr = [];
|
|
7
|
-
const items = await readdir(dir, { withFileTypes: true });
|
|
8
|
-
for (const it of items) {
|
|
9
|
-
const path = join(dir, it.name);
|
|
10
|
-
const item = it.isFile() ? { type: "file", path } : { type: "directory", path, children: await tree(path) };
|
|
11
|
-
arr.push(item);
|
|
12
|
-
}
|
|
13
|
-
return arr;
|
|
14
|
-
}
|
|
15
2
|
|
|
16
3
|
function to(value, block) {
|
|
17
4
|
return block(value);
|
|
@@ -31,5 +18,12 @@ function isNotEmpty(o) {
|
|
|
31
18
|
function singleOrNull(arr) {
|
|
32
19
|
return arr.length === 1 ? arr[0] : null;
|
|
33
20
|
}
|
|
21
|
+
function createArray(length, fn) {
|
|
22
|
+
const block = fn ? (_, idx) => fn(idx) : (_, idx) => idx;
|
|
23
|
+
return Array.from({ length }, block);
|
|
24
|
+
}
|
|
25
|
+
function random(max, min = 0) {
|
|
26
|
+
return Math.floor(Math.random() * max) + min;
|
|
27
|
+
}
|
|
34
28
|
|
|
35
|
-
export { isNotEmpty, singleOrNull, takeIf, to,
|
|
29
|
+
export { createArray, isNotEmpty, random, singleOrNull, takeIf, to, use };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jiakun-zhao/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.7",
|
|
5
5
|
"description": "Utils.",
|
|
6
6
|
"author": "Jiakun Zhao <jiakun.zhao@outlook.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -30,13 +30,13 @@
|
|
|
30
30
|
"@antfu/utils": "^0.7.6"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@jiakun-zhao/eslint-config": "^1.
|
|
34
|
-
"@types/node": "^20.
|
|
33
|
+
"@jiakun-zhao/eslint-config": "^1.3.1",
|
|
34
|
+
"@types/node": "^20.8.10",
|
|
35
35
|
"bumpp": "^9.2.0",
|
|
36
|
-
"eslint": "^8.
|
|
36
|
+
"eslint": "^8.53.0",
|
|
37
37
|
"esno": "^0.17.0",
|
|
38
|
-
"typescript": "^5.
|
|
39
|
-
"unbuild": "^
|
|
38
|
+
"typescript": "^5.2.2",
|
|
39
|
+
"unbuild": "^2.0.0"
|
|
40
40
|
},
|
|
41
41
|
"eslintConfig": {
|
|
42
42
|
"extends": "@jiakun-zhao"
|