-
Notifications
You must be signed in to change notification settings - Fork 0
/
string-utils.spec.ts
229 lines (213 loc) · 10.4 KB
/
string-utils.spec.ts
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { expect } from "chai";
import "mocha";
import { MockData } from "../MockData";
import * as Validators from "../validators";
import * as StringUtils from "./string-utils";
describe("StringUtils", () => {
const testString = "ąàáäâãåæăćčĉęèéëêĝĥìíïîĵłľńňòóöőôõðøśșşšŝťțţŭùúüűûñÿýçżźž";
const resultString = "aaaaaaaaaccceeeeeghiiiijllnnoooooooossssstttuuuuuunyyczzz";
const text = "abcdedabcdedabcdjidABdsdcdsjo";
describe("Capitalize", () => {
it("It should make first letter to upper and other letter to lower", () => {
expect(StringUtils.capitalize("gabo")).to.be.equal("Gabo");
expect(StringUtils.capitalize("GABO")).to.be.equal("Gabo");
expect(StringUtils.capitalize("gABO")).to.be.equal("Gabo");
});
});
describe("Format", () => {
it("It should replace default placeholder with parameters", () => {
expect(StringUtils.format("{} is a big {}", ["Gabo", "hero"])).to.be.equal("Gabo is a big hero");
});
it("It should replace custom placeholder with parameters", () => {
expect(StringUtils.format("<> is a big <>", ["Gabo", "hero"], "<>")).to.be.equal("Gabo is a big hero");
});
});
describe("UpperCamelCase", () => {
it("It should turn string into upper camel case", () => {
const result = "HelloWorldIAmComputer";
MockData.stringHelloWorldIAmComputer.forEach((word) => {
expect(StringUtils.toUpperCamelCase(word), `'${word}' should be converted to '${result}'`)
.to
.be
.equal(result);
});
});
});
describe("LowerCamelCase", () => {
it("It should turn string into lower camel case", () => {
const result = "helloWorldIAmComputer";
MockData.stringHelloWorldIAmComputer.forEach((word) => {
expect(StringUtils.toLowerCamelCase(word), `'${word}' should be converted to '${result}'`)
.to
.be
.equal(result);
});
});
});
describe("UpperSnakeCase", () => {
it("It should turn string into upper snake case", () => {
const result = "HELLO_WORLD_I_AM_COMPUTER";
MockData.stringHelloWorldIAmComputer.forEach((word) => {
expect(StringUtils.toUpperSnakeCase(word), `'${word}' should be converted to '${result}'`)
.to
.be
.equal(result);
});
});
});
describe("LowerSnakeCase", () => {
it("It should turn string into lower snake case", () => {
const result = "hello_world_i_am_computer";
MockData.stringHelloWorldIAmComputer.forEach((word) => {
expect(StringUtils.toLowerSnakeCase(word), `'${word}' should be converted to '${result}'`)
.to
.be
.equal(result);
});
});
});
describe("RemoveAccented", () => {
const notString: string | any = 23;
const finalTestString = testString + testString.toUpperCase();
const finalResultString = resultString + resultString.toUpperCase();
it("It should remove accented charactersString from string", () => {
expect(StringUtils.removeAccentedCharacters(finalTestString)).to.be.equal(finalResultString);
expect(StringUtils.removeAccentedCharacters(finalResultString)).to.be.equal(finalResultString);
expect(StringUtils.removeAccentedCharacters(MockData.charactersString))
.to
.be
.equal(MockData.charactersString);
expect(StringUtils.removeAccentedCharacters(notString)).to.equal(notString);
});
});
describe("Count", () => {
it("It should return number of occurrences of substring", () => {
expect(StringUtils.count("I am the most expensive and the best IDE on the world", "the")).to.equal(3);
});
});
describe("RemoveAll", () => {
it("It should remove all occurrences of substring from string", () => {
expect(StringUtils.removeAll(text, ["a"])).to.be.equal("bcdedbcdedbcdjidABdsdcdsjo");
expect(StringUtils.removeAll(text, ["a", "b", "c"])).to.be.equal("deddeddjidABdsddsjo");
});
});
describe("Between", () => {
it("It should return substring between two substrings", () => {
expect(StringUtils.between(MockData.numbersString, "34", "67")).to.be.equal("5");
expect(StringUtils.between(MockData.numbersString, "34", "ab")).to.be.equal("56789");
expect(StringUtils.between(MockData.numbersString, "ab", "67")).to.be.equal("012345");
});
});
describe("RemoveEmptyLines", () => {
it("it should remove empty lines from string", () => {
expect(StringUtils.removeEmptyLines(`
`)).to.be.equal("");
expect(StringUtils.removeEmptyLines(`
a
b
`)).to.be.equal(`a
b
`);
});
});
describe("Template", () => {
it("It should replace placeholders with values", () => {
const params = {
age : "23",
name: "Gabo",
};
expect(StringUtils.template("{{name}} is {{age}} years old", params)).to.be.equal("Gabo is 23 years old");
});
});
describe("Occurrences", () => {
it("It should return number of occurrences of substring", () => {
expect(StringUtils.occurrences("I am the most expensive and the best IDE on the world", "the")).to.equal(3);
expect(StringUtils.occurrences("foofoofoo", "bar")).to.equal(0);
expect(StringUtils.occurrences("foofoofoo", "foo")).to.equal(3);
expect(StringUtils.occurrences("foofoofoo", "foofoo")).to.equal(1);
expect(StringUtils.occurrences("foofoofoo", "foofoo", true)).to.equal(2);
});
});
describe("CollapseWhiteSpace", () => {
it("It should collapse whitespace", () => {
expect(StringUtils.collapseWhitespace(" i i i ")).to.equal(" i i i ");
expect(StringUtils.collapseWhitespace("i i i")).to.equal("i i i");
});
});
describe("IsEmpty", () => {
it("It should check if string contains any not white charactersString", () => {
MockData.charactersEmpty.forEach((character) => {
expect(Validators.isEmpty(" "), `'${character}' should be empty`).to.be.true;
});
expect(Validators.isEmpty(" 3 ")).to.be.false;
expect(Validators.isEmpty("a")).to.be.false;
expect(Validators.isEmpty("0")).to.be.false;
expect(Validators.isEmpty("[]")).to.be.false;
expect(Validators.isEmpty("A")).to.be.false;
expect(Validators.isEmpty("{}")).to.be.false;
expect(Validators.isEmpty(".")).to.be.false;
});
});
describe("SwapCase", () => {
it("It should swap string case", () => {
expect(StringUtils.swapCase(MockData.charactersString)).to.be.equal(MockData.charactersString);
expect(StringUtils.swapCase(MockData.numbersString)).to.be.equal(MockData.numbersString);
expect(StringUtils.swapCase(testString)).to.be.equal(testString.toUpperCase());
expect(StringUtils.swapCase("GABO")).to.be.equal("gabo");
expect(StringUtils.swapCase("gabo")).to.be.equal("GABO");
expect(StringUtils.swapCase("GaBo")).to.be.equal("gAbO");
expect(StringUtils.swapCase("gAbO")).to.be.equal("GaBo");
});
});
describe("GetLastPart", () => {
it("It should return last part of string divided by delimiter", () => {
expect(StringUtils.getLastPart("ababababa", "b")).to.be.equal("a");
expect(StringUtils.getLastPart("ababababaa", "b")).to.be.equal("aa");
expect(StringUtils.getLastPart("i am here")).to.be.equal("here");
expect(StringUtils.getLastPart("i ambhere", "b")).to.be.equal("here");
expect(StringUtils.getLastPart("", "")).to.be.equal("");
expect(StringUtils.getLastPart("")).to.be.equal("");
});
});
describe("IsValidPhoneNumber", () => {
it("It should return true if phone number is valid", () => {
[...MockData.randomStrings, ...MockData.emails, ""].forEach((num) => {
expect(Validators.isValidPhoneNumber(num), `'${num}' should not be phone number`).to.be.false;
});
MockData.phoneNumbers.forEach((num) => {
expect(Validators.isValidPhoneNumber(num), `'${num}' should be phone number`).to.be.true;
});
});
});
describe("GetAsciiArray", () => {
it("It should return array of number representing giver string ascii numbersString", () => {
expect(StringUtils.getAsciiArray("abcdefg")).to.deep.equal([97, 98, 99, 100, 101, 102, 103]);
});
});
describe("IsValidEmail", () => {
it("It should return true if email is valid", () => {
[...MockData.randomStrings, ...MockData.phoneNumbers, ...MockData.notEmails, ""].forEach((email) => {
expect(Validators.isValidEmail(email), `'${email}' should not be email`).to.be.false;
});
MockData.emails.forEach((email) => {
expect(Validators.isValidEmail(email), `'${email}' should be email`).to.be.true;
});
});
});
describe("CutUsing", () => {
it("It should return cut end and append suffix", () => {
expect(StringUtils.cutUsing("abcdefghij", 10)).to.be.equal("abcdefghij");
expect(StringUtils.cutUsing("abcdefghij", 15)).to.be.equal("abcdefghij");
expect(StringUtils.cutUsing("abcdefghij", 9)).to.be.equal("abcdefg...");
expect(StringUtils.cutUsing("abcdefghij", 9, "...", false)).to.be.equal("abcdefghi...");
});
});
describe("JoinSingle", () => {
it("It should return true if email is valid", () => {
expect(StringUtils.joinSingle("result", ".", "js")).to.be.equal("result.js");
expect(StringUtils.joinSingle("result.", ".", "js")).to.be.equal("result.js");
expect(StringUtils.joinSingle("result", ".", ".js")).to.be.equal("result.js");
expect(StringUtils.joinSingle("result.", ".", ".js")).to.be.equal("result.js");
});
});
});