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

feat: add concat #25

Merged
merged 5 commits into from
Oct 4, 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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ npm install string-ts
- [uncapitalize](#uncapitalize)
- [Strongly-typed alternatives to native runtime utilities](#strongly-typed-alternatives-to-native-runtime-utilities)
- [chartAt](#charat)
- [concat](#concat)
- [join](#join)
- [length](#length)
- [replace](#replace)
Expand Down Expand Up @@ -175,6 +176,17 @@ const result = charAt(str, 6)
// ^ 'w'
```

### concat

This function is a strongly-typed counterpart of `String.prototype.concat`.

```ts
import { concat } from 'string-ts'

const result = concat('a', 'bc', 'def')
// ^ 'abcdef'
```

### join

This function is a strongly-typed counterpart of `Array.prototype.join`.
Expand Down Expand Up @@ -627,6 +639,7 @@ Uppercase<'hello world'> // 'HELLO WORLD'

```ts
St.CharAt<'hello world', 6> // 'w'
St.Concat<['a', 'bc', 'def']> // 'abcdef'
St.Join<['hello', 'world'], '-'> // 'hello-world'
St.Length<'hello'> // 5
St.Replace<'hello-world', 'l', '1'> // 'he1lo-world'
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// PRIMITIVES
export type {
CharAt,
Concat,
Join,
Length,
Replace,
Expand All @@ -13,6 +14,7 @@ export type {
} from './primitives'
export {
charAt,
concat,
join,
length,
replace,
Expand Down
7 changes: 7 additions & 0 deletions src/primitives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ namespace TypeTests {
Equal<Subject.Slice<'some nice string', 5>, 'nice string'>
>
type test10 = Expect<Equal<Subject.Length<'some nice string'>, 16>>

type test11 = Expect<
Equal<
Subject.Concat<['a', 'bc', 'def'] | ['1', '23', '456']>,
'abcdef' | '123456'
>
>
}

describe('primitives', () => {
Expand Down
38 changes: 28 additions & 10 deletions src/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Math } from './math'
*/
type CharAt<T extends string, index extends number> = Split<T>[index]
/**
* A strongly typed version of `String.prototype.charAt`.
* A strongly-typed version of `String.prototype.charAt`.
* @param str the string to get the character from.
* @param index the index of the character.
* @returns the character in both type level and runtime.
Expand All @@ -20,6 +20,22 @@ function charAt<T extends string, I extends number>(
return str.charAt(index)
}

/**
* Concatenates a tuple of strings.
* T: The tuple of strings to concatenate.
*/
type Concat<T extends string[]> = Join<T>

/**
* A strongly-typed version of `String.prototype.concat`.
* @param strings the tuple of strings to concatenate.
* @returns the concatenated string in both type level and runtime.
* @example concat('a', 'bc', 'def') // 'abcdef'
*/
function concat<T extends string[]>(...strings: T): Concat<T> {
return join(strings)
}

/**
* Joins a tuple of strings with the given delimiter.
* T: The tuple of strings to join.
Expand All @@ -40,7 +56,7 @@ type Join<
: ''

/**
* A strongly typed version of `Array.prototype.join`.
* A strongly-typed version of `Array.prototype.join`.
* @param tuple the tuple of strings to join.
* @param delimiter the delimiter.
* @returns the joined string in both type level and runtime.
Expand All @@ -58,7 +74,7 @@ function join<const T extends readonly string[], D extends string = ''>(
*/
type Length<T extends string> = Split<T>['length']
/**
* A strongly typed version of `String.prototype.length`.
* A strongly-typed version of `String.prototype.length`.
* @param str the string to get the length from.
* @returns the length of the string in both type level and runtime.
* @example length('hello world') // 11
Expand All @@ -82,7 +98,7 @@ type Replace<
: sentence

/**
* A strongly typed version of `String.prototype.replace`.
* A strongly-typed version of `String.prototype.replace`.
* @param sentence the sentence to replace.
* @param lookup the lookup string to be replaced.
* @param replacement the replacement string.
Expand Down Expand Up @@ -116,7 +132,7 @@ type ReplaceAll<
: sentence

/**
* A strongly typed version of `String.prototype.replaceAll`.
* A strongly-typed version of `String.prototype.replaceAll`.
* @param sentence the sentence to replace.
* @param lookup the lookup string to be replaced.
* @param replacement the replacement string.
Expand Down Expand Up @@ -165,7 +181,7 @@ type Slice<
>}`
: ''
/**
* A strongly typed version of `String.prototype.slice`.
* A strongly-typed version of `String.prototype.slice`.
* @param str the string to slice.
* @param start the start index.
* @param end the end index.
Expand Down Expand Up @@ -196,7 +212,7 @@ type Split<
? []
: [T]
/**
* A strongly typed version of `String.prototype.split`.
* 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.
Expand All @@ -214,7 +230,7 @@ type TrimStart<T extends string> = T extends ` ${infer rest}`
? TrimStart<rest>
: T
/**
* A strongly typed version of `String.prototype.trimStart`.
* A strongly-typed version of `String.prototype.trimStart`.
* @param str the string to trim.
* @returns the trimmed string in both type level and runtime.
* @example trimStart(' hello world ') // 'hello world '
Expand All @@ -229,7 +245,7 @@ function trimStart<T extends string>(str: T) {
*/
type TrimEnd<T extends string> = T extends `${infer rest} ` ? TrimEnd<rest> : T
/**
* A strongly typed version of `String.prototype.trimEnd`.
* A strongly-typed version of `String.prototype.trimEnd`.
* @param str the string to trim.
* @returns the trimmed string in both type level and runtime.
* @example trimEnd(' hello world ') // ' hello world'
Expand All @@ -245,7 +261,7 @@ function trimEnd<T extends string>(str: T) {
type Trim<T extends string> = TrimEnd<TrimStart<T>>

/**
* A strongly typed version of `String.prototype.trim`.
* A strongly-typed version of `String.prototype.trim`.
* @param str the string to trim.
* @returns the trimmed string in both type level and runtime.
* @example trim(' hello world ') // 'hello world'
Expand All @@ -256,6 +272,7 @@ function trim<T extends string>(str: T) {

export type {
CharAt,
Concat,
Join,
Length,
Replace,
Expand All @@ -268,6 +285,7 @@ export type {
}
export {
charAt,
concat,
join,
length,
replace,
Expand Down
Loading