forked from MrXujiang/xijs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.test.js
84 lines (73 loc) · 2.21 KB
/
string.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import {
base64,
camelize,
charCount,
formatNumber,
formatPercent,
hyphenate,
hyCompact,
randomStr,
repeat,
uuid,
completeIp,
} from '../src/index';
describe('字符串操作相关测试', () => {
test('base64 编码和解码', () => {
const normalStr = 'base64 编码和解码';
const encodeStr = base64.encode(normalStr);
const decodeStr = base64.decode(encodeStr);
expect(decodeStr).toBe(normalStr);
});
test('横线转驼峰命名', () => {
const initStr = 'ab-cd-ef-123--';
const transformStr = camelize(initStr);
const expectStr = 'abCdEf123--';
expect(transformStr).toBe(expectStr);
});
test('获取字符串中指定字符的个数', () => {
const s = 'abc-def-h5-dooring';
expect(charCount(s, '-')).toBe(3);
expect(charCount(s, '!')).toBe(0);
});
test('数值千分位格式化', () => {
const num = 123456.789;
const formatNum = formatNumber(num);
expect(formatNum).toBe(num.toLocaleString());
});
test('数值转换为百分数表示', () => {
const num1 = 0.456789,
num2 = 3,
num3 = 10.234;
const percentNum1 = formatPercent(num1, 1);
const percentNum2 = formatPercent(num2, 2);
const percentNum3 = formatPercent(num3, 3);
expect(percentNum1).toBe('45.7%');
expect(percentNum2).toBe('300%');
expect(percentNum3).toBe('1023.400%');
});
test('驼峰命名转横线命名', () => {
const s = 'AaBb1Cc2Dd_Ee.Ff';
expect(hyphenate(s)).toBe('aa-bb1-cc2-dd_-ee.ff');
expect(hyphenate(s, '_')).toBe('aa_bb1_cc2_dd__ee.ff');
});
test('紧凑型驼峰命名转横线命名', () => {
const s = 'AABBCcDd';
expect(hyCompact(s)).toBe('aa-bb-cc-dd');
expect(hyCompact(s, '+')).toBe('aa+bb+cc+dd');
});
test('生成随机字符串', () => {
const len = 6;
expect(randomStr(len).length).toBe(len);
});
test('生成一个重复的字符串,有 n 个 str 组成', () => {
const str = 'xijs';
expect(repeat(str, 5)).toBe('xijsxijsxijsxijsxijs');
});
test('生成唯一 id', () => {
// Why uuid is undefined?
console.log('uuid function ---> ', uuid);
});
test('ip 补全', () => {
expect(completeIp('127.0.0.1')).toBe('127.000.000.001');
});
});