@jiakun-zhao/utils 1.0.0-beta.1 → 1.0.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 +46 -9
- package/dist/index.d.cts +13 -2
- package/dist/index.d.mts +13 -2
- package/dist/index.d.ts +13 -2
- package/dist/index.mjs +35 -2
- package/dist/shared.cjs +16 -3
- package/dist/shared.d.cts +7 -2
- package/dist/shared.d.mts +7 -2
- package/dist/shared.d.ts +7 -2
- package/dist/shared.mjs +16 -4
- package/package.json +8 -7
package/dist/index.cjs
CHANGED
|
@@ -1,20 +1,57 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const utils = require('@antfu/utils');
|
|
4
|
+
const shared = require('./shared.cjs');
|
|
5
|
+
const promises = require('node:fs/promises');
|
|
6
|
+
const node_path = require('node:path');
|
|
3
7
|
const fastGlob = require('fast-glob');
|
|
4
|
-
const ohash = require('ohash');
|
|
5
|
-
const localPkg = require('local-pkg');
|
|
6
8
|
|
|
7
9
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
8
10
|
|
|
9
11
|
const fastGlob__default = /*#__PURE__*/_interopDefaultCompat(fastGlob);
|
|
10
12
|
|
|
13
|
+
async function isExists(...paths) {
|
|
14
|
+
try {
|
|
15
|
+
await promises.access(node_path.join(...paths));
|
|
16
|
+
return true;
|
|
17
|
+
} catch {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
async function exists(...paths) {
|
|
22
|
+
return await promises.readFile(node_path.join(...paths), "utf-8").then(() => true, () => false);
|
|
23
|
+
}
|
|
24
|
+
async function readText(...paths) {
|
|
25
|
+
return await promises.readFile(node_path.join(...paths), "utf-8");
|
|
26
|
+
}
|
|
27
|
+
async function readJson(...paths) {
|
|
28
|
+
return JSON.parse(await readText(...paths));
|
|
29
|
+
}
|
|
30
|
+
async function writeText(content, ...paths) {
|
|
31
|
+
await promises.writeFile(node_path.join(...paths), content, "utf-8");
|
|
32
|
+
}
|
|
33
|
+
async function writeJson(content, ...paths) {
|
|
34
|
+
await writeText(`${JSON.stringify(content, null, 2)}
|
|
35
|
+
`, ...paths);
|
|
36
|
+
}
|
|
11
37
|
|
|
38
|
+
const alias = {
|
|
39
|
+
"@jiakun-zhao/utils": "@jiakun-zhao/utils/shared"
|
|
40
|
+
};
|
|
12
41
|
|
|
42
|
+
exports.random = shared.random;
|
|
43
|
+
exports.safe = shared.safe;
|
|
44
|
+
exports.singleOrNull = shared.singleOrNull;
|
|
45
|
+
exports.take = shared.take;
|
|
46
|
+
exports.takeIf = shared.takeIf;
|
|
13
47
|
exports.glob = fastGlob__default;
|
|
14
|
-
exports.
|
|
15
|
-
exports.
|
|
16
|
-
exports.
|
|
17
|
-
exports.
|
|
18
|
-
exports.
|
|
19
|
-
exports.
|
|
20
|
-
exports.
|
|
48
|
+
exports.alias = alias;
|
|
49
|
+
exports.exists = exists;
|
|
50
|
+
exports.isExists = isExists;
|
|
51
|
+
exports.readJson = readJson;
|
|
52
|
+
exports.readText = readText;
|
|
53
|
+
exports.writeJson = writeJson;
|
|
54
|
+
exports.writeText = writeText;
|
|
55
|
+
Object.keys(utils).forEach(function (k) {
|
|
56
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = utils[k];
|
|
57
|
+
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
export * from '@antfu/utils';
|
|
2
|
+
export { random, safe, singleOrNull, take, takeIf } from './shared.cjs';
|
|
1
3
|
export { default as glob } from 'fast-glob';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
+
|
|
5
|
+
declare function isExists(...paths: string[]): Promise<boolean>;
|
|
6
|
+
declare function exists(...paths: string[]): Promise<boolean>;
|
|
7
|
+
declare function readText(...paths: string[]): Promise<string>;
|
|
8
|
+
declare function readJson<T>(...paths: string[]): Promise<T>;
|
|
9
|
+
declare function writeText(content: string, ...paths: string[]): Promise<void>;
|
|
10
|
+
declare function writeJson(content: object, ...paths: string[]): Promise<void>;
|
|
11
|
+
|
|
12
|
+
declare const alias: Record<string, string>;
|
|
13
|
+
|
|
14
|
+
export { alias, exists, isExists, readJson, readText, writeJson, writeText };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
export * from '@antfu/utils';
|
|
2
|
+
export { random, safe, singleOrNull, take, takeIf } from './shared.mjs';
|
|
1
3
|
export { default as glob } from 'fast-glob';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
+
|
|
5
|
+
declare function isExists(...paths: string[]): Promise<boolean>;
|
|
6
|
+
declare function exists(...paths: string[]): Promise<boolean>;
|
|
7
|
+
declare function readText(...paths: string[]): Promise<string>;
|
|
8
|
+
declare function readJson<T>(...paths: string[]): Promise<T>;
|
|
9
|
+
declare function writeText(content: string, ...paths: string[]): Promise<void>;
|
|
10
|
+
declare function writeJson(content: object, ...paths: string[]): Promise<void>;
|
|
11
|
+
|
|
12
|
+
declare const alias: Record<string, string>;
|
|
13
|
+
|
|
14
|
+
export { alias, exists, isExists, readJson, readText, writeJson, writeText };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
export * from '@antfu/utils';
|
|
2
|
+
export { random, safe, singleOrNull, take, takeIf } from './shared.js';
|
|
1
3
|
export { default as glob } from 'fast-glob';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
+
|
|
5
|
+
declare function isExists(...paths: string[]): Promise<boolean>;
|
|
6
|
+
declare function exists(...paths: string[]): Promise<boolean>;
|
|
7
|
+
declare function readText(...paths: string[]): Promise<string>;
|
|
8
|
+
declare function readJson<T>(...paths: string[]): Promise<T>;
|
|
9
|
+
declare function writeText(content: string, ...paths: string[]): Promise<void>;
|
|
10
|
+
declare function writeJson(content: object, ...paths: string[]): Promise<void>;
|
|
11
|
+
|
|
12
|
+
declare const alias: Record<string, string>;
|
|
13
|
+
|
|
14
|
+
export { alias, exists, isExists, readJson, readText, writeJson, writeText };
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,36 @@
|
|
|
1
|
+
export * from '@antfu/utils';
|
|
2
|
+
export { random, safe, singleOrNull, take, takeIf } from './shared.mjs';
|
|
3
|
+
import { access, readFile, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { join } from 'node:path';
|
|
1
5
|
export { default as glob } from 'fast-glob';
|
|
2
|
-
|
|
3
|
-
|
|
6
|
+
|
|
7
|
+
async function isExists(...paths) {
|
|
8
|
+
try {
|
|
9
|
+
await access(join(...paths));
|
|
10
|
+
return true;
|
|
11
|
+
} catch {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
async function exists(...paths) {
|
|
16
|
+
return await readFile(join(...paths), "utf-8").then(() => true, () => false);
|
|
17
|
+
}
|
|
18
|
+
async function readText(...paths) {
|
|
19
|
+
return await readFile(join(...paths), "utf-8");
|
|
20
|
+
}
|
|
21
|
+
async function readJson(...paths) {
|
|
22
|
+
return JSON.parse(await readText(...paths));
|
|
23
|
+
}
|
|
24
|
+
async function writeText(content, ...paths) {
|
|
25
|
+
await writeFile(join(...paths), content, "utf-8");
|
|
26
|
+
}
|
|
27
|
+
async function writeJson(content, ...paths) {
|
|
28
|
+
await writeText(`${JSON.stringify(content, null, 2)}
|
|
29
|
+
`, ...paths);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const alias = {
|
|
33
|
+
"@jiakun-zhao/utils": "@jiakun-zhao/utils/shared"
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export { alias, exists, isExists, readJson, readText, writeJson, writeText };
|
package/dist/shared.cjs
CHANGED
|
@@ -2,18 +2,31 @@
|
|
|
2
2
|
|
|
3
3
|
const utils = require('@antfu/utils');
|
|
4
4
|
|
|
5
|
-
function takeIf(value, predicate) {
|
|
6
|
-
return predicate(value) ? value : null;
|
|
7
|
-
}
|
|
8
5
|
function singleOrNull(arr) {
|
|
9
6
|
return arr.length === 1 ? arr[0] : null;
|
|
10
7
|
}
|
|
8
|
+
|
|
11
9
|
function random(max, min = 0) {
|
|
12
10
|
return Math.floor(Math.random() * max) + min;
|
|
13
11
|
}
|
|
14
12
|
|
|
13
|
+
function take(value, fn) {
|
|
14
|
+
return fn(value);
|
|
15
|
+
}
|
|
16
|
+
function takeIf(value, predicate, defaultValue) {
|
|
17
|
+
return predicate(value) ? value : utils.notUndefined(defaultValue) ? defaultValue : null;
|
|
18
|
+
}
|
|
19
|
+
function safe(fn) {
|
|
20
|
+
try {
|
|
21
|
+
return fn();
|
|
22
|
+
} catch {
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
15
26
|
exports.random = random;
|
|
27
|
+
exports.safe = safe;
|
|
16
28
|
exports.singleOrNull = singleOrNull;
|
|
29
|
+
exports.take = take;
|
|
17
30
|
exports.takeIf = takeIf;
|
|
18
31
|
Object.keys(utils).forEach(function (k) {
|
|
19
32
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = utils[k];
|
package/dist/shared.d.cts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
export * from '@antfu/utils';
|
|
2
2
|
|
|
3
|
-
declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
|
|
4
3
|
declare function singleOrNull<T>(arr: T[]): T | null;
|
|
4
|
+
|
|
5
5
|
/**
|
|
6
6
|
* min <= result < max
|
|
7
7
|
*/
|
|
8
8
|
declare function random(max: number, min?: number): number;
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
declare function take<T, R = void>(value: T, fn: (it: T) => R): R;
|
|
11
|
+
declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
|
|
12
|
+
declare function takeIf<T>(value: T, predicate: (it: T) => boolean, defaultValue: T): T;
|
|
13
|
+
declare function safe<R>(fn: () => R): R | undefined;
|
|
14
|
+
|
|
15
|
+
export { random, safe, singleOrNull, take, takeIf };
|
package/dist/shared.d.mts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
export * from '@antfu/utils';
|
|
2
2
|
|
|
3
|
-
declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
|
|
4
3
|
declare function singleOrNull<T>(arr: T[]): T | null;
|
|
4
|
+
|
|
5
5
|
/**
|
|
6
6
|
* min <= result < max
|
|
7
7
|
*/
|
|
8
8
|
declare function random(max: number, min?: number): number;
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
declare function take<T, R = void>(value: T, fn: (it: T) => R): R;
|
|
11
|
+
declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
|
|
12
|
+
declare function takeIf<T>(value: T, predicate: (it: T) => boolean, defaultValue: T): T;
|
|
13
|
+
declare function safe<R>(fn: () => R): R | undefined;
|
|
14
|
+
|
|
15
|
+
export { random, safe, singleOrNull, take, takeIf };
|
package/dist/shared.d.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
export * from '@antfu/utils';
|
|
2
2
|
|
|
3
|
-
declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
|
|
4
3
|
declare function singleOrNull<T>(arr: T[]): T | null;
|
|
4
|
+
|
|
5
5
|
/**
|
|
6
6
|
* min <= result < max
|
|
7
7
|
*/
|
|
8
8
|
declare function random(max: number, min?: number): number;
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
declare function take<T, R = void>(value: T, fn: (it: T) => R): R;
|
|
11
|
+
declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
|
|
12
|
+
declare function takeIf<T>(value: T, predicate: (it: T) => boolean, defaultValue: T): T;
|
|
13
|
+
declare function safe<R>(fn: () => R): R | undefined;
|
|
14
|
+
|
|
15
|
+
export { random, safe, singleOrNull, take, takeIf };
|
package/dist/shared.mjs
CHANGED
|
@@ -1,13 +1,25 @@
|
|
|
1
|
+
import { notUndefined } from '@antfu/utils';
|
|
1
2
|
export * from '@antfu/utils';
|
|
2
3
|
|
|
3
|
-
function takeIf(value, predicate) {
|
|
4
|
-
return predicate(value) ? value : null;
|
|
5
|
-
}
|
|
6
4
|
function singleOrNull(arr) {
|
|
7
5
|
return arr.length === 1 ? arr[0] : null;
|
|
8
6
|
}
|
|
7
|
+
|
|
9
8
|
function random(max, min = 0) {
|
|
10
9
|
return Math.floor(Math.random() * max) + min;
|
|
11
10
|
}
|
|
12
11
|
|
|
13
|
-
|
|
12
|
+
function take(value, fn) {
|
|
13
|
+
return fn(value);
|
|
14
|
+
}
|
|
15
|
+
function takeIf(value, predicate, defaultValue) {
|
|
16
|
+
return predicate(value) ? value : notUndefined(defaultValue) ? defaultValue : null;
|
|
17
|
+
}
|
|
18
|
+
function safe(fn) {
|
|
19
|
+
try {
|
|
20
|
+
return fn();
|
|
21
|
+
} catch {
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { random, safe, singleOrNull, take, takeIf };
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jiakun-zhao/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.0
|
|
4
|
+
"version": "1.0.0",
|
|
5
5
|
"description": "Utils.",
|
|
6
|
-
"author": "Jiakun Zhao <
|
|
6
|
+
"author": "Jiakun Zhao <hi@zhaojiakun.com>",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"homepage": "https://github.com/jiakun-zhao/utils#readme",
|
|
9
9
|
"repository": {
|
|
@@ -35,24 +35,25 @@
|
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@antfu/utils": "^0.7.6",
|
|
38
|
-
"fast-glob": "^3.3.2"
|
|
39
|
-
"local-pkg": "^0.5.0",
|
|
40
|
-
"ohash": "^1.1.3"
|
|
38
|
+
"fast-glob": "^3.3.2"
|
|
41
39
|
},
|
|
42
40
|
"devDependencies": {
|
|
43
41
|
"@jiakun-zhao/eslint-config": "^1.3.1",
|
|
44
|
-
"@types/node": "^20.9.
|
|
42
|
+
"@types/node": "^20.9.4",
|
|
45
43
|
"bumpp": "^9.2.0",
|
|
46
44
|
"eslint": "^8.54.0",
|
|
47
45
|
"esno": "^4.0.0",
|
|
48
46
|
"typescript": "^5.3.2",
|
|
49
|
-
"unbuild": "^2.0.0"
|
|
47
|
+
"unbuild": "^2.0.0",
|
|
48
|
+
"vitest": "^0.34.6"
|
|
50
49
|
},
|
|
51
50
|
"eslintConfig": {
|
|
52
51
|
"extends": "@jiakun-zhao"
|
|
53
52
|
},
|
|
54
53
|
"scripts": {
|
|
55
54
|
"build": "unbuild",
|
|
55
|
+
"stub": "unbuild --stub",
|
|
56
|
+
"test": "vitest -u",
|
|
56
57
|
"release": "bumpp && pnpm publish && npx cnpm sync @jiakun-zhao/utils"
|
|
57
58
|
}
|
|
58
59
|
}
|