convert-case

0.1.0 • Public • Published

Convert Case

Work in progress. 🚧

Reference

This library exports the identifiers toCamelCase, toKebabCase, and toSnakeCase. There is no default export.


toCamelCase(string, options?)

Converts a given string to camel case.

import { toCamelCase } from 'convert-case'

toCamelCase('foo_bar')
//=> 'fooBar'

Parameters

  • string: A string you want to convert to camel case.

  • options (optional): An object containing any settings you want to apply.

    • fromCase (optional): Determines where words start and end in the original string. You can either choose a predefined case by its name or define a custom case by listing its word boundaries.

      Must be either a string that is one of: 'camel', 'kebab', 'snake', 'mixed', or an array of strings that consists of: 'upper-upper-lower', 'lower-upper', 'digit-upper', 'upper-digit', 'lower-digit', 'hyphen', 'underscore'. Default: 'mixed'.

      import { toCamelCase } from 'convert-case'
      
      toCamelCase('foo_bar-baz')
      //=> 'fooBarBaz'
      
      toCamelCase('foo_bar-baz', { fromCase: 'snake' })
      //=> 'fooBar-baz'
      
      toCamelCase('foo_bar-baz', { fromCase: ['underscore'] })
      //=> 'fooBar-baz'
    • trim (optional): Determines which sides of the string to clean up by removing non-word characters.

      Must be a string that is one of: 'none', 'start', 'end', 'both'. Default: 'none'.

      import { toCamelCase } from 'convert-case'
      
      toCamelCase('--foo-bar')
      //=> '--fooBar'
      
      toCamelCase('--foo-bar', { trim: 'start' })
      //=> 'fooBar'
    • variant (optional): Determines which case variant to apply when converting the string ('UpperCamelCase', 'MixedUpperCAMELCase', 'lowerCamelCase', 'mixedLowerCAMELCase').

      Must be a string that is one of: 'upper', 'mixed-upper', 'lower', 'mixed-lower'. Default: 'lower'.

      import { toCamelCase } from 'convert-case'
      
      toCamelCase('foo_bar')
      //=> 'fooBar'
      
      toCamelCase('foo_bar', { variant: 'upper' })
      //=> 'FooBar'

Returns

A new string in camel case.


toKebabCase(string, options?)

Converts a given string to kebab case.

import { toKebabCase } from 'convert-case'

toKebabCase('fooBar')
//=> 'foo-bar'

Parameters

  • string: A string you want to convert to kebab case.

  • options (optional): An object containing any settings you want to apply.

    • fromCase (optional): Determines where words start and end in the original string. You can either choose a predefined case by its name or define a custom case by listing its word boundaries.

      Must be either a string that is one of: 'camel', 'kebab', 'snake', 'mixed', or an array of strings that consists of: 'upper-upper-lower', 'lower-upper', 'digit-upper', 'upper-digit', 'lower-digit', 'hyphen', 'underscore'. Default: 'mixed'.

      import { toKebabCase } from 'convert-case'
      
      toKebabCase('fooBar_baz')
      //=> 'foo-bar-baz'
      
      toKebabCase('fooBar_baz', { fromCase: 'camel' })
      //=> 'foo-bar_baz'
      
      toKebabCase('fooBar_baz', { fromCase: ['lower-upper'] })
      //=> 'foo-bar_baz'
    • trim (optional): Determines which sides of the string to clean up by removing non-word characters.

      Must be a string that is one of: 'none', 'start', 'end', 'both'. Default: 'none'.

      import { toKebabCase } from 'convert-case'
      
      toKebabCase('__foo_bar')
      //=> '__foo-bar'
      
      toKebabCase('__foo_bar', { trim: 'start' })
      //=> 'foo-bar'
    • variant (optional): Determines which case variant to apply when converting the string ('UPPER-KEBAB-CASE', 'lower-kebab-case', 'Title-Kebab-Case', 'Mixed-Title-KEBAB-Case').

      Must be a string that is one of: 'upper', 'lower', 'title', 'mixed-title'. Default: 'lower'.

      import { toKebabCase } from 'convert-case'
      
      toKebabCase('fooBar')
      //=> 'foo-bar'
      
      toKebabCase('fooBar', { variant: 'upper' })
      //=> 'FOO-BAR'

Returns

A new string in kebab case.


toSnakeCase(string, options?)

Converts a given string to snake case.

import { toSnakeCase } from 'convert-case'

toSnakeCase('fooBar')
//=> 'foo_bar'

Parameters

  • string: A string you want to convert to snake case.

  • options (optional): An object containing any settings you want to apply.

    • fromCase (optional): Determines where words start and end in the original string. You can either choose a predefined case by its name or define a custom case by listing its word boundaries.

      Must be either a string that is one of: 'camel', 'kebab', 'snake', 'mixed', or an array of strings that consists of: 'upper-upper-lower', 'lower-upper', 'digit-upper', 'upper-digit', 'lower-digit', 'hyphen', 'underscore'. Default: 'mixed'.

      import { toSnakeCase } from 'convert-case'
      
      toSnakeCase('fooBar-baz')
      //=> 'foo_bar_baz'
      
      toSnakeCase('fooBar-baz', { fromCase: 'camel' })
      //=> 'foo_bar-baz'
      
      toSnakeCase('fooBar-baz', { fromCase: ['lower-upper'] })
      //=> 'foo_bar-baz'
    • trim (optional): Determines which sides of the string to clean up by removing non-word characters.

      Must be a string that is one of: 'none', 'start', 'end', 'both'. Default: 'none'.

      import { toSnakeCase } from 'convert-case'
      
      toSnakeCase('--foo-bar')
      //=> '--foo_bar'
      
      toSnakeCase('--foo-bar', { trim: 'start' })
      //=> 'foo_bar'
    • variant (optional): Determines which case variant to apply when converting the string ('UPPER-SNAKE-CASE', 'lower-snake-case', 'Title-Snake-Case', 'Mixed-Title-SNAKE-Case').

      Must be a string that is one of: 'upper', 'lower', 'title', 'mixed-title'. Default: 'lower'.

      import { toSnakeCase } from 'convert-case'
      
      toSnakeCase('fooBar')
      //=> 'foo_bar'
      
      toSnakeCase('fooBar', { variant: 'upper' })
      //=> 'FOO_BAR'

Returns

A new string in snake case.

Readme

Keywords

none

Package Sidebar

Install

npm i convert-case

Weekly Downloads

1

Version

0.1.0

License

none

Unpacked Size

18.4 kB

Total Files

15

Last publish

Collaborators

  • ngrushkovsky