From ba39ce69d27a9890be1705db8de927592d7088e4 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 15 Nov 2023 17:49:29 +0100 Subject: [PATCH] style: lint and format with prettier v3 --- README.md | 17 ++++--- src/index.ts | 38 +++++++------- src/types.ts | 137 ++++++++++++++++++++++++++------------------------- 3 files changed, 98 insertions(+), 94 deletions(-) diff --git a/README.md b/README.md index a34bb5d..6c7991a 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,10 @@ Import: ```js // CommonJS -const { pascalCase } = require('scule') +const { pascalCase } = require("scule"); // ESM -import { pascalCase } from 'scule' +import { pascalCase } from "scule"; ``` **Notice:** You may need to transpile package for legacy environments. @@ -34,7 +34,7 @@ import { pascalCase } from 'scule' Splits string and joins by PascalCase convention: ```ts -pascalCase('foo-bar_baz') +pascalCase("foo-bar_baz"); // FooBarBaz ``` @@ -45,7 +45,7 @@ pascalCase('foo-bar_baz') Splits string and joins by camelCase convention: ```ts -camelCase('foo-bar_baz') +camelCase("foo-bar_baz"); // fooBarBaz ``` @@ -54,7 +54,7 @@ camelCase('foo-bar_baz') Splits string and joins by kebab-case convention: ```ts -kebabCase('fooBar_Baz') +kebabCase("fooBar_Baz"); // foo-bar-baz ``` @@ -65,7 +65,7 @@ kebabCase('fooBar_Baz') Splits string and joins by snake_case convention: ```ts -snakeCase('foo-barBaz') +snakeCase("foo-barBaz"); // foo_bar_baz ``` @@ -74,7 +74,7 @@ snakeCase('foo-barBaz') Converts first character to upper case: ```ts -upperFirst('hello world!') +upperFirst("hello world!"); // Hello world! ``` @@ -83,7 +83,7 @@ upperFirst('hello world!') Converts first character to lower case: ```ts -lowerFirst('Hello world!') +lowerFirst("Hello world!"); // hello world! ``` @@ -108,6 +108,7 @@ lowerFirst('Hello world!') [MIT](./LICENSE) + [npm-version-src]: https://img.shields.io/npm/v/scule?style=flat&colorA=18181B&colorB=F0DB4F [npm-version-href]: https://npmjs.com/package/scule [npm-downloads-src]: https://img.shields.io/npm/dm/scule?style=flat&colorA=18181B&colorB=F0DB4F diff --git a/src/index.ts b/src/index.ts index e314b85..fdeaf73 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,11 +13,11 @@ export function isUppercase(char = ""): boolean | undefined { export function splitByCase(string_: T): SplitByCase; export function splitByCase< T extends string, - Separator extends readonly string[] + Separator extends readonly string[], >(string_: T, separators: Separator): SplitByCase; export function splitByCase< T extends string, - Separator extends readonly string[] + Separator extends readonly string[], >(string_: T, separators?: Separator) { const splitters = separators ?? STR_SPLITTERS; const parts: string[] = []; @@ -52,7 +52,7 @@ export function splitByCase< } // Case falling edge if (previousUpper === true && isUpper === false && buff.length > 1) { - const lastChar = buff[buff.length - 1]; + const lastChar = buff.at(-1); parts.push(buff.slice(0, Math.max(0, buff.length - 1))); buff = lastChar + char; previousUpper = isUpper; @@ -73,31 +73,31 @@ export function splitByCase< export function upperFirst(string_: S): Capitalize { return ( - !string_ ? "" : string_[0].toUpperCase() + string_.slice(1) + string_ ? string_[0].toUpperCase() + string_.slice(1) : "" ) as Capitalize; } export function lowerFirst(string_: S): Uncapitalize { return ( - !string_ ? "" : string_[0].toLowerCase() + string_.slice(1) + string_ ? string_[0].toLowerCase() + string_.slice(1) : "" ) as Uncapitalize; } export function pascalCase(): ""; export function pascalCase( - string_: T + string_: T, ): PascalCase; export function pascalCase(string_?: T) { - return !string_ - ? "" - : ((Array.isArray(string_) ? string_ : splitByCase(string_ as string)) + return string_ + ? ((Array.isArray(string_) ? string_ : splitByCase(string_ as string)) .map((p) => upperFirst(p)) - .join("") as PascalCase); + .join("") as PascalCase) + : ""; } export function camelCase(): ""; export function camelCase( - string_: T + string_: T, ): CamelCase; export function camelCase(string_?: T) { return lowerFirst(pascalCase(string_ || "")) as CamelCase; @@ -105,26 +105,26 @@ export function camelCase(string_?: T) { export function kebabCase(): ""; export function kebabCase( - string_: T + string_: T, ): JoinByCase; export function kebabCase< T extends string | readonly string[], - Joiner extends string + Joiner extends string, >(string_: T, joiner: Joiner): JoinByCase; export function kebabCase< T extends string | readonly string[], - Joiner extends string + Joiner extends string, >(string_?: T, joiner?: Joiner) { - return !string_ - ? "" - : ((Array.isArray(string_) ? string_ : splitByCase(string_ as string)) + return string_ + ? ((Array.isArray(string_) ? string_ : splitByCase(string_ as string)) .map((p) => p.toLowerCase()) - .join(joiner ?? "-") as JoinByCase); + .join(joiner ?? "-") as JoinByCase) + : ""; } export function snakeCase(): ""; export function snakeCase( - string_: T + string_: T, ): JoinByCase; export function snakeCase(string_?: T) { return kebabCase(string_ || "", "_") as JoinByCase; diff --git a/src/types.ts b/src/types.ts index 7511643..eeb39ac 100644 --- a/src/types.ts +++ b/src/types.ts @@ -16,22 +16,22 @@ type IsUpper = S extends Uppercase ? true : false; type IsLower = S extends Lowercase ? true : false; type SameLetterCase< X extends string, - Y extends string + Y extends string, > = IsUpper extends IsUpper ? true : IsLower extends IsLower - ? true - : false; + ? true + : false; type CapitalizedWords< T extends readonly string[], - Accumulator extends string = "" + Accumulator extends string = "", > = T extends readonly [infer F extends string, ...infer R extends string[]] ? CapitalizedWords}`> : Accumulator; type JoinLowercaseWords< T extends readonly string[], Joiner extends string, - Accumulator extends string = "" + Accumulator extends string = "", > = T extends readonly [infer F extends string, ...infer R extends string[]] ? Accumulator extends "" ? JoinLowercaseWords}`> @@ -46,85 +46,88 @@ type RemoveLastOfArray = T extends [...infer F, any] export type SplitByCase< T, Separator extends string = Splitter, - Accumulator extends unknown[] = [] + Accumulator extends unknown[] = [], > = string extends Separator ? string[] : T extends `${infer F}${infer R}` - ? [LastOfArray] extends [never] - ? SplitByCase - : LastOfArray extends string - ? R extends "" - ? SplitByCase< - R, - Separator, - [...RemoveLastOfArray, `${LastOfArray}${F}`] - > - : SameLetterCase> extends true - ? F extends Separator - ? FirstOfString extends Separator - ? SplitByCase - : IsUpper> extends true + ? [LastOfArray] extends [never] + ? SplitByCase + : LastOfArray extends string + ? R extends "" ? SplitByCase< - RemoveFirstOfString, + R, Separator, - [...Accumulator, FirstOfString] + [ + ...RemoveLastOfArray, + `${LastOfArray}${F}`, + ] > - : SplitByCase - : SplitByCase< - R, - Separator, - [ - ...RemoveLastOfArray, - `${LastOfArray}${F}` - ] - > - : IsLower extends true - ? SplitByCase< - RemoveFirstOfString, - Separator, - [ - ...RemoveLastOfArray, - `${LastOfArray}${F}`, - FirstOfString - ] - > - : SplitByCase - : never - : Accumulator extends [] - ? T extends "" - ? [] - : string[] - : Accumulator; + : SameLetterCase> extends true + ? F extends Separator + ? FirstOfString extends Separator + ? SplitByCase + : IsUpper> extends true + ? SplitByCase< + RemoveFirstOfString, + Separator, + [...Accumulator, FirstOfString] + > + : SplitByCase + : SplitByCase< + R, + Separator, + [ + ...RemoveLastOfArray, + `${LastOfArray}${F}`, + ] + > + : IsLower extends true + ? SplitByCase< + RemoveFirstOfString, + Separator, + [ + ...RemoveLastOfArray, + `${LastOfArray}${F}`, + FirstOfString, + ] + > + : SplitByCase + : never + : Accumulator extends [] + ? T extends "" + ? [] + : string[] + : Accumulator; export type PascalCase = string extends T ? string : string[] extends T - ? string - : T extends string - ? SplitByCase extends readonly string[] - ? CapitalizedWords> - : never - : T extends readonly string[] - ? CapitalizedWords - : never; + ? string + : T extends string + ? SplitByCase extends readonly string[] + ? CapitalizedWords> + : never + : T extends readonly string[] + ? CapitalizedWords + : never; export type CamelCase = string extends T ? string : string[] extends T - ? string - : Uncapitalize>; + ? string + : Uncapitalize>; export type JoinByCase = string extends T ? string : string[] extends T - ? string - : T extends string - ? SplitByCase extends readonly string[] - ? JoinLowercaseWords, Joiner> - : never - : T extends readonly string[] - ? JoinLowercaseWords - : never; + ? string + : T extends string + ? SplitByCase extends readonly string[] + ? JoinLowercaseWords, Joiner> + : never + : T extends readonly string[] + ? JoinLowercaseWords + : never; // eslint-disable-next-line @typescript-eslint/no-unused-vars type __tests = [ @@ -184,6 +187,6 @@ type __tests = [ Assert, "foo-ba-rb">>, Assert, "foo-bar-baz-qux">>, // array - Assert, "foo-bar">> + Assert, "foo-bar">>, ]; /* eslint-enable @typescript-eslint/no-unused-vars */