Files
Josh Goldberg ✨andGitHub 36a353c719 chore: formatted files with Prettier via trunk fmt (#19355)
* chore: remove formatting-related lint rules internally

* chore: formatted files with Prettier via trunk fmt

* rm .prettierrc.json

* chore: correct comment directives and formatting

* Apply introduced fmt changes post-merge

* Also disable MD010 and MD030 in preparation for 19355

* Also disable MD013 in preparation for 19355

* Updated tools/update-rule-type-headers.js to print tabs

* Fix introduced issues from the main merge

* Manually format conf/*.json

* Switch conf/*.json files back to two spaces, per prettier config

* chore: empty commit to hopefully unblock GitHub
2025-03-19 11:18:08 -04:00

93 lines
2.9 KiB
JavaScript

/**
* @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", () => {
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,
"πŸ‘©β€πŸ¦°πŸ‘©β€πŸ‘©β€πŸ‘¦β€πŸ‘¦πŸ³οΈβ€πŸŒˆ": 3, // 3 grapheme, 14 code points, 22 code units
};
Object.entries(expectedResults).forEach(([key, value]) => {
it(`should return ${value} for ${escapeControlCharacters(key)}`, () => {
assert.strictEqual(getGraphemeCount(key), value);
});
});
});