Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
eranhirsch committed Nov 10, 2024
1 parent 5751324 commit bf9f5cc
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 108 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export type {DelimiterCasedProperties} from './source/delimiter-cased-properties
export type {DelimiterCasedPropertiesDeep} from './source/delimiter-cased-properties-deep';
export type {Join} from './source/join';
export type {Split} from './source/split';
export type {SplitWords} from './source/split-words';
export type {Words as SplitWords} from './source/words';
export type {Trim} from './source/trim';
export type {Replace} from './source/replace';
export type {StringRepeat} from './source/string-repeat';
Expand Down
4 changes: 2 additions & 2 deletions source/camel-case.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {SplitWords} from './split-words';
import type {Words} from './words';

/**
CamelCase options.
Expand Down Expand Up @@ -76,5 +76,5 @@ const dbResult: CamelCasedProperties<RawOptions> = {
export type CamelCase<Type, Options extends CamelCaseOptions = {preserveConsecutiveUppercase: true}> = Type extends string
? string extends Type
? Type
: Uncapitalize<CamelCaseFromArray<SplitWords<Type extends Uppercase<Type> ? Lowercase<Type> : Type>, Options>>
: Uncapitalize<CamelCaseFromArray<Words<Type extends Uppercase<Type> ? Lowercase<Type> : Type>, Options>>
: Type;
19 changes: 9 additions & 10 deletions source/split-words.d.ts → source/words.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,35 @@ type Words3 = SplitWords<'--hello the_world'>; // ['hello', 'the', 'world']
type Words4 = SplitWords<'lifeIs42'>; // ['life', 'Is', '42']
```
@internal
@category Change case
@category Template literal
*/
export type SplitWords<
export type Words<
Sentence extends string,
LastCharacter extends string = '',
CurrentWord extends string = '',
> = Sentence extends `${infer FirstCharacter}${infer RemainingCharacters}`
? FirstCharacter extends WordSeparators
// Skip word separator
? [...SkipEmptyWord<CurrentWord>, ...SplitWords<RemainingCharacters>]
? [...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters>]
: LastCharacter extends ''
// Fist char of word
? SplitWords<RemainingCharacters, FirstCharacter, FirstCharacter>
? Words<RemainingCharacters, FirstCharacter, FirstCharacter>
// Case change: non-numeric to numeric, push word
: [false, true] extends [IsNumeric<LastCharacter>, IsNumeric<FirstCharacter>]
? [...SkipEmptyWord<CurrentWord>, ...SplitWords<RemainingCharacters, FirstCharacter, FirstCharacter>]
? [...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters, FirstCharacter, FirstCharacter>]
// Case change: numeric to non-numeric, push word
: [true, false] extends [IsNumeric<LastCharacter>, IsNumeric<FirstCharacter>]
? [...SkipEmptyWord<CurrentWord>, ...SplitWords<RemainingCharacters, FirstCharacter, FirstCharacter>]
? [...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters, FirstCharacter, FirstCharacter>]
// No case change: concat word
: [true, true] extends [IsNumeric<LastCharacter>, IsNumeric<FirstCharacter>]
? SplitWords<RemainingCharacters, FirstCharacter, `${CurrentWord}${FirstCharacter}`>
? Words<RemainingCharacters, FirstCharacter, `${CurrentWord}${FirstCharacter}`>
// Case change: lower to upper, push word
: [true, true] extends [IsLowerCase<LastCharacter>, IsUpperCase<FirstCharacter>]
? [...SkipEmptyWord<CurrentWord>, ...SplitWords<RemainingCharacters, FirstCharacter, FirstCharacter>]
? [...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters, FirstCharacter, FirstCharacter>]
// Case change: upper to lower, brings back the last character, push word
: [true, true] extends [IsUpperCase<LastCharacter>, IsLowerCase<FirstCharacter>]
? [...RemoveLastCharacter<CurrentWord, LastCharacter>, ...SplitWords<RemainingCharacters, FirstCharacter, `${LastCharacter}${FirstCharacter}`>]
? [...RemoveLastCharacter<CurrentWord, LastCharacter>, ...Words<RemainingCharacters, FirstCharacter, `${LastCharacter}${FirstCharacter}`>]
// No case change: concat word
: SplitWords<RemainingCharacters, FirstCharacter, `${CurrentWord}${FirstCharacter}`>
: Words<RemainingCharacters, FirstCharacter, `${CurrentWord}${FirstCharacter}`>
: [...SkipEmptyWord<CurrentWord>];
95 changes: 0 additions & 95 deletions test-d/split-words.ts

This file was deleted.

95 changes: 95 additions & 0 deletions test-d/words.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {expectType} from 'tsd';
import type {Words} from '../source/words';

expectType<Words<''>>([]);
expectType<Words<'a'>>(['a']);
expectType<Words<'B'>>(['B']);
expectType<Words<'aa'>>(['aa']);
expectType<Words<'aB'>>(['a', 'B']);
expectType<Words<'Ba'>>(['Ba']);
expectType<Words<'BB'>>(['BB']);
expectType<Words<'aaa'>>(['aaa']);
expectType<Words<'aaB'>>(['aa', 'B']);
expectType<Words<'aBa'>>(['a', 'Ba']);
expectType<Words<'aBB'>>(['a', 'BB']);
expectType<Words<'Baa'>>(['Baa']);
expectType<Words<'BaB'>>(['Ba', 'B']);
expectType<Words<'BBa'>>(['B', 'Ba']);
expectType<Words<'BBB'>>(['BBB']);
expectType<Words<'aaaa'>>(['aaaa']);
expectType<Words<'aaaB'>>(['aaa', 'B']);
expectType<Words<'aaBa'>>(['aa', 'Ba']);
expectType<Words<'aaBB'>>(['aa', 'BB']);
expectType<Words<'aBaa'>>(['a', 'Baa']);
expectType<Words<'aBaB'>>(['a', 'Ba', 'B']);
expectType<Words<'aBBa'>>(['a', 'B', 'Ba']);
expectType<Words<'aBBB'>>(['a', 'BBB']);
expectType<Words<'Baaa'>>(['Baaa']);
expectType<Words<'BaaB'>>(['Baa', 'B']);
expectType<Words<'BaBa'>>(['Ba', 'Ba']);
expectType<Words<'BaBB'>>(['Ba', 'BB']);
expectType<Words<'BBaa'>>(['B', 'Baa']);
expectType<Words<'BBaB'>>(['B', 'Ba', 'B']);
expectType<Words<'BBBa'>>(['BB', 'Ba']);
expectType<Words<'BBBB'>>(['BBBB']);
expectType<Words<'aaaaa'>>(['aaaaa']);
expectType<Words<'aaaaB'>>(['aaaa', 'B']);
expectType<Words<'aaaBa'>>(['aaa', 'Ba']);
expectType<Words<'aaaBB'>>(['aaa', 'BB']);
expectType<Words<'aaBaa'>>(['aa', 'Baa']);
expectType<Words<'aaBaB'>>(['aa', 'Ba', 'B']);
expectType<Words<'aaBBa'>>(['aa', 'B', 'Ba']);
expectType<Words<'aaBBB'>>(['aa', 'BBB']);
expectType<Words<'aBaaa'>>(['a', 'Baaa']);
expectType<Words<'aBaaB'>>(['a', 'Baa', 'B']);
expectType<Words<'aBaBa'>>(['a', 'Ba', 'Ba']);
expectType<Words<'aBaBB'>>(['a', 'Ba', 'BB']);
expectType<Words<'aBBaa'>>(['a', 'B', 'Baa']);
expectType<Words<'aBBaB'>>(['a', 'B', 'Ba', 'B']);
expectType<Words<'aBBBa'>>(['a', 'BB', 'Ba']);
expectType<Words<'aBBBB'>>(['a', 'BBBB']);
expectType<Words<'Baaaa'>>(['Baaaa']);
expectType<Words<'BaaaB'>>(['Baaa', 'B']);
expectType<Words<'BaaBa'>>(['Baa', 'Ba']);
expectType<Words<'BaaBB'>>(['Baa', 'BB']);
expectType<Words<'BaBaa'>>(['Ba', 'Baa']);
expectType<Words<'BaBaB'>>(['Ba', 'Ba', 'B']);
expectType<Words<'BaBBa'>>(['Ba', 'B', 'Ba']);
expectType<Words<'BaBBB'>>(['Ba', 'BBB']);
expectType<Words<'BBaaa'>>(['B', 'Baaa']);
expectType<Words<'BBaaB'>>(['B', 'Baa', 'B']);
expectType<Words<'BBaBa'>>(['B', 'Ba', 'Ba']);
expectType<Words<'BBaBB'>>(['B', 'Ba', 'BB']);
expectType<Words<'BBBaa'>>(['BB', 'Baa']);
expectType<Words<'BBBaB'>>(['BB', 'Ba', 'B']);
expectType<Words<'BBBBa'>>(['BBB', 'Ba']);
expectType<Words<'BBBBB'>>(['BBBBB']);

expectType<Words<'hello world'>>(['hello', 'world']);
expectType<Words<'Hello-world'>>(['Hello', 'world']);
expectType<Words<'hello_world'>>(['hello', 'world']);
expectType<Words<'hello world'>>(['hello', 'world']);
expectType<Words<'Hello--world'>>(['Hello', 'world']);
expectType<Words<'hello__world'>>(['hello', 'world']);
expectType<Words<' hello world'>>(['hello', 'world']);
expectType<Words<'---Hello--world'>>(['Hello', 'world']);
expectType<Words<'___hello__world'>>(['hello', 'world']);
expectType<Words<'hello world '>>(['hello', 'world']);
expectType<Words<'hello\tworld'>>(['hello', 'world']);
expectType<Words<'Hello--world--'>>(['Hello', 'world']);
expectType<Words<'hello__world___'>>(['hello', 'world']);
expectType<Words<'___ hello -__ _world'>>(['hello', 'world']);
expectType<Words<'__HelloWorld-HELLOWorld helloWORLD'>>(['Hello', 'World', 'HELLO', 'World', 'hello', 'WORLD']);
expectType<Words<'hello WORLD lowercase'>>(['hello', 'WORLD', 'lowercase']);
expectType<Words<'hello WORLD-lowercase'>>(['hello', 'WORLD', 'lowercase']);
expectType<Words<'hello WORLD Uppercase'>>(['hello', 'WORLD', 'Uppercase']);

expectType<Words<'item0'>>(['item', '0']);
expectType<Words<'item01'>>(['item', '01']);
expectType<Words<'item10'>>(['item', '10']);
expectType<Words<'item010'>>(['item', '010']);
expectType<Words<'0item0'>>(['0', 'item', '0']);
expectType<Words<'01item01'>>(['01', 'item', '01']);
expectType<Words<'10item10'>>(['10', 'item', '10']);
expectType<Words<'010item010'>>(['010', 'item', '010']);
expectType<Words<'item0_item_1 item -2'>>(['item', '0', 'item', '1', 'item', '2']);

0 comments on commit bf9f5cc

Please sign in to comment.