Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add split method #7

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 76 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,76 @@
export * from './primitives'
export * from './utils'
export * from './casing'
export * from './key-casing'
// PRIMITIVES
export type {
Join,
Replace,
ReplaceAll,
Split,
TrimStart,
TrimEnd,
Trim,
} from './primitives'
export {
join,
replace,
replaceAll,
split,
trim,
trimStart,
trimEnd,
} from './primitives'

// UTILS
export type {
Digit,
Is,
IsDigit,
IsLetter,
IsLower,
IsSeparator,
IsSpecial,
IsUpper,
Separator,
Words,
} from './utils'
export { words } from './utils'

// CASING
export type {
CamelCase,
ConstantCase,
DelimiterCase,
KebabCase,
PascalCase,
SnakeCase,
TitleCase,
} from './casing'
export {
capitalize,
toCamelCase,
toConstantCase,
toDelimiterCase,
toKebabCase,
toLowerCase,
toPascalCase,
toSnakeCase,
toTitleCase,
toUpperCase,
} from './casing'

// KEY CASING
export type {
DeepCamelKeys,
DeepConstantKeys,
DeepDelimiterKeys,
DeepKebabKeys,
DeepPascalKeys,
DeepSnakeKeys,
} from './key-casing'
export {
deepCamelKeys,
deepConstantKeys,
deepDelimiterKeys,
deepKebabKeys,
deepPascalKeys,
deepSnakeKeys,
deepTransformKeys,
} from './key-casing'
12 changes: 12 additions & 0 deletions src/primitives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace TypeTests {
type test6 = Expect<
Equal<Subject.Trim<' some nice string '>, 'some nice string'>
>
type test7 = Expect<
Equal<Subject.Split<'some nice string', ' '>, ['some', 'nice', 'string']>
>
}

describe('primitives', () => {
Expand Down Expand Up @@ -57,6 +60,15 @@ describe('primitives', () => {
})
})

test('split', () => {
test('should split a string by a delimiter into an array of substrings', () => {
const data = 'some nice string'
const result = subject.split(data, ' ')
expect(result).toEqual(['some', 'nice', 'string'])
type test = Expect<Equal<typeof result, ['some', 'nice', 'string']>>
})
})

test('trimStart', () => {
test('should trim the start of a string at both type level and runtime level', () => {
const data = ' some nice string '
Expand Down
26 changes: 24 additions & 2 deletions src/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ function replaceAll<T extends string, S extends string, R extends string = ''>(
) as ReplaceAll<T, S, R>
}

/**
* Splits a string into an array of substrings.
* T: The string to split.
* delimiter: The delimiter.
*/
type Split<
T,
delimiter extends string,
> = T extends `${infer first}${delimiter}${infer rest}`
? [first, ...Split<rest, delimiter>]
: [T]
/**
* A strongly typed version of `String.prototype.split`.
* @param str the string to split.
* @param delimiter the delimiter.
* @returns the splitted string in both type level and runtime.
* @example split('hello world', ' ') // ['hello', 'world']
*/
function split<T extends string, D extends string = ''>(str: T, delimiter?: D) {
return str.split(delimiter ?? ('' as const)) as Split<T, D>
}

/**
* Trims all whitespaces at the start of a string.
* T: The string to trim.
Expand Down Expand Up @@ -146,5 +168,5 @@ function trim<T extends string>(str: T) {
return str.trim() as Trim<T>
}

export type { Join, Replace, ReplaceAll, TrimStart, TrimEnd, Trim }
export { join, replace, replaceAll, trim, trimStart, trimEnd }
export type { Join, Replace, ReplaceAll, Split, TrimStart, TrimEnd, Trim }
export { join, replace, replaceAll, split, trim, trimStart, trimEnd }
Loading