From ef7564e8dee625dda3565b99cd1f9223fc980255 Mon Sep 17 00:00:00 2001 From: Mesqueeb Date: Mon, 27 Jan 2020 10:20:12 +0900 Subject: [PATCH] =?UTF-8?q?v1.0.0=20-=20Rework=20handling=20of=20spaced=20?= =?UTF-8?q?strings=20=F0=9F=A7=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 121 +++++++++++++++++++--------------------------- dist/index.cjs.js | 66 +++++++++++++------------ dist/index.esm.js | 66 +++++++++++++------------ package.json | 2 +- src/core.ts | 56 +++++++++++---------- src/utils.ts | 14 +++++- test/index.js | 47 +++++++++--------- types/core.d.ts | 10 ++-- types/utils.d.ts | 9 +++- 9 files changed, 199 insertions(+), 192 deletions(-) diff --git a/README.md b/README.md index 9245d50..ffbc721 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ I wanted to try my hand at the smallest iteration possible. ## Usage -case-anything supports tree-shaking. +case-anything supports tree-shaking and is side-effect free! ```js import { camelCase, pascalCase, kebabCase, snakeCase, constantCase } from 'case-anything' @@ -32,37 +32,27 @@ import { camelCase, pascalCase, kebabCase, snakeCase, constantCase } from 'case- const testString = 'PonytaVaporeon_poliwrath-BUTTERFREE' // or any variant on this -camelCase(testString) - === 'ponytaVaporeonPoliwrathButterfree' +camelCase(testString) === 'ponytaVaporeonPoliwrathButterfree' -pascalCase(testString) - === 'PonytaVaporeonPoliwrathButterfree' +pascalCase(testString) === 'PonytaVaporeonPoliwrathButterfree' -kebabCase(testString) - === 'ponyta-vaporeon-poliwrath-butterfree' +kebabCase(testString) === 'ponyta-vaporeon-poliwrath-butterfree' -snakeCase(testString) - === 'ponyta_vaporeon_poliwrath_butterfree' +snakeCase(testString) === 'ponyta_vaporeon_poliwrath_butterfree' -constantCase(testString) - === 'PONYTA_VAPOREON_POLIWRATH_BUTTERFREE' - -pathCase(testString) - === 'Ponyta/Vaporeon/Poliwrath/BUTTERFREE' +constantCase(testString) === 'PONYTA_VAPOREON_POLIWRATH_BUTTERFREE' ``` -There is also `spaceCase` and `pathCase`, which does not convert the casing: +There is also `spaceCase` and `pathCase`, which does **not convert the casing**: ```js import { spaceCase, pathCase } from 'case-anything' const testString = 'PonytaVaporeon_poliwrath-BUTTERFREE' -spaceCase(testString) - === 'Ponyta Vaporeon poliwrath BUTTERFREE' +spaceCase(testString) === 'Ponyta Vaporeon poliwrath BUTTERFREE' -pathCase(testString) - === 'Ponyta/Vaporeon/poliwrath/BUTTERFREE' +pathCase(testString) === 'Ponyta/Vaporeon/poliwrath/BUTTERFREE' ``` There is also upper, lower and capital case. These will all convert the casing & also add spaces in between: @@ -72,77 +62,66 @@ import { upperCase, lowerCase, capitalCase } from 'case-anything' const testString = 'PonytaVaporeon_poliwrath-BUTTERFREE' -upperCase(testString) - === 'PONYTA VAPOREON POLIWRATH BUTTERFREE' -lowerCase(testString) - === 'ponyta vaporeon poliwrath butterfree' -capitalCase(testString) - === 'Ponyta Vaporeon Poliwrath Butterfree' +upperCase(testString) === 'PONYTA VAPOREON POLIWRATH BUTTERFREE' +lowerCase(testString) === 'ponyta vaporeon poliwrath butterfree' +capitalCase(testString) === 'Ponyta Vaporeon Poliwrath Butterfree' ``` -### 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. +### When spaces are involved -This is possible for the `capitalCase`, `pascalCase` or `camelCase`. +As soon as there is a space in the target string, it will regard the input as a "sentence" and only split each part at the spaces. -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: +See this example to understand each case: + ```js -const testString = "listen I'M O.K.!" +const testString = "listen I'm O.K.!" + +// splits on spaces & removes special characters +camelCase(listenImOK) === 'listenImOk' +pascalCase(listenImOK) === 'ListenImOk' +kebabCase(listenImOK) === 'listen-im-ok' +snakeCase(listenImOK) === 'listen_im_ok' +constantCase(listenImOK) === 'LISTEN_IM_OK' + +// splits on spaces & keeps special characters +spaceCase(listenImOK) === "listen I'm O.K.!" +pathCase(listenImOK) === "listen/I'm/O.K.!" +lowerCase(listenImOK) === "listen i'm o.k.!" +upperCase(listenImOK) === "LISTEN I'M O.K.!" +capitalCase(listenImOK) === "Listen I'm O.k.!" +``` + -// capitalCase expected behaviour: -capitalCase(testString) - === 'Listen I M O K' +### When special alphabet is involved -// capitalCase with own split function: -capitalCase(testString, s => s.split(' ')) - === "Listen I'm O.k.!" -``` +Currently what keeps the package small is the fact that I use a simple regex to find all the parts in a string: -The reason this is only possible for three functions is because the logic behind the other functions is simple enough to implement yourself. Eg.: +- `/^[a-z]+|[A-Z][a-z]+|[a-z]+|[0-9]+|[A-Z]+(?![a-z])/g` -```js -// snakeCase with own split function: -testString.split(' ').join('_').toLowerCase() - === "listen_i'm_o.k.!" -``` +That means that alphabet letters like é, ç, ü, ī and many others aren't compatible. + +If there is a simple way to include these via unicode ranges in the regex, please feel free to open a PR or issue! ## Package size We'll compare this package with [blakeembrey/change-case](https://github.com/blakeembrey/change-case), a very famous package on npm. -| | case-anything | change-case | -| --- | --- | --- | -| camelCase | 1.1K (572) | 27.2K (6K) | -| pascalCase | 1.1K (561) | 27.4K (6.1K) | -| kebabCase | 1.1K (541) | 26.8K (5.9K) | -| snakeCase | 1.1K (540) | 26.8K (5.9K) | -| constantCase | 1.1K (540) | 27.2K (6K) | -| pathCase | 1K (530) | 26.8K (5.9K) | +| | case-anything | change-case | +| ------------ | ------------- | ------------ | +| camelCase | 1.1K (572) | 27.2K (6K) | +| pascalCase | 1.1K (561) | 27.4K (6.1K) | +| kebabCase | 1.1K (541) | 26.8K (5.9K) | +| snakeCase | 1.1K (540) | 26.8K (5.9K) | +| constantCase | 1.1K (540) | 27.2K (6K) | +| pathCase | 1K (530) | 26.8K (5.9K) | ## Source code -It is literally just this code: +What keeps my package small, is that it's literally just a regex: ```js -function getParts (string) { - return string.match(/^[a-z]+|[A-Z][a-z]+|[A-Z]+|[a-z]+/g) -} - -export function camelCase (string) { - return getParts(string) - .reduce((result, match, index) => { - return (index === 0) - ? match.toLowerCase() - : result + match[0].toUpperCase() + match.slice(1).toLowerCase() - }, '') +export function splitOnSpecialChars (string: string): any[] { + return string.match(/^[a-z]+|[A-Z][a-z]+|[a-z]+|[0-9]+|[A-Z]+(?![a-z])/g) } - -export function kebabCase (string) { - return getParts(string) - .join('-').toLowerCase() -} - -// etc... ``` diff --git a/dist/index.cjs.js b/dist/index.cjs.js index 3916114..75566e7 100644 --- a/dist/index.cjs.js +++ b/dist/index.cjs.js @@ -8,9 +8,21 @@ Object.defineProperty(exports, '__esModule', { value: true }); * @param {string} string * @returns {string[]} */ -function getParts(string) { +function splitOnSpecialChars(string) { return string.match(/^[a-z]+|[A-Z][a-z]+|[a-z]+|[0-9]+|[A-Z]+(?![a-z])/g); } +/** + * A string.match function that will return an array of "string parts" + * + * @param {string} string + * @returns {string[]} + */ +function getParts(string, noSpecialChars) { + if (noSpecialChars === void 0) { noSpecialChars = false; } + var target = string.trim(); + var parts = target.includes(' ') ? target.split(' ') : splitOnSpecialChars(target); + return noSpecialChars ? parts.map(function (part) { return part.replace(/[^a-zA-Z0-9]/g, ''); }) : parts; +} /** * Capitalises a single word * @@ -22,21 +34,17 @@ function capitaliseWord(string) { return string[0].toUpperCase() + string.slice(1).toLowerCase(); } +var noSpecialChars = true; /** * converts strings to camelCase * * @export * @param {string} string - * @param {function} [splitFn=getParts] the function to split the string. Defaults to `getParts` * @returns {string} in camelCase */ -function camelCase(string, splitFn) { - if (splitFn === void 0) { splitFn = getParts; } - return splitFn(string) - .reduce(function (result, match, index) { - return (index === 0) - ? match.toLowerCase() - : result + capitaliseWord(match); +function camelCase(string) { + return getParts(string, noSpecialChars).reduce(function (result, match, index) { + return index === 0 ? match.toLowerCase() : result + capitaliseWord(match); }, ''); } /** @@ -44,13 +52,10 @@ function camelCase(string, splitFn) { * * @export * @param {string} string - * @param {function} [splitFn=getParts] the function to split the string. Defaults to `getParts` * @returns {string} in PascalCase */ -function pascalCase(string, splitFn) { - if (splitFn === void 0) { splitFn = getParts; } - return splitFn(string) - .reduce(function (result, match) { +function pascalCase(string) { + return getParts(string, noSpecialChars).reduce(function (result, match) { return result + capitaliseWord(match); }, ''); } @@ -62,8 +67,9 @@ function pascalCase(string, splitFn) { * @returns {string} in kebab-case */ function kebabCase(string) { - return getParts(string) - .join('-').toLowerCase(); + return getParts(string, noSpecialChars) + .join('-') + .toLowerCase(); } /** * converts strings to snake_case @@ -73,8 +79,9 @@ function kebabCase(string) { * @returns {string} in snake_case */ function snakeCase(string) { - return getParts(string) - .join('_').toLowerCase(); + return getParts(string, noSpecialChars) + .join('_') + .toLowerCase(); } /** * converts strings to CONSTANT_CASE @@ -84,8 +91,9 @@ function snakeCase(string) { * @returns {string} in CONSTANT_CASE */ function constantCase(string) { - return getParts(string) - .join('_').toUpperCase(); + return getParts(string, noSpecialChars) + .join('_') + .toUpperCase(); } /** * converts strings to path/case @@ -95,8 +103,7 @@ function constantCase(string) { * @returns {string} in path/case */ function pathCase(string) { - return getParts(string) - .join('/'); + return getParts(string).join('/'); } /** * converts strings to space case (will add spaces but not change casing) @@ -106,20 +113,17 @@ function pathCase(string) { * @returns {string} in path case */ function spaceCase(string) { - return getParts(string) - .join(' '); + return getParts(string).join(' '); } /** * converts strings to Capital Case (with spaces) * * @export * @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, splitFn) { - if (splitFn === void 0) { splitFn = getParts; } - return splitFn(string) +function capitalCase(string) { + return getParts(string) .reduce(function (result, match) { return result + " " + capitaliseWord(match); }, '') @@ -134,7 +138,8 @@ function capitalCase(string, splitFn) { */ function lowerCase(string) { return getParts(string) - .join(' ').toLowerCase(); + .join(' ') + .toLowerCase(); } /** * converts strings to UPPER CASE (with spaces) @@ -145,7 +150,8 @@ function lowerCase(string) { */ function upperCase(string) { return getParts(string) - .join(' ').toUpperCase(); + .join(' ') + .toUpperCase(); } exports.camelCase = camelCase; diff --git a/dist/index.esm.js b/dist/index.esm.js index 26f62a4..d77afbf 100644 --- a/dist/index.esm.js +++ b/dist/index.esm.js @@ -4,9 +4,21 @@ * @param {string} string * @returns {string[]} */ -function getParts(string) { +function splitOnSpecialChars(string) { return string.match(/^[a-z]+|[A-Z][a-z]+|[a-z]+|[0-9]+|[A-Z]+(?![a-z])/g); } +/** + * A string.match function that will return an array of "string parts" + * + * @param {string} string + * @returns {string[]} + */ +function getParts(string, noSpecialChars) { + if (noSpecialChars === void 0) { noSpecialChars = false; } + var target = string.trim(); + var parts = target.includes(' ') ? target.split(' ') : splitOnSpecialChars(target); + return noSpecialChars ? parts.map(function (part) { return part.replace(/[^a-zA-Z0-9]/g, ''); }) : parts; +} /** * Capitalises a single word * @@ -18,21 +30,17 @@ function capitaliseWord(string) { return string[0].toUpperCase() + string.slice(1).toLowerCase(); } +var noSpecialChars = true; /** * converts strings to camelCase * * @export * @param {string} string - * @param {function} [splitFn=getParts] the function to split the string. Defaults to `getParts` * @returns {string} in camelCase */ -function camelCase(string, splitFn) { - if (splitFn === void 0) { splitFn = getParts; } - return splitFn(string) - .reduce(function (result, match, index) { - return (index === 0) - ? match.toLowerCase() - : result + capitaliseWord(match); +function camelCase(string) { + return getParts(string, noSpecialChars).reduce(function (result, match, index) { + return index === 0 ? match.toLowerCase() : result + capitaliseWord(match); }, ''); } /** @@ -40,13 +48,10 @@ function camelCase(string, splitFn) { * * @export * @param {string} string - * @param {function} [splitFn=getParts] the function to split the string. Defaults to `getParts` * @returns {string} in PascalCase */ -function pascalCase(string, splitFn) { - if (splitFn === void 0) { splitFn = getParts; } - return splitFn(string) - .reduce(function (result, match) { +function pascalCase(string) { + return getParts(string, noSpecialChars).reduce(function (result, match) { return result + capitaliseWord(match); }, ''); } @@ -58,8 +63,9 @@ function pascalCase(string, splitFn) { * @returns {string} in kebab-case */ function kebabCase(string) { - return getParts(string) - .join('-').toLowerCase(); + return getParts(string, noSpecialChars) + .join('-') + .toLowerCase(); } /** * converts strings to snake_case @@ -69,8 +75,9 @@ function kebabCase(string) { * @returns {string} in snake_case */ function snakeCase(string) { - return getParts(string) - .join('_').toLowerCase(); + return getParts(string, noSpecialChars) + .join('_') + .toLowerCase(); } /** * converts strings to CONSTANT_CASE @@ -80,8 +87,9 @@ function snakeCase(string) { * @returns {string} in CONSTANT_CASE */ function constantCase(string) { - return getParts(string) - .join('_').toUpperCase(); + return getParts(string, noSpecialChars) + .join('_') + .toUpperCase(); } /** * converts strings to path/case @@ -91,8 +99,7 @@ function constantCase(string) { * @returns {string} in path/case */ function pathCase(string) { - return getParts(string) - .join('/'); + return getParts(string).join('/'); } /** * converts strings to space case (will add spaces but not change casing) @@ -102,20 +109,17 @@ function pathCase(string) { * @returns {string} in path case */ function spaceCase(string) { - return getParts(string) - .join(' '); + return getParts(string).join(' '); } /** * converts strings to Capital Case (with spaces) * * @export * @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, splitFn) { - if (splitFn === void 0) { splitFn = getParts; } - return splitFn(string) +function capitalCase(string) { + return getParts(string) .reduce(function (result, match) { return result + " " + capitaliseWord(match); }, '') @@ -130,7 +134,8 @@ function capitalCase(string, splitFn) { */ function lowerCase(string) { return getParts(string) - .join(' ').toLowerCase(); + .join(' ') + .toLowerCase(); } /** * converts strings to UPPER CASE (with spaces) @@ -141,7 +146,8 @@ function lowerCase(string) { */ function upperCase(string) { return getParts(string) - .join(' ').toUpperCase(); + .join(' ') + .toUpperCase(); } export { camelCase, capitalCase, constantCase, kebabCase, lowerCase, pascalCase, pathCase, snakeCase, spaceCase, upperCase }; diff --git a/package.json b/package.json index 70f57c4..85b2b3d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "case-anything", - "version": "0.3.1", + "version": "1.0.0", "sideEffects": false, "description": "camelCase, kebab-case, PascalCase... a simple integration with nano package size. (SMALL footprint!)", "main": "dist/index.cjs.js", diff --git a/src/core.ts b/src/core.ts index 9961a2a..7c8b8b7 100644 --- a/src/core.ts +++ b/src/core.ts @@ -1,20 +1,18 @@ import { getParts, capitaliseWord } from './utils' +const noSpecialChars = true + /** * converts strings to camelCase * * @export * @param {string} string - * @param {function} [splitFn=getParts] the function to split the string. Defaults to `getParts` * @returns {string} in camelCase */ -export function camelCase (string: string, splitFn = getParts): string { - return splitFn(string) - .reduce((result, match, index) => { - return (index === 0) - ? match.toLowerCase() - : result + capitaliseWord(match) - }, '') +export function camelCase (string: string): string { + return getParts(string, noSpecialChars).reduce((result, match, index) => { + return index === 0 ? match.toLowerCase() : result + capitaliseWord(match) + }, '') } /** @@ -22,14 +20,12 @@ export function camelCase (string: string, splitFn = getParts): string { * * @export * @param {string} string - * @param {function} [splitFn=getParts] the function to split the string. Defaults to `getParts` * @returns {string} in PascalCase */ -export function pascalCase (string: string, splitFn = getParts): string { - return splitFn(string) - .reduce((result, match) => { - return result + capitaliseWord(match) - }, '') +export function pascalCase (string: string): string { + return getParts(string, noSpecialChars).reduce((result, match) => { + return result + capitaliseWord(match) + }, '') } /** @@ -40,8 +36,9 @@ export function pascalCase (string: string, splitFn = getParts): string { * @returns {string} in kebab-case */ export function kebabCase (string: string): string { - return getParts(string) - .join('-').toLowerCase() + return getParts(string, noSpecialChars) + .join('-') + .toLowerCase() } /** @@ -52,8 +49,9 @@ export function kebabCase (string: string): string { * @returns {string} in snake_case */ export function snakeCase (string: string): string { - return getParts(string) - .join('_').toLowerCase() + return getParts(string, noSpecialChars) + .join('_') + .toLowerCase() } /** @@ -64,8 +62,9 @@ export function snakeCase (string: string): string { * @returns {string} in CONSTANT_CASE */ export function constantCase (string: string): string { - return getParts(string) - .join('_').toUpperCase() + return getParts(string, noSpecialChars) + .join('_') + .toUpperCase() } /** @@ -76,8 +75,7 @@ export function constantCase (string: string): string { * @returns {string} in path/case */ export function pathCase (string: string): string { - return getParts(string) - .join('/') + return getParts(string).join('/') } /** @@ -88,8 +86,7 @@ export function pathCase (string: string): string { * @returns {string} in path case */ export function spaceCase (string: string): string { - return getParts(string) - .join(' ') + return getParts(string).join(' ') } /** @@ -97,11 +94,10 @@ export function spaceCase (string: string): string { * * @export * @param {string} string - * @param {function} [splitFn=getParts] the function to split the string. Defaults to `getParts` * @returns {string} in Capital Case (with spaces) */ -export function capitalCase (string: string, splitFn = getParts): string { - return splitFn(string) +export function capitalCase (string: string): string { + return getParts(string) .reduce((result, match) => { return `${result} ${capitaliseWord(match)}` }, '') @@ -117,7 +113,8 @@ export function capitalCase (string: string, splitFn = getParts): string { */ export function lowerCase (string: string): string { return getParts(string) - .join(' ').toLowerCase() + .join(' ') + .toLowerCase() } /** @@ -129,5 +126,6 @@ export function lowerCase (string: string): string { */ export function upperCase (string: string): string { return getParts(string) - .join(' ').toUpperCase() + .join(' ') + .toUpperCase() } diff --git a/src/utils.ts b/src/utils.ts index d6bee8a..62d81df 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -4,10 +4,22 @@ * @param {string} string * @returns {string[]} */ -export function getParts (string: string): any[] { +export function splitOnSpecialChars (string: string): any[] { return string.match(/^[a-z]+|[A-Z][a-z]+|[a-z]+|[0-9]+|[A-Z]+(?![a-z])/g) } +/** + * A string.match function that will return an array of "string parts" + * + * @param {string} string + * @returns {string[]} + */ +export function getParts (string: string, noSpecialChars = false): any[] { + const target = string.trim() + const parts = target.includes(' ') ? target.split(' ') : splitOnSpecialChars(target) + return noSpecialChars ? parts.map(part => part.replace(/[^a-zA-Z0-9]/g, '')) : parts +} + /** * Capitalises a single word * diff --git a/test/index.js b/test/index.js index 6236af7..8ec13e9 100644 --- a/test/index.js +++ b/test/index.js @@ -13,10 +13,10 @@ import { } from '../dist/index.cjs' const tests = [ - 'ponytaVaporeonPOLIWRATH ButterfreeA', - 'PonytaVaporeonPOLIWRATH ButterfreeA', + 'ponytaVaporeonPOLIWRATH_ButterfreeA', + 'PonytaVaporeonPOLIWRATH_ButterfreeA', 'ponyta-vaporeon-POLIWRATH-ButterfreeA', - 'Ponyta vaporeon POLIWRATH ButterfreeA', + 'Ponyta~vaporeon~POLIWRATH/ButterfreeA', 'ponyta_vaporeon_POLIWRATH_ButterfreeA', 'ponyta_Vaporeon_POLIWRATH_ButterfreeA', 'ponyta.Vaporeon.POLIWRATH.ButterfreeA', @@ -89,23 +89,26 @@ test('constantCase - QTableA', t => { t.is(constantCase(QTableA), 'Q_TABLE_A') } // 3 const ImaMIB101OK = "I'm a M.I.B. 101 OK?" -test('spaceCase I\'m a M.I.B. 101 OK?', t => { t.is(spaceCase(ImaMIB101OK), 'I m a M I B 101 OK') }) // prettier-ignore -test('pathCase I\'m a M.I.B. 101 OK?', t => { t.is(pathCase(ImaMIB101OK), 'I/m/a/M/I/B/101/OK') }) // prettier-ignore -test('capitalCase I\'m a M.I.B. 101 OK?', t => { t.is(capitalCase(ImaMIB101OK), 'I M A M I B 101 Ok') }) // prettier-ignore -test('upperCase I\'m a M.I.B. 101 OK?', t => { t.is(upperCase(ImaMIB101OK), 'I M A M I B 101 OK') }) // prettier-ignore -test('lowerCase I\'m a M.I.B. 101 OK?', t => { t.is(lowerCase(ImaMIB101OK), 'i m a m i b 101 ok') }) // prettier-ignore -test('pascalCase I\'m a M.I.B. 101 OK?', t => { t.is(pascalCase(ImaMIB101OK), 'IMAMIB101Ok') }) // prettier-ignore -test('camelCase I\'m a M.I.B. 101 OK?', t => { t.is(camelCase(ImaMIB101OK), 'iMAMIB101Ok') }) // prettier-ignore -test('kebabCase I\'m a M.I.B. 101 OK?', t => { t.is(kebabCase(ImaMIB101OK), 'i-m-a-m-i-b-101-ok') }) // prettier-ignore -test('snakeCase I\'m a M.I.B. 101 OK?', t => { t.is(snakeCase(ImaMIB101OK), 'i_m_a_m_i_b_101_ok') }) // prettier-ignore -test('constantCase I\'m a M.I.B. 101 OK?', t => { t.is(constantCase(ImaMIB101OK), 'I_M_A_M_I_B_101_OK') }) // prettier-ignore +test("camelCase I'm a M.I.B. 101 OK?", t => { t.is(camelCase(ImaMIB101OK), 'imAMib101Ok') }) // prettier-ignore +test("pascalCase I'm a M.I.B. 101 OK?", t => { t.is(pascalCase(ImaMIB101OK), 'ImAMib101Ok') }) // prettier-ignore +test("kebabCase I'm a M.I.B. 101 OK?", t => { t.is(kebabCase(ImaMIB101OK), 'im-a-mib-101-ok') }) // prettier-ignore +test("snakeCase I'm a M.I.B. 101 OK?", t => { t.is(snakeCase(ImaMIB101OK), 'im_a_mib_101_ok') }) // prettier-ignore +test("constantCase I'm a M.I.B. 101 OK?", t => { t.is(constantCase(ImaMIB101OK), 'IM_A_MIB_101_OK') }) // prettier-ignore +test("spaceCase I'm a M.I.B. 101 OK?", t => { t.is(spaceCase(ImaMIB101OK), "I'm a M.I.B. 101 OK?") }) // prettier-ignore +test("pathCase I'm a M.I.B. 101 OK?", t => { t.is(pathCase(ImaMIB101OK), "I'm/a/M.I.B./101/OK?") }) // prettier-ignore +test("lowerCase I'm a M.I.B. 101 OK?", t => { t.is(lowerCase(ImaMIB101OK), "i'm a m.i.b. 101 ok?") }) // prettier-ignore +test("upperCase I'm a M.I.B. 101 OK?", t => { t.is(upperCase(ImaMIB101OK), "I'M A M.I.B. 101 OK?") }) // prettier-ignore +test("capitalCase I'm a M.I.B. 101 OK?", t => { t.is(capitalCase(ImaMIB101OK), "I'm A M.i.b. 101 Ok?") }) // prettier-ignore -test('capitalCase -_-', t => { - t.is(capitalCase(ImaMIB101OK, s => s.split(' ')), "I'm A M.i.b. 101 Ok?") // prettier-ignore -}) -test('pascalCase -_-', t => { - t.is(pascalCase(ImaMIB101OK, s => s.split(' ')), "I'mAM.i.b.101Ok?") // prettier-ignore -}) -test('camelCase -_-', t => { - t.is(camelCase(ImaMIB101OK, s => s.split(' ')), "i'mAM.i.b.101Ok?") // prettier-ignore -}) +const listenImOK = "listen I'm O.K.!" + +test("camelCase listen I'm O.K.!", t => { t.is(camelCase(listenImOK), 'listenImOk') }) // prettier-ignore +test("pascalCase listen I'm O.K.!", t => { t.is(pascalCase(listenImOK), 'ListenImOk') }) // prettier-ignore +test("kebabCase listen I'm O.K.!", t => { t.is(kebabCase(listenImOK), 'listen-im-ok') }) // prettier-ignore +test("snakeCase listen I'm O.K.!", t => { t.is(snakeCase(listenImOK), 'listen_im_ok') }) // prettier-ignore +test("constantCase listen I'm O.K.!", t => { t.is(constantCase(listenImOK), 'LISTEN_IM_OK') }) // prettier-ignore +test("spaceCase listen I'm O.K.!", t => { t.is(spaceCase(listenImOK), "listen I'm O.K.!") }) // prettier-ignore +test("pathCase listen I'm O.K.!", t => { t.is(pathCase(listenImOK), "listen/I'm/O.K.!") }) // prettier-ignore +test("lowerCase listen I'm O.K.!", t => { t.is(lowerCase(listenImOK), "listen i'm o.k.!") }) // prettier-ignore +test("upperCase listen I'm O.K.!", t => { t.is(upperCase(listenImOK), "LISTEN I'M O.K.!") }) // prettier-ignore +test("capitalCase listen I'm O.K.!", t => { t.is(capitalCase(listenImOK), "Listen I'm O.k.!") }) // prettier-ignore diff --git a/types/core.d.ts b/types/core.d.ts index 5f24659..6f9ccfa 100644 --- a/types/core.d.ts +++ b/types/core.d.ts @@ -1,22 +1,19 @@ -import { getParts } from './utils'; /** * converts strings to camelCase * * @export * @param {string} string - * @param {function} [splitFn=getParts] the function to split the string. Defaults to `getParts` * @returns {string} in camelCase */ -export declare function camelCase(string: string, splitFn?: typeof getParts): string; +export declare function camelCase(string: string): string; /** * converts strings to PascalCase * * @export * @param {string} string - * @param {function} [splitFn=getParts] the function to split the string. Defaults to `getParts` * @returns {string} in PascalCase */ -export declare function pascalCase(string: string, splitFn?: typeof getParts): string; +export declare function pascalCase(string: string): string; /** * converts strings to kebab-case * @@ -62,10 +59,9 @@ export declare function spaceCase(string: string): string; * * @export * @param {string} string - * @param {function} [splitFn=getParts] the function to split the string. Defaults to `getParts` * @returns {string} in Capital Case (with spaces) */ -export declare function capitalCase(string: string, splitFn?: typeof getParts): string; +export declare function capitalCase(string: string): string; /** * converts strings to lower case (with spaces) * diff --git a/types/utils.d.ts b/types/utils.d.ts index af99d8f..543fdd7 100644 --- a/types/utils.d.ts +++ b/types/utils.d.ts @@ -4,7 +4,14 @@ * @param {string} string * @returns {string[]} */ -export declare function getParts(string: string): any[]; +export declare function splitOnSpecialChars(string: string): any[]; +/** + * A string.match function that will return an array of "string parts" + * + * @param {string} string + * @returns {string[]} + */ +export declare function getParts(string: string, noSpecialChars?: boolean): any[]; /** * Capitalises a single word *