Strings
import { camelCase, kebabCase, pascalCase } from '@studiometa/js-toolkit/utils';Case
import { camelCase, kebabCase, pascalCase } from '@studiometa/js-toolkit/utils';
pascalCase('drag-threshold'); // 'DragThreshold'
camelCase('drag-threshold'); // 'dragThreshold'
kebabCase('dragThreshold'); // 'drag-threshold'These are the functions the framework itself uses: kebabCase turns dragThreshold into data-option-drag-threshold, and pascalCase turns an option name into optionDragThresholdChanged.
Four of them are memoised
pascalCase, camelCase, kebabCase and snakeCase are memo-wrapped, because the framework converts the same names on every option read and every handler resolution. lowerCase, upperCase and capitalize are not — they are single-pass and there is nothing to cache.
lowerCase
lowerCase(string: string): stringupperCase
upperCase(string: string): stringcapitalize
capitalize(string: string): stringpascalCase
pascalCase(string: string): string'foo bar' → 'FooBar'. Memoised.
camelCase
camelCase(string: string): string'foo bar' → 'fooBar'. Memoised.
kebabCase
kebabCase(string: string): string'fooBar' → 'foo-bar'. Memoised.
snakeCase
snakeCase(string: string): string'fooBar' → 'foo_bar'. Memoised.
Leading and trailing characters
import {
withLeadingCharacters,
withoutTrailingCharactersRecursive,
} from '@studiometa/js-toolkit/utils';
withLeadingCharacters('bar', 'foo'); // 'foobar'
withLeadingCharacters('foobar', 'foo'); // 'foobar' — already there
withoutTrailingCharactersRecursive('path///', '/'); // 'path'The recursive pair exists because a URL joined from parts collects separators, and removing one is not the same as removing them all.
withLeadingCharacters
withLeadingCharacters(string: string, characters: string): stringAdds them if absent.
withoutLeadingCharacters
withoutLeadingCharacters(string: string, characters: string): stringRemoves them once.
withoutLeadingCharactersRecursive
withoutLeadingCharactersRecursive(string: string, characters: string): stringRemoves them as long as they are there.
withTrailingCharacters
withTrailingCharacters(string: string, characters: string): stringAdds them if absent.
withoutTrailingCharacters
withoutTrailingCharacters(string: string, characters: string): stringRemoves them once.
withoutTrailingCharactersRecursive
withoutTrailingCharactersRecursive(string: string, characters: string): stringRemoves them as long as they are there.
Slashes
The four named cases, for the one separator every path uses. Each is the matching *Characters function with '/' filled in, which is what makes a call site readable at a glance.
import {
withLeadingSlash,
withoutLeadingSlash,
withoutTrailingSlash,
withTrailingSlash,
} from '@studiometa/js-toolkit/utils';
withLeadingSlash('about'); // '/about'
withoutLeadingSlash('/about'); // 'about'
withTrailingSlash('/about'); // '/about/'
withoutTrailingSlash('/about/'); // '/about'withLeadingSlash
withLeadingSlash(string: string): stringwithoutLeadingSlash
withoutLeadingSlash(string: string): stringwithTrailingSlash
withTrailingSlash(string: string): stringwithoutTrailingSlash
withoutTrailingSlash(string: string): string