Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including transform-case with all npm packages installed. Try it out:

var transformCase = require("transform-case")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

transform-case v0.2.0

Transforms casing and spacing of a string

TransformCase

Here is the case and seperation transformer that transliterates diacriticals and ligatures when your texts are in a Latin script other than modern english.

Install

Install the package as npm package. Provided are a umd-formatted file in the dist folder to require or just read and an es-module in the module folder to import.

Usage

A human text can be transformed to a systematic phrase like this:

transformCase('A sentence, text for humans.').camelCase()

These will render:

const textIntake = transformCase('A sentence, text for humans.')

textIntake.camelCase()   // ==> 'aSentenceTextForHumans'
textIntake.pascalCase()  // ==> 'ASentenceTextForHumans'
textIntake.dotCase()     // ==> 'a.sentence.text.for.humans'
textIntake.paramCase()   // ==> 'a-sentence-text-for-humans'
textIntake.pathCase()    // ==> 'a/sentence/text/for/humans'
textIntake.searchCase()   // ==> 'a+sentence+text+for+humans'
textIntake.snakeCase()   // ==> 'a_sentence_text_for_humans'
textIntake.spaceCase()   // ==> 'a sentence text for humans'
textIntake.constantCase()// ==> 'THIS_SENTENCE_TEXT_FOR_HUMANS'
textIntake.headerCase()  // ==> 'This-Sentence-Text-For-Humans'

A systematic text can be transformed to a human phrase like this:

const textIntake = transformCase('camelCasedInput')
textIntake.humanSentence()  // ==> 'Camel cased input'

const textIntake2 = transformCase('snake_cased_input', {delimitInput: '_'})
textIntake2.humanTitle()     // ==> 'Snake Cased Input'

With a second argument, an options object can be passed:

{
    delimit: [word-or-regex1, word-or-regex2, ...],
    preserve: [word-or-regex1, word-or-regex2, ...],
}
delimit: {Array}
    keeps a letter-combination or a regular expression match
    as a delimited word,
    the word will be processed according to the pattern
preserve: {Array}
    keeps a letter-combination or a regular expression match
    as a delimited word and protects the case

Options for pure alphanumeric input

delimitLetterNumber: {Boolean}
    delimit when a letter is followed by a number (default: true)
delimitLowerUpper: {Boolean}
    delimit when a lowercase is followed by a uppercase (default: true)
delimitNumberLetter: {Boolean}
    delimit when a number is followed by a letter (default: true)
delimitUpperLower: {Boolean}
    delimit when a uppercase is followed by a lowercase (default: false)
delimitUpperUpperLower: {Boolean}
    delimit when a uppercase is followed by a uppercase plus lowercase (default: true)

Demo

.../transform-case/demo/demo.html

Transformation process

This module has two steps, an intake and a render step.

The intake step deduplicates whitespace in a space character, removes control characters, finds a delimiter, isolates delimit and preserve options and ends with an array of words.

The render step is merely choosing a pattern to treat the array of words. There are three groups of similar patterns:

Cap-marked regexp word (camelCase, pascalCase) Human, linguistic (humanSentence, humanTitle) delimited lowercase (dotCase, paramCase, etcetera)

Apart from the human group, in all patterns punctuation is stripped, diacritics are stripped, ligatures are decomposed

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free