The _change_case
operator uses the change-case
package to transform an input between camelCase, PascalCase, Capital Case, snake_case, param-case, CONSTANT_CASE and others.
Arguments
The _change_case
operator takes on
and options
as arguments.
The on
argument is the input to be transformed and can be a string
, array
or object
.
The options
argument is an object with the following properties:
splitRegexp
: The regular expression used to spliton
into word segments. Can be astring
or anobject
withpattern
andflags
.stripRegexp
: The regular expression used to remove extraneous characters (default:/[^A-Z0-9]/gi
). Can be astring
or anobject
withpattern
andflags
.delimiter
: Value used between words (e.g. " ").convertValues
: Only used whenon
is an object. Toggles whether the object values are converted to the new case (true
) or left as-is (false
). Defaulttrue
.convertKeys
: Only used whenon
is an object. Toggles whether the object keys are converted to the new case (true
) or left as-is (false
). Defaultfalse
.
The on
argument behaves as follows:
string
Transforms the case of the string.
array
Transform the case of each string in the array.
object
Transforms the case of the values (when options.convertValues: true
) and keys (when options.convertKeys: true
) of the object.
Note that the
key
andvalue
pairs must be strings.
Examples
String with no options:
_change_case.capitalCase:
on: 'foo bar'
Returns: "Foo Bar"
Array with no options:
_change_case.capitalCase:
on:
- 'foo'
- 'bar'
Returns: ["Foo", "Bar"]
Object with no options:
_change_case.capitalCase:
on:
foo: 'bar'
Returns: { "foo": "Bar" }
Object with options.convertKeys: true:
_change_case.capitalCase:
on:
foo: 'bar'
options:
convertKeys: true
Returns: { "Foo": "Bar" }
Object with options.convertValues: false:
_change_case.capitalCase:
on:
foo: 'bar'
options:
convertValues: false
Returns: { "foo": "bar" }
String with options.splitRegexp string:
_change_case.sentenceCase:
on: 'foo8ar'
options:
splitRegexp: '([a-z])([A-Z0-9])'
Returns: "Foo 8ar"
String with options.stripRegexp string:
_change_case.sentenceCase:
on: 'FOO8AR'
options:
stripRegexp: '[^A-Z]'
Returns: "Foo ar"
String with options.splitRegexp object:
_change_case.sentenceCase:
on: 'foo8ar'
options:
splitRegexp:
pattern: '([a-z])([A-Z0-9])'
flags: 'gi'
Returns: "F oo 8a r"
String with options.stripRegexp object:
_change_case.sentenceCase:
on: 'foo8ar'
options:
stripRegexp:
pattern: '[^A-Z]'
flags: gi
Returns: "Foo ar"
Operator methods:
_change_case.camelCase
({on: string, options: object}): string
([on: string, options: object]): string
({on: object, options: object}): object
([on: object, options: object]): object
({on: array, options: object}): array
([on: array, options: object]): array
The _change_case.camelCase
method transforms on
into a string with the separator denoted by the next word capitalized.
Examples
Transform snake_case variable to camelCase:
_change_case.camelCase:
on: 'my_variable'
Returns: "myVariable"
_change_case.capitalCase
({on: string, options: object}): string
([on: string, options: object]): string
({on: object, options: object}): object
([on: object, options: object]): object
({on: array, options: object}): array
([on: array, options: object]): array
The _change_case.capitalCase
method transforms on
into a space separated string with each word capitalized.
Examples
Transform snake_case variable to capitalCase:
_change_case.capitalCase:
on: 'my_variable'
Returns: "My Variable"
_change_case.constantCase
({on: string, options: object}): string
([on: string, options: object]): string
({on: object, options: object}): object
([on: object, options: object]): object
({on: array, options: object}): array
([on: array, options: object]): array
The _change_case.constantCase
method transforms on
into upper case string with an underscore between words.
Examples
Transform camelCase variable to constantCase:
_change_case.constantCase:
on: 'myVariable'
Returns: "MY_VARIABLE"
_change_case.dotCase
({on: string, options: object}): string
([on: string, options: object]): string
({on: object, options: object}): object
([on: object, options: object]): object
({on: array, options: object}): array
([on: array, options: object]): array
The _change_case.dotCase
method transforms on
into a lower case string with a period between words.
Examples
Transform camelCase variable to dotCase:
_change_case.dotCase:
on: 'myVariable'
Returns: "my.variable"
_change_case.headerCase
({on: string, options: object}): string
([on: string, options: object]): string
({on: object, options: object}): object
([on: object, options: object]): object
({on: array, options: object}): array
([on: array, options: object]): array
The _change_case.headerCase
method transforms on
into a dash separated string of capitalized words.
Examples
Transform snake_case variable to headerCase:
_change_case.headerCase:
on: 'my_variable'
Returns: "My-Variable"
_change_case.noCase
({on: string, options: object}): string
([on: string, options: object]): string
({on: object, options: object}): object
([on: object, options: object]): object
({on: array, options: object}): array
([on: array, options: object]): array
The _change_case.noCase
method transforms on
into a lower cased string with spaces between words.
Examples
Transform camelCase variable to noCase:
_change_case.noCase:
on: 'myVariable'
Returns: "my variable"
_change_case.paramCase
({on: string, options: object}): string
([on: string, options: object]): string
({on: object, options: object}): object
([on: object, options: object]): object
({on: array, options: object}): array
([on: array, options: object]): array
The _change_case.paramCase
method transforms on
into a lower cased string with dashes between words.
Examples
Transform snake_case variable to paramCase:
_change_case.paramCase:
on: 'my_variable'
Returns: "my-variable"
_change_case.pascalCase
({on: string, options: object}): string
([on: string, options: object]): string
({on: object, options: object}): object
([on: object, options: object]): object
({on: array, options: object}): array
([on: array, options: object]): array
The _change_case.pascalCase
method transforms on
into a string of capitalized words without separators.
Examples
Transform snake_case variable to pascalCase:
_change_case.pascalCase:
on: 'my_variable'
Returns: "MyVariable"
_change_case.pathCase
({on: string, options: object}): string
([on: string, options: object]): string
({on: object, options: object}): object
([on: object, options: object]): object
({on: array, options: object}): array
([on: array, options: object]): array
The _change_case.pathCase
method transforms on
into a lower case string with slashes between words.
Examples
Transform snake_case variable to pathCase:
_change_case.pathCase:
on: 'my_variable'
Returns:
_change_case.sentenceCase
({on: string, options: object}): string
([on: string, options: object]): string
({on: object, options: object}): object
([on: object, options: object]): object
({on: array, options: object}): array
([on: array, options: object]): array
The _change_case.sentenceCase
method transforms on
into a lower case with spaces between words, then capitalize the first word.
Examples
Transform snake_case variable to sentenceCase:
_change_case.sentenceCase:
on: 'my_variable'
Returns: "My variable"
_change_case.snakeCase
({on: string, options: object}): string
([on: string, options: object]): string
({on: object, options: object}): object
([on: object, options: object]): object
({on: array, options: object}): array
([on: array, options: object]): array
The _change_case.snakeCase
method transforms on
into a lower case string with underscores between words.
Examples
Transform sentence case variable to snakeCase:
_change_case.snakeCase:
on: 'My variable'
Returns: "my_variable"