case-anything
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -12,5 +12,16 @@ 'use strict'; | ||
function getParts(string) { | ||
return string.match(/^[a-z]+|[A-Z][a-z]+|[A-Z]+|[a-z]+/g); | ||
return string.match(/^[a-z]+|[0-9]+|[A-Z][a-z]+|[A-Z]+|[a-z]+/g); | ||
} | ||
/** | ||
* Capitalises a single word | ||
* | ||
* @export | ||
* @param {string} string the word | ||
* @returns {string} the word with the first character in uppercase and the rest in lowercase | ||
*/ | ||
function capitaliseWord(string) { | ||
return string[0].toUpperCase() + string.slice(1).toLowerCase(); | ||
} | ||
/** | ||
* converts strings to camelCase | ||
@@ -20,10 +31,12 @@ * | ||
* @param {string} string | ||
* @param {function} [splitFn=getParts] the function to split the string. Defaults to `getParts` | ||
* @returns {string} in camelCase | ||
*/ | ||
function camelCase(string) { | ||
return getParts(string) | ||
function camelCase(string, splitFn) { | ||
if (splitFn === void 0) { splitFn = getParts; } | ||
return splitFn(string) | ||
.reduce(function (result, match, index) { | ||
return (index === 0) | ||
? match.toLowerCase() | ||
: result + match[0].toUpperCase() + match.slice(1).toLowerCase(); | ||
: result + capitaliseWord(match); | ||
}, ''); | ||
@@ -36,8 +49,10 @@ } | ||
* @param {string} string | ||
* @param {function} [splitFn=getParts] the function to split the string. Defaults to `getParts` | ||
* @returns {string} in PascalCase | ||
*/ | ||
function pascalCase(string) { | ||
return getParts(string) | ||
function pascalCase(string, splitFn) { | ||
if (splitFn === void 0) { splitFn = getParts; } | ||
return splitFn(string) | ||
.reduce(function (result, match) { | ||
return result + match[0].toUpperCase() + match.slice(1).toLowerCase(); | ||
return result + capitaliseWord(match); | ||
}, ''); | ||
@@ -105,6 +120,12 @@ } | ||
* @param {string} string | ||
* @param {function} [splitFn=getParts] the function to split the string. Defaults to `getParts` | ||
* @returns {string} in Capital Case (with spaces) | ||
*/ | ||
function capitalCase(string) { | ||
return spaceCase(pascalCase(string)); | ||
function capitalCase(string, splitFn) { | ||
if (splitFn === void 0) { splitFn = getParts; } | ||
return splitFn(string) | ||
.reduce(function (result, match) { | ||
return result + " " + capitaliseWord(match); | ||
}, '') | ||
.trim(); | ||
} | ||
@@ -137,3 +158,2 @@ /** | ||
exports.constantCase = constantCase; | ||
exports.getParts = getParts; | ||
exports.kebabCase = kebabCase; | ||
@@ -140,0 +160,0 @@ exports.lowerCase = lowerCase; |
@@ -8,5 +8,16 @@ /** | ||
function getParts(string) { | ||
return string.match(/^[a-z]+|[A-Z][a-z]+|[A-Z]+|[a-z]+/g); | ||
return string.match(/^[a-z]+|[0-9]+|[A-Z][a-z]+|[A-Z]+|[a-z]+/g); | ||
} | ||
/** | ||
* Capitalises a single word | ||
* | ||
* @export | ||
* @param {string} string the word | ||
* @returns {string} the word with the first character in uppercase and the rest in lowercase | ||
*/ | ||
function capitaliseWord(string) { | ||
return string[0].toUpperCase() + string.slice(1).toLowerCase(); | ||
} | ||
/** | ||
* converts strings to camelCase | ||
@@ -16,10 +27,12 @@ * | ||
* @param {string} string | ||
* @param {function} [splitFn=getParts] the function to split the string. Defaults to `getParts` | ||
* @returns {string} in camelCase | ||
*/ | ||
function camelCase(string) { | ||
return getParts(string) | ||
function camelCase(string, splitFn) { | ||
if (splitFn === void 0) { splitFn = getParts; } | ||
return splitFn(string) | ||
.reduce(function (result, match, index) { | ||
return (index === 0) | ||
? match.toLowerCase() | ||
: result + match[0].toUpperCase() + match.slice(1).toLowerCase(); | ||
: result + capitaliseWord(match); | ||
}, ''); | ||
@@ -32,8 +45,10 @@ } | ||
* @param {string} string | ||
* @param {function} [splitFn=getParts] the function to split the string. Defaults to `getParts` | ||
* @returns {string} in PascalCase | ||
*/ | ||
function pascalCase(string) { | ||
return getParts(string) | ||
function pascalCase(string, splitFn) { | ||
if (splitFn === void 0) { splitFn = getParts; } | ||
return splitFn(string) | ||
.reduce(function (result, match) { | ||
return result + match[0].toUpperCase() + match.slice(1).toLowerCase(); | ||
return result + capitaliseWord(match); | ||
}, ''); | ||
@@ -101,6 +116,12 @@ } | ||
* @param {string} string | ||
* @param {function} [splitFn=getParts] the function to split the string. Defaults to `getParts` | ||
* @returns {string} in Capital Case (with spaces) | ||
*/ | ||
function capitalCase(string) { | ||
return spaceCase(pascalCase(string)); | ||
function capitalCase(string, splitFn) { | ||
if (splitFn === void 0) { splitFn = getParts; } | ||
return splitFn(string) | ||
.reduce(function (result, match) { | ||
return result + " " + capitaliseWord(match); | ||
}, '') | ||
.trim(); | ||
} | ||
@@ -130,2 +151,2 @@ /** | ||
export { camelCase, capitalCase, constantCase, getParts, kebabCase, lowerCase, pascalCase, pathCase, snakeCase, spaceCase, upperCase }; | ||
export { camelCase, capitalCase, constantCase, kebabCase, lowerCase, pascalCase, pathCase, snakeCase, spaceCase, upperCase }; |
{ | ||
"name": "case-anything", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "camelCase, kebab-case, PascalCase... a simple integration with nano package size. (SMALL footprint!)", | ||
@@ -17,3 +17,3 @@ "main": "dist/index.cjs.js", | ||
"rollup-plugin-typescript2": "^0.24.3", | ||
"typescript": "^3.6.4" | ||
"typescript": "^3.7.2" | ||
}, | ||
@@ -20,0 +20,0 @@ "repository": { |
@@ -30,3 +30,3 @@ # Case anything 🐫 | ||
```js | ||
import { camelCase, pascalCase, kebabCase, snakeCase, constantCase, pathCase } from 'case-anything' | ||
import { camelCase, pascalCase, kebabCase, snakeCase, constantCase } from 'case-anything' | ||
@@ -55,6 +55,6 @@ const testString = 'PonytaVaporeon_poliwrath-BUTTERFREE' | ||
There is also `spaceCase`, which will place spaces in between words, but not convert the casing. | ||
There is also `spaceCase` and `pathCase`, which does not convert the casing: | ||
```js | ||
import { spaceCase } from 'case-anything' | ||
import { spaceCase, pathCase } from 'case-anything' | ||
@@ -65,5 +65,8 @@ const testString = 'PonytaVaporeon_poliwrath-BUTTERFREE' | ||
=== 'Ponyta Vaporeon poliwrath BUTTERFREE' | ||
pathCase(testString) | ||
=== 'Ponyta/Vaporeon/poliwrath/BUTTERFREE' | ||
``` | ||
There is also upper, lower and capital case. These will all add spaces. | ||
There is also upper, lower and capital case. These will all convert the casing & also add spaces in between: | ||
@@ -83,2 +86,30 @@ ```js | ||
### Custom split function | ||
The split function used in case-anything will remove any special characters (besides numbers) as well. If however, you require a different split function, you can provide one yourself as second parameter. | ||
This is possible for the `capitalCase`, `pascalCase` or `camelCase`. | ||
One use case example is when working with sentences. Eg. you want the capital case of this sentence: `listen I'm O.K.!`. Let's see how this can be done: | ||
```js | ||
const testString = "listen I'M O.K.!" | ||
// capitalCase expected behaviour: | ||
capitalCase(testString) | ||
=== 'Listen I M O K' | ||
// capitalCase with own split function: | ||
capitalCase(testString, s => s.split(' ')) | ||
=== "Listen I'm O.k.!" | ||
``` | ||
The reason this is only possible for three functions is because the logic behind the other functions is simple enough to implement yourself. Eg.: | ||
```js | ||
// snakeCase with own split function: | ||
testString.split(' ').join('_').toLowerCase() | ||
=== "listen_i'm_o.k.!" | ||
``` | ||
## Package size | ||
@@ -85,0 +116,0 @@ |
147
src/index.ts
@@ -1,135 +0,12 @@ | ||
/** | ||
* A string.match function that will return an array of "string parts" | ||
* | ||
* @param {string} string | ||
* @returns {string[]} | ||
*/ | ||
export function getParts (string: string): any[] { | ||
return string.match(/^[a-z]+|[A-Z][a-z]+|[A-Z]+|[a-z]+/g) | ||
} | ||
/** | ||
* converts strings to camelCase | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in camelCase | ||
*/ | ||
export function camelCase (string: string): string { | ||
return getParts(string) | ||
.reduce((result, match, index) => { | ||
return (index === 0) | ||
? match.toLowerCase() | ||
: result + match[0].toUpperCase() + match.slice(1).toLowerCase() | ||
}, '') | ||
} | ||
/** | ||
* converts strings to PascalCase | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in PascalCase | ||
*/ | ||
export function pascalCase (string: string): string { | ||
return getParts(string) | ||
.reduce((result, match) => { | ||
return result + match[0].toUpperCase() + match.slice(1).toLowerCase() | ||
}, '') | ||
} | ||
/** | ||
* converts strings to kebab-case | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in kebab-case | ||
*/ | ||
export function kebabCase (string: string): string { | ||
return getParts(string) | ||
.join('-').toLowerCase() | ||
} | ||
/** | ||
* converts strings to snake_case | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in snake_case | ||
*/ | ||
export function snakeCase (string: string): string { | ||
return getParts(string) | ||
.join('_').toLowerCase() | ||
} | ||
/** | ||
* converts strings to CONSTANT_CASE | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in CONSTANT_CASE | ||
*/ | ||
export function constantCase (string: string): string { | ||
return getParts(string) | ||
.join('_').toUpperCase() | ||
} | ||
/** | ||
* converts strings to path/case | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in path/case | ||
*/ | ||
export function pathCase (string: string): string { | ||
return getParts(string) | ||
.join('/') | ||
} | ||
/** | ||
* converts strings to space case (will add spaces but not change casing) | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in path case | ||
*/ | ||
export function spaceCase (string: string): string { | ||
return getParts(string) | ||
.join(' ') | ||
} | ||
/** | ||
* converts strings to Capital Case (with spaces) | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in Capital Case (with spaces) | ||
*/ | ||
export function capitalCase (string: string): string { | ||
return spaceCase(pascalCase(string)) | ||
} | ||
/** | ||
* converts strings to lower case (with spaces) | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in lower case (with spaces) | ||
*/ | ||
export function lowerCase (string: string): string { | ||
return getParts(string) | ||
.join(' ').toLowerCase() | ||
} | ||
/** | ||
* converts strings to UPPER CASE (with spaces) | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in UPPER CASE (with spaces) | ||
*/ | ||
export function upperCase (string: string): string { | ||
return getParts(string) | ||
.join(' ').toUpperCase() | ||
} | ||
export { | ||
camelCase, | ||
pascalCase, | ||
kebabCase, | ||
snakeCase, | ||
constantCase, | ||
pathCase, | ||
spaceCase, | ||
capitalCase, | ||
lowerCase, | ||
upperCase, | ||
} from './core' |
@@ -24,2 +24,3 @@ import test from 'ava' | ||
] | ||
const testSentence = "I'm a M.O.F.O 101 OK?" | ||
@@ -36,40 +37,48 @@ test('spaceCase', t => { | ||
]) | ||
t.is(spaceCase(testSentence), 'I m a M O F O 101 OK') | ||
}) | ||
test('capitalCase', t => { | ||
t.deepEqual(tests.map(capitalCase), [ | ||
'Ponyta Vaporeon Poliwrath Butterfree A', | ||
'Ponyta Vaporeon Poliwrath Butterfree A', | ||
'Ponyta Vaporeon Poliwrath Butterfree A', | ||
'Ponyta Vaporeon Poliwrath Butterfree A', | ||
'Ponyta Vaporeon Poliwrath Butterfree A', | ||
'Ponyta Vaporeon Poliwrath Butterfree A', | ||
'Ponyta Vaporeon Poliwrath Butterfree A', | ||
test('pathCase', t => { | ||
t.deepEqual(tests.map(pathCase), [ | ||
'ponyta/Vaporeon/POLIWRATH/Butterfree/A', | ||
'Ponyta/Vaporeon/POLIWRATH/Butterfree/A', | ||
'ponyta/vaporeon/POLIWRATH/Butterfree/A', | ||
'Ponyta/vaporeon/POLIWRATH/Butterfree/A', | ||
'ponyta/vaporeon/POLIWRATH/Butterfree/A', | ||
'ponyta/Vaporeon/POLIWRATH/Butterfree/A', | ||
'ponyta/Vaporeon/POLIWRATH/Butterfree/A', | ||
]) | ||
t.is(pathCase(testSentence), 'I/m/a/M/O/F/O/101/OK') | ||
}) | ||
test('capitalCase', t => { | ||
tests.forEach(w => { | ||
t.is(capitalCase(w), 'Ponyta Vaporeon Poliwrath Butterfree A') | ||
}) | ||
t.is(capitalCase(testSentence), 'I M A M O F O 101 Ok') | ||
t.is(capitalCase(testSentence, s => s.split(' ')), 'I\'m A M.o.f.o 101 Ok?') | ||
}) | ||
test('upperCase', t => { | ||
t.deepEqual(tests.map(upperCase), [ | ||
'PONYTA VAPOREON POLIWRATH BUTTERFREE A', | ||
'PONYTA VAPOREON POLIWRATH BUTTERFREE A', | ||
'PONYTA VAPOREON POLIWRATH BUTTERFREE A', | ||
'PONYTA VAPOREON POLIWRATH BUTTERFREE A', | ||
'PONYTA VAPOREON POLIWRATH BUTTERFREE A', | ||
'PONYTA VAPOREON POLIWRATH BUTTERFREE A', | ||
'PONYTA VAPOREON POLIWRATH BUTTERFREE A', | ||
]) | ||
tests.forEach(w => { | ||
t.is(upperCase(w), 'PONYTA VAPOREON POLIWRATH BUTTERFREE A') | ||
}) | ||
t.is(upperCase(testSentence), 'I M A M O F O 101 OK') | ||
}) | ||
test('lowerCase', t => { | ||
t.deepEqual(tests.map(lowerCase), [ | ||
'ponyta vaporeon poliwrath butterfree a', | ||
'ponyta vaporeon poliwrath butterfree a', | ||
'ponyta vaporeon poliwrath butterfree a', | ||
'ponyta vaporeon poliwrath butterfree a', | ||
'ponyta vaporeon poliwrath butterfree a', | ||
'ponyta vaporeon poliwrath butterfree a', | ||
'ponyta vaporeon poliwrath butterfree a', | ||
]) | ||
tests.forEach(w => { | ||
t.is(lowerCase(w), 'ponyta vaporeon poliwrath butterfree a') | ||
}) | ||
t.is(lowerCase(testSentence), 'i m a m o f o 101 ok') | ||
}) | ||
test('pascalCase', t => { | ||
tests.forEach(w => { | ||
t.is(pascalCase(w), 'PonytaVaporeonPoliwrathButterfreeA') | ||
}) | ||
t.is(pascalCase(testSentence), 'IMAMOFO101Ok') | ||
t.is(pascalCase(testSentence, s => s.split(' ')), 'I\'mAM.o.f.o101Ok?') | ||
}) | ||
test('camelCase', t => { | ||
@@ -79,2 +88,4 @@ tests.forEach(w => { | ||
}) | ||
t.is(camelCase(testSentence), 'iMAMOFO101Ok') | ||
t.is(camelCase(testSentence, s => s.split(' ')), 'i\'mAM.o.f.o101Ok?') | ||
}) | ||
@@ -86,10 +97,5 @@ | ||
}) | ||
t.is(kebabCase(testSentence), 'i-m-a-m-o-f-o-101-ok') | ||
}) | ||
test('pascalCase', t => { | ||
tests.forEach(w => { | ||
t.is(pascalCase(w), 'PonytaVaporeonPoliwrathButterfreeA') | ||
}) | ||
}) | ||
test('snakeCase', t => { | ||
@@ -99,2 +105,3 @@ tests.forEach(w => { | ||
}) | ||
t.is(snakeCase(testSentence), 'i_m_a_m_o_f_o_101_ok') | ||
}) | ||
@@ -106,14 +113,3 @@ | ||
}) | ||
t.is(constantCase(testSentence), 'I_M_A_M_O_F_O_101_OK') | ||
}) | ||
test('pathCase', t => { | ||
t.deepEqual(tests.map(pathCase), [ | ||
'ponyta/Vaporeon/POLIWRATH/Butterfree/A', | ||
'Ponyta/Vaporeon/POLIWRATH/Butterfree/A', | ||
'ponyta/vaporeon/POLIWRATH/Butterfree/A', | ||
'Ponyta/vaporeon/POLIWRATH/Butterfree/A', | ||
'ponyta/vaporeon/POLIWRATH/Butterfree/A', | ||
'ponyta/Vaporeon/POLIWRATH/Butterfree/A', | ||
'ponyta/Vaporeon/POLIWRATH/Butterfree/A', | ||
]) | ||
}) |
@@ -1,87 +0,1 @@ | ||
/** | ||
* A string.match function that will return an array of "string parts" | ||
* | ||
* @param {string} string | ||
* @returns {string[]} | ||
*/ | ||
export declare function getParts(string: string): any[]; | ||
/** | ||
* converts strings to camelCase | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in camelCase | ||
*/ | ||
export declare function camelCase(string: string): string; | ||
/** | ||
* converts strings to PascalCase | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in PascalCase | ||
*/ | ||
export declare function pascalCase(string: string): string; | ||
/** | ||
* converts strings to kebab-case | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in kebab-case | ||
*/ | ||
export declare function kebabCase(string: string): string; | ||
/** | ||
* converts strings to snake_case | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in snake_case | ||
*/ | ||
export declare function snakeCase(string: string): string; | ||
/** | ||
* converts strings to CONSTANT_CASE | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in CONSTANT_CASE | ||
*/ | ||
export declare function constantCase(string: string): string; | ||
/** | ||
* converts strings to path/case | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in path/case | ||
*/ | ||
export declare function pathCase(string: string): string; | ||
/** | ||
* converts strings to space case (will add spaces but not change casing) | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in path case | ||
*/ | ||
export declare function spaceCase(string: string): string; | ||
/** | ||
* converts strings to Capital Case (with spaces) | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in Capital Case (with spaces) | ||
*/ | ||
export declare function capitalCase(string: string): string; | ||
/** | ||
* converts strings to lower case (with spaces) | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in lower case (with spaces) | ||
*/ | ||
export declare function lowerCase(string: string): string; | ||
/** | ||
* converts strings to UPPER CASE (with spaces) | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in UPPER CASE (with spaces) | ||
*/ | ||
export declare function upperCase(string: string): string; | ||
export { camelCase, pascalCase, kebabCase, snakeCase, constantCase, pathCase, spaceCase, capitalCase, lowerCase, upperCase, } from './core'; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26919
15
712
149