@alextanhongpin/stringcase
Advanced tools
Comparing version 1.1.1 to 1.2.0
104
index.js
@@ -7,16 +7,20 @@ const normalizeCase = require("./normalize-case"); | ||
class StringCase { | ||
#norm = ""; | ||
constructor(value) { | ||
this.value = value; | ||
this.#norm = normalizeCase(value, "_"); | ||
} | ||
get camel() { | ||
return this.value; | ||
return this.#norm.replace(/_[a-z0-9]/g, match => | ||
match.charAt(1).toUpperCase() | ||
); | ||
} | ||
get snake() { | ||
return this.value; | ||
return this.#norm; | ||
} | ||
get kebab() { | ||
return this.value; | ||
return this.#norm.replace(/_/g, "-"); | ||
} | ||
get pascal() { | ||
return this.value; | ||
return upper(this.camel); | ||
} | ||
@@ -48,91 +52,7 @@ valueOf() { | ||
class SnakeCase extends StringCase { | ||
get snake() { | ||
return normalizeCase(this.value.toLowerCase(), "_"); | ||
} | ||
get camel() { | ||
const result = this.snake.replace(/_[a-z0-9]/gi, str => | ||
str.charAt(1).toUpperCase() | ||
); | ||
return lower(result); | ||
} | ||
get pascal() { | ||
return upper(this.camel); | ||
} | ||
get kebab() { | ||
return this.snake.replace(/_/g, "-"); | ||
} | ||
} | ||
class CamelCase extends StringCase { | ||
get camel() { | ||
const normalized = normalizeCase(this.value); | ||
const breakWord = normalized.includes("-") | ||
? normalized | ||
.split("-") | ||
.map((str, i) => (i === 0 ? str.toLowerCase() : upper(str))) | ||
.join("-") | ||
: normalized; | ||
const result = breakWord.replace(/-[a-z0-9]/gi, match => | ||
match.charAt(1).toUpperCase() | ||
); | ||
return lower(result); | ||
} | ||
get pascal() { | ||
return upper(this.camel); | ||
} | ||
get kebab() { | ||
const result = this.camel.replace(/[a-z][A-Z]/g, word => | ||
word.split("").join("-") | ||
); | ||
return result.toLowerCase(); | ||
} | ||
get snake() { | ||
return this.kebab.replace(/-/g, "_").toLowerCase(); | ||
} | ||
} | ||
class PascalCase extends StringCase { | ||
get pascal() { | ||
const normalized = normalizeCase(this.value); | ||
const result = normalized.replace(/-[a-z0-9]/gi, match => | ||
match.charAt(1).toUpperCase() | ||
); | ||
return upper(result); | ||
} | ||
get camel() { | ||
return lower(this.pascal); | ||
} | ||
get kebab() { | ||
return this.pascal | ||
.replace(/[a-z][A-Z0-9]/g, char => char.split("").join("-")) | ||
.toLowerCase(); | ||
} | ||
get snake() { | ||
return this.kebab.replace(/-/g, "_"); | ||
} | ||
} | ||
class KebabCase extends StringCase { | ||
get kebab() { | ||
return normalizeCase(this.value.toLowerCase()); | ||
} | ||
get snake() { | ||
return this.kebab.replace(/-/g, "_"); | ||
} | ||
get camel() { | ||
const result = this.kebab.replace(/-[a-z0-9]/gi, word => | ||
word.charAt(1).toUpperCase() | ||
); | ||
return lower(result); | ||
} | ||
get pascal() { | ||
return upper(this.camel); | ||
} | ||
} | ||
class SnakeCase extends StringCase {} | ||
class CamelCase extends StringCase {} | ||
class PascalCase extends StringCase {} | ||
class KebabCase extends StringCase {} | ||
class UpperCase extends StringCase {} | ||
class LowerCase extends StringCase {} | ||
@@ -139,0 +59,0 @@ |
@@ -57,6 +57,6 @@ const { | ||
{ | ||
camel: "HELLOWORLD", | ||
pascal: "HELLOWORLD", | ||
snake: "HELLOWORLD", | ||
kebab: "HELLOWORLD" | ||
camel: "helloworld", | ||
pascal: "Helloworld", | ||
snake: "helloworld", | ||
kebab: "helloworld" | ||
} | ||
@@ -69,3 +69,3 @@ ], | ||
camel: "helloworld", | ||
pascal: "helloworld", | ||
pascal: "Helloworld", | ||
snake: "helloworld", | ||
@@ -79,4 +79,4 @@ kebab: "helloworld" | ||
{ | ||
camel: "createdAT", | ||
pascal: "CreatedAT", | ||
camel: "createdAt", | ||
pascal: "CreatedAt", | ||
snake: "created_at", | ||
@@ -90,4 +90,4 @@ kebab: "created-at" | ||
{ | ||
camel: "userID", | ||
pascal: "UserID", | ||
camel: "userId", | ||
pascal: "UserId", | ||
snake: "user_id", | ||
@@ -121,4 +121,4 @@ kebab: "user-id" | ||
{ | ||
camel: "userAB100", | ||
pascal: "UserAB100", | ||
camel: "userAb100", | ||
pascal: "UserAb100", | ||
snake: "user_ab100", | ||
@@ -132,4 +132,4 @@ kebab: "user-ab100" | ||
{ | ||
camel: "johnDOE", | ||
pascal: "JohnDOE", | ||
camel: "johnDoe", | ||
pascal: "JohnDoe", | ||
snake: "john_doe", | ||
@@ -136,0 +136,0 @@ kebab: "john-doe" |
function normalizeCase(str, delimiter = "-") { | ||
return str | ||
.replace(/[^a-z0-9-_]/gi, delimiter) // Replace everything expect alphanumeric, hyphen and underscore with the given delimiter. | ||
.replace(/[^a-z0-9-_]/gi, delimiter) // Replace everything except alphanumeric, hyphen and underscore with the given delimiter. | ||
.replace(/[a-z][A-Z0-9]/g, match => match.split("").join(delimiter)) // Split camel-case word with the delimiter. | ||
.replace(/[_|-]+/g, delimiter) // Replace repeated delimiter with single delimiter. | ||
.replace(/^[-_]+|[-_]+$/g, ""); // Replace starting and trailing delimiters. | ||
.replace(/^[-_]+|[-_]+$/g, "") // Replace starting and trailing delimiters. | ||
.toLowerCase(); | ||
} | ||
module.exports = normalizeCase; |
@@ -7,5 +7,5 @@ const normalizeCase = require("./normalize-case"); | ||
["hello_world", "hello-world"], // Snake case. | ||
["helloWorld", "helloWorld"], // Camel case. | ||
["HelloWorld", "HelloWorld"], // Pascal case. | ||
["HELLOWORLD", "HELLOWORLD"], // Upper case. | ||
["helloWorld", "hello-world"], // Camel case. | ||
["HelloWorld", "hello-world"], // Pascal case. | ||
["HELLOWORLD", "helloworld"], // Upper case. | ||
["helloworld", "helloworld"], // Lower case. | ||
@@ -22,5 +22,5 @@ ["!@#$%^", ""] // Random. | ||
["hello_world", "hello_world"], // Snake case. | ||
["helloWorld", "helloWorld"], // Camel case. | ||
["HelloWorld", "HelloWorld"], // Pascal case. | ||
["HELLOWORLD", "HELLOWORLD"], // Upper case. | ||
["helloWorld", "hello_world"], // Camel case. | ||
["HelloWorld", "hello_world"], // Pascal case. | ||
["HELLOWORLD", "helloworld"], // Upper case. | ||
["helloworld", "helloworld"], // Lower case. | ||
@@ -27,0 +27,0 @@ ["!@#$%^", ""] // Random. |
{ | ||
"name": "@alextanhongpin/stringcase", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Detects and converts stringcases like camelCase, kebab-case, PascalCase and snake_case", | ||
@@ -8,3 +8,3 @@ "main": "index.js", | ||
"test": "jest --watchAll", | ||
"publish": "npm publish --access public" | ||
"publish-package": "npm publish --access public" | ||
}, | ||
@@ -15,3 +15,23 @@ "author": "alextanhongpin <alextan220990@gmail.com>", | ||
"jest": "^26.2.2" | ||
} | ||
}, | ||
"dependencies": {}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/alextanhongpin/stringcase.git" | ||
}, | ||
"keywords": [ | ||
"stringcase", | ||
"camel", | ||
"case", | ||
"pascal", | ||
"case", | ||
"snake", | ||
"case", | ||
"kebab", | ||
"case" | ||
], | ||
"bugs": { | ||
"url": "https://github.com/alextanhongpin/stringcase/issues" | ||
}, | ||
"homepage": "https://github.com/alextanhongpin/stringcase#readme" | ||
} |
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
0
0
0
18798
15
566