Skip to content

Strings ​

js
import { 
camelCase
,
kebabCase
,
pascalCase
} from '@studiometa/js-toolkit/utils';

Case ​

js
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 ​

ts
lowerCase(string: string): string

upperCase ​

ts
upperCase(string: string): string

capitalize ​

ts
capitalize(string: string): string

pascalCase ​

ts
pascalCase(string: string): string

'foo bar' → 'FooBar'. Memoised.

camelCase ​

ts
camelCase(string: string): string

'foo bar' → 'fooBar'. Memoised.

kebabCase ​

ts
kebabCase(string: string): string

'fooBar' → 'foo-bar'. Memoised.

snakeCase ​

ts
snakeCase(string: string): string

'fooBar' → 'foo_bar'. Memoised.

Leading and trailing characters ​

js
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 ​

ts
withLeadingCharacters(string: string, characters: string): string

Adds them if absent.

withoutLeadingCharacters ​

ts
withoutLeadingCharacters(string: string, characters: string): string

Removes them once.

withoutLeadingCharactersRecursive ​

ts
withoutLeadingCharactersRecursive(string: string, characters: string): string

Removes them as long as they are there.

withTrailingCharacters ​

ts
withTrailingCharacters(string: string, characters: string): string

Adds them if absent.

withoutTrailingCharacters ​

ts
withoutTrailingCharacters(string: string, characters: string): string

Removes them once.

withoutTrailingCharactersRecursive ​

ts
withoutTrailingCharactersRecursive(string: string, characters: string): string

Removes 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.

js
import {
  
withLeadingSlash
,
withoutLeadingSlash
,
withoutTrailingSlash
,
withTrailingSlash
,
} from '@studiometa/js-toolkit/utils';
withLeadingSlash
('about'); // '/about'
withoutLeadingSlash
('/about'); // 'about'
withTrailingSlash
('/about'); // '/about/'
withoutTrailingSlash
('/about/'); // '/about'

withLeadingSlash ​

ts
withLeadingSlash(string: string): string

withoutLeadingSlash ​

ts
withoutLeadingSlash(string: string): string

withTrailingSlash ​

ts
withTrailingSlash(string: string): string

withoutTrailingSlash ​

ts
withoutTrailingSlash(string: string): string

MIT Licensed