Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ import type {
} from "./types";

const NUMBER_CHAR_RE = /\d/;
const UPPERCASE_RE = /\p{Lu}/u;
const LOWERCASE_RE = /\p{Ll}/u;
const CASE_FALLING_RE = /^([\s\S]+)([\s\S])$/u;
const STR_SPLITTERS = ["-", "_", "/", "."] as const;

export function isUppercase(char = ""): boolean | undefined {
if (NUMBER_CHAR_RE.test(char)) {
return undefined;
}
if (UPPERCASE_RE.test(char)) {
return true;
}
if (LOWERCASE_RE.test(char)) {
return false;
}
return char !== char.toLowerCase();
}

Expand Down Expand Up @@ -60,12 +69,14 @@ export function splitByCase<
continue;
}
// Case falling edge
if (previousUpper === true && isUpper === false && buff.length > 1) {
const lastChar = buff.at(-1);
parts.push(buff.slice(0, Math.max(0, buff.length - 1)));
buff = lastChar + char;
previousUpper = isUpper;
continue;
if (previousUpper === true && isUpper === false) {
const match = buff.match(CASE_FALLING_RE);
if (match) {
parts.push(match[1]);
buff = match[2] + char;
previousUpper = isUpper;
continue;
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions test/scule.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, test, expect } from "vitest";
import {
splitByCase,
isUppercase,
pascalCase,
kebabCase,
camelCase,
Expand Down Expand Up @@ -32,6 +33,16 @@ describe("splitByCase", () => {
["\\", ".", "-"],
],
["new-name-value", ["new-name-value"], ["_"]],
// Unicode
["𐐀foo", ["𐐀foo"]],
["𐐀𐐁foo", ["𐐀", "𐐁foo"]],
["A𐐀foo", ["A", "𐐀foo"]],
["foo𐐀bar", ["foo", "𐐀bar"]],
["fooBärBaz", ["foo", "Bär", "Baz"]],
["fooБАРbaz", ["foo", "БА", "Рbaz"]],
["БАРБар", ["БАР", "Бар"]],
["ADža", ["A", "Dža"]],
["Dža", ["Dža"]],
])("%s => %s", (input, expected, customSplitters?) => {
if (customSplitters) {
expect(splitByCase(input, customSplitters)).toMatchObject(expected);
Expand All @@ -41,6 +52,22 @@ describe("splitByCase", () => {
});
});

describe("isUppercase", () => {
test.each([
["", false],
["A", true],
["a", false],
["1", undefined],
["-", false],
["É", true],
["é", false],
["𐐀", true],
["𐐨", false],
])("%s => %s", (input, expected) => {
expect(isUppercase(input)).toBe(expected);
});
});

describe("pascalCase", () => {
test.each([
["", ""],
Expand Down Expand Up @@ -74,6 +101,8 @@ describe("kebabCase", () => {
["FooBAR", "foo-bar"],
["ALink", "a-link"],
["FOO_BAR", "foo-bar"],
["𐐀foo", "𐐨foo"],
["foo𐐀bar", "foo-𐐨bar"],
])("%s => %s", (input, expected) => {
expect(kebabCase(input)).toMatchObject(expected);
});
Expand Down