words

说明

按照指定模式, 拆分字符串 string 中的词为数组

源码

用法

words.test.ts
1
import { words } from "../../utility/string/words";
2
3
describe('Method words() test: ', () => {
4
test('words() should use the default pattern while receive none pattern', () => {
5
const received = {
6
str: 'duan zhao _*$*$ y & ang a p g',
7
reg: /\W+/,
8
};
9
const expected = ["duan", "zhao _*$*$ y & ang a p g"];
10
11
const result = words(received.str, received.reg);
12
13
result.forEach((v, i) => {
14
expect(v).toBe(expected[i]);
15
});
16
});
17
18
test('words() can use custom pattern', () => {
19
const received = [
20
{
21
str: 'duan zhao yang',
22
reg: /\s+/g,
23
},
24
{
25
str: 'duan ### zhaoyang |%# s d h dan',
26
reg: /#+/g,
27
},
28
];
29
const expected = [
30
["duan", "zhao", "yang"],
31
["duan ", " zhaoyang ", "%", " s d h dan"],
32
];
33
34
received.forEach((outerV, outerI) => {
35
const result = words(outerV.str, outerV.reg);
36
37
result.forEach((innerV, innerI) => {
38
expect(innerV).toBe(expected[outerI][innerI]);
39
});
40
});
41
});
42
});
Copied!
最近更新 1yr ago
复制链接