foreslash 0.0.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/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # Foreslash
2
+
3
+ Foreslash 是一个 javascript 工具库,包含大量函数。
4
+
5
+ Foreslash is a javascript utilities lib which contains plenty of functions.
6
+
7
+ ## 安装
8
+
9
+ ```bash
10
+ npm install foreslash # 使用 npm 安装
11
+ yarn add foreslash # 使用 yarn 安装
12
+ pnpm install foreslash # 使用 pnpm 安装
13
+ ```
package/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './random/index';
2
+ export * from './utils/index';
package/lib/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './random/index';
2
+ export * from './utils/index';
@@ -0,0 +1,3 @@
1
+ export * from './randomChoice';
2
+ export * from './randomInt';
3
+ export * from './randomString';
@@ -0,0 +1,3 @@
1
+ export * from './randomChoice';
2
+ export * from './randomInt';
3
+ export * from './randomString';
@@ -0,0 +1,6 @@
1
+ /**
2
+ * 从给定的数组中返回随机一个元素
3
+ * @param arr 可以是数组或类数组对象
4
+ * @returns 从数组中随机选择的元素
5
+ */
6
+ export declare function randomChoice<T>(arr: ArrayLike<T>): T;
@@ -0,0 +1,9 @@
1
+ import { randomIntFloor } from './randomInt';
2
+ /**
3
+ * 从给定的数组中返回随机一个元素
4
+ * @param arr 可以是数组或类数组对象
5
+ * @returns 从数组中随机选择的元素
6
+ */
7
+ export function randomChoice(arr) {
8
+ return arr[randomIntFloor(0, arr.length)];
9
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * 生成指定范围内的随机整数
3
+ * @param min 随机数的下界,包含于此范围内
4
+ * @param max 随机数的上界,包含于此范围内
5
+ * @returns 返回一个在`[min, max]`范围内的随机整数
6
+ */
7
+ export declare function randomInt(min: number, max: number): number;
8
+ /**
9
+ * 生成指定范围内的随机整数,**不含上界**
10
+ * @param min 随机数的下界,包含于此范围内
11
+ * @param max 随机数的上界,**不包含**于此范围内
12
+ * @returns 返回一个在`[min, max)`范围内的随机整数
13
+ */
14
+ export declare function randomIntFloor(min: number, max: number): number;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * 生成指定范围内的随机整数
3
+ * @param min 随机数的下界,包含于此范围内
4
+ * @param max 随机数的上界,包含于此范围内
5
+ * @returns 返回一个在`[min, max]`范围内的随机整数
6
+ */
7
+ export function randomInt(min, max) {
8
+ return Math.floor(Math.random() * (max - min + 1)) + min;
9
+ }
10
+ /**
11
+ * 生成指定范围内的随机整数,**不含上界**
12
+ * @param min 随机数的下界,包含于此范围内
13
+ * @param max 随机数的上界,**不包含**于此范围内
14
+ * @returns 返回一个在`[min, max)`范围内的随机整数
15
+ */
16
+ export function randomIntFloor(min, max) {
17
+ return Math.floor(Math.random() * (max - min)) + min;
18
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * 生成指定长度的随机字符串
3
+ * @param length 字符串的长度
4
+ * @param chars 字符集,默认为大小写字母和数字
5
+ * @returns 生成的随机字符串
6
+ */
7
+ export declare function randomString(length: number, chars?: string): string;
8
+ /**
9
+ * 生成指定长度的随机十六进制字符串(小写),`randomString`也能实现此功能但是性能更差
10
+ * @param length 字符串的长度
11
+ * @returns 返回一个长度为`length`的随机十六进制字符串
12
+ */
13
+ export declare function randomHexString(length: number): string;
@@ -0,0 +1,50 @@
1
+ import { randomChoice } from './randomChoice';
2
+ /**
3
+ * 生成指定长度的随机字符串
4
+ * @param length 字符串的长度
5
+ * @param chars 字符集,默认为大小写字母和数字
6
+ * @returns 生成的随机字符串
7
+ */
8
+ export function randomString(length, chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {
9
+ if (!Number.isInteger(length) || length <= 0) {
10
+ throw new Error('Invalid length parameter');
11
+ }
12
+ let res = '';
13
+ for (let i = 0; i < length; i++)
14
+ res += randomChoice(chars);
15
+ return res;
16
+ }
17
+ /**
18
+ * 生成指定长度的随机十六进制字符串(小写),`randomString`也能实现此功能但是性能更差
19
+ * @param length 字符串的长度
20
+ * @returns 返回一个长度为`length`的随机十六进制字符串
21
+ */
22
+ export function randomHexString(length) {
23
+ // return randomString(length, '0123456789abcdef')
24
+ if (!Number.isInteger(length) || length <= 0) {
25
+ throw new Error('Invalid length parameter');
26
+ }
27
+ // length 大于 13 时需要分多次生成,避免超出 Number.MAX_SAFE_INTEGER
28
+ // 16 ** 13 < Number.MAX_SAFE_INTEGER < 16 ** 14
29
+ if (length > 13) {
30
+ const count = Math.floor(length / 13);
31
+ let res = _randomHexString(length % 13);
32
+ for (let i = 0; i < count; i++)
33
+ res += _randomHexString(13);
34
+ return res;
35
+ }
36
+ else {
37
+ return _randomHexString(length);
38
+ }
39
+ }
40
+ /**
41
+ * 内部方法,生成指定长度的随机十六进制字符串
42
+ * @param length 字符串的长度,不能超过 13
43
+ * @returns 返回一个长度为`length`的随机十六进制字符串
44
+ */
45
+ function _randomHexString(length) {
46
+ let res = Math.floor(Math.random() * 16 ** length).toString(16);
47
+ while (res.length < length)
48
+ res = '0' + res;
49
+ return res;
50
+ }
@@ -0,0 +1 @@
1
+ export * from './noop';
@@ -0,0 +1 @@
1
+ export * from './noop';
@@ -0,0 +1,13 @@
1
+ /**
2
+ * 不做任何操作,返回`void`,一般用于函数式编程
3
+ */
4
+ declare const noop: (...args: any[]) => void;
5
+ /**
6
+ * 不做任何操作,返回第一个参数,一般用于函数式编程
7
+ * @param value 任意值
8
+ * @returns 传入的第一个值
9
+ */
10
+ export declare function pass<T>(value: T, ...args: any[]): T;
11
+ export declare function pass<T>(...args: T[]): T;
12
+ export declare function pass(): undefined;
13
+ export { noop };
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 不做任何操作,返回`void`,一般用于函数式编程
3
+ */
4
+ const noop = /*#__PURE__*/ function noop() { };
5
+ export function pass(value) {
6
+ return value;
7
+ }
8
+ export { noop };
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "foreslash",
3
+ "version": "0.0.1",
4
+ "description": "Foreslash is a javascript utilities lib which contains plenty of functions.",
5
+ "main": "lib/index.js",
6
+ "files": [
7
+ "lib"
8
+ ],
9
+ "scripts": {
10
+ "test": "jest --coverage"
11
+ },
12
+ "keywords": [
13
+ "foreslash",
14
+ "lib",
15
+ "util",
16
+ "modules"
17
+ ],
18
+ "author": "moushu",
19
+ "license": "ISC",
20
+ "bugs": {
21
+ "url": "https://github.com/Moushudyx/foreslash/issues"
22
+ },
23
+ "homepage": "https://github.com/Moushudyx/foreslash#readme",
24
+ "devDependencies": {
25
+ "@types/jest": "^29.5.12",
26
+ "jest": "^29.7.0",
27
+ "jest-environment-jsdom": "^29.7.0",
28
+ "ts-jest": "^29.2.5",
29
+ "typescript": "^5.5.4",
30
+ "yarn": "^1.22.22"
31
+ }
32
+ }