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 Url2Json type #262

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export {Trim} from './source/trim';
export {Includes} from './source/includes';
export {Get} from './source/get';
export {LastArrayElement} from './source/last-array-element';
export {Url2Json} from './source/url2json';

// Miscellaneous
export {PackageJson} from './source/package-json';
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ Click the type names for complete docs.
- [`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.
- [`LastArrayElement`](source/last-array-element.d.ts) - Extracts the type of the last element of an array.
- [`Url2Json`](source/url2json.d.ts) - Get query object from string.

### Miscellaneous

Expand Down
45 changes: 45 additions & 0 deletions source/url2json.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Get keys from target
*/
type GetQueryKeys<String extends string, Keys = ''> = String extends `${infer Key}=${any}`
? String extends `${string}&${infer NextString}`
? GetQueryKeys<NextString, Keys | Key>
: Keys | Key
: Keys;

/**
* If has query string, get it
*/
type GetQueryString<String extends string> = String extends `${string}?${infer QueryString}` ? GetQueryKeys<QueryString> : never;

/**
* Get value according to key
*/
type GetValue<Parameters_, URL extends string> = {
[Key in keyof Parameters_ & string]: URL extends `${any}${'?' | '&'}${Key}=${infer NextString}`
? NextString extends `${infer Value}&${any}`
? Value
: NextString
: Parameters_[Key]
};

/**
* Generate object based on key
*/
type QueryParameters<String extends string> = Record<GetQueryString<String>, unknown>;

/**
* @example
* ```
* declare const url = 'https://google.com?a=1&b=2'
* type UrlQuery = Url2Json<typeof url>
* ```
* it will be
* {
* "a": 1,
* "b": 2
* }
*
* @category Template Literals
*/
export type Url2Json<String extends string> = Omit<GetValue<QueryParameters<String>, String>, ''>;
13 changes: 13 additions & 0 deletions test-d/url2json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {expectType, expectError} from 'tsd';
import {Url2Json} from '..';

const url = 'https://google.com?a=1&b=2';
const url2 = 'https://google.com';

declare function getQuery<S extends string>(value: S): Url2Json<S>;

expectType<{a: '1'; b: '2'}>(getQuery(url));
expectError<{c: '2'}>(getQuery(url));

expectType<{}>(getQuery(url2));
expectError<{a: '1'}>(getQuery(url2));