|
1 | | -import { numberSet } from './charSet'; |
| 1 | +import { numberSet, hexSet, base64Set, uppercaseCharactorset, lowercaseCharactorSet, alphanumericSet, specialCharactorSet } from './charSet'; |
2 | 2 |
|
3 | 3 | // supported gengerated string types |
4 | | -enum rsType { number, string, base64, hex, password, customized }; |
| 4 | +enum rsType { number, string, base64, hex, alphanumeric, password, customized }; |
5 | 5 |
|
6 | 6 | // supported charactor set types |
7 | | -enum rsCharSetType { number, } |
| 7 | +// enum rsCharSetType { number, } |
8 | 8 |
|
| 9 | +interface RSOption { |
| 10 | + length?: number, |
| 11 | + type?: string, |
| 12 | + capitalization?: boolean, |
| 13 | + charactorSet?: [], |
| 14 | + excludeCharSet?: string |
| 15 | +} |
| 16 | + |
| 17 | +const rs = (option: RSOption): string => { |
| 18 | + // 判断参数格式并处理参数 |
| 19 | + option = option || {}; |
| 20 | + option.length = option.length || 8; |
| 21 | + option.type = option.type || 'number'; |
| 22 | + option.capitalization = option.capitalization || false; |
| 23 | + option.excludeCharSet = option.excludeCharSet || ''; |
| 24 | + option.charactorSet = option.charactorSet || []; |
| 25 | + |
| 26 | + let charset: string[]; |
| 27 | + // set charactor set |
| 28 | + switch (rsType[option.type]) { |
| 29 | + /** |
| 30 | + * 步骤: |
| 31 | + * 1、 获取指定类型的字符集合(customized用户自定义类型除外) |
| 32 | + * 2、 加上获取用户提供的charactorSet字符集合 |
| 33 | + * 3、 去除用户提供的excludeCharSet集合 |
| 34 | + */ |
| 35 | + case rsType.number: |
| 36 | + charset = numberSet.split(''); |
| 37 | + break; |
| 38 | + case rsType.base64: |
| 39 | + charset = base64Set.split(''); |
| 40 | + break; |
| 41 | + case rsType.hex: |
| 42 | + charset = hexSet.split(''); |
| 43 | + break; |
| 44 | + case rsType.string: |
| 45 | + charset = lowercaseCharactorSet.split(''); |
| 46 | + if (option.capitalization) { |
| 47 | + charset = charset.concat(uppercaseCharactorset.split('')); |
| 48 | + } |
| 49 | + break; |
| 50 | + case rsType.alphanumeric: |
| 51 | + charset = alphanumericSet.split(''); |
| 52 | + break; |
| 53 | + case rsType.password: |
| 54 | + if (option.length < 8) { |
| 55 | + console.warn('strongly recommended that the password length be no less than 8'); |
| 56 | + } |
| 57 | + charset = (specialCharactorSet + alphanumericSet).split(''); |
| 58 | + break; |
| 59 | + case rsType.customized: |
| 60 | + charset = option.charactorSet.join('').split(''); |
| 61 | + break; |
| 62 | + default: |
| 63 | + throw new Error('the assigned type is not recognizable'); |
| 64 | + break; |
| 65 | + } |
| 66 | + execExcludeCharSet(charset, option.excludeCharSet); |
| 67 | + return getRandomString(option.length, charset); |
| 68 | +} |
9 | 69 |
|
10 | | -interface argments { |
11 | | - length: number, |
12 | | - type: string, |
13 | | - capitalization: boolean, |
14 | | - charactorSet: [] |
| 70 | +/** |
| 71 | + * remove user-specified exclude charset |
| 72 | + * @param originCharSet 原有的字符集合 |
| 73 | + * @param excludeCharSet 移除的字符集合 |
| 74 | + */ |
| 75 | +const execExcludeCharSet = (originCharSet: string[], excludeCharSet: string) => { |
| 76 | + if (excludeCharSet.length !== 0) { |
| 77 | + excludeCharSet.trim().split('').map((value: string) => { |
| 78 | + if (originCharSet.includes(value)) { |
| 79 | + let deleteCharIndex = originCharSet.indexOf(value); |
| 80 | + originCharSet.splice(deleteCharIndex, 1); |
| 81 | + } else { |
| 82 | + throw new Error('excludeCharSet must be in the assigned type set...'); |
| 83 | + } |
| 84 | + }) |
| 85 | + } |
15 | 86 | } |
16 | 87 |
|
17 | | -const rs = (option: argments): string => { |
| 88 | +/** |
| 89 | + * generator random string |
| 90 | + * @param stringLen charactor set's length |
| 91 | + * @param charset charatctor set |
| 92 | + */ |
| 93 | +const getRandomString = (stringLen: number, charset: string[]): string => { |
18 | 94 | let res = ''; |
19 | | - for (let i = 0; i < option.length; i++) { |
20 | | - res += numberSet.charAt(getIndex(option.length)); |
| 95 | + for (let i = 0; i < stringLen; i++) { |
| 96 | + let index = getIndex(charset.length); |
| 97 | + res += charset[index]; |
21 | 98 | } |
22 | 99 | return res; |
23 | 100 | } |
24 | 101 |
|
| 102 | +/** |
| 103 | + * generatior random subscript |
| 104 | + * @param setLength |
| 105 | + */ |
25 | 106 | const getIndex = (setLength: number): number => { |
26 | 107 | const charIndex: number = parseFloat(Math.random().toFixed(2)) * setLength; |
27 | | - return charIndex % setLength; |
| 108 | + return Math.floor(charIndex % setLength); |
28 | 109 | } |
29 | 110 |
|
30 | 111 | export default rs; |
0 commit comments