Split a string by case or separators
mainsplitByCase function breaks a string into an array of parts based on case changes (e.g., camelCase to ['camel', 'Case']) or specific separators. By default, it uses `[repository·main·Indexed 19 days ago
https://github.com/unjs/sculeA utility library for string case conversion and manipulation, providing functions to convert strings or arrays of strings between camelCase, PascalCase, kebab-case, flatcase, and Title Case, as well as capitalization utilities like upperFirst and lowerFirst.
splitByCase function breaks a string into an array of parts based on case changes (e.g., camelCase to ['camel', 'Case']) or specific separators. By default, it uses `[Convert a string or an array of strings to Title Case (parts joined by spaces). This function includes logic to keep specific small words (e.g., 'a', 'an', 'the', 'of', 'to') in lowercase unless they are at the start of a part. You can use normalize: true to ensure consistent casing.
import { titleCase } from 'scule';
titleCase('the lord of the rings'); // 'The Lord of the Rings'
titleCase(['the', 'lord', 'of', 'the', 'rings']); // 'The Lord of the Rings'When using Scule's case transformation functions, you can provide an optional CaseOptions object to control how strings are processed. The primary option is normalize, which determines whether words should be lowercased before being transformed (e.g., during PascalCase or TrainCase operations).
export type CaseOptions = {
normalize?: boolean;
};The isUppercase function checks if a given character is an uppercase letter. It returns undefined if the character is a number.
import { isUppercase } from 'scule';
isUppercase('A'); // true
isUppercase('a'); // false
isUppercase('1'); // undefinedConvert a string or an array of strings to flatcase by joining parts with an empty string ("").
import { flatCase } from 'scule';
flatCase('hello-world'); // 'helloworld'
flatCase(['hello', 'world']); // 'helloworld'Convert a string or an array of strings to kebab-case. You can optionally specify a custom joiner string to replace the default hyphen (-).
import { kebabCase } from 'scule';
// Default hyphen
kebabCase('helloWorld'); // 'hello-world'
// Custom joiner
kebabCase('helloWorld', '.'); // 'hello.world'
// From array
kebabCase(['hello', 'world']); // 'hello-world'Use upperFirst and lowerFirst to capitalize or uncapitalize only the first character of a string without affecting the rest of the string.
import { upperFirst, lowerFirst } from 'scule';
upperFirst('hello'); // 'Hello'
lowerFirst('Hello'); // 'hello'Convert a string or an array of strings to camelCase. This is implemented by generating a PascalCase string and then applying lowerFirst to the result.
import { camelCase } from 'scule';
// From string
camelCase('hello-world'); // 'helloWorld'
camelCase('HELLO_WORLD', { normalize: true }); // 'helloWorld'
// From array
camelCase(['hello', 'world']); // 'helloWorld'