@jiakun-zhao/utils 1.0.4 → 1.1.1

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/fs.cjs ADDED
@@ -0,0 +1,45 @@
1
+ 'use strict';
2
+
3
+ const node_crypto = require('node:crypto');
4
+ const node_fs = require('node:fs');
5
+ const promises = require('node:fs/promises');
6
+ const node_path = require('node:path');
7
+ const fastGlob = require('fast-glob');
8
+
9
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
10
+
11
+ const fastGlob__default = /*#__PURE__*/_interopDefaultCompat(fastGlob);
12
+
13
+ async function exists(...path) {
14
+ return await promises.access(node_path.join(...path), promises.constants.F_OK).then(() => true, () => false);
15
+ }
16
+ async function readText(...path) {
17
+ return await promises.readFile(node_path.join(...path), "utf-8");
18
+ }
19
+ async function readJson(...path) {
20
+ return JSON.parse(await readText(...path));
21
+ }
22
+ async function writeText(content, ...path) {
23
+ await promises.writeFile(node_path.join(...path), content, "utf-8");
24
+ }
25
+ async function writeJson(content, ...path) {
26
+ await writeText(`${JSON.stringify(content, null, 2)}
27
+ `, ...path);
28
+ }
29
+ async function toMD5(...path) {
30
+ const hash = node_crypto.createHash("md5");
31
+ const stream = node_fs.createReadStream(node_path.join(...path));
32
+ return new Promise((resolve, reject) => {
33
+ stream.on("data", (chunk) => hash.update(chunk));
34
+ stream.on("end", () => resolve(hash.digest("base64url")));
35
+ stream.on("error", reject);
36
+ });
37
+ }
38
+
39
+ exports.glob = fastGlob__default;
40
+ exports.exists = exists;
41
+ exports.readJson = readJson;
42
+ exports.readText = readText;
43
+ exports.toMD5 = toMD5;
44
+ exports.writeJson = writeJson;
45
+ exports.writeText = writeText;
package/dist/fs.d.cts ADDED
@@ -0,0 +1,10 @@
1
+ export { default as glob } from 'fast-glob';
2
+
3
+ declare function exists(...path: string[]): Promise<boolean>;
4
+ declare function readText(...path: string[]): Promise<string>;
5
+ declare function readJson<T>(...path: string[]): Promise<T>;
6
+ declare function writeText(content: string, ...path: string[]): Promise<void>;
7
+ declare function writeJson(content: object, ...path: string[]): Promise<void>;
8
+ declare function toMD5(...path: string[]): Promise<string>;
9
+
10
+ export { exists, readJson, readText, toMD5, writeJson, writeText };
package/dist/fs.d.mts ADDED
@@ -0,0 +1,10 @@
1
+ export { default as glob } from 'fast-glob';
2
+
3
+ declare function exists(...path: string[]): Promise<boolean>;
4
+ declare function readText(...path: string[]): Promise<string>;
5
+ declare function readJson<T>(...path: string[]): Promise<T>;
6
+ declare function writeText(content: string, ...path: string[]): Promise<void>;
7
+ declare function writeJson(content: object, ...path: string[]): Promise<void>;
8
+ declare function toMD5(...path: string[]): Promise<string>;
9
+
10
+ export { exists, readJson, readText, toMD5, writeJson, writeText };
package/dist/fs.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export { default as glob } from 'fast-glob';
2
+
3
+ declare function exists(...path: string[]): Promise<boolean>;
4
+ declare function readText(...path: string[]): Promise<string>;
5
+ declare function readJson<T>(...path: string[]): Promise<T>;
6
+ declare function writeText(content: string, ...path: string[]): Promise<void>;
7
+ declare function writeJson(content: object, ...path: string[]): Promise<void>;
8
+ declare function toMD5(...path: string[]): Promise<string>;
9
+
10
+ export { exists, readJson, readText, toMD5, writeJson, writeText };
package/dist/fs.mjs ADDED
@@ -0,0 +1,33 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { createReadStream } from 'node:fs';
3
+ import { access, constants, readFile, writeFile } from 'node:fs/promises';
4
+ import { join } from 'node:path';
5
+ export { default as glob } from 'fast-glob';
6
+
7
+ async function exists(...path) {
8
+ return await access(join(...path), constants.F_OK).then(() => true, () => false);
9
+ }
10
+ async function readText(...path) {
11
+ return await readFile(join(...path), "utf-8");
12
+ }
13
+ async function readJson(...path) {
14
+ return JSON.parse(await readText(...path));
15
+ }
16
+ async function writeText(content, ...path) {
17
+ await writeFile(join(...path), content, "utf-8");
18
+ }
19
+ async function writeJson(content, ...path) {
20
+ await writeText(`${JSON.stringify(content, null, 2)}
21
+ `, ...path);
22
+ }
23
+ async function toMD5(...path) {
24
+ const hash = createHash("md5");
25
+ const stream = createReadStream(join(...path));
26
+ return new Promise((resolve, reject) => {
27
+ stream.on("data", (chunk) => hash.update(chunk));
28
+ stream.on("end", () => resolve(hash.digest("base64url")));
29
+ stream.on("error", reject);
30
+ });
31
+ }
32
+
33
+ export { exists, readJson, readText, toMD5, writeJson, writeText };
package/dist/index.cjs CHANGED
@@ -1,60 +1,64 @@
1
1
  'use strict';
2
2
 
3
- const utils = require('@antfu/utils');
4
- const shared = require('./shared.cjs');
5
- const node_crypto = require('node:crypto');
6
- const node_fs = require('node:fs');
7
- const promises = require('node:fs/promises');
8
- const node_path = require('node:path');
9
- const fastGlob = require('fast-glob');
3
+ const isFunction = (val) => typeof val === "function";
4
+ const isBoolean = (val) => typeof val === "boolean";
5
+ const isNumber = (val) => typeof val === "number";
6
+ const isString = (val) => typeof val === "string";
7
+ const toString = (value) => Object.prototype.toString.call(value);
8
+ const isObject = (val) => toString(val) === "[object Object]";
9
+ const isUndefined = (val) => toString(val) === "[object Undefined]";
10
+ const isNull = (val) => toString(val) === "[object Null]";
11
+ const isRegExp = (val) => toString(val) === "[object RegExp]";
12
+ const isDate = (val) => toString(val) === "[object Date]";
13
+ const isArray = Array.isArray;
14
+ const notNullish = (v) => v != null;
15
+ const notNull = (v) => v !== null;
16
+ const notUndefined = (v) => v !== void 0;
10
17
 
11
- function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
12
-
13
- const fastGlob__default = /*#__PURE__*/_interopDefaultCompat(fastGlob);
14
-
15
- async function exists(...path) {
16
- return await promises.access(node_path.join(...path), promises.constants.F_OK).then(() => true, () => false);
18
+ function singleOrNull(arr) {
19
+ return arr.length === 1 ? arr[0] : null;
17
20
  }
18
- async function readText(...path) {
19
- return await promises.readFile(node_path.join(...path), "utf-8");
21
+ function toArray(array) {
22
+ array = array ?? [];
23
+ return isArray(array) ? array : [array];
20
24
  }
21
- async function readJson(...path) {
22
- return JSON.parse(await readText(...path));
25
+
26
+ function take(value, fn) {
27
+ return fn(value);
23
28
  }
24
- async function writeText(content, ...path) {
25
- await promises.writeFile(node_path.join(...path), content, "utf-8");
29
+ function takeIf(value, predicate, defaultValue) {
30
+ return predicate(value) ? value : notUndefined(defaultValue) ? defaultValue : null;
26
31
  }
27
- async function writeJson(content, ...path) {
28
- await writeText(`${JSON.stringify(content, null, 2)}
29
- `, ...path);
32
+ function safe(fn) {
33
+ try {
34
+ return fn();
35
+ } catch {
36
+ }
30
37
  }
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
- });
38
+
39
+ function random(max, min = 0) {
40
+ return Math.floor(Math.random() * max) + min;
39
41
  }
40
42
 
41
- const alias = {
42
- "@jiakun-zhao/utils": "@jiakun-zhao/utils/shared"
43
- };
43
+ const timestamp = () => +Date.now();
44
44
 
45
- exports.random = shared.random;
46
- exports.safe = shared.safe;
47
- exports.singleOrNull = shared.singleOrNull;
48
- exports.take = shared.take;
49
- exports.takeIf = shared.takeIf;
50
- exports.glob = fastGlob__default;
51
- exports.alias = alias;
52
- exports.exists = exists;
53
- exports.readJson = readJson;
54
- exports.readText = readText;
55
- exports.toMD5 = toMD5;
56
- exports.writeJson = writeJson;
57
- exports.writeText = writeText;
58
- Object.keys(utils).forEach(function (k) {
59
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = utils[k];
60
- });
45
+ exports.isArray = isArray;
46
+ exports.isBoolean = isBoolean;
47
+ exports.isDate = isDate;
48
+ exports.isFunction = isFunction;
49
+ exports.isNull = isNull;
50
+ exports.isNumber = isNumber;
51
+ exports.isObject = isObject;
52
+ exports.isRegExp = isRegExp;
53
+ exports.isString = isString;
54
+ exports.isUndefined = isUndefined;
55
+ exports.notNull = notNull;
56
+ exports.notNullish = notNullish;
57
+ exports.notUndefined = notUndefined;
58
+ exports.random = random;
59
+ exports.safe = safe;
60
+ exports.singleOrNull = singleOrNull;
61
+ exports.take = take;
62
+ exports.takeIf = takeIf;
63
+ exports.timestamp = timestamp;
64
+ exports.toArray = toArray;
package/dist/index.d.cts CHANGED
@@ -1,14 +1,38 @@
1
- export * from '@antfu/utils';
2
- export { random, safe, singleOrNull, take, takeIf } from './shared.cjs';
3
- export { default as glob } from 'fast-glob';
1
+ declare function singleOrNull<T>(arr: T[]): T | null;
2
+ declare function toArray<T>(array?: T | Array<T>): Array<T>;
4
3
 
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>;
4
+ declare function take<T, R = void>(value: T, fn: (it: T) => R): R;
5
+ declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
6
+ declare function takeIf<T>(value: T, predicate: (it: T) => boolean, defaultValue: T): T;
7
+ declare function safe<R>(fn: () => R): R | undefined;
11
8
 
12
- declare const alias: Record<string, string>;
9
+ type Nullable<T> = T | null | undefined;
10
+ type NotNullable<T> = Exclude<T, null | undefined>;
11
+ type NotNull<T> = Exclude<T, null>;
12
+ type NotUndefined<T> = Exclude<T, undefined>;
13
+ type Arrayable<T> = T | Array<T>;
14
+ type ElementOf<T> = T extends Array<infer Element> ? Element : never;
15
+ type Fn<R = void> = (...args: any[]) => R;
13
16
 
14
- export { alias, exists, readJson, readText, toMD5, writeJson, writeText };
17
+ declare const isFunction: (val: any) => val is Fn;
18
+ declare const isBoolean: (val: any) => val is boolean;
19
+ declare const isNumber: (val: any) => val is number;
20
+ declare const isString: (val: unknown) => val is string;
21
+ declare const isObject: (val: any) => val is object;
22
+ declare const isUndefined: (val: any) => val is undefined;
23
+ declare const isNull: (val: any) => val is null;
24
+ declare const isRegExp: (val: any) => val is RegExp;
25
+ declare const isDate: (val: any) => val is Date;
26
+ declare const isArray: (arg: any) => arg is any[];
27
+ declare const notNullish: <T>(v: T | null | undefined) => v is NonNullable<T>;
28
+ declare const notNull: <T>(v: T | null) => v is Exclude<T, null>;
29
+ declare const notUndefined: <T>(v: T | undefined) => v is Exclude<T, undefined>;
30
+
31
+ /**
32
+ * @description min <= result < max
33
+ */
34
+ declare function random(max: number, min?: number): number;
35
+
36
+ declare const timestamp: () => number;
37
+
38
+ export { type Arrayable, type ElementOf, type Fn, type NotNull, type NotNullable, type NotUndefined, type Nullable, isArray, isBoolean, isDate, isFunction, isNull, isNumber, isObject, isRegExp, isString, isUndefined, notNull, notNullish, notUndefined, random, safe, singleOrNull, take, takeIf, timestamp, toArray };
package/dist/index.d.mts CHANGED
@@ -1,14 +1,38 @@
1
- export * from '@antfu/utils';
2
- export { random, safe, singleOrNull, take, takeIf } from './shared.mjs';
3
- export { default as glob } from 'fast-glob';
1
+ declare function singleOrNull<T>(arr: T[]): T | null;
2
+ declare function toArray<T>(array?: T | Array<T>): Array<T>;
4
3
 
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>;
4
+ declare function take<T, R = void>(value: T, fn: (it: T) => R): R;
5
+ declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
6
+ declare function takeIf<T>(value: T, predicate: (it: T) => boolean, defaultValue: T): T;
7
+ declare function safe<R>(fn: () => R): R | undefined;
11
8
 
12
- declare const alias: Record<string, string>;
9
+ type Nullable<T> = T | null | undefined;
10
+ type NotNullable<T> = Exclude<T, null | undefined>;
11
+ type NotNull<T> = Exclude<T, null>;
12
+ type NotUndefined<T> = Exclude<T, undefined>;
13
+ type Arrayable<T> = T | Array<T>;
14
+ type ElementOf<T> = T extends Array<infer Element> ? Element : never;
15
+ type Fn<R = void> = (...args: any[]) => R;
13
16
 
14
- export { alias, exists, readJson, readText, toMD5, writeJson, writeText };
17
+ declare const isFunction: (val: any) => val is Fn;
18
+ declare const isBoolean: (val: any) => val is boolean;
19
+ declare const isNumber: (val: any) => val is number;
20
+ declare const isString: (val: unknown) => val is string;
21
+ declare const isObject: (val: any) => val is object;
22
+ declare const isUndefined: (val: any) => val is undefined;
23
+ declare const isNull: (val: any) => val is null;
24
+ declare const isRegExp: (val: any) => val is RegExp;
25
+ declare const isDate: (val: any) => val is Date;
26
+ declare const isArray: (arg: any) => arg is any[];
27
+ declare const notNullish: <T>(v: T | null | undefined) => v is NonNullable<T>;
28
+ declare const notNull: <T>(v: T | null) => v is Exclude<T, null>;
29
+ declare const notUndefined: <T>(v: T | undefined) => v is Exclude<T, undefined>;
30
+
31
+ /**
32
+ * @description min <= result < max
33
+ */
34
+ declare function random(max: number, min?: number): number;
35
+
36
+ declare const timestamp: () => number;
37
+
38
+ export { type Arrayable, type ElementOf, type Fn, type NotNull, type NotNullable, type NotUndefined, type Nullable, isArray, isBoolean, isDate, isFunction, isNull, isNumber, isObject, isRegExp, isString, isUndefined, notNull, notNullish, notUndefined, random, safe, singleOrNull, take, takeIf, timestamp, toArray };
package/dist/index.d.ts CHANGED
@@ -1,14 +1,38 @@
1
- export * from '@antfu/utils';
2
- export { random, safe, singleOrNull, take, takeIf } from './shared.js';
3
- export { default as glob } from 'fast-glob';
1
+ declare function singleOrNull<T>(arr: T[]): T | null;
2
+ declare function toArray<T>(array?: T | Array<T>): Array<T>;
4
3
 
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>;
4
+ declare function take<T, R = void>(value: T, fn: (it: T) => R): R;
5
+ declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
6
+ declare function takeIf<T>(value: T, predicate: (it: T) => boolean, defaultValue: T): T;
7
+ declare function safe<R>(fn: () => R): R | undefined;
11
8
 
12
- declare const alias: Record<string, string>;
9
+ type Nullable<T> = T | null | undefined;
10
+ type NotNullable<T> = Exclude<T, null | undefined>;
11
+ type NotNull<T> = Exclude<T, null>;
12
+ type NotUndefined<T> = Exclude<T, undefined>;
13
+ type Arrayable<T> = T | Array<T>;
14
+ type ElementOf<T> = T extends Array<infer Element> ? Element : never;
15
+ type Fn<R = void> = (...args: any[]) => R;
13
16
 
14
- export { alias, exists, readJson, readText, toMD5, writeJson, writeText };
17
+ declare const isFunction: (val: any) => val is Fn;
18
+ declare const isBoolean: (val: any) => val is boolean;
19
+ declare const isNumber: (val: any) => val is number;
20
+ declare const isString: (val: unknown) => val is string;
21
+ declare const isObject: (val: any) => val is object;
22
+ declare const isUndefined: (val: any) => val is undefined;
23
+ declare const isNull: (val: any) => val is null;
24
+ declare const isRegExp: (val: any) => val is RegExp;
25
+ declare const isDate: (val: any) => val is Date;
26
+ declare const isArray: (arg: any) => arg is any[];
27
+ declare const notNullish: <T>(v: T | null | undefined) => v is NonNullable<T>;
28
+ declare const notNull: <T>(v: T | null) => v is Exclude<T, null>;
29
+ declare const notUndefined: <T>(v: T | undefined) => v is Exclude<T, undefined>;
30
+
31
+ /**
32
+ * @description min <= result < max
33
+ */
34
+ declare function random(max: number, min?: number): number;
35
+
36
+ declare const timestamp: () => number;
37
+
38
+ export { type Arrayable, type ElementOf, type Fn, type NotNull, type NotNullable, type NotUndefined, type Nullable, isArray, isBoolean, isDate, isFunction, isNull, isNumber, isObject, isRegExp, isString, isUndefined, notNull, notNullish, notUndefined, random, safe, singleOrNull, take, takeIf, timestamp, toArray };
package/dist/index.mjs CHANGED
@@ -1,39 +1,43 @@
1
- export * from '@antfu/utils';
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';
6
- import { join } from 'node:path';
7
- export { default as glob } from 'fast-glob';
1
+ const isFunction = (val) => typeof val === "function";
2
+ const isBoolean = (val) => typeof val === "boolean";
3
+ const isNumber = (val) => typeof val === "number";
4
+ const isString = (val) => typeof val === "string";
5
+ const toString = (value) => Object.prototype.toString.call(value);
6
+ const isObject = (val) => toString(val) === "[object Object]";
7
+ const isUndefined = (val) => toString(val) === "[object Undefined]";
8
+ const isNull = (val) => toString(val) === "[object Null]";
9
+ const isRegExp = (val) => toString(val) === "[object RegExp]";
10
+ const isDate = (val) => toString(val) === "[object Date]";
11
+ const isArray = Array.isArray;
12
+ const notNullish = (v) => v != null;
13
+ const notNull = (v) => v !== null;
14
+ const notUndefined = (v) => v !== void 0;
8
15
 
9
- async function exists(...path) {
10
- return await access(join(...path), constants.F_OK).then(() => true, () => false);
16
+ function singleOrNull(arr) {
17
+ return arr.length === 1 ? arr[0] : null;
11
18
  }
12
- async function readText(...path) {
13
- return await readFile(join(...path), "utf-8");
19
+ function toArray(array) {
20
+ array = array ?? [];
21
+ return isArray(array) ? array : [array];
14
22
  }
15
- async function readJson(...path) {
16
- return JSON.parse(await readText(...path));
23
+
24
+ function take(value, fn) {
25
+ return fn(value);
17
26
  }
18
- async function writeText(content, ...path) {
19
- await writeFile(join(...path), content, "utf-8");
27
+ function takeIf(value, predicate, defaultValue) {
28
+ return predicate(value) ? value : notUndefined(defaultValue) ? defaultValue : null;
20
29
  }
21
- async function writeJson(content, ...path) {
22
- await writeText(`${JSON.stringify(content, null, 2)}
23
- `, ...path);
30
+ function safe(fn) {
31
+ try {
32
+ return fn();
33
+ } catch {
34
+ }
24
35
  }
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
- });
36
+
37
+ function random(max, min = 0) {
38
+ return Math.floor(Math.random() * max) + min;
33
39
  }
34
40
 
35
- const alias = {
36
- "@jiakun-zhao/utils": "@jiakun-zhao/utils/shared"
37
- };
41
+ const timestamp = () => +Date.now();
38
42
 
39
- export { alias, exists, readJson, readText, toMD5, writeJson, writeText };
43
+ export { isArray, isBoolean, isDate, isFunction, isNull, isNumber, isObject, isRegExp, isString, isUndefined, notNull, notNullish, notUndefined, random, safe, singleOrNull, take, takeIf, timestamp, toArray };
package/fs.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './dist/fs'
2
+ export { default } from './dist/fs'
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jiakun-zhao/utils",
3
3
  "type": "module",
4
- "version": "1.0.4",
4
+ "version": "1.1.1",
5
5
  "description": "Utils.",
6
6
  "author": "Jiakun Zhao <hi@zhaojiakun.com>",
7
7
  "license": "MIT",
@@ -19,10 +19,10 @@
19
19
  "import": "./dist/index.mjs",
20
20
  "require": "./dist/index.cjs"
21
21
  },
22
- "./shared": {
23
- "types": "./dist/shared.d.ts",
24
- "import": "./dist/shared.mjs",
25
- "require": "./dist/shared.cjs"
22
+ "./fs": {
23
+ "types": "./dist/fs.d.ts",
24
+ "import": "./dist/fs.mjs",
25
+ "require": "./dist/fs.cjs"
26
26
  }
27
27
  },
28
28
  "main": "./dist/index.cjs",
@@ -31,10 +31,9 @@
31
31
  "files": [
32
32
  "dist",
33
33
  "index.d.ts",
34
- "shared.d.ts"
34
+ "fs.d.ts"
35
35
  ],
36
36
  "dependencies": {
37
- "@antfu/utils": "^0.7.6",
38
37
  "fast-glob": "^3.3.2"
39
38
  },
40
39
  "devDependencies": {
package/dist/shared.cjs DELETED
@@ -1,33 +0,0 @@
1
- 'use strict';
2
-
3
- const utils = require('@antfu/utils');
4
-
5
- function singleOrNull(arr) {
6
- return arr.length === 1 ? arr[0] : null;
7
- }
8
-
9
- function random(max, min = 0) {
10
- return Math.floor(Math.random() * max) + min;
11
- }
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
-
26
- exports.random = random;
27
- exports.safe = safe;
28
- exports.singleOrNull = singleOrNull;
29
- exports.take = take;
30
- exports.takeIf = takeIf;
31
- Object.keys(utils).forEach(function (k) {
32
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = utils[k];
33
- });
package/dist/shared.d.cts DELETED
@@ -1,15 +0,0 @@
1
- export * from '@antfu/utils';
2
-
3
- declare function singleOrNull<T>(arr: T[]): T | null;
4
-
5
- /**
6
- * min <= result < max
7
- */
8
- declare function random(max: number, min?: number): number;
9
-
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 DELETED
@@ -1,15 +0,0 @@
1
- export * from '@antfu/utils';
2
-
3
- declare function singleOrNull<T>(arr: T[]): T | null;
4
-
5
- /**
6
- * min <= result < max
7
- */
8
- declare function random(max: number, min?: number): number;
9
-
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 DELETED
@@ -1,15 +0,0 @@
1
- export * from '@antfu/utils';
2
-
3
- declare function singleOrNull<T>(arr: T[]): T | null;
4
-
5
- /**
6
- * min <= result < max
7
- */
8
- declare function random(max: number, min?: number): number;
9
-
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 DELETED
@@ -1,25 +0,0 @@
1
- import { notUndefined } from '@antfu/utils';
2
- export * from '@antfu/utils';
3
-
4
- function singleOrNull(arr) {
5
- return arr.length === 1 ? arr[0] : null;
6
- }
7
-
8
- function random(max, min = 0) {
9
- return Math.floor(Math.random() * max) + min;
10
- }
11
-
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/shared.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './dist/shared'
2
- export { default } from './dist/shared'