@jiakun-zhao/utils 0.0.12 → 0.1.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/README.md +6 -1
- package/dist/index.cjs +10 -67
- package/dist/index.d.ts +4 -14
- package/dist/index.mjs +6 -52
- package/package.json +4 -1
package/README.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -1,73 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const node_path = require('node:path');
|
|
5
|
-
const process = require('node:process');
|
|
6
|
-
const crypto = require('node:crypto');
|
|
3
|
+
const utils = require('@antfu/utils');
|
|
7
4
|
|
|
8
|
-
function
|
|
9
|
-
|
|
10
|
-
const process__default = /*#__PURE__*/_interopDefaultCompat(process);
|
|
11
|
-
const crypto__default = /*#__PURE__*/_interopDefaultCompat(crypto);
|
|
12
|
-
|
|
13
|
-
async function dist(cwd = process__default.cwd()) {
|
|
14
|
-
const path = node_path.join(cwd, "dist");
|
|
15
|
-
await promises.rm(path, { recursive: true, force: true });
|
|
16
|
-
await promises.mkdir(path, { recursive: true });
|
|
17
|
-
return path;
|
|
18
|
-
}
|
|
19
|
-
async function isFileExist(path) {
|
|
20
|
-
try {
|
|
21
|
-
await promises.access(path);
|
|
22
|
-
return true;
|
|
23
|
-
} catch {
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
async function readText(path) {
|
|
28
|
-
return await isFileExist(path) ? await promises.readFile(path, "utf-8") : null;
|
|
29
|
-
}
|
|
30
|
-
async function readJson(path) {
|
|
31
|
-
const raw = await readText(path);
|
|
32
|
-
return raw ? JSON.parse(raw) : null;
|
|
33
|
-
}
|
|
34
|
-
async function writeText(path, data) {
|
|
35
|
-
const { dir } = node_path.parse(path);
|
|
36
|
-
await promises.mkdir(dir, { recursive: true });
|
|
37
|
-
await promises.writeFile(path, data, "utf-8");
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const md5 = crypto__default.createHash("md5");
|
|
41
|
-
function toMD5(str) {
|
|
42
|
-
return md5.update(str).digest("hex");
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function toArray() {
|
|
46
|
-
return Array.isArray(this) ? this : [this];
|
|
47
|
-
}
|
|
48
|
-
function filterNotNull() {
|
|
49
|
-
return this.filter((it) => it != null);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function isString(value) {
|
|
53
|
-
return typeof value === "string";
|
|
54
|
-
}
|
|
55
|
-
function it(block) {
|
|
56
|
-
block(this);
|
|
57
|
-
return this;
|
|
5
|
+
function fromTo(value, block) {
|
|
6
|
+
return block(value);
|
|
58
7
|
}
|
|
59
|
-
function
|
|
60
|
-
return
|
|
8
|
+
function takeIf(value, predicate) {
|
|
9
|
+
return predicate(value) ? value : null;
|
|
61
10
|
}
|
|
62
11
|
|
|
63
|
-
exports.
|
|
64
|
-
exports.
|
|
65
|
-
|
|
66
|
-
exports.
|
|
67
|
-
|
|
68
|
-
exports.readJson = readJson;
|
|
69
|
-
exports.readText = readText;
|
|
70
|
-
exports.to = to;
|
|
71
|
-
exports.toArray = toArray;
|
|
72
|
-
exports.toMD5 = toMD5;
|
|
73
|
-
exports.writeText = writeText;
|
|
12
|
+
exports.fromTo = fromTo;
|
|
13
|
+
exports.takeIf = takeIf;
|
|
14
|
+
Object.keys(utils).forEach(function (k) {
|
|
15
|
+
if (k !== 'default' && !exports.hasOwnProperty(k)) exports[k] = utils[k];
|
|
16
|
+
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
declare function isFileExist(path: string): Promise<boolean>;
|
|
3
|
-
declare function readText(path: string): Promise<string | null>;
|
|
4
|
-
declare function readJson<T>(path: string): Promise<T | null>;
|
|
5
|
-
declare function writeText(path: string, data: string): Promise<void>;
|
|
1
|
+
export * from '@antfu/utils';
|
|
6
2
|
|
|
7
|
-
declare function
|
|
3
|
+
declare function fromTo<T, R>(value: T, block: (it: T) => R): R;
|
|
4
|
+
declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
|
|
8
5
|
|
|
9
|
-
|
|
10
|
-
declare function filterNotNull<T>(this: T[]): NonNullable<T>[];
|
|
11
|
-
|
|
12
|
-
declare function isString(value: any): value is string;
|
|
13
|
-
declare function it<T>(this: T, block: (it: T) => void): T;
|
|
14
|
-
declare function to<T, R>(this: T, block: (it: T) => R): R;
|
|
15
|
-
|
|
16
|
-
export { dist, filterNotNull, isFileExist, isString, it, readJson, readText, to, toArray, toMD5, writeText };
|
|
6
|
+
export { fromTo, takeIf };
|
package/dist/index.mjs
CHANGED
|
@@ -1,56 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
import { join, parse } from 'node:path';
|
|
3
|
-
import process from 'node:process';
|
|
4
|
-
import crypto from 'node:crypto';
|
|
1
|
+
export * from '@antfu/utils';
|
|
5
2
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
await rm(path, { recursive: true, force: true });
|
|
9
|
-
await mkdir(path, { recursive: true });
|
|
10
|
-
return path;
|
|
3
|
+
function fromTo(value, block) {
|
|
4
|
+
return block(value);
|
|
11
5
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
await access(path);
|
|
15
|
-
return true;
|
|
16
|
-
} catch {
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
async function readText(path) {
|
|
21
|
-
return await isFileExist(path) ? await readFile(path, "utf-8") : null;
|
|
22
|
-
}
|
|
23
|
-
async function readJson(path) {
|
|
24
|
-
const raw = await readText(path);
|
|
25
|
-
return raw ? JSON.parse(raw) : null;
|
|
26
|
-
}
|
|
27
|
-
async function writeText(path, data) {
|
|
28
|
-
const { dir } = parse(path);
|
|
29
|
-
await mkdir(dir, { recursive: true });
|
|
30
|
-
await writeFile(path, data, "utf-8");
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const md5 = crypto.createHash("md5");
|
|
34
|
-
function toMD5(str) {
|
|
35
|
-
return md5.update(str).digest("hex");
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function toArray() {
|
|
39
|
-
return Array.isArray(this) ? this : [this];
|
|
40
|
-
}
|
|
41
|
-
function filterNotNull() {
|
|
42
|
-
return this.filter((it) => it != null);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function isString(value) {
|
|
46
|
-
return typeof value === "string";
|
|
47
|
-
}
|
|
48
|
-
function it(block) {
|
|
49
|
-
block(this);
|
|
50
|
-
return this;
|
|
51
|
-
}
|
|
52
|
-
function to(block) {
|
|
53
|
-
return block(this);
|
|
6
|
+
function takeIf(value, predicate) {
|
|
7
|
+
return predicate(value) ? value : null;
|
|
54
8
|
}
|
|
55
9
|
|
|
56
|
-
export {
|
|
10
|
+
export { fromTo, takeIf };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jiakun-zhao/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "0.1.0",
|
|
5
5
|
"description": "Utils.",
|
|
6
6
|
"author": "Jiakun Zhao <jiakun.zhao@outlook.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -26,6 +26,9 @@
|
|
|
26
26
|
"files": [
|
|
27
27
|
"dist"
|
|
28
28
|
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@antfu/utils": "^0.7.5"
|
|
31
|
+
},
|
|
29
32
|
"devDependencies": {
|
|
30
33
|
"@jiakun-zhao/eslint-config": "^1.2.0",
|
|
31
34
|
"@types/node": "^20.4.5",
|