mirror of
https://github.com/hanzoai/eslint.git
synced 2026-10-30 19:38:10 +00:00
* 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
93 lines
2.9 KiB
JavaScript
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);
|
|
});
|
|
});
|
|
});
|