1.1.3 • Published 5 years ago

to-caption v1.1.3

Weekly downloads
10
License
MIT
Repository
github
Last release
5 years ago

to-caption

Converts camelCase and delimited strings to captions.

Delimiters are dot, underscore, and dash. [ . _ - ]

Examples

foo.bar / foo_bar / fooBar / foo-bar

Foo Bar

_hello_world_

Hello World

thisIsATest

This Is A Test

Usage

Node / NPM

npm install --save to-caption
const toCaption = require('to-caption');
const myCaption = toCaption('helloWorld');
// myCaption should now be 'Hello World';

Browser

A webpacked distribution is included at dist/toCaption.js. The function is exported at window.toCaption(...)

<html>
<head>
  <script src="../dist/bundle.js"></script>
  <script>
    console.log(toCaption('helloWorld'))
  </script>
</head>
</html>

Rules

  • Delimiters are period, dash, and underscore.
  • Leading and trailing delimiters are ignored.
  • The first non-delimited character is capitalized (if not already capitalized).
  • Underscores, dots, and slashes are replaced with spaces and the following non-delimiter character is uppercased.
  • Uppercase letters are prefixed with spaces.

More Samples

This table should give you an idea of the process.

IDLabelComment
this.is.a.testThis Is A Test
fooFoo
FooFoo
foobarFoobar
fooBarFoo Bar
foo_barFoo Bar
foo.barFoo Bar
foo-barFoo Bar
_fooFoo
foo_Foo
_foo_Foo
_foo_bar_Foo Bar
__fooFoo
foo__Foo
--foo--bar--Foo Bar
FOOBARF O O B A RSee option for onAllUppercase to avoid this behavior.
FOOBARFOOBARonAllUppercase = 'keep'
FOOBARFoobaronAllUppercase = 'titlecase'

Options

onAllUppercase

toCaption('HELLO', {onAllUppercase: 'keep'})

This option will set the default behavior when the string is all uppercase

'keep'

The string will remain all uppercase. (HELLO)

'titlecase'

The string will become titlecase. (Hello)

'default', undefined, null, or anything else

The string will have spaces between each letter, as if each letter was a word. (H E L L O)

License

MIT. Check the LICENSE file for all the details.

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.4

5 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago