-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
string-utils.js
88 lines (75 loc) · 3.13 KB
/
string-utils.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
85
86
87
88
/**
* @fileoverview Tests for string utils.
* @author Stephen Wade
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const assert = require("chai").assert;
const { upperCaseFirst, getGraphemeCount } = require("../../../lib/shared/string-utils");
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* Replaces raw control characters with the `\xXX` form.
* @param {string} text The text to process.
* @returns {string} `text` with escaped control characters.
*/
function escapeControlCharacters(text) {
return text.replace(
/[\u0000-\u001F\u007F-\u009F]/gu, // eslint-disable-line no-control-regex -- intentionally including control characters
c => `\\x${c.codePointAt(0).toString(16).padStart(2, "0")}`
);
}
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
describe("upperCaseFirst", () => {
it("uppercases the first letter of a string", () => {
assert(upperCaseFirst("e") === "E");
assert(upperCaseFirst("alphabet") === "Alphabet");
assert(upperCaseFirst("one two three") === "One two three");
});
it("only changes the case of the first letter", () => {
assert(upperCaseFirst("alphaBet") === "AlphaBet");
assert(upperCaseFirst("one TWO three") === "One TWO three");
});
it("does not change the case if the first letter is already uppercase", () => {
assert(upperCaseFirst("E") === "E");
assert(upperCaseFirst("Alphabet") === "Alphabet");
assert(upperCaseFirst("One Two Three") === "One Two Three");
});
it("properly handles an empty string", () => {
assert(upperCaseFirst("") === "");
});
});
describe("getGraphemeCount", () => {
/* eslint-disable quote-props -- Make consistent here for readability */
const expectedResults = {
"": 0,
"a": 1,
"ab": 2,
"aa": 2,
"123": 3,
"cccc": 4,
[Array.from({ length: 128 }, (_, i) => String.fromCharCode(i)).join("")]: 128, // all ASCII characters
"👍": 1, // 1 grapheme, 1 code point, 2 code units
"👍👍": 2,
"👍9👍": 3,
"a👍b": 3,
"👶🏽": 1, // 1 grapheme, 2 code points, 4 code units
"👨👩👦": 1, // 1 grapheme, 5 code points, 8 code units
"👨👩👦👨👩👦": 2,
"👨👩👦a👨👩👦": 3,
"a👨👩👦b👨👩👦c": 5,
"👨👩👦👍": 2,
"👶🏽👨👩👦": 2
};
/* eslint-enable quote-props -- Make consistent here for readability */
Object.entries(expectedResults).forEach(([key, value]) => {
it(`should return ${value} for ${escapeControlCharacters(key)}`, () => {
assert.strictEqual(getGraphemeCount(key), value);
});
});
});