From 8b4415d41499706fd4cab3d375391a5bce401999 Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Wed, 4 Oct 2023 12:31:42 -0600 Subject: [PATCH 1/5] rebase --- src/primitives.test.ts | 7 +++++++ src/primitives.ts | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/src/primitives.test.ts b/src/primitives.test.ts index 733a231..5a80d97 100644 --- a/src/primitives.test.ts +++ b/src/primitives.test.ts @@ -28,6 +28,13 @@ namespace TypeTests { Equal, 'nice string'> > type test10 = Expect, 16>> + + type test11 = Expect< + Equal< + Subject.Concat<['a', 'bc', 'def'] | ['1', '23', '456']>, + 'abcdef' | '123456' + > + > } describe('primitives', () => { diff --git a/src/primitives.ts b/src/primitives.ts index c5a6b36..d2f8fc5 100644 --- a/src/primitives.ts +++ b/src/primitives.ts @@ -20,6 +20,12 @@ function charAt( return str.charAt(index) } +type Concat = Join + +function concat(...strings: T): Concat { + return join(strings) as Concat +} + /** * Joins a tuple of strings with the given delimiter. * T: The tuple of strings to join. @@ -256,6 +262,7 @@ function trim(str: T) { export type { CharAt, + Concat, Join, Length, Replace, @@ -268,6 +275,7 @@ export type { } export { charAt, + concat, join, length, replace, From 47defba9207ba14f9dad73ca7c55f0868c0299d7 Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Wed, 4 Oct 2023 12:42:47 -0600 Subject: [PATCH 2/5] Fixes --- README.md | 13 +++++++++++++ src/index.ts | 2 ++ src/primitives.ts | 4 ++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d523478..546cae8 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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`. @@ -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' diff --git a/src/index.ts b/src/index.ts index 54da1cc..5ee23b2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ // PRIMITIVES export type { CharAt, + Concat, Join, Length, Replace, @@ -13,6 +14,7 @@ export type { } from './primitives' export { charAt, + concat, join, length, replace, diff --git a/src/primitives.ts b/src/primitives.ts index d2f8fc5..cb8f12c 100644 --- a/src/primitives.ts +++ b/src/primitives.ts @@ -20,10 +20,10 @@ function charAt( return str.charAt(index) } -type Concat = Join +type Concat = Join function concat(...strings: T): Concat { - return join(strings) as Concat + return join(strings) } /** From d7541bf154cfcba67f6818000d1c55cb2b31509d Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Wed, 4 Oct 2023 12:47:29 -0600 Subject: [PATCH 3/5] jsdoc --- src/primitives.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/primitives.ts b/src/primitives.ts index cb8f12c..04309dd 100644 --- a/src/primitives.ts +++ b/src/primitives.ts @@ -20,12 +20,24 @@ function charAt( return str.charAt(index) } +/** + * Concatenates a tuple of strings. + * T: The tuple of strings to concatenate. + */ type Concat = Join +/** + * 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(...strings: T): Concat { return join(strings) } +'abc'.concat() + /** * Joins a tuple of strings with the given delimiter. * T: The tuple of strings to join. From a453dc0403818b9543cb0563d59f0f2d6d681f3c Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Wed, 4 Oct 2023 12:48:29 -0600 Subject: [PATCH 4/5] dash --- src/primitives.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/primitives.ts b/src/primitives.ts index 04309dd..2d491c5 100644 --- a/src/primitives.ts +++ b/src/primitives.ts @@ -7,7 +7,7 @@ import type { Math } from './math' */ type CharAt = Split[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. @@ -27,7 +27,7 @@ function charAt( type Concat = Join /** - * A strongly typed version of `String.prototype.concat`. + * 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' @@ -58,7 +58,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. @@ -76,7 +76,7 @@ function join( */ type Length = Split['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 @@ -100,7 +100,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. @@ -134,7 +134,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. @@ -183,7 +183,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. @@ -214,7 +214,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. @@ -232,7 +232,7 @@ type TrimStart = T extends ` ${infer rest}` ? TrimStart : 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 ' @@ -247,7 +247,7 @@ function trimStart(str: T) { */ type TrimEnd = T extends `${infer rest} ` ? TrimEnd : 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' @@ -263,7 +263,7 @@ function trimEnd(str: T) { type Trim = TrimEnd> /** - * 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' From 975909393e2af70175b904a484ac1936e6204df1 Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Wed, 4 Oct 2023 12:50:36 -0600 Subject: [PATCH 5/5] oof --- src/primitives.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/primitives.ts b/src/primitives.ts index 2d491c5..7e032b3 100644 --- a/src/primitives.ts +++ b/src/primitives.ts @@ -36,8 +36,6 @@ function concat(...strings: T): Concat { return join(strings) } -'abc'.concat() - /** * Joins a tuple of strings with the given delimiter. * T: The tuple of strings to join.