-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
240 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*.ts] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"root": true, | ||
"parser": "@typescript-eslint/parser", | ||
"parserOptions": { | ||
"project": "./tsconfig.json" | ||
}, | ||
"plugins": [ | ||
"@typescript-eslint" | ||
], | ||
"extends": [ | ||
"plugin:@typescript-eslint/recommended", | ||
"prettier", | ||
"prettier/@typescript-eslint" | ||
], | ||
"rules": { | ||
"@typescript-eslint/array-type": [ | ||
"error", | ||
"array-simple" | ||
], | ||
"@typescript-eslint/explicit-member-accessibility": [ | ||
"off" | ||
], | ||
"@typescript-eslint/no-non-null-assertion": [ | ||
"off" | ||
], | ||
"@typescript-eslint/no-parameter-properties": [ | ||
"off" | ||
], | ||
"@typescript-eslint/no-unused-vars": [ | ||
"error", | ||
{ | ||
"argsIgnorePattern": "^_", | ||
"varsIgnorePattern": "_" | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
language: node_js | ||
node_js: | ||
- "lts/*" | ||
env: | ||
- DENO_VERSION="v0.3.10" TS_VERSION="3.4.4" | ||
before_script: | ||
- curl -L https://deno.land/x/install/install.sh | sh -s $DENO_VERSION | ||
- export PATH="$HOME/.deno/bin:$PATH" | ||
- npm install -g prettier eslint typescript@$TS_VERSION @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-config-prettier | ||
script: | ||
- prettier -c "./**/*.ts" | ||
- eslint **/*.ts --max-warnings=0 | ||
- deno -A _test.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,31 @@ | ||
# deno_caseStyle | ||
# Deno Case Style | ||
|
||
A string validator and formater for case Style | ||
|
||
## Validate | ||
|
||
Returns a boolean if the string is validated by the style Case: | ||
```ts | ||
import { validate } from "./mod.ts"; | ||
|
||
validate("SALADE-TOMATE-OIGNONS", caseStyle.screamingKebabCase); // true | ||
validate("DIZ_IZ_DA_GLOBAL_VAL", caseStyle.screamingSnakeCase); // true | ||
validate("SmokingIsBad", caseStyle.camelCase); // false | ||
validate("imNotPascal", caseStyle.pascalCase); // false | ||
``` | ||
|
||
## Format | ||
|
||
Format the input string to the wanted style case | ||
```ts | ||
import { format } from "./mod.ts"; | ||
|
||
format("FOO Bar", caseStyle.kebabCase); // output: foo-bar | ||
format("FOO Bar", caseStyle.snakeCase); // output: foo_bar | ||
format("FOO Bar", caseStyle.camelCase); // output: fooBar | ||
format("FOO Bar", caseStyle.pascalCase); // output: FooBar | ||
format("FOO Bar", caseStyle.kebabCase); // output: foo-bar | ||
format("FOO Bar", caseStyle.screamingKebabCase); // output: FOO-BAR | ||
format("FOO Bar", caseStyle.screamingSnakeCase); // output: FOO_BAR | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import "./mod_test.ts"; | ||
import "https://deno.land/std/testing/main.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Case Style enum | ||
export enum caseStyle { | ||
// eg : LastName | ||
pascalCase = "pascalCase", | ||
// eg : lastName | ||
camelCase = "camelCase", | ||
// eg : last_name | ||
snakeCase = "snakeCase", | ||
// eg : last-name | ||
kebabCase = "kebabCase", | ||
// eg : LAST_NAME | ||
screamingSnakeCase = "screamingSnakeCase", | ||
// eg : LAST-NAME | ||
screamingKebabCase = "screamingKebabCase" | ||
} | ||
|
||
const validators = { | ||
pascalCase: /[A-Z][a-z0-9]+(?:[A-Z][a-z0-9]+)*/, | ||
camelCase: /[a-z][a-z0-9]+(?:[A-Z][a-z0-9]+)*/, | ||
snakeCase: /[a-z][a-z0-9]+(?:[_][a-z][a-z0-9]+)*/, | ||
screamingSnakeCase: /[A-Z][A-Z0-9]+(?:[_][A-Z][A-Z0-9]+)*/, | ||
kebabCase: /[a-z][a-z0-9]+(?:[-][a-z][a-z0-9]+)*/, | ||
screamingKebabCase: /[A-Z][A-Z0-9]+(?:[-][A-Z][A-Z0-9]+)*/ | ||
}; | ||
|
||
/** | ||
* Validate the string into the caseStyle wanted | ||
* @param str String to validate | ||
* @param caseStyle caseStyle to validate the string into | ||
*/ | ||
export function validate(str: string, style: caseStyle): boolean { | ||
const m = validators[style].exec(str); | ||
return m && m[0] === str; | ||
} | ||
|
||
/** | ||
* Format the string into the caseStyle wanted | ||
* @param str String to format | ||
* @param caseStyle caseStyle to format the string into | ||
* TODO (zekth): handle diacritics | ||
*/ | ||
export function format(str: string, style: caseStyle): string { | ||
const acc = []; | ||
const reg = /([a-zA-Z0-9À-ž]+)/gm; | ||
str = str.trim().toLowerCase(); | ||
const m = str.match(reg); | ||
switch (style) { | ||
case caseStyle.pascalCase: | ||
case caseStyle.camelCase: | ||
for (let i = 0; i < m.length; i++) { | ||
if ( | ||
style === caseStyle.pascalCase || | ||
(style === caseStyle.camelCase && i !== 0) | ||
) { | ||
acc.push(m[i][0].toUpperCase() + m[i].slice(1)); | ||
} else { | ||
acc.push(m[i]); | ||
} | ||
} | ||
return acc.join(""); | ||
case caseStyle.snakeCase: | ||
return m.join("_"); | ||
case caseStyle.kebabCase: | ||
return m.join("-"); | ||
case caseStyle.screamingSnakeCase: | ||
return m.join("_").toUpperCase(); | ||
case caseStyle.screamingKebabCase: | ||
return m.join("-").toUpperCase(); | ||
default: | ||
throw Error("Unknown case style"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { test,runIfMain } from "https://deno.land/std/testing/mod.ts"; | ||
import { assert, assertEquals } from "https://deno.land/std/testing/asserts.ts"; | ||
import { validate, format, caseStyle } from "./mod.ts"; | ||
|
||
test({ | ||
name: "[string] Case style validator", | ||
fn(): void { | ||
assert(validate("jaime-les-fruits-au-sirop", caseStyle.kebabCase)); | ||
assert(validate("jaime_les_fruits_au_sirop", caseStyle.snakeCase)); | ||
assert(validate("jaimeLesFruitsAuSirop", caseStyle.camelCase)); | ||
assert(validate("JaimeLesFruitsAuSirop", caseStyle.pascalCase)); | ||
assert(validate("DIZ_IZ_DA_GLOBAL_VAL", caseStyle.screamingSnakeCase)); | ||
assert(validate("SALADE-TOMATE-OIGNONS", caseStyle.screamingKebabCase)); | ||
assert(!validate("OhMyGodASnake", caseStyle.snakeCase)); | ||
assert(!validate("imNotPascal", caseStyle.pascalCase)); | ||
assert(!validate("SmokingIsBad", caseStyle.camelCase)); | ||
assert(!validate("salade_TomateOignons", caseStyle.kebabCase)); | ||
assert( | ||
!validate("jaime-les-fruits-au-sirop", caseStyle.screamingSnakeCase) | ||
); | ||
assert( | ||
!validate("jaime_les_fruits_au_sirop", caseStyle.screamingKebabCase) | ||
); | ||
} | ||
}); | ||
|
||
test({ | ||
name: "[string] Case style formater", | ||
fn(): void { | ||
const input = `deja vu i've just been in this place before | ||
Higher on the street | ||
And I know it's my time to go…`; | ||
assertEquals( | ||
format(input, caseStyle.kebabCase), | ||
"deja-vu-i-ve-just-been-in-this-place-before-higher-on-the-street-and-i-know-it-s-my-time-to-go" | ||
); | ||
assertEquals( | ||
format(input, caseStyle.snakeCase), | ||
"deja_vu_i_ve_just_been_in_this_place_before_higher_on_the_street_and_i_know_it_s_my_time_to_go" | ||
); | ||
assertEquals( | ||
format(input, caseStyle.camelCase), | ||
"dejaVuIVeJustBeenInThisPlaceBeforeHigherOnTheStreetAndIKnowItSMyTimeToGo" | ||
); | ||
assertEquals( | ||
format(input, caseStyle.pascalCase), | ||
"DejaVuIVeJustBeenInThisPlaceBeforeHigherOnTheStreetAndIKnowItSMyTimeToGo" | ||
); | ||
assertEquals( | ||
format(input, caseStyle.screamingSnakeCase), | ||
"DEJA_VU_I_VE_JUST_BEEN_IN_THIS_PLACE_BEFORE_HIGHER_ON_THE_STREET_AND_I_KNOW_IT_S_MY_TIME_TO_GO" | ||
); | ||
assertEquals( | ||
format(input, caseStyle.screamingKebabCase), | ||
"DEJA-VU-I-VE-JUST-BEEN-IN-THIS-PLACE-BEFORE-HIGHER-ON-THE-STREET-AND-I-KNOW-IT-S-MY-TIME-TO-GO" | ||
); | ||
} | ||
}); | ||
|
||
runIfMain(import.meta); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"compilerOptions": { | ||
"allowJs": true, | ||
"baseUrl": ".", | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"noEmit": true, | ||
"noLib": true, | ||
"pretty": true, | ||
"resolveJsonModule": true, | ||
"strict": true, | ||
"target": "esnext" | ||
}, | ||
"include": [ | ||
"./**/*.ts" | ||
] | ||
} |