a-js-tools 0.1.4 → 0.2.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 CHANGED
@@ -15,6 +15,7 @@ npm install a-js-tools --save
15
15
  - `lmThrottle` 节流函数
16
16
  - `getRandomInt` 获取随机的整数
17
17
  - `getRandomFloat` 获取随机的浮点数
18
+ - `getRandomString` 获取随机字符串
18
19
 
19
20
  ## class 名称转化
20
21
 
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,113 @@
1
+ 'use strict';
2
+
3
+ var aTypeOfJs = require('a-type-of-js');
4
+
5
+ /**
6
+ * 获取随机字符串
7
+ *
8
+ * @packageDocumentation
9
+ * @module @a-js-tools/get-random-string
10
+ * @license MIT
11
+ */
12
+ /**
13
+ *
14
+ * 获取随机字符串
15
+ *
16
+ * @param length - 字符串长度
17
+ * @returns - 随机字符串
18
+ *
19
+ *
20
+ */
21
+ function getRandomString(length) {
22
+ // 验证输入参数
23
+ if (
24
+ // 参数类型错误
25
+ (!aTypeOfJs.isPlainObject(length) && !aTypeOfJs.isNumber(length)) ||
26
+ // 参数为 NaN
27
+ (aTypeOfJs.isNumber(length) && aTypeOfJs.isNaN(length)) ||
28
+ // 参数为数字时为无穷大
29
+ (aTypeOfJs.isNumber(length) && !isFinite(length)) ||
30
+ // 参数为数字时为非整数
31
+ (aTypeOfJs.isNumber(length) && !Number.isInteger(length)) ||
32
+ // 参数为数字时为负数
33
+ (aTypeOfJs.isNumber(length) && Number.isInteger(length) && length < 1) ||
34
+ // 参数为数值然而却小于 1
35
+ (aTypeOfJs.isNumber(length) && length < 1) ||
36
+ (aTypeOfJs.isPlainObject(length) && aTypeOfJs.isNumber(length.length) && length.length < 1)) {
37
+ throw new TypeError('Invalid argument type (getRandomString)');
38
+ }
39
+ const initOptions = {
40
+ length: 32,
41
+ chars: 'abcdefghijklmnopqrstuvwxyz',
42
+ chars2: '0123456789',
43
+ chars3: '!@#$%^&*()_+~`|}{[]:;?><,./-=',
44
+ type: 'string',
45
+ includeUppercaseLetters: false,
46
+ includeNumbers: false,
47
+ includeSpecial: false,
48
+ };
49
+ /// 生成 UUID
50
+ if (initOptions.type === 'uuid') {
51
+ return crypto.randomUUID();
52
+ }
53
+ if (aTypeOfJs.isNumber(length) && Number.isInteger(length) && length > 0) {
54
+ // 验证输入参数
55
+ Object.assign(initOptions, { length });
56
+ }
57
+ if (aTypeOfJs.isPlainObject(length)) {
58
+ Object.assign(initOptions, length);
59
+ initOptions.length = initOptions.length < 1 ? 32 : initOptions.length;
60
+ }
61
+ /** 生成随机字符串 */
62
+ const templateCharsArr = initOptions.chars.split('');
63
+ // 添加大写字母
64
+ if (initOptions.includeUppercaseLetters) {
65
+ interleaveString(templateCharsArr, initOptions.chars.toUpperCase());
66
+ }
67
+ // 添加数字
68
+ if (initOptions.includeNumbers) {
69
+ interleaveString(templateCharsArr, initOptions.chars2);
70
+ }
71
+ // 添加特殊字符
72
+ if (initOptions.includeSpecial) {
73
+ interleaveString(templateCharsArr, initOptions.chars3);
74
+ }
75
+ // 使用密码学安全的随机数生成器
76
+ const bytes = !aTypeOfJs.isUndefined(window) && window.crypto
77
+ ? window.crypto.getRandomValues(new Uint8Array(initOptions.length))
78
+ : global.crypto.getRandomValues(new Uint8Array(initOptions.length));
79
+ let result = '';
80
+ /** 获取最后的 chars 数据 */
81
+ const chars = templateCharsArr.join('');
82
+ // 循环遍历
83
+ bytes.forEach(byte => (result += chars.charAt(byte % chars.length)));
84
+ /**
85
+ *
86
+ * 字符串交叉函数
87
+ *
88
+ * 非线形串交叉,对相交叉
89
+ *
90
+ * @param str1 - 字符串1
91
+ * @param str2 - 字符串2
92
+ * @returns - 交叉后的字符串
93
+ * @example
94
+ * ```ts
95
+ * interleaveString('abc', '123') // 'a1b2c3'
96
+ * ```
97
+ */
98
+ function interleaveString(str1, str2) {
99
+ const str1Length = str1.length, str2Length = str2.length;
100
+ const maxLength = Math.max(str1Length, str2Length);
101
+ for (let i = 0; i < maxLength; i++) {
102
+ if (i < str1Length && str2[i] !== undefined) {
103
+ str1[i] += str2[i];
104
+ }
105
+ else if (i < str2Length) {
106
+ str1[i] = str2[i];
107
+ }
108
+ }
109
+ }
110
+ return result;
111
+ }
112
+
113
+ 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,111 @@
1
+ import { isPlainObject, isNumber, isNaN, isUndefined } from 'a-type-of-js';
2
+
3
+ /**
4
+ * 获取随机字符串
5
+ *
6
+ * @packageDocumentation
7
+ * @module @a-js-tools/get-random-string
8
+ * @license MIT
9
+ */
10
+ /**
11
+ *
12
+ * 获取随机字符串
13
+ *
14
+ * @param length - 字符串长度
15
+ * @returns - 随机字符串
16
+ *
17
+ *
18
+ */
19
+ function getRandomString(length) {
20
+ // 验证输入参数
21
+ if (
22
+ // 参数类型错误
23
+ (!isPlainObject(length) && !isNumber(length)) ||
24
+ // 参数为 NaN
25
+ (isNumber(length) && isNaN(length)) ||
26
+ // 参数为数字时为无穷大
27
+ (isNumber(length) && !isFinite(length)) ||
28
+ // 参数为数字时为非整数
29
+ (isNumber(length) && !Number.isInteger(length)) ||
30
+ // 参数为数字时为负数
31
+ (isNumber(length) && Number.isInteger(length) && length < 1) ||
32
+ // 参数为数值然而却小于 1
33
+ (isNumber(length) && length < 1) ||
34
+ (isPlainObject(length) && isNumber(length.length) && length.length < 1)) {
35
+ throw new TypeError('Invalid argument type (getRandomString)');
36
+ }
37
+ const initOptions = {
38
+ length: 32,
39
+ chars: 'abcdefghijklmnopqrstuvwxyz',
40
+ chars2: '0123456789',
41
+ chars3: '!@#$%^&*()_+~`|}{[]:;?><,./-=',
42
+ type: 'string',
43
+ includeUppercaseLetters: false,
44
+ includeNumbers: false,
45
+ includeSpecial: false,
46
+ };
47
+ /// 生成 UUID
48
+ if (initOptions.type === 'uuid') {
49
+ return crypto.randomUUID();
50
+ }
51
+ if (isNumber(length) && Number.isInteger(length) && length > 0) {
52
+ // 验证输入参数
53
+ Object.assign(initOptions, { length });
54
+ }
55
+ if (isPlainObject(length)) {
56
+ Object.assign(initOptions, length);
57
+ initOptions.length = initOptions.length < 1 ? 32 : initOptions.length;
58
+ }
59
+ /** 生成随机字符串 */
60
+ const templateCharsArr = initOptions.chars.split('');
61
+ // 添加大写字母
62
+ if (initOptions.includeUppercaseLetters) {
63
+ interleaveString(templateCharsArr, initOptions.chars.toUpperCase());
64
+ }
65
+ // 添加数字
66
+ if (initOptions.includeNumbers) {
67
+ interleaveString(templateCharsArr, initOptions.chars2);
68
+ }
69
+ // 添加特殊字符
70
+ if (initOptions.includeSpecial) {
71
+ interleaveString(templateCharsArr, initOptions.chars3);
72
+ }
73
+ // 使用密码学安全的随机数生成器
74
+ const bytes = !isUndefined(window) && window.crypto
75
+ ? window.crypto.getRandomValues(new Uint8Array(initOptions.length))
76
+ : global.crypto.getRandomValues(new Uint8Array(initOptions.length));
77
+ let result = '';
78
+ /** 获取最后的 chars 数据 */
79
+ const chars = templateCharsArr.join('');
80
+ // 循环遍历
81
+ bytes.forEach(byte => (result += chars.charAt(byte % chars.length)));
82
+ /**
83
+ *
84
+ * 字符串交叉函数
85
+ *
86
+ * 非线形串交叉,对相交叉
87
+ *
88
+ * @param str1 - 字符串1
89
+ * @param str2 - 字符串2
90
+ * @returns - 交叉后的字符串
91
+ * @example
92
+ * ```ts
93
+ * interleaveString('abc', '123') // 'a1b2c3'
94
+ * ```
95
+ */
96
+ function interleaveString(str1, str2) {
97
+ const str1Length = str1.length, str2Length = str2.length;
98
+ const maxLength = Math.max(str1Length, str2Length);
99
+ for (let i = 0; i < maxLength; i++) {
100
+ if (i < str1Length && str2[i] !== undefined) {
101
+ str1[i] += str2[i];
102
+ }
103
+ else if (i < str2Length) {
104
+ str1[i] = str2[i];
105
+ }
106
+ }
107
+ }
108
+ return result;
109
+ }
110
+
111
+ export { getRandomString };
package/package.json CHANGED
@@ -1,11 +1,15 @@
1
1
  {
2
2
  "type": "module",
3
- "version": "0.1.4",
3
+ "version": "0.2.1",
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 函数",
12
+ "types": "types/index.d.ts",
9
13
  "files": [
10
14
  "mjs",
11
15
  "cjs",
@@ -29,7 +33,6 @@
29
33
  "url": "https://earthnut.dev/about"
30
34
  },
31
35
  "homepage": "https://earthnut.dev/a-js-tools",
32
- "license": "ISC",
33
36
  "bugs": {
34
37
  "url": "https://github.com/earthnutDev/a-js-tools/issues",
35
38
  "email": "earthnut.dev@outlook.com"
@@ -37,8 +40,5 @@
37
40
  "publishConfig": {
38
41
  "access": "public",
39
42
  "registry": "https://registry.npmjs.org/"
40
- },
41
- "dependencies": {
42
- "a-type-of-js": ">=0.2.0 <1.0.0"
43
43
  }
44
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';