@jiakun-zhao/utils 0.0.13 → 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 CHANGED
@@ -1,5 +1,10 @@
1
1
  # Utils
2
2
 
3
+ ![npm version](https://img.shields.io/npm/v/@jiakun-zhao/utils?color=%236054ba)
4
+
5
+ Extends [@antfu/utils](https://github.com/antfu/utils) and add some utils.
6
+
3
7
  ```bash
4
8
  pnpm i -D @jiakun-zhao/utils
5
- ```
9
+ ```
10
+
package/dist/index.cjs CHANGED
@@ -1,73 +1,16 @@
1
1
  'use strict';
2
2
 
3
- const promises = require('node:fs/promises');
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 _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
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(value, block) {
56
- block(value);
57
- return value;
58
- }
59
- function to(value, block) {
5
+ function fromTo(value, block) {
60
6
  return block(value);
61
7
  }
8
+ function takeIf(value, predicate) {
9
+ return predicate(value) ? value : null;
10
+ }
62
11
 
63
- exports.dist = dist;
64
- exports.filterNotNull = filterNotNull;
65
- exports.isFileExist = isFileExist;
66
- exports.isString = isString;
67
- exports.it = it;
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
- declare function dist(cwd?: string): Promise<string>;
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 toMD5(str: string): string;
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
- declare function toArray<T>(this: T | T[]): T[];
10
- declare function filterNotNull<T>(this: T[]): NonNullable<T>[];
11
-
12
- declare function isString(value: any): value is string;
13
- declare function it<T>(value: T, block: (i: T) => void): T;
14
- declare function to<T, R>(value: T, block: (u: 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
- import { rm, mkdir, access, readFile, writeFile } from 'node:fs/promises';
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
- async function dist(cwd = process.cwd()) {
7
- const path = join(cwd, "dist");
8
- await rm(path, { recursive: true, force: true });
9
- await mkdir(path, { recursive: true });
10
- return path;
11
- }
12
- async function isFileExist(path) {
13
- try {
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(value, block) {
49
- block(value);
50
- return value;
51
- }
52
- function to(value, block) {
3
+ function fromTo(value, block) {
53
4
  return block(value);
54
5
  }
6
+ function takeIf(value, predicate) {
7
+ return predicate(value) ? value : null;
8
+ }
55
9
 
56
- export { dist, filterNotNull, isFileExist, isString, it, readJson, readText, to, toArray, toMD5, writeText };
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.13",
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",