Skip to content

Commit

Permalink
Add Join type
Browse files Browse the repository at this point in the history
  • Loading branch information
avaly committed Jul 29, 2021
1 parent 624c331 commit bf977bf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions readme.md
Expand Up @@ -140,6 +140,7 @@ Click the type names for complete docs.
- [`DelimiterCase`](ts41/delimiter-case.d.ts) – Convert a string literal to a custom string delimiter casing.
- [`DelimiterCasedProperties`](ts41/delimiter-cased-properties.d.ts) – Convert object properties to a custom string delimiter casing.
- [`DelimiterCasedPropertiesDeep`](ts41/delimiter-cased-properties-deep.d.ts) – Convert object properties to a custom string delimiter casing recursively.
- [`Join`](ts41/join.d.ts) - Joins an array of strings using a given string as delimiter.
- [`Split`](ts41/split.d.ts) - Represents an array of strings split using a given character or character set.
- [`Trim`](ts41/trim.d.ts) - Remove leading and trailing spaces from a string.
- [`Get`](ts41/get.d.ts) - Get a deeply-nested property from an object using a key path, like [Lodash's `.get()`](https://lodash.com/docs/latest#get) function.
Expand Down
19 changes: 19 additions & 0 deletions test-d/join.ts
@@ -0,0 +1,19 @@
import {expectError, expectType} from 'tsd';
import {Join} from '../ts41';

// General use.
const generalTest: Join<['foo', 'bar', 'baz'], '.'> = 'foo.bar.baz';
expectType<'foo.bar.baz'>(generalTest);
expectError<'foo'>(generalTest);
expectError<'foo.bar'>(generalTest);
expectError<'foo.bar.ham'>(generalTest);

// Empty string delimiter.
const emptyDelimiter: Join<['foo', 'bar', 'baz'], ''> = 'foobarbaz';
expectType<'foobarbaz'>(emptyDelimiter);
expectError<'foo.bar.baz'>(emptyDelimiter);

// Empty input.
const emptyInput: Join<[], '.'> = '';
expectType<''>(emptyInput);
expectError<'foo'>(emptyInput);
1 change: 1 addition & 0 deletions ts41/index.d.ts
Expand Up @@ -18,6 +18,7 @@ export {ScreamingSnakeCase} from './screaming-snake-case';
export {DelimiterCase} from './delimiter-case';
export {DelimiterCasedProperties} from './delimiter-cased-properties';
export {DelimiterCasedPropertiesDeep} from './delimiter-cased-properties-deep';
export {Join} from './join';
export {Split} from './split';
export {Trim} from './trim';
export {Get} from './get';
Expand Down
22 changes: 22 additions & 0 deletions ts41/join.d.ts
@@ -0,0 +1,22 @@
/**
Joins an array of strings using a given string as delimiter.
Use-case: Defining the type field paths in a nested object for things like MongoDB queries.
@example
```
import {Join} from 'type-fest';
const path: Join<['foo', 'bar', 'baz'], '.'> = ['foo', 'bar', 'baz'].join('.');
```
@category Template Literals
*/
export type Join<
Strings extends string[],
Delimiter extends string
> = Strings extends [] ? '' :
Strings extends [string] ? `${Strings[0]}` :
// @ts-expect-error `Rest` is inferred as `unknown` here
Strings extends [string, ...infer Rest] ? `${Strings[0]}${Delimiter}${Join<Rest, Delimiter>}` :
string;

0 comments on commit bf977bf

Please sign in to comment.