string-case-kit 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +96 -0
- package/package.json +21 -0
- package/tsconfig.json +15 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* =========================================
|
|
3
|
+
STRING CASE KIT (TypeScript)
|
|
4
|
+
Zero-dependency | Chainable | NPM-ready
|
|
5
|
+
========================================= */
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.converter = converter;
|
|
8
|
+
/* ---------- HELPERS ---------- */
|
|
9
|
+
function getWords(text) {
|
|
10
|
+
if (typeof text !== "string" || !text.trim()) {
|
|
11
|
+
throw new Error("Input must be a non-empty string");
|
|
12
|
+
}
|
|
13
|
+
return text
|
|
14
|
+
.trim()
|
|
15
|
+
.replace(/([a-z])([A-Z])/g, "$1 $2")
|
|
16
|
+
.replace(/[_\-.\/]+/g, " ")
|
|
17
|
+
.toLowerCase()
|
|
18
|
+
.split(/\s+/);
|
|
19
|
+
}
|
|
20
|
+
function capitalize(word) {
|
|
21
|
+
return word.charAt(0).toUpperCase() + word.slice(1);
|
|
22
|
+
}
|
|
23
|
+
/* ---------- CASE DETECTOR ---------- */
|
|
24
|
+
function detectCase(text) {
|
|
25
|
+
if (/^[a-z]+([A-Z][a-z]*)+$/.test(text))
|
|
26
|
+
return "camelCase";
|
|
27
|
+
if (/^([A-Z][a-z]+)+$/.test(text))
|
|
28
|
+
return "pascalCase";
|
|
29
|
+
if (/^[a-z]+(_[a-z]+)+$/.test(text))
|
|
30
|
+
return "snakeCase";
|
|
31
|
+
if (/^[A-Z]+(_[A-Z]+)+$/.test(text))
|
|
32
|
+
return "screamingSnakeCase";
|
|
33
|
+
if (/^[a-z]+(-[a-z]+)+$/.test(text))
|
|
34
|
+
return "kebabCase";
|
|
35
|
+
if (/^[a-z]+(\.[a-z]+)+$/.test(text))
|
|
36
|
+
return "dotCase";
|
|
37
|
+
return "unknown";
|
|
38
|
+
}
|
|
39
|
+
/* ---------- MAIN CONVERTER ---------- */
|
|
40
|
+
function converter(input) {
|
|
41
|
+
const words = getWords(input);
|
|
42
|
+
const api = {
|
|
43
|
+
toCamelCase: () => words[0] + words.slice(1).map(capitalize).join(""),
|
|
44
|
+
toPascalCase: () => words.map(capitalize).join(""),
|
|
45
|
+
toSnakeCase: () => words.join("_"),
|
|
46
|
+
toScreamingSnakeCase: () => words.join("_").toUpperCase(),
|
|
47
|
+
toKebabCase: () => words.join("-"),
|
|
48
|
+
toDotCase: () => words.join("."),
|
|
49
|
+
toPathCase: () => words.join("/"),
|
|
50
|
+
toTitleCase: () => words.map(capitalize).join(" "),
|
|
51
|
+
toSentenceCase: () => capitalize(words.join(" ")),
|
|
52
|
+
toUpperCase: () => words.join(" ").toUpperCase(),
|
|
53
|
+
toLowerCase: () => words.join(" "),
|
|
54
|
+
toggleCase: () => input
|
|
55
|
+
.split("")
|
|
56
|
+
.map(c => c === c.toUpperCase()
|
|
57
|
+
? c.toLowerCase()
|
|
58
|
+
: c.toUpperCase())
|
|
59
|
+
.join(""),
|
|
60
|
+
reverse: () => input.split("").reverse().join(""),
|
|
61
|
+
all: () => ({
|
|
62
|
+
camelCase: api.toCamelCase(),
|
|
63
|
+
pascalCase: api.toPascalCase(),
|
|
64
|
+
snakeCase: api.toSnakeCase(),
|
|
65
|
+
screamingSnakeCase: api.toScreamingSnakeCase(),
|
|
66
|
+
kebabCase: api.toKebabCase(),
|
|
67
|
+
dotCase: api.toDotCase(),
|
|
68
|
+
pathCase: api.toPathCase(),
|
|
69
|
+
titleCase: api.toTitleCase(),
|
|
70
|
+
sentenceCase: api.toSentenceCase(),
|
|
71
|
+
upperCase: api.toUpperCase(),
|
|
72
|
+
lowerCase: api.toLowerCase()
|
|
73
|
+
}),
|
|
74
|
+
detect: () => detectCase(input),
|
|
75
|
+
is: {
|
|
76
|
+
camelCase: () => detectCase(input) === "camelCase",
|
|
77
|
+
pascalCase: () => detectCase(input) === "pascalCase",
|
|
78
|
+
snakeCase: () => detectCase(input) === "snakeCase",
|
|
79
|
+
kebabCase: () => detectCase(input) === "kebabCase",
|
|
80
|
+
},
|
|
81
|
+
custom: (separator = "-") => words.join(separator),
|
|
82
|
+
slug: () => words.join("-").replace(/[^a-z0-9-]/g, ""),
|
|
83
|
+
words: () => [...words],
|
|
84
|
+
count: () => ({
|
|
85
|
+
words: words.length,
|
|
86
|
+
chars: input.length
|
|
87
|
+
})
|
|
88
|
+
};
|
|
89
|
+
return api;
|
|
90
|
+
}
|
|
91
|
+
console.log(converter("satyam sharma").toCamelCase());
|
|
92
|
+
// satyamSharma
|
|
93
|
+
console.log(converter("satyam sharma").toKebabCase());
|
|
94
|
+
// satyam-sharma
|
|
95
|
+
console.log(converter("satyam_sharma").toPascalCase());
|
|
96
|
+
// SatyamSharma
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "string-case-kit",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "ts-node-dev --transpile-only src/index.ts",
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"start": "node dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [],
|
|
12
|
+
"author": "",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"description": "",
|
|
15
|
+
"dependencies": {
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"ts-node-dev": "^2.0.0",
|
|
19
|
+
"typescipt": "^1.0.0"
|
|
20
|
+
}
|
|
21
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "node16",
|
|
5
|
+
"outDir": "./dist",
|
|
6
|
+
"rootDir": "./src",
|
|
7
|
+
"strict": true,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"moduleResolution": "node16",
|
|
10
|
+
"forceConsistentCasingInFileNames": true,
|
|
11
|
+
"skipLibCheck": true
|
|
12
|
+
},
|
|
13
|
+
"include": ["src/**/*"],
|
|
14
|
+
"exclude": ["dist"]
|
|
15
|
+
}
|