@full-pack/string-pack
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -133,3 +133,143 @@ /** | ||
declare function capitalizeWords(str: string): string; | ||
/** | ||
* Checks if a string is in snake_case format. | ||
* @param {string} str - The string to be checked. | ||
* @param {boolean} alphanumeric - If true, allows numbers in the string. | ||
* @returns {boolean} - True if the string is in snake_case format, false otherwise. | ||
* ----- | ||
* ### Conventions | ||
* - Use lowercase letters. | ||
* - Separate words with underscores ("_"). | ||
* - *No numbers allowed, unless specified otherwise.* | ||
* ----- | ||
* @example | ||
* // Valid | ||
* isSnakeCase('snake_case_example'); // true | ||
* isSnakeCase('hello_world'); // true | ||
* | ||
* // Valid with alphanumeric flag | ||
* isSnakeCase('with_1234', true); // true | ||
* isSnakeCase('pi_3_14', true); // true | ||
* | ||
* // Invalid | ||
* isSnakeCase('123at_start'); // false | ||
* isSnakeCase(' no_space_allowed'); // false | ||
* isSnakeCase('no_CAPS'); // false | ||
*/ | ||
declare function isSnakeCase(str: string, alphanumeric?: boolean): boolean; | ||
/** | ||
* Checks if a string is in kebab-case format. | ||
* @param {string} str - The string to be checked. | ||
* @param {boolean} alphanumeric - If true, allows numbers in the string. | ||
* @returns {boolean} True if the string is in kebab-case format, false otherwise. | ||
* ----- | ||
* ### Conventions | ||
* - Use lowercase letters. | ||
* - Separate words with hyphens ("-"). | ||
* - *No numbers allowed, unless specified otherwise.* | ||
* ----- | ||
* @example | ||
* // Valid | ||
* isKebabCase('kebab-case-example'); // true | ||
* isKebabCase('hello-world'); // true | ||
* | ||
* // Valid with alphanumeric flag | ||
* isKebabCase('with-1234', true); // true | ||
* isKebabCase('pi-3-14', true); // true | ||
* | ||
* // Invalid | ||
* isKebabCase('123at-start'); // false | ||
* isKebabCase(' no-space-allowed'); // false | ||
* isKebabCase('no-CAPS'); // false | ||
*/ | ||
declare function isKebabCase(str: string, alphanumeric?: boolean): boolean; | ||
/** | ||
* Checks if a string is in camelCase format. | ||
* @param {string} str - The string to be checked. | ||
* @returns {boolean} True if the string is in camelCase format, false otherwise. | ||
* ----- | ||
* ### Conventions | ||
* - Start with a lowercase letter. | ||
* - Use uppercase for each new word. | ||
* - Should consist of only letters. | ||
* - No separator between words allowed. | ||
* - No numbers allowed. | ||
* ----- | ||
* @example | ||
* // Valid | ||
* isCamelCase('camelCaseExample'); // true | ||
* isCamelCase('helloWorld'); // true | ||
* | ||
* // Invalid | ||
* isCamelCase('CAMEL'); // false | ||
* isCamelCase(' noSpaceAllowed'); // false | ||
* isCamelCase('withThe1234'); // false | ||
*/ | ||
declare function isCamelCase(str: string): boolean; | ||
/** | ||
* Checks if a string is in PascalCase format. | ||
* @param {string} str - The string to be checked. | ||
* @returns {boolean} True if the string is in PascalCase format, false otherwise. | ||
* ----- | ||
* ### Conventions | ||
* - Start with an uppercase letter. | ||
* - Use uppercase for each new word. | ||
* - Should consist of only letters. | ||
* - No separator between words allowed. | ||
* - No numbers allowed. | ||
* ----- | ||
* @example | ||
* // Valid | ||
* isPascalCase('PascalCaseExample'); // true | ||
* isPascalCase('HelloWorld'); // true | ||
* | ||
* // Invalid | ||
* isPascalCase('PASCAL'); // false | ||
* isPascalCase(' NoSpaceAllowed');; // false | ||
* isPascalCase('WithThe1234'); // false | ||
*/ | ||
declare function isPascalCase(str: string): boolean; | ||
/** | ||
* Converts a string to snake_case format. | ||
* @param {string} str The string to be converted. | ||
* @param {boolean} inWords If true, converts numbers to words in snake_case format. | ||
* @returns {string} The string converted to snake_case format. | ||
* @example | ||
* snakeCase('hello WorLd'); // 'hello_world' | ||
* snakeCase('from-kebab-case'); // 'from_kebab_case' | ||
* snakeCase('snake Case With Numbers123', true); // 'snake_case_with_numbers_one_two_three' | ||
*/ | ||
declare function snakeCase(str: string, inWords?: boolean): string; | ||
/** | ||
* Converts a string to kebab-case format. | ||
* @param {string} str The string to be converted. | ||
* @param {boolean} inWords If true, converts numbers to words in kebab-case format. | ||
* @returns {string} The string converted to kebab-case format. | ||
* @example | ||
* kebabCase('h3llo WoRld'); // 'h3llo-world' | ||
* kebabCase('from_snake_case'); // 'from-snake-case' | ||
* kebabCase('kebab Case With Numbers123', true); // 'kebab-case-with-numbers-one-two-three' | ||
*/ | ||
declare function kebabCase(str: string, inWords?: boolean): string; | ||
/** | ||
* Converts a string to camelCase format. | ||
* @param {string} str The string to be converted. | ||
* @returns {string} The string converted to camelCase format. | ||
* @example | ||
* camelCase('hello WoRld'); // 'helloWorld' | ||
* camelCase('Test CaSe ExamplE'); // 'testCaseExample' | ||
* camelCase('camel Case With Numbers123'); // 'camelCaseWithNumbers' | ||
*/ | ||
declare function camelCase(str: string): string; | ||
/** | ||
* Converts a string to PascalCase format. | ||
* @param {string} str The string to be converted. | ||
* @returns {string} The string converted to PascalCase format. | ||
* @example | ||
* pascalCase('hello WoRld'); // 'HelloWorld' | ||
* pascalCase('Test CaSe ExamplE'); // 'TestCaseExample' | ||
* pascalCase('pasCal Case With Numbers123'); // 'PascalCaseWithNumbers' | ||
*/ | ||
declare function pascalCase(str: string): string; | ||
export { capitalizeInitial, capitalizeWords, compare, looseCompare, merge, padBidirectional, padEnd, padStart }; | ||
export { camelCase, capitalizeInitial, capitalizeWords, compare, isCamelCase, isKebabCase, isPascalCase, isSnakeCase, kebabCase, looseCompare, merge, padBidirectional, padEnd, padStart, pascalCase, snakeCase }; |
@@ -23,5 +23,11 @@ "use strict"; | ||
__export(src_exports, { | ||
camelCase: () => camelCase, | ||
capitalizeInitial: () => capitalizeInitial, | ||
capitalizeWords: () => capitalizeWords, | ||
compare: () => compare, | ||
isCamelCase: () => isCamelCase, | ||
isKebabCase: () => isKebabCase, | ||
isPascalCase: () => isPascalCase, | ||
isSnakeCase: () => isSnakeCase, | ||
kebabCase: () => kebabCase, | ||
looseCompare: () => looseCompare, | ||
@@ -31,3 +37,5 @@ merge: () => merge, | ||
padEnd: () => padEnd, | ||
padStart: () => padStart | ||
padStart: () => padStart, | ||
pascalCase: () => pascalCase, | ||
snakeCase: () => snakeCase | ||
}); | ||
@@ -108,2 +116,36 @@ module.exports = __toCommonJS(src_exports); | ||
// src/.internal/getWords.ts | ||
function getWords(str, stringCase = "normal") { | ||
const splitted = []; | ||
switch (stringCase) { | ||
case "camel": { | ||
const regExpArr = str.match(/^([a-z]+)([a-zA-Z]*)$/); | ||
regExpArr !== null ? splitted.push(regExpArr[1]) : splitted.push(""); | ||
} | ||
case "pascal": { | ||
const restOfWord = str.split(/[A-Z]/); | ||
restOfWord.shift(); | ||
const capLetters = str.match(/[A-Z]/g); | ||
for (let i = 0; i < restOfWord.length && capLetters !== null; i++) | ||
splitted.push(capLetters[i] + restOfWord[i]); | ||
break; | ||
} | ||
case "kebab": { | ||
splitted.push(...str.split("-")); | ||
break; | ||
} | ||
case "snake": { | ||
splitted.push(...str.split("-")); | ||
break; | ||
} | ||
default: { | ||
str = str.replace(/[\W_]/g, " "); | ||
str = str.replace(/\s{2,}|\t{1,}/g, " "); | ||
str = str.trim(); | ||
splitted.push(...str.split(" ")); | ||
} | ||
} | ||
return splitted; | ||
} | ||
// src/case.ts | ||
@@ -116,7 +158,72 @@ function capitalizeInitial(str) { | ||
} | ||
function isSnakeCase(str, alphanumeric = false) { | ||
const snakeCase2 = /^[a-z]+(_[a-z]+)*$/; | ||
const snakeCaseWithNumbers = /^([a-z][0-9]*)+(?:_[a-z0-9]+)*$/; | ||
return alphanumeric ? snakeCaseWithNumbers.test(str) : snakeCase2.test(str); | ||
} | ||
function isKebabCase(str, alphanumeric = false) { | ||
const kebabCase2 = /^[a-z]+(-[a-z]+)*$/; | ||
const kebabCaseWithNumbers = /^([a-z][0-9]*)+(?:-[a-z0-9]+)*$/; | ||
return alphanumeric ? kebabCaseWithNumbers.test(str) : kebabCase2.test(str); | ||
} | ||
function isCamelCase(str) { | ||
return /^[a-z]+[a-zA-Z]*$/.test(str); | ||
} | ||
function isPascalCase(str) { | ||
return /^[A-Z][a-z]+[a-zA-Z]*$/.test(str); | ||
} | ||
function snakeCase(str, inWords = false) { | ||
if (isCamelCase(str)) | ||
return getWords(str, "camel").join("_").toLowerCase(); | ||
if (isPascalCase(str)) | ||
return getWords(str, "pascal").join("_").toLowerCase(); | ||
const numbersInWords = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]; | ||
for (let i = 0; inWords && i < numbersInWords.length; i++) { | ||
str = str.replace(RegExp(i.toString(), "g"), (_char) => " " + numbersInWords[i] + " "); | ||
} | ||
return getWords(str).join("_").toLowerCase(); | ||
} | ||
function kebabCase(str, inWords = false) { | ||
if (isCamelCase(str)) | ||
return getWords(str, "camel").join("-").toLowerCase(); | ||
if (isPascalCase(str)) | ||
return getWords(str, "pascal").join("-").toLowerCase(); | ||
const numbersInWords = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]; | ||
for (let i = 0; inWords && i < numbersInWords.length; i++) { | ||
str = str.replace(RegExp(i.toString(), "g"), (_char) => " " + numbersInWords[i] + " "); | ||
} | ||
return getWords(str).join("-").toLowerCase(); | ||
} | ||
function camelCase(str) { | ||
str = str.replace(/[^a-zA-Z]/g, " "); | ||
if (isCamelCase(str)) | ||
return str; | ||
if (isPascalCase(str)) | ||
return str.charAt(0).toLowerCase() + str.substring(1); | ||
return getWords(str).reduce( | ||
(prev, curVal, i) => i !== 1 ? prev + curVal.charAt(0).toUpperCase() + curVal.substring(1).toLowerCase() : prev.toLowerCase() + curVal.charAt(0).toUpperCase() + curVal.substring(1).toLowerCase() | ||
); | ||
} | ||
function pascalCase(str) { | ||
str = str.replace(/[^a-zA-Z]/g, " "); | ||
if (isPascalCase(str)) | ||
return str; | ||
if (isCamelCase(str)) | ||
return str.charAt(0).toUpperCase() + str.substring(1); | ||
return getWords(str).reduce( | ||
(prev, curVal) => prev + curVal.charAt(0).toUpperCase() + curVal.substring(1).toLowerCase(), | ||
"" | ||
); | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
camelCase, | ||
capitalizeInitial, | ||
capitalizeWords, | ||
compare, | ||
isCamelCase, | ||
isKebabCase, | ||
isPascalCase, | ||
isSnakeCase, | ||
kebabCase, | ||
looseCompare, | ||
@@ -126,4 +233,6 @@ merge, | ||
padEnd, | ||
padStart | ||
padStart, | ||
pascalCase, | ||
snakeCase | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@full-pack/string-pack", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "A lightweight and versatile String Utility Package for Node.js & Browser.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
110
README.md
@@ -25,5 +25,14 @@ # @full-pack/string-pack | ||
* [capitalizeWords](#capitalizewords) | ||
* [Case Conversion](#case-conversion) | ||
* [snakeCase](#snakeCase) | ||
* [kebabCase](#kebabCase) | ||
* [camelCase](#camelCase) | ||
* [pascalCase](#pascalCase) | ||
* [Case Validation](#case-validation) | ||
* [isSnakeCase](#isSnakeCase) | ||
* [isKebabCase](#isKebabCase) | ||
* [isCamelCase](#isCamelCase) | ||
* [isPascalCase](#isPascalCase) | ||
(Coming Soon) | ||
* [caseConversion](#) | ||
* [regionMatch](#) | ||
@@ -113,3 +122,3 @@ * [looseRegionMatch](#) | ||
looseCompare("abc", "123"); // false | ||
looseCompare('abc', '123'); // false | ||
``` | ||
@@ -133,2 +142,99 @@ | ||
### Case Conversion | ||
#### snakeCase | ||
Converts a string to snake_case format. | ||
```js | ||
snakeCase('hello WorLd'); // 'hello_world' | ||
snakeCase('from-kebab-case'); // 'from_kebab_case' | ||
snakeCase('snake Case With Numbers123', true); // 'snake_case_with_numbers_one_two_three' | ||
``` | ||
#### kebabCase | ||
Converts a string to kebab-case format. | ||
```js | ||
kebabCase('h3llo WoRld'); // 'h3llo-world' | ||
kebabCase('from_snake_case'); // 'from-snake-case' | ||
kebabCase('kebab Case With Numbers123', true); // 'kebab-case-with-numbers-one-two-three' | ||
``` | ||
#### camelCase | ||
Converts a string to camelCase format. | ||
```js | ||
camelCase('hello WoRld'); // 'helloWorld' | ||
camelCase('Test CaSe ExamplE'); // 'testCaseExample' | ||
camelCase('camel Case With Numbers123'); // 'camelCaseWithNumbers' | ||
``` | ||
#### pascalCase | ||
Converts a string to PascalCase format. | ||
```js | ||
pascalCase('hello WoRld'); // 'HelloWorld' | ||
pascalCase('Test CaSe ExamplE'); // 'TestCaseExample' | ||
pascalCase('pasCal Case With Numbers123'); // 'PascalCaseWithNumbers' | ||
``` | ||
### Case Validation | ||
#### isSnakeCase | ||
Checks if a string is in snake_case format. | ||
```js | ||
// Valid | ||
isSnakeCase('snake_case_example'); // true | ||
isSnakeCase('hello_world'); // true | ||
// Valid with alphanumeric flag | ||
isSnakeCase('with_1234', true); // true | ||
isSnakeCase('pi_3_14', true); // true | ||
// Invalid | ||
isSnakeCase('123at_start'); // false | ||
isSnakeCase(' no_space_allowed'); // false | ||
isSnakeCase('no_CAPS'); // false | ||
``` | ||
#### isKebabCase | ||
Checks if a string is in kebab-case format. | ||
```js | ||
// Valid | ||
isKebabCase('kebab-case-example'); // true | ||
isKebabCase('hello-world'); // true | ||
// Valid with alphanumeric flag | ||
isKebabCase('with-1234', true); // true | ||
isKebabCase('pi-3-14', true); // true | ||
// Invalid | ||
isKebabCase('123at-start'); // false | ||
isKebabCase(' no-space-allowed'); // false | ||
isKebabCase('no-CAPS'); // false | ||
``` | ||
#### isCamelCase | ||
Checks if a string is in camelCase format. | ||
```js | ||
// Valid | ||
isCamelCase('camelCaseExample'); // true | ||
isCamelCase('helloWorld'); // true | ||
// Invalid | ||
isCamelCase('CAMEL'); // false | ||
isCamelCase(' noSpaceAllowed'); // false | ||
isCamelCase('withThe1234'); // false | ||
``` | ||
#### isPascalCase | ||
Checks if a string is in PascalCase format. | ||
```js | ||
// Valid | ||
isPascalCase('PascalCaseExample'); // true | ||
isPascalCase('HelloWorld'); // true | ||
// Invalid | ||
isPascalCase('PASCAL'); // false | ||
isPascalCase(' NoSpaceAllowed');; // false | ||
isPascalCase('WithThe1234'); // false | ||
``` | ||
## Build | ||
@@ -135,0 +241,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
78586
682
244