leca
v0.0.10
Published
Letter Case Transformer
Downloads
6
Readme
leca
Letter Case Transformer
To define your own letter case style and to deal with text by it. Or, to deal with text with predefined letter case styles.
Predefined Case Styles
| predefined case | case name | example | remark | |:---------------------- |:--------- |:-------------- |:----------------- | | leca.bicaps* | BiCapitalization | AltaVista eBay | | | leca.camel | camelCase | missYou | | | leca.kebab | kebab-case | i-love-you | | | leca.lowercamel | lowerCamelCase | missYou | s.a. camelCase | leca.pascal | PascalCase | PostScript | | | leca.sentence | Sentence case | I love you | | | leca.snake | snake_case | i_love_you | | | leca.title* | Title Case | I Love Your | | | leca.uppercamel* | UpperCamelCase | PostScript | s.a. PascalCase |
- Case postfixed with asterisk * may be in dispute with its impletation in leca. Please BE CAREFUL by yourself when using them.
- s.a. = the same as
- Because of its arbitrariness and creation, it unable to parse a phrase in StUdLyCaPs.
Table of contents
Links
Get Started
Use predefined common case styles:
const leca = require('leca');
leca.camel.test('camelCase');
// RETURN true
leca.camle.test('camel case');
// RETURN false
leca.camel.parse('camelCase');
// RETURN [ "camel", "case" ];
leca.camel.format('Camel', 'CASE');
// RETURN "camelCase"
Or, create your own case style:
const leca = require('leca');
const myCase = new leca.Case({
// Used to split a formatted string.
splitter: /(?=[A-Z])/,
// Used to transform a word on formatting.
wordFormatter: (word, index) => {
if (index == 0) {
return word.toLowerCase();
}
else {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
},
// Used to transform a word on parsing.
wordParser: (word) => {
return /^[a-zA-Z]+$/.test(word) ? word.toLowerCase() : false;
}
});
myCase.test('camelCase'); // RETURN true
myCase.test('camel case'); // RETURN false
myCase.parse('camelCase'); // RETURN [ "camel", "case" ]
myCase.format('Camel', 'CASE'); // RETURN "camelCase"
API
leca.Case
An instance of class leca.Case
represents a case style.
class leca.Case(object options)
Hereafter use <ci> to represent an instance of leca.Case. See below for details of options.boolean <ci>.test(string text)
Returntrue
if text matches the <ci>. Otherwise returnfalse
.string[] | false <ci>.parse(string text)
Split text into words.false
will be returned if text doesnot match the case (e.g. doesnot start with options.prefix).string | false <ci>.format(string[] words | ...words)
Combine the words together according to the case format.false
will be returned if any word doesnot match the case (depends on options.wordFormatter).string | false <ci>.reformat(string text)
Parse the text and format the words according to the case's format rule.
Following options may be used in new leca.Case(options)
to define a letter case:
options.prefix string OPTIONAL
What matching texts start with.options.postfix string OPTIONAL
What matching texts end with.options.jointer string | Function OPTIONAL DEFAULT
""
Used in formatting.
As a string, it will be put between every two words.
As a function, it should conform to:/** * @param {number} [index] * @param {string} [left] * @param {string} [right] * @return {string} */ function(index, left, right) { // ... }
options.splitter string | RegExp OPTIONAL
Used in parsing.options.terms string[] OPTIONAL
Words to be preserved with its original style.
In other words, the terms will escape from the constrains of options.wordFormatter and options.wordParser.options.wordFormatter Function OPTIONAL
Used in formatting.
It should conform to:/** * @param {string} word * @param {number} [index] * @param {number} [length] Total count of words * @return {string} */ function(word, index, length) { // ... }
options.wordParser Function OPTIONAL
Used in parsing.
It should conform to:/** * @param {string} word * @param {number} [index] * @param {number} [length] Total count of words * @return {string | false} */ function(word, index, length) { // ... }
Predefined Common Letter Cases
For the convenience of developers, the most frequently-used letter case styles are predefined. Developers may access a predefined case style in form of leca.<stylename>
, e.g.
const leca = require('leca');
// Access the predefined case style named "camel".
const mycase = leca.camel;
// ATTENTION: All predefined style names are lowercase, and there are no
// punctuations or word "case" in names. Please DO NOT take the following for
// granted:
leca.Pascal // undefined
leca.camelCase // undefined
leca['kebab-case'] // undefined
leca.snake_case // undefined
// For those who have some kind of compulsive disorder, or just only wanna make
// code more comprehensible, invoke `leca()` may be helpful:
leca('Pascal') === leca.pascal
leca('camelCase') === leca.camel
leca('kebab-case') === leca.kebab
leca('snake_case') === leca.snake
In addition, some (acctually only 1 so far) accompanying methods are offered for each predefined letter case:
- Case leca.*.terms(string | string[] terms)
Return case instance based on corresponding predfined leca.Case. e.g.leca.camelCase.terms(['HTTP', 'HTTPS']).parse('myHTTPClient'); // RETURN [ "my", "HTTP", "client" ]
This is multi-cultural world, and a letter case style will be named differently in different occassions. With respect to more people, leca offers some alias for the foregoing predefined case style.
See the table at beginning of this document for available predefined case styles.
Examples
Developers can learn how to create own letter case by leca via unit tests and predefined common letter cases:
- Test Unit: customised
- Code: common/bicaps
- Code: common/camel
- Code: common/kebab
- Code: common/pascal
- Code: common/sentence
- Code: common/snake
- Code: common/title
References
- WIKIPEDIA: Letter case
- TitleCase, online case converters
- Guidelines for Using Capital Letters