Skip to content

Commit 0490231

Browse files
committed
finish basical part
1 parent 948a0ef commit 0490231

File tree

3 files changed

+145
-16
lines changed

3 files changed

+145
-16
lines changed

example/index.html

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,54 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<meta name="viewport" content="width=device-width, initial-scale=1.0">
67
<meta http-equiv="X-UA-Compatible" content="ie=edge">
78
<title>Random-string test</title>
89
</head>
10+
911
<body>
1012
<script src="./rs.js"></script>
1113
<script>
12-
console.log(rs);
13-
console.log(rs({length: 8}));
14+
console.log(rs({
15+
type: 'number',
16+
length: 6,
17+
excludeCharSet: '567'
18+
}));
19+
// hex
20+
console.log(rs({
21+
type: 'hex',
22+
length: 30,
23+
excludeCharSet: 'abcde'
24+
}));
25+
// base64
26+
console.log(rs({
27+
type: 'base64',
28+
length: 100
29+
}))
30+
// string
31+
console.log(rs({
32+
type: 'string',
33+
length: 20,
34+
capitalization: true
35+
}))
36+
// alphanumeric
37+
console.log(rs({
38+
type: 'alphanumeric',
39+
length: 20,
40+
}))
41+
// password
42+
console.log(rs({
43+
type: 'password',
44+
length: 16
45+
}))
46+
console.log(rs({
47+
type: 'customized',
48+
length: 20,
49+
charactorSet: ['abcdefg@#$%^&*']
50+
}))
1451
</script>
1552
</body>
53+
1654
</html>

src/charSet.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,17 @@ const alphabeticSet: string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW
1010

1111
const hexSet: string = '0123456789abcdef'
1212

13-
const symbolSet: string = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~';
13+
const base64Set: string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/=';
1414

15+
const specialCharactorSet: string = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~';
1516

16-
export {numberSet};
17+
18+
export {
19+
numberSet,
20+
hexSet,
21+
base64Set,
22+
lowercaseCharactorSet,
23+
uppercaseCharactorset,
24+
alphanumericSet,
25+
specialCharactorSet
26+
};

src/index.ts

Lines changed: 93 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,111 @@
1-
import { numberSet } from './charSet';
1+
import { numberSet, hexSet, base64Set, uppercaseCharactorset, lowercaseCharactorSet, alphanumericSet, specialCharactorSet } from './charSet';
22

33
// supported gengerated string types
4-
enum rsType { number, string, base64, hex, password, customized };
4+
enum rsType { number, string, base64, hex, alphanumeric, password, customized };
55

66
// supported charactor set types
7-
enum rsCharSetType { number, }
7+
// enum rsCharSetType { number, }
88

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+
}
969

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+
}
1586
}
1687

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 => {
1894
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];
2198
}
2299
return res;
23100
}
24101

102+
/**
103+
* generatior random subscript
104+
* @param setLength
105+
*/
25106
const getIndex = (setLength: number): number => {
26107
const charIndex: number = parseFloat(Math.random().toFixed(2)) * setLength;
27-
return charIndex % setLength;
108+
return Math.floor(charIndex % setLength);
28109
}
29110

30111
export default rs;

0 commit comments

Comments
 (0)