getRandomStr

说明

获取随机乱序字符串

源码

用法

getRandomStr.test.ts
1
import { getRandomStr } from "../../utility/string/getRandomStr";
2
3
describe('getRandomStr', () => {
4
test('getRandomStr should return default string when not received parameters', () => {
5
const expected: [string, number] = [
6
'string',
7
16,
8
];
9
const result: string = getRandomStr();
10
11
expect(typeof result).toBe(expected[0]);
12
expect(result.length).toBe(expected[1]);
13
});
14
15
test('getRandomStr should return default string when received a empty array', () => {
16
const received: string[] = [];
17
const expected: [string, number] = [
18
'string',
19
16,
20
];
21
const result: string = getRandomStr(received);
22
23
expect(typeof result).toBe(expected[0]);
24
expect(result.length).toBe(expected[1]);
25
});
26
27
test('getRandomStr should return assignment length of string when received the length parameter', () => {
28
const received: [string[], number] = [
29
['a', 'b', 'c', '1', '2', '3', '_', '-'],
30
8,
31
];
32
const expected: [string, number] = [
33
'string',
34
8,
35
];
36
const result: string = getRandomStr(...received);
37
38
expect(typeof result).toBe(expected[0]);
39
expect(result.length).toBe(expected[1]);
40
});
41
42
test('getRandomStr should return a string that not contains special characters when received the truthy parameter names `enableSpecialCharacter`', () => {
43
const received: [
44
undefined,
45
undefined,
46
boolean,
47
] = [undefined, undefined, false];
48
const expected: RegExp = /[_\-&[email protected]^]+/g;
49
const result: string = getRandomStr(...received);
50
51
expect(expected.test(result)).toBeFalsy();
52
});
53
});
Copied!
最近更新 1yr ago
复制链接