Skip to content

Commit

Permalink
Add Join type
Browse files Browse the repository at this point in the history
  • Loading branch information
avaly committed Aug 24, 2021
1 parent 6de66eb commit 4d8ee87
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export {ScreamingSnakeCase} from './source/screaming-snake-case';
export {DelimiterCase} from './source/delimiter-case';
export {DelimiterCasedProperties} from './source/delimiter-cased-properties';
export {DelimiterCasedPropertiesDeep} from './source/delimiter-cased-properties-deep';
export {Join} from './source/join';
export {Split} from './source/split';
export {Trim} from './source/trim';
export {Includes} from './source/includes';
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Click the type names for complete docs.
- [`DelimiterCase`](source/delimiter-case.d.ts) – Convert a string literal to a custom string delimiter casing.
- [`DelimiterCasedProperties`](source/delimiter-cased-properties.d.ts) – Convert object properties to a custom string delimiter casing.
- [`DelimiterCasedPropertiesDeep`](source/delimiter-cased-properties-deep.d.ts) – Convert object properties to a custom string delimiter casing recursively.
- [`Join`](source/join.d.ts) - Join an array of strings using the given string as delimiter.
- [`Split`](source/split.d.ts) - Represents an array of strings split using a given character or character set.
- [`Trim`](source/trim.d.ts) - Remove leading and trailing spaces from a string.
- [`Get`](source/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
22 changes: 22 additions & 0 deletions source/join.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
Join an array of strings using the given string as delimiter.
Use-case: Defining key paths in a nested object. For example, for dot-notation fields in 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: https://github.com/microsoft/TypeScript/issues/45281
Strings extends [string, ...infer Rest] ? `${Strings[0]}${Delimiter}${Join<Rest, Delimiter>}` :
string;
19 changes: 19 additions & 0 deletions test-d/join.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {expectError, expectType} from 'tsd';
import {Join} from '../index';

// 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);

0 comments on commit 4d8ee87

Please sign in to comment.