@jiakun-zhao/utils 1.0.2 → 1.0.4

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
@@ -2,6 +2,8 @@
2
2
 
3
3
  const utils = require('@antfu/utils');
4
4
  const shared = require('./shared.cjs');
5
+ const node_crypto = require('node:crypto');
6
+ const node_fs = require('node:fs');
5
7
  const promises = require('node:fs/promises');
6
8
  const node_path = require('node:path');
7
9
  const fastGlob = require('fast-glob');
@@ -10,36 +12,36 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
10
12
 
11
13
  const fastGlob__default = /*#__PURE__*/_interopDefaultCompat(fastGlob);
12
14
 
13
- async function isExists(...paths) {
14
- try {
15
- await promises.access(node_path.join(...paths));
16
- return true;
17
- } catch {
18
- return false;
19
- }
15
+ async function exists(...path) {
16
+ return await promises.access(node_path.join(...path), promises.constants.F_OK).then(() => true, () => false);
20
17
  }
21
- async function exists(...paths) {
22
- return await promises.readFile(node_path.join(...paths), "utf-8").then(() => true, () => false);
18
+ async function readText(...path) {
19
+ return await promises.readFile(node_path.join(...path), "utf-8");
23
20
  }
24
- async function readText(...paths) {
25
- return await promises.readFile(node_path.join(...paths), "utf-8");
21
+ async function readJson(...path) {
22
+ return JSON.parse(await readText(...path));
26
23
  }
27
- async function readJson(...paths) {
28
- return JSON.parse(await readText(...paths));
24
+ async function writeText(content, ...path) {
25
+ await promises.writeFile(node_path.join(...path), content, "utf-8");
29
26
  }
30
- async function writeText(content, ...paths) {
31
- await promises.writeFile(node_path.join(...paths), content, "utf-8");
32
- }
33
- async function writeJson(content, ...paths) {
27
+ async function writeJson(content, ...path) {
34
28
  await writeText(`${JSON.stringify(content, null, 2)}
35
- `, ...paths);
29
+ `, ...path);
30
+ }
31
+ async function toMD5(...path) {
32
+ const hash = node_crypto.createHash("md5");
33
+ const stream = node_fs.createReadStream(node_path.join(...path));
34
+ return new Promise((resolve, reject) => {
35
+ stream.on("data", (chunk) => hash.update(chunk));
36
+ stream.on("end", () => resolve(hash.digest("base64url")));
37
+ stream.on("error", reject);
38
+ });
36
39
  }
37
40
 
38
41
  const alias = {
39
42
  "@jiakun-zhao/utils": "@jiakun-zhao/utils/shared"
40
43
  };
41
44
 
42
- exports.createRegistryUrlGetter = shared.createRegistryUrlGetter;
43
45
  exports.random = shared.random;
44
46
  exports.safe = shared.safe;
45
47
  exports.singleOrNull = shared.singleOrNull;
@@ -48,9 +50,9 @@ exports.takeIf = shared.takeIf;
48
50
  exports.glob = fastGlob__default;
49
51
  exports.alias = alias;
50
52
  exports.exists = exists;
51
- exports.isExists = isExists;
52
53
  exports.readJson = readJson;
53
54
  exports.readText = readText;
55
+ exports.toMD5 = toMD5;
54
56
  exports.writeJson = writeJson;
55
57
  exports.writeText = writeText;
56
58
  Object.keys(utils).forEach(function (k) {
package/dist/index.d.cts CHANGED
@@ -1,14 +1,14 @@
1
1
  export * from '@antfu/utils';
2
- export { createRegistryUrlGetter, random, safe, singleOrNull, take, takeIf } from './shared.cjs';
2
+ export { random, safe, singleOrNull, take, takeIf } from './shared.cjs';
3
3
  export { default as glob } from 'fast-glob';
4
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>;
5
+ declare function exists(...path: string[]): Promise<boolean>;
6
+ declare function readText(...path: string[]): Promise<string>;
7
+ declare function readJson<T>(...path: string[]): Promise<T>;
8
+ declare function writeText(content: string, ...path: string[]): Promise<void>;
9
+ declare function writeJson(content: object, ...path: string[]): Promise<void>;
10
+ declare function toMD5(...path: string[]): Promise<string>;
11
11
 
12
12
  declare const alias: Record<string, string>;
13
13
 
14
- export { alias, exists, isExists, readJson, readText, writeJson, writeText };
14
+ export { alias, exists, readJson, readText, toMD5, writeJson, writeText };
package/dist/index.d.mts CHANGED
@@ -1,14 +1,14 @@
1
1
  export * from '@antfu/utils';
2
- export { createRegistryUrlGetter, random, safe, singleOrNull, take, takeIf } from './shared.mjs';
2
+ export { random, safe, singleOrNull, take, takeIf } from './shared.mjs';
3
3
  export { default as glob } from 'fast-glob';
4
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>;
5
+ declare function exists(...path: string[]): Promise<boolean>;
6
+ declare function readText(...path: string[]): Promise<string>;
7
+ declare function readJson<T>(...path: string[]): Promise<T>;
8
+ declare function writeText(content: string, ...path: string[]): Promise<void>;
9
+ declare function writeJson(content: object, ...path: string[]): Promise<void>;
10
+ declare function toMD5(...path: string[]): Promise<string>;
11
11
 
12
12
  declare const alias: Record<string, string>;
13
13
 
14
- export { alias, exists, isExists, readJson, readText, writeJson, writeText };
14
+ export { alias, exists, readJson, readText, toMD5, writeJson, writeText };
package/dist/index.d.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  export * from '@antfu/utils';
2
- export { createRegistryUrlGetter, random, safe, singleOrNull, take, takeIf } from './shared.js';
2
+ export { random, safe, singleOrNull, take, takeIf } from './shared.js';
3
3
  export { default as glob } from 'fast-glob';
4
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>;
5
+ declare function exists(...path: string[]): Promise<boolean>;
6
+ declare function readText(...path: string[]): Promise<string>;
7
+ declare function readJson<T>(...path: string[]): Promise<T>;
8
+ declare function writeText(content: string, ...path: string[]): Promise<void>;
9
+ declare function writeJson(content: object, ...path: string[]): Promise<void>;
10
+ declare function toMD5(...path: string[]): Promise<string>;
11
11
 
12
12
  declare const alias: Record<string, string>;
13
13
 
14
- export { alias, exists, isExists, readJson, readText, writeJson, writeText };
14
+ export { alias, exists, readJson, readText, toMD5, writeJson, writeText };
package/dist/index.mjs CHANGED
@@ -1,36 +1,39 @@
1
1
  export * from '@antfu/utils';
2
- export { createRegistryUrlGetter, random, safe, singleOrNull, take, takeIf } from './shared.mjs';
3
- import { access, readFile, writeFile } from 'node:fs/promises';
2
+ export { random, safe, singleOrNull, take, takeIf } from './shared.mjs';
3
+ import { createHash } from 'node:crypto';
4
+ import { createReadStream } from 'node:fs';
5
+ import { access, constants, readFile, writeFile } from 'node:fs/promises';
4
6
  import { join } from 'node:path';
5
7
  export { default as glob } from 'fast-glob';
6
8
 
7
- async function isExists(...paths) {
8
- try {
9
- await access(join(...paths));
10
- return true;
11
- } catch {
12
- return false;
13
- }
9
+ async function exists(...path) {
10
+ return await access(join(...path), constants.F_OK).then(() => true, () => false);
14
11
  }
15
- async function exists(...paths) {
16
- return await readFile(join(...paths), "utf-8").then(() => true, () => false);
12
+ async function readText(...path) {
13
+ return await readFile(join(...path), "utf-8");
17
14
  }
18
- async function readText(...paths) {
19
- return await readFile(join(...paths), "utf-8");
15
+ async function readJson(...path) {
16
+ return JSON.parse(await readText(...path));
20
17
  }
21
- async function readJson(...paths) {
22
- return JSON.parse(await readText(...paths));
18
+ async function writeText(content, ...path) {
19
+ await writeFile(join(...path), content, "utf-8");
23
20
  }
24
- async function writeText(content, ...paths) {
25
- await writeFile(join(...paths), content, "utf-8");
26
- }
27
- async function writeJson(content, ...paths) {
21
+ async function writeJson(content, ...path) {
28
22
  await writeText(`${JSON.stringify(content, null, 2)}
29
- `, ...paths);
23
+ `, ...path);
24
+ }
25
+ async function toMD5(...path) {
26
+ const hash = createHash("md5");
27
+ const stream = createReadStream(join(...path));
28
+ return new Promise((resolve, reject) => {
29
+ stream.on("data", (chunk) => hash.update(chunk));
30
+ stream.on("end", () => resolve(hash.digest("base64url")));
31
+ stream.on("error", reject);
32
+ });
30
33
  }
31
34
 
32
35
  const alias = {
33
36
  "@jiakun-zhao/utils": "@jiakun-zhao/utils/shared"
34
37
  };
35
38
 
36
- export { alias, exists, isExists, readJson, readText, writeJson, writeText };
39
+ export { alias, exists, readJson, readText, toMD5, writeJson, writeText };
package/dist/shared.cjs CHANGED
@@ -23,22 +23,6 @@ function safe(fn) {
23
23
  }
24
24
  }
25
25
 
26
- function createRegistryUrlGetter(base) {
27
- return function(...args) {
28
- switch (args.length) {
29
- case 1:
30
- return base.slice(0, base.indexOf(":name")) + args[0];
31
- case 2:
32
- return base.slice(0, base.indexOf(":version")).replace(":name", args[0]) + args[1];
33
- case 3:
34
- return base.replace(":name", args[0]).replace(":version", args[1]).replace(":path", args[2]);
35
- default:
36
- return void 0;
37
- }
38
- };
39
- }
40
-
41
- exports.createRegistryUrlGetter = createRegistryUrlGetter;
42
26
  exports.random = random;
43
27
  exports.safe = safe;
44
28
  exports.singleOrNull = singleOrNull;
package/dist/shared.d.cts CHANGED
@@ -12,6 +12,4 @@ declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
12
12
  declare function takeIf<T>(value: T, predicate: (it: T) => boolean, defaultValue: T): T;
13
13
  declare function safe<R>(fn: () => R): R | undefined;
14
14
 
15
- declare function createRegistryUrlGetter(base: `${string}/:name${string}:version${string}:path`): (...args: string[]) => string | undefined;
16
-
17
- export { createRegistryUrlGetter, random, safe, singleOrNull, take, takeIf };
15
+ export { random, safe, singleOrNull, take, takeIf };
package/dist/shared.d.mts CHANGED
@@ -12,6 +12,4 @@ declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
12
12
  declare function takeIf<T>(value: T, predicate: (it: T) => boolean, defaultValue: T): T;
13
13
  declare function safe<R>(fn: () => R): R | undefined;
14
14
 
15
- declare function createRegistryUrlGetter(base: `${string}/:name${string}:version${string}:path`): (...args: string[]) => string | undefined;
16
-
17
- export { createRegistryUrlGetter, random, safe, singleOrNull, take, takeIf };
15
+ export { random, safe, singleOrNull, take, takeIf };
package/dist/shared.d.ts CHANGED
@@ -12,6 +12,4 @@ declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
12
12
  declare function takeIf<T>(value: T, predicate: (it: T) => boolean, defaultValue: T): T;
13
13
  declare function safe<R>(fn: () => R): R | undefined;
14
14
 
15
- declare function createRegistryUrlGetter(base: `${string}/:name${string}:version${string}:path`): (...args: string[]) => string | undefined;
16
-
17
- export { createRegistryUrlGetter, random, safe, singleOrNull, take, takeIf };
15
+ export { random, safe, singleOrNull, take, takeIf };
package/dist/shared.mjs CHANGED
@@ -22,19 +22,4 @@ function safe(fn) {
22
22
  }
23
23
  }
24
24
 
25
- function createRegistryUrlGetter(base) {
26
- return function(...args) {
27
- switch (args.length) {
28
- case 1:
29
- return base.slice(0, base.indexOf(":name")) + args[0];
30
- case 2:
31
- return base.slice(0, base.indexOf(":version")).replace(":name", args[0]) + args[1];
32
- case 3:
33
- return base.replace(":name", args[0]).replace(":version", args[1]).replace(":path", args[2]);
34
- default:
35
- return void 0;
36
- }
37
- };
38
- }
39
-
40
- export { createRegistryUrlGetter, random, safe, singleOrNull, take, takeIf };
25
+ export { random, safe, singleOrNull, take, takeIf };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jiakun-zhao/utils",
3
3
  "type": "module",
4
- "version": "1.0.2",
4
+ "version": "1.0.4",
5
5
  "description": "Utils.",
6
6
  "author": "Jiakun Zhao <hi@zhaojiakun.com>",
7
7
  "license": "MIT",
@@ -39,7 +39,7 @@
39
39
  },
40
40
  "devDependencies": {
41
41
  "@jiakun-zhao/eslint-config": "^1.3.1",
42
- "@types/node": "^20.9.4",
42
+ "@types/node": "^20.10.0",
43
43
  "bumpp": "^9.2.0",
44
44
  "eslint": "^8.54.0",
45
45
  "esno": "^4.0.0",