a-js-tools 0.1.3 → 0.2.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,15 +1,6 @@
1
1
  # a-js-tools
2
2
 
3
- [![version](<https://img.shields.io/npm/v/a-js-tools.svg?logo=npm&logoColor=rgb(0,0,0)&label=版本号&labelColor=rgb(73,73,228)&color=rgb(0,0,0)>)](https://www.npmjs.com/package/a-js-tools) [![NPM Last Update](<https://img.shields.io/npm/last-update/a-js-tools?logo=npm&label=版本最后更新&labelColor=rgb(255,36,63)&color=rgb(0,0,0)>)](https://www.npmjs.com/package/a-js-tools) [![📦 size](<https://img.shields.io/bundlephobia/minzip/a-js-tools.svg?logo=npm&label=压缩包大小&labelColor=rgb(201,158,140)&color=rgb(0,0,0)>)](https://www.npmjs.com/package/a-js-tools) [![downloads](<https://img.shields.io/npm/dm/a-js-tools.svg?logo=npm&logoColor=rgb(0,0,0)&label=月下载量&labelColor=rgb(194,112,210)&color=rgb(0,0,0)>)](https://www.npmjs.com/package/a-js-tools) [![downloads](<https://img.shields.io/npm/dt/a-js-tools.svg?logo=npm&label=总下载量&labelColor=rgb(107,187,124)&color=rgb(0,0,0)>)](https://www.npmjs.com/package/a-js-tools)
4
-
5
- [![last commit](<https://img.shields.io/github/last-commit/earthnutDev/a-js-tools.svg?logo=github&logoColor=rgb(0,0,0)&label=最后推码&labelColor=rgb(255,165,0)&color=rgb(0,0,0)>)](https://github.com/earthnutDev/a-js-tools) [![GitHub commit activity](<https://img.shields.io/github/commit-activity/y/earthnutDev/a-js-tools.svg?logo=github&label=推码数&labelColor=rgb(128,0,128)&color=rgb(0,0,0)>)](https://github.com/earthnutDev/a-js-tools) [![Coverage Status](<https://img.shields.io/coverallsCoverage/github/earthnutDev/a-js-tools?logo=coveralls&label=coveralls&labelColor=rgb(12, 244, 39)&color=rgb(0,0,0)>)](https://coveralls.io/github/earthnutDev/a-js-tools?branch=main) [![codecov](<https://img.shields.io/codecov/c/github/earthnutDev/a-js-tools/main?logo=codecov&label=codecov&labelColor=rgb(7, 245, 245)&color=rgb(0,0,0)>)](https://codecov.io/gh/earthnutDev/a-js-tools)
6
-
7
- ---
8
-
9
- [![查看 📔 日志](<https://img.shields.io/badge/👀-日_%20_志-rgb(0,125,206)>)](https://github.com/earthnutDev/a-js-tools/blob/main/CHANGELOG.md) [![bug 🙋‍♂️ 提交](<https://img.shields.io/badge/☣️-bug_%20_提交-rgb(255,0,63)>)](https://github.com/earthnutDev/a-js-tools/issues)
10
-
11
- ---
12
-
3
+ [![version](<https://img.shields.io/npm/v/a-js-tools.svg?logo=npm&logoColor=rgb(0,0,0)&label=版本号&labelColor=rgb(73,73,228)&color=rgb(0,0,0)>)](https://www.npmjs.com/package/a-js-tools) [![Coverage Status](<https://img.shields.io/coverallsCoverage/github/earthnutDev/a-js-tools?logo=coveralls&label=coveralls&labelColor=rgb(12, 244, 39)&color=rgb(0,0,0)>)](https://coveralls.io/github/earthnutDev/a-js-tools?branch=main) [![codecov](<https://img.shields.io/codecov/c/github/earthnutDev/a-js-tools/main?logo=codecov&label=codecov&labelColor=rgb(7, 245, 245)&color=rgb(0,0,0)>)](https://codecov.io/gh/earthnutDev/a-js-tools) [![issues 提交](<https://img.shields.io/badge/issues-提交-rgb(255,0,63)?logo=github>)](https://github.com/earthnutDev/a-js-tools/issues)
13
4
  一个纯函数的工具
14
5
 
15
6
  ## 安装
@@ -24,11 +15,12 @@ npm install a-js-tools --save
24
15
  - `lmThrottle` 节流函数
25
16
  - `getRandomInt` 获取随机的整数
26
17
  - `getRandomFloat` 获取随机的浮点数
18
+ - `getRandomString` 获取随机字符串
27
19
 
28
20
  ## class 名称转化
29
21
 
30
- - `toLowerCamelCase` Convert hyphenation to hump
31
- - `toSplitCase` Convert small hump to hyphen split
22
+ - `toLowerCamelCase` 转化为小驼峰
23
+ - `toSplitCase` 转化为连接符分隔
32
24
 
33
25
  ## 查看文档
34
26
 
package/cjs/index.cjs CHANGED
@@ -2,8 +2,7 @@
2
2
 
3
3
  var getRandomNumber = require('./src/getRandomNumber.cjs');
4
4
  var className = require('./src/className.cjs');
5
- require('a-type-of-js');
6
- require('crypto');
5
+ var getRandomString = require('./src/getRandomString.cjs');
7
6
  var performance = require('./src/performance.cjs');
8
7
 
9
8
 
@@ -12,5 +11,6 @@ exports.getRandomFloat = getRandomNumber.getRandomFloat;
12
11
  exports.getRandomInt = getRandomNumber.getRandomInt;
13
12
  exports.toLowerCamelCase = className.toLowerCamelCase;
14
13
  exports.toSplitCase = className.toSplitCase;
14
+ exports.getRandomString = getRandomString.getRandomString;
15
15
  exports.debounce = performance.debounce;
16
16
  exports.throttle = performance.throttle;
@@ -0,0 +1,112 @@
1
+ 'use strict';
2
+
3
+ var aTypeOfJs = require('a-type-of-js');
4
+ var crypto$1 = require('crypto');
5
+
6
+ /**
7
+ * 获取随机字符串
8
+ *
9
+ * @packageDocumentation
10
+ * @module @a-js-tools/get-random-string
11
+ * @license MIT
12
+ */
13
+ /**
14
+ *
15
+ * 获取随机字符串
16
+ *
17
+ * @param length - 字符串长度
18
+ * @returns - 随机字符串
19
+ *
20
+ *
21
+ */
22
+ function getRandomString(length) {
23
+ // 验证输入参数
24
+ if (
25
+ // 参数类型错误
26
+ (!aTypeOfJs.isPlainObject(length) && !aTypeOfJs.isNumber(length)) ||
27
+ // 参数为 NaN
28
+ (aTypeOfJs.isNumber(length) && aTypeOfJs.isNaN(length)) ||
29
+ // 参数为数字时为无穷大
30
+ (aTypeOfJs.isNumber(length) && !isFinite(length)) ||
31
+ // 参数为数字时为非整数
32
+ (aTypeOfJs.isNumber(length) && !Number.isInteger(length)) ||
33
+ // 参数为数字时为负数
34
+ (aTypeOfJs.isNumber(length) && Number.isInteger(length) && length < 1) ||
35
+ // 参数为数值然而却小于 1
36
+ (aTypeOfJs.isNumber(length) && length < 1) ||
37
+ (aTypeOfJs.isPlainObject(length) && aTypeOfJs.isNumber(length.length) && length.length < 1)) {
38
+ throw new TypeError('Invalid argument type (getRandomString)');
39
+ }
40
+ const initOptions = {
41
+ length: 32,
42
+ chars: 'abcdefghijklmnopqrstuvwxyz',
43
+ chars2: '0123456789',
44
+ chars3: '!@#$%^&*()_+~`|}{[]:;?><,./-=',
45
+ type: 'string',
46
+ includeUppercaseLetters: false,
47
+ includeNumbers: false,
48
+ includeSpecial: false,
49
+ };
50
+ /// 生成 UUID
51
+ if (initOptions.type === 'uuid') {
52
+ return crypto.randomUUID();
53
+ }
54
+ if (aTypeOfJs.isNumber(length) && Number.isInteger(length) && length > 0) {
55
+ // 验证输入参数
56
+ Object.assign(initOptions, { length });
57
+ }
58
+ if (aTypeOfJs.isPlainObject(length)) {
59
+ Object.assign(initOptions, length);
60
+ initOptions.length = initOptions.length < 1 ? 32 : initOptions.length;
61
+ }
62
+ /** 生成随机字符串 */
63
+ const templateCharsArr = initOptions.chars.split('');
64
+ // 添加大写字母
65
+ if (initOptions.includeUppercaseLetters) {
66
+ interleaveString(templateCharsArr, initOptions.chars.toUpperCase());
67
+ }
68
+ // 添加数字
69
+ if (initOptions.includeNumbers) {
70
+ interleaveString(templateCharsArr, initOptions.chars2);
71
+ }
72
+ // 添加特殊字符
73
+ if (initOptions.includeSpecial) {
74
+ interleaveString(templateCharsArr, initOptions.chars3);
75
+ }
76
+ // 使用密码学安全的随机数生成器
77
+ const bytes = crypto$1.randomBytes(initOptions.length);
78
+ let result = '';
79
+ /** 获取最后的 chars 数据 */
80
+ const chars = templateCharsArr.join('');
81
+ // 循环遍历
82
+ bytes.forEach(byte => (result += chars.charAt(byte % chars.length)));
83
+ /**
84
+ *
85
+ * 字符串交叉函数
86
+ *
87
+ * 非线形串交叉,对相交叉
88
+ *
89
+ * @param str1 - 字符串1
90
+ * @param str2 - 字符串2
91
+ * @returns - 交叉后的字符串
92
+ * @example
93
+ * ```ts
94
+ * interleaveString('abc', '123') // 'a1b2c3'
95
+ * ```
96
+ */
97
+ function interleaveString(str1, str2) {
98
+ const str1Length = str1.length, str2Length = str2.length;
99
+ const maxLength = Math.max(str1Length, str2Length);
100
+ for (let i = 0; i < maxLength; i++) {
101
+ if (i < str1Length && str2[i] !== undefined) {
102
+ str1[i] += str2[i];
103
+ }
104
+ else if (i < str2Length) {
105
+ str1[i] = str2[i];
106
+ }
107
+ }
108
+ }
109
+ return result;
110
+ }
111
+
112
+ exports.getRandomString = getRandomString;
package/mjs/index.mjs CHANGED
@@ -1,5 +1,4 @@
1
1
  export { getRandomFloat, getRandomInt } from './src/getRandomNumber.mjs';
2
2
  export { toLowerCamelCase, toSplitCase } from './src/className.mjs';
3
- import 'a-type-of-js';
4
- import 'crypto';
3
+ export { getRandomString } from './src/getRandomString.mjs';
5
4
  export { debounce, throttle } from './src/performance.mjs';
@@ -0,0 +1,110 @@
1
+ import { isPlainObject, isNumber, isNaN } from 'a-type-of-js';
2
+ import { randomBytes } from 'crypto';
3
+
4
+ /**
5
+ * 获取随机字符串
6
+ *
7
+ * @packageDocumentation
8
+ * @module @a-js-tools/get-random-string
9
+ * @license MIT
10
+ */
11
+ /**
12
+ *
13
+ * 获取随机字符串
14
+ *
15
+ * @param length - 字符串长度
16
+ * @returns - 随机字符串
17
+ *
18
+ *
19
+ */
20
+ function getRandomString(length) {
21
+ // 验证输入参数
22
+ if (
23
+ // 参数类型错误
24
+ (!isPlainObject(length) && !isNumber(length)) ||
25
+ // 参数为 NaN
26
+ (isNumber(length) && isNaN(length)) ||
27
+ // 参数为数字时为无穷大
28
+ (isNumber(length) && !isFinite(length)) ||
29
+ // 参数为数字时为非整数
30
+ (isNumber(length) && !Number.isInteger(length)) ||
31
+ // 参数为数字时为负数
32
+ (isNumber(length) && Number.isInteger(length) && length < 1) ||
33
+ // 参数为数值然而却小于 1
34
+ (isNumber(length) && length < 1) ||
35
+ (isPlainObject(length) && isNumber(length.length) && length.length < 1)) {
36
+ throw new TypeError('Invalid argument type (getRandomString)');
37
+ }
38
+ const initOptions = {
39
+ length: 32,
40
+ chars: 'abcdefghijklmnopqrstuvwxyz',
41
+ chars2: '0123456789',
42
+ chars3: '!@#$%^&*()_+~`|}{[]:;?><,./-=',
43
+ type: 'string',
44
+ includeUppercaseLetters: false,
45
+ includeNumbers: false,
46
+ includeSpecial: false,
47
+ };
48
+ /// 生成 UUID
49
+ if (initOptions.type === 'uuid') {
50
+ return crypto.randomUUID();
51
+ }
52
+ if (isNumber(length) && Number.isInteger(length) && length > 0) {
53
+ // 验证输入参数
54
+ Object.assign(initOptions, { length });
55
+ }
56
+ if (isPlainObject(length)) {
57
+ Object.assign(initOptions, length);
58
+ initOptions.length = initOptions.length < 1 ? 32 : initOptions.length;
59
+ }
60
+ /** 生成随机字符串 */
61
+ const templateCharsArr = initOptions.chars.split('');
62
+ // 添加大写字母
63
+ if (initOptions.includeUppercaseLetters) {
64
+ interleaveString(templateCharsArr, initOptions.chars.toUpperCase());
65
+ }
66
+ // 添加数字
67
+ if (initOptions.includeNumbers) {
68
+ interleaveString(templateCharsArr, initOptions.chars2);
69
+ }
70
+ // 添加特殊字符
71
+ if (initOptions.includeSpecial) {
72
+ interleaveString(templateCharsArr, initOptions.chars3);
73
+ }
74
+ // 使用密码学安全的随机数生成器
75
+ const bytes = randomBytes(initOptions.length);
76
+ let result = '';
77
+ /** 获取最后的 chars 数据 */
78
+ const chars = templateCharsArr.join('');
79
+ // 循环遍历
80
+ bytes.forEach(byte => (result += chars.charAt(byte % chars.length)));
81
+ /**
82
+ *
83
+ * 字符串交叉函数
84
+ *
85
+ * 非线形串交叉,对相交叉
86
+ *
87
+ * @param str1 - 字符串1
88
+ * @param str2 - 字符串2
89
+ * @returns - 交叉后的字符串
90
+ * @example
91
+ * ```ts
92
+ * interleaveString('abc', '123') // 'a1b2c3'
93
+ * ```
94
+ */
95
+ function interleaveString(str1, str2) {
96
+ const str1Length = str1.length, str2Length = str2.length;
97
+ const maxLength = Math.max(str1Length, str2Length);
98
+ for (let i = 0; i < maxLength; i++) {
99
+ if (i < str1Length && str2[i] !== undefined) {
100
+ str1[i] += str2[i];
101
+ }
102
+ else if (i < str2Length) {
103
+ str1[i] = str2[i];
104
+ }
105
+ }
106
+ }
107
+ return result;
108
+ }
109
+
110
+ export { getRandomString };
package/package.json CHANGED
@@ -1,17 +1,19 @@
1
1
  {
2
2
  "type": "module",
3
- "version": "0.1.3",
3
+ "version": "0.2.0",
4
4
  "name": "a-js-tools",
5
+ "description": "一点点 🤏 js 函数",
6
+ "license": "ISC",
7
+ "dependencies": {
8
+ "a-type-of-js": ">=0.2.0 <1.0.0"
9
+ },
5
10
  "main": "cjs/index.cjs",
6
11
  "module": "mjs/index.mjs",
7
- "typings": "types/index.d.ts",
8
- "description": "寒酸的 js 函数,建议出门右拐直接 npm install --save lodash",
12
+ "types": "types/index.d.ts",
9
13
  "files": [
10
14
  "mjs",
11
15
  "cjs",
12
- "types",
13
- "CHANGELOG-en.md",
14
- "CHANGELOG.md"
16
+ "types"
15
17
  ],
16
18
  "exports": {
17
19
  "import": "./mjs/index.mjs",
@@ -31,7 +33,6 @@
31
33
  "url": "https://earthnut.dev/about"
32
34
  },
33
35
  "homepage": "https://earthnut.dev/a-js-tools",
34
- "license": "ISC",
35
36
  "bugs": {
36
37
  "url": "https://github.com/earthnutDev/a-js-tools/issues",
37
38
  "email": "earthnut.dev@outlook.com"
@@ -39,8 +40,5 @@
39
40
  "publishConfig": {
40
41
  "access": "public",
41
42
  "registry": "https://registry.npmjs.org/"
42
- },
43
- "dependencies": {
44
- "a-type-of-js": ">=0.1.6 <1.0.0"
45
43
  }
46
44
  }
package/types/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { toLowerCamelCase, toSplitCase, getRandomFloat, getRandomInt, } from './src/index';
1
+ export { toLowerCamelCase, toSplitCase, getRandomFloat, getRandomInt, getRandomString, } from './src/index';
2
2
  export { throttle, debounce } from './src/performance';
3
3
  export type { DebounceAndThrottleReturnType } from './src/performance';