type-fest 0.21.2 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/license +1 -1
- package/package.json +1 -1
- package/readme.md +17 -0
- package/source/.DS_Store +0 -0
- package/source/basic.d.ts +1 -1
- package/source/entry.d.ts +1 -1
- package/source/except.d.ts +1 -1
- package/ts41/camel-case.d.ts +2 -2
- package/ts41/camel-cased-properties-deep.d.ts +48 -0
- package/ts41/camel-cased-properties.d.ts +30 -0
- package/ts41/delimiter-case.d.ts +2 -2
- package/ts41/delimiter-cased-properties-deep.d.ts +54 -0
- package/ts41/delimiter-cased-properties.d.ts +31 -0
- package/ts41/index.d.ts +13 -0
- package/ts41/kebab-case.d.ts +2 -2
- package/ts41/kebab-cased-properties-deep.d.ts +41 -0
- package/ts41/kebab-cased-properties.d.ts +24 -0
- package/ts41/last-array-element.d.ts +23 -0
- package/ts41/pascal-case.d.ts +1 -1
- package/ts41/pascal-cased-properties-deep.d.ts +48 -0
- package/ts41/pascal-cased-properties.d.ts +28 -0
- package/ts41/snake-case.d.ts +2 -2
- package/ts41/snake-cased-properties-deep.d.ts +41 -0
- package/ts41/snake-cased-properties.d.ts +24 -0
- package/ts41/split.d.ts +22 -0
- package/ts41/trim.d.ts +22 -0
package/license
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https
|
|
3
|
+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
6
|
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -110,11 +110,24 @@ Click the type names for complete docs.
|
|
|
110
110
|
*Note:* These require [TypeScript 4.1 or newer](https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/#template-literal-types).
|
|
111
111
|
|
|
112
112
|
- [`CamelCase`](ts41/camel-case.d.ts) – Convert a string literal to camel-case (`fooBar`).
|
|
113
|
+
- [`CamelCasedProperties`](ts41/camel-cased-properties.d.ts) – Convert object properties to camel-case (`fooBar`).
|
|
114
|
+
- [`CamelCasedPropertiesDeep`](ts41/camel-cased-properties-deep.d.ts) – Convert object properties to camel-case recursively (`fooBar`).
|
|
113
115
|
- [`KebabCase`](ts41/kebab-case.d.ts) – Convert a string literal to kebab-case (`foo-bar`).
|
|
116
|
+
- [`KebabCasedProperties`](ts41/kebab-cased-properties.d.ts) – Convert a object properties to kebab-case recursively (`foo-bar`).
|
|
117
|
+
- [`KebabCasedPropertiesDeep`](ts41/kebab-cased-properties-deep.d.ts) – Convert object properties to kebab-case (`foo-bar`).
|
|
114
118
|
- [`PascalCase`](ts41/pascal-case.d.ts) – Converts a string literal to pascal-case (`FooBar`)
|
|
119
|
+
- [`PascalCasedProperties`](ts41/pascal-cased-properties.d.ts) – Converts object properties to pascal-case (`FooBar`)
|
|
120
|
+
- [`PascalCasedPropertiesDeep`](ts41/pascal-cased-properties-deep.d.ts) – Converts object properties to pascal-case (`FooBar`)
|
|
115
121
|
- [`SnakeCase`](ts41/snake-case.d.ts) – Convert a string literal to snake-case (`foo_bar`).
|
|
122
|
+
- [`SnakeCasedProperties`](ts41/snake-cased-properties-deep.d.ts) – Convert object properties to snake-case (`foo_bar`).
|
|
123
|
+
- [`SnakeCasedPropertiesDeep`](ts41/snake-cased-properties-deep.d.ts) – Convert object properties to snake-case recursively (`foo_bar`).
|
|
116
124
|
- [`DelimiterCase`](ts41/delimiter-case.d.ts) – Convert a string literal to a custom string delimiter casing.
|
|
125
|
+
- [`DelimiterCasedProperties`](ts41/delimiter-cased-properties.d.ts) – Convert object properties to a custom string delimiter casing.
|
|
126
|
+
- [`DelimiterCasedPropertiesDeep`](ts41/delimiter-cased-properties-deep.d.ts) – Convert object properties to a custom string delimiter casing recursively.
|
|
127
|
+
- [`Split`](ts41/split.d.ts) - Represents an array of strings split using a given character or character set.
|
|
128
|
+
- [`Trim`](ts41/trim.d.ts) - Remove leading and trailing spaces from a string.
|
|
117
129
|
- [`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.
|
|
130
|
+
- [`LastArrayElement`](ts41/last-array-element.d.ts) - Extracts the type of the last element of an array.
|
|
118
131
|
|
|
119
132
|
### Miscellaneous
|
|
120
133
|
|
|
@@ -132,6 +145,10 @@ Click the type names for complete docs.
|
|
|
132
145
|
|
|
133
146
|
## Tips
|
|
134
147
|
|
|
148
|
+
### Related
|
|
149
|
+
|
|
150
|
+
- [typed-query-selector](https://github.com/g-plane/typed-query-selector) - Enhances `document.querySelector` and `document.querySelectorAll` with a template literal type that matches element types returned from an HTML element query selector.
|
|
151
|
+
|
|
135
152
|
### Built-in types
|
|
136
153
|
|
|
137
154
|
There are many advanced types most users don't know about.
|
package/source/.DS_Store
ADDED
|
Binary file
|
package/source/basic.d.ts
CHANGED
package/source/entry.d.ts
CHANGED
|
@@ -44,7 +44,7 @@ const arrayEntryNumber: Entry<typeof arrayExample> = [1, 1];
|
|
|
44
44
|
|
|
45
45
|
// Maps
|
|
46
46
|
const mapExample = new Map([['a', 1]]);
|
|
47
|
-
const mapEntry: Entry<typeof
|
|
47
|
+
const mapEntry: Entry<typeof mapExample> = ['a', 1];
|
|
48
48
|
|
|
49
49
|
// Sets
|
|
50
50
|
const setExample = new Set(['a', 1]);
|
package/source/except.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ Create a type from an object type without certain keys.
|
|
|
3
3
|
|
|
4
4
|
This type is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#the-omit-helper-type). The `Omit` type does not restrict the omitted keys to be keys present on the given type, while `Except` does. The benefits of a stricter type are avoiding typos and allowing the compiler to pick up on rename refactors automatically.
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
This type was proposed to the TypeScript team, which declined it, saying they prefer that libraries implement stricter versions of the built-in types ([microsoft/TypeScript#30825](https://github.com/microsoft/TypeScript/issues/30825#issuecomment-523668235)).
|
|
7
7
|
|
|
8
8
|
@example
|
|
9
9
|
```
|
package/ts41/camel-case.d.ts
CHANGED
|
@@ -44,7 +44,7 @@ const someVariable: CamelCase<'foo-bar'> = 'fooBar';
|
|
|
44
44
|
|
|
45
45
|
// Advanced
|
|
46
46
|
|
|
47
|
-
type
|
|
47
|
+
type CamelCasedProperties<T> = {
|
|
48
48
|
[K in keyof T as CamelCase<K>]: T[K]
|
|
49
49
|
};
|
|
50
50
|
|
|
@@ -54,7 +54,7 @@ interface RawOptions {
|
|
|
54
54
|
foo: number;
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
const dbResult:
|
|
57
|
+
const dbResult: CamelCasedProperties<ModelProps> = {
|
|
58
58
|
dryRun: true,
|
|
59
59
|
fullFamilyName: 'bar.js',
|
|
60
60
|
foo: 123
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import {CamelCase} from './camel-case';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Convert object properties to camel case recursively.
|
|
5
|
+
|
|
6
|
+
This can be useful when, for example, converting some API types from a different style.
|
|
7
|
+
|
|
8
|
+
@see CamelCasedProperties
|
|
9
|
+
@see CamelCase
|
|
10
|
+
|
|
11
|
+
@example
|
|
12
|
+
```
|
|
13
|
+
interface User {
|
|
14
|
+
UserId: number;
|
|
15
|
+
UserName: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface UserWithFriends {
|
|
19
|
+
UserInfo: User;
|
|
20
|
+
UserFriends: User[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const result: CamelCasedPropertiesDeep<UserWithFriends> = {
|
|
24
|
+
userInfo: {
|
|
25
|
+
userId: 1,
|
|
26
|
+
userName: 'Tom',
|
|
27
|
+
},
|
|
28
|
+
userFriends: [
|
|
29
|
+
{
|
|
30
|
+
userId: 2,
|
|
31
|
+
userName: 'Jerry',
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
userId: 3,
|
|
35
|
+
userName: 'Spike',
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
};
|
|
39
|
+
```
|
|
40
|
+
*/
|
|
41
|
+
export type CamelCasedPropertiesDeep<Value> = Value extends Function
|
|
42
|
+
? Value
|
|
43
|
+
: Value extends Array<infer U>
|
|
44
|
+
? Array<CamelCasedPropertiesDeep<U>>
|
|
45
|
+
: Value extends Set<infer U>
|
|
46
|
+
? Set<CamelCasedPropertiesDeep<U>> : {
|
|
47
|
+
[K in keyof Value as CamelCase<K>]: CamelCasedPropertiesDeep<Value[K]>;
|
|
48
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {CamelCase} from './camel-case';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Convert object properties to camel case but not recursively.
|
|
5
|
+
|
|
6
|
+
This can be useful when, for example, converting some API types from a different style.
|
|
7
|
+
|
|
8
|
+
@see CamelCasedPropertiesDeep
|
|
9
|
+
@see CamelCase
|
|
10
|
+
|
|
11
|
+
@example
|
|
12
|
+
```
|
|
13
|
+
interface User {
|
|
14
|
+
UserId: number;
|
|
15
|
+
UserName: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const result: CamelCasedProperties<User> = {
|
|
19
|
+
userId: 1,
|
|
20
|
+
userName: 'Tom',
|
|
21
|
+
};
|
|
22
|
+
```
|
|
23
|
+
*/
|
|
24
|
+
export type CamelCasedProperties<Value> = Value extends Function
|
|
25
|
+
? Value
|
|
26
|
+
: Value extends Array<infer U>
|
|
27
|
+
? Value
|
|
28
|
+
: {
|
|
29
|
+
[K in keyof Value as CamelCase<K>]: Value[K];
|
|
30
|
+
};
|
package/ts41/delimiter-case.d.ts
CHANGED
|
@@ -57,7 +57,7 @@ const someVariable: DelimiterCase<'fooBar', '#'> = 'foo#bar';
|
|
|
57
57
|
|
|
58
58
|
// Advanced
|
|
59
59
|
|
|
60
|
-
type
|
|
60
|
+
type OddlyCasedProperties<T> = {
|
|
61
61
|
[K in keyof T as DelimiterCase<K, '#'>]: T[K]
|
|
62
62
|
};
|
|
63
63
|
|
|
@@ -67,7 +67,7 @@ interface SomeOptions {
|
|
|
67
67
|
foo: number;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
const rawCliOptions:
|
|
70
|
+
const rawCliOptions: OddlyCasedProperties<SomeOptions> = {
|
|
71
71
|
'dry#run': true,
|
|
72
72
|
'include#file': 'bar.js',
|
|
73
73
|
foo: 123
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {DelimiterCase} from './delimiter-case';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Convert object properties to delimiter case recursively.
|
|
5
|
+
|
|
6
|
+
This can be useful when, for example, converting some API types from a different style.
|
|
7
|
+
|
|
8
|
+
@see DelimiterCase
|
|
9
|
+
@see DelimiterCasedProperties
|
|
10
|
+
|
|
11
|
+
@example
|
|
12
|
+
```
|
|
13
|
+
interface User {
|
|
14
|
+
userId: number;
|
|
15
|
+
userName: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface UserWithFriends {
|
|
19
|
+
userInfo: User;
|
|
20
|
+
userFriends: User[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const result: DelimiterCasedPropertiesDeep<UserWithFriends, '-'> = {
|
|
24
|
+
'user-info': {
|
|
25
|
+
'user-id': 1,
|
|
26
|
+
'user-name': 'Tom',
|
|
27
|
+
},
|
|
28
|
+
'user-friends': [
|
|
29
|
+
{
|
|
30
|
+
'user-id': 2,
|
|
31
|
+
'user-name': 'Jerry',
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
'user-id': 3,
|
|
35
|
+
'user-name': 'Spike',
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
};
|
|
39
|
+
```
|
|
40
|
+
*/
|
|
41
|
+
export type DelimiterCasedPropertiesDeep<
|
|
42
|
+
Value,
|
|
43
|
+
Delimiter extends string
|
|
44
|
+
> = Value extends Function
|
|
45
|
+
? Value
|
|
46
|
+
: Value extends Array<infer U>
|
|
47
|
+
? Array<DelimiterCasedPropertiesDeep<U, Delimiter>>
|
|
48
|
+
: Value extends Set<infer U>
|
|
49
|
+
? Set<DelimiterCasedPropertiesDeep<U, Delimiter>> : {
|
|
50
|
+
[K in keyof Value as DelimiterCase<
|
|
51
|
+
K,
|
|
52
|
+
Delimiter
|
|
53
|
+
>]: DelimiterCasedPropertiesDeep<Value[K], Delimiter>;
|
|
54
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {DelimiterCase} from './delimiter-case';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Convert object properties to delimiter case but not recursively.
|
|
5
|
+
|
|
6
|
+
This can be useful when, for example, converting some API types from a different style.
|
|
7
|
+
|
|
8
|
+
@see DelimiterCase
|
|
9
|
+
@see DelimiterCasedPropertiesDeep
|
|
10
|
+
|
|
11
|
+
@example
|
|
12
|
+
```
|
|
13
|
+
interface User {
|
|
14
|
+
userId: number;
|
|
15
|
+
userName: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const result: DelimiterCasedProperties<User, '-'> = {
|
|
19
|
+
'user-id': 1,
|
|
20
|
+
'user-name': 'Tom',
|
|
21
|
+
};
|
|
22
|
+
```
|
|
23
|
+
*/
|
|
24
|
+
export type DelimiterCasedProperties<
|
|
25
|
+
Value,
|
|
26
|
+
Delimiter extends string
|
|
27
|
+
> = Value extends Function
|
|
28
|
+
? Value
|
|
29
|
+
: Value extends Array<infer U>
|
|
30
|
+
? Value
|
|
31
|
+
: { [K in keyof Value as DelimiterCase<K, Delimiter>]: Value[K] };
|
package/ts41/index.d.ts
CHANGED
|
@@ -3,8 +3,21 @@ export * from '../base';
|
|
|
3
3
|
|
|
4
4
|
// These are special types that require at least TypeScript 4.1.
|
|
5
5
|
export {CamelCase} from './camel-case';
|
|
6
|
+
export {CamelCasedProperties} from './camel-cased-properties';
|
|
7
|
+
export {CamelCasedPropertiesDeep} from './camel-cased-properties-deep';
|
|
6
8
|
export {KebabCase} from './kebab-case';
|
|
9
|
+
export {KebabCasedProperties} from './kebab-cased-properties';
|
|
10
|
+
export {KebabCasedPropertiesDeep} from './kebab-cased-properties-deep';
|
|
7
11
|
export {PascalCase} from './pascal-case';
|
|
12
|
+
export {PascalCasedProperties} from './pascal-cased-properties';
|
|
13
|
+
export {PascalCasedPropertiesDeep} from './pascal-cased-properties-deep';
|
|
8
14
|
export {SnakeCase} from './snake-case';
|
|
15
|
+
export {SnakeCasedProperties} from './snake-cased-properties';
|
|
16
|
+
export {SnakeCasedPropertiesDeep} from './snake-cased-properties-deep';
|
|
9
17
|
export {DelimiterCase} from './delimiter-case';
|
|
18
|
+
export {DelimiterCasedProperties} from './delimiter-cased-properties';
|
|
19
|
+
export {DelimiterCasedPropertiesDeep} from './delimiter-cased-properties-deep';
|
|
20
|
+
export {Split} from './split';
|
|
21
|
+
export {Trim} from './trim';
|
|
10
22
|
export {Get} from './get';
|
|
23
|
+
export {LastArrayElement} from './last-array-element';
|
package/ts41/kebab-case.d.ts
CHANGED
|
@@ -15,7 +15,7 @@ const someVariable: KebabCase<'fooBar'> = 'foo-bar';
|
|
|
15
15
|
|
|
16
16
|
// Advanced
|
|
17
17
|
|
|
18
|
-
type
|
|
18
|
+
type KebabCasedProperties<T> = {
|
|
19
19
|
[K in keyof T as KebabCase<K>]: T[K]
|
|
20
20
|
};
|
|
21
21
|
|
|
@@ -25,7 +25,7 @@ interface CliOptions {
|
|
|
25
25
|
foo: number;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
const rawCliOptions:
|
|
28
|
+
const rawCliOptions: KebabCasedProperties<CliOptions> = {
|
|
29
29
|
'dry-run': true,
|
|
30
30
|
'include-file': 'bar.js',
|
|
31
31
|
foo: 123
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {DelimiterCasedPropertiesDeep} from './delimiter-cased-properties-deep';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Convert object properties to kebab case recursively.
|
|
5
|
+
|
|
6
|
+
This can be useful when, for example, converting some API types from a different style.
|
|
7
|
+
|
|
8
|
+
@see KebabCase
|
|
9
|
+
@see KebabCasedProperties
|
|
10
|
+
|
|
11
|
+
@example
|
|
12
|
+
```
|
|
13
|
+
interface User {
|
|
14
|
+
userId: number;
|
|
15
|
+
userName: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface UserWithFriends {
|
|
19
|
+
userInfo: User;
|
|
20
|
+
userFriends: User[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const result: KebabCasedPropertiesDeep<UserWithFriends> = {
|
|
24
|
+
'user-info': {
|
|
25
|
+
'user-id': 1,
|
|
26
|
+
'user-name': 'Tom',
|
|
27
|
+
},
|
|
28
|
+
'user-friends': [
|
|
29
|
+
{
|
|
30
|
+
'user-id': 2,
|
|
31
|
+
'user-name': 'Jerry',
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
'user-id': 3,
|
|
35
|
+
'user-name': 'Spike',
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
};
|
|
39
|
+
```
|
|
40
|
+
*/
|
|
41
|
+
export type KebabCasedPropertiesDeep<Value> = DelimiterCasedPropertiesDeep<Value, '-'>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import {DelimiterCasedProperties} from './delimiter-cased-properties';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Convert object properties to kebab case but not recursively.
|
|
5
|
+
|
|
6
|
+
This can be useful when, for example, converting some API types from a different style.
|
|
7
|
+
|
|
8
|
+
@see KebabCase
|
|
9
|
+
@see KebabCasedPropertiesDeep
|
|
10
|
+
|
|
11
|
+
@example
|
|
12
|
+
```
|
|
13
|
+
interface User {
|
|
14
|
+
userId: number;
|
|
15
|
+
userName: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const result: KebabCasedProperties<User> = {
|
|
19
|
+
'user-id': 1,
|
|
20
|
+
'user-name': 'Tom',
|
|
21
|
+
};
|
|
22
|
+
```
|
|
23
|
+
*/
|
|
24
|
+
export type KebabCasedProperties<Value> = DelimiterCasedProperties<Value, '-'>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Extracts the type of the last element of an array.
|
|
3
|
+
|
|
4
|
+
Use-case: Defining the return type of functions that extract the last element of an array, for example [`lodash.last`](https://lodash.com/docs/4.17.15#last).
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import {LastArrayElement} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
declare function lastOf<V extends any[]>(array: V): LastArrayElement<V>;
|
|
11
|
+
|
|
12
|
+
const array = ['foo', 2];
|
|
13
|
+
|
|
14
|
+
typeof lastOf(array);
|
|
15
|
+
//=> number
|
|
16
|
+
```
|
|
17
|
+
*/
|
|
18
|
+
export type LastArrayElement<ValueType extends unknown[]> =
|
|
19
|
+
ValueType extends [infer ElementType]
|
|
20
|
+
? ElementType
|
|
21
|
+
: ValueType extends [infer _, ...infer Tail]
|
|
22
|
+
? LastArrayElement<Tail>
|
|
23
|
+
: never;
|
package/ts41/pascal-case.d.ts
CHANGED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import {PascalCase} from './pascal-case';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Convert object properties to pascal case recursively.
|
|
5
|
+
|
|
6
|
+
This can be useful when, for example, converting some API types from a different style.
|
|
7
|
+
|
|
8
|
+
@see PascalCase
|
|
9
|
+
@see PascalCasedProperties
|
|
10
|
+
|
|
11
|
+
@example
|
|
12
|
+
```
|
|
13
|
+
interface User {
|
|
14
|
+
userId: number;
|
|
15
|
+
userName: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface UserWithFriends {
|
|
19
|
+
userInfo: User;
|
|
20
|
+
userFriends: User[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const result: PascalCasedPropertiesDeep<UserWithFriends> = {
|
|
24
|
+
UserInfo: {
|
|
25
|
+
UserId: 1,
|
|
26
|
+
UserName: 'Tom',
|
|
27
|
+
},
|
|
28
|
+
UserFriends: [
|
|
29
|
+
{
|
|
30
|
+
UserId: 2,
|
|
31
|
+
UserName: 'Jerry',
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
UserId: 3,
|
|
35
|
+
UserName: 'Spike',
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
};
|
|
39
|
+
```
|
|
40
|
+
*/
|
|
41
|
+
export type PascalCasedPropertiesDeep<Value> = Value extends Function
|
|
42
|
+
? Value
|
|
43
|
+
: Value extends Array<infer U>
|
|
44
|
+
? Array<PascalCasedPropertiesDeep<U>>
|
|
45
|
+
: Value extends Set<infer U>
|
|
46
|
+
? Set<PascalCasedPropertiesDeep<U>> : {
|
|
47
|
+
[K in keyof Value as PascalCase<K>]: PascalCasedPropertiesDeep<Value[K]>;
|
|
48
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {PascalCase} from './pascal-case';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Convert object properties to pascal case but not recursively.
|
|
5
|
+
|
|
6
|
+
This can be useful when, for example, converting some API types from a different style.
|
|
7
|
+
|
|
8
|
+
@see PascalCase
|
|
9
|
+
@see PascalCasedPropertiesDeep
|
|
10
|
+
|
|
11
|
+
@example
|
|
12
|
+
```
|
|
13
|
+
interface User {
|
|
14
|
+
userId: number;
|
|
15
|
+
userName: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const result: PascalCasedProperties<User> = {
|
|
19
|
+
UserId: 1,
|
|
20
|
+
UserName: 'Tom',
|
|
21
|
+
};
|
|
22
|
+
```
|
|
23
|
+
*/
|
|
24
|
+
export type PascalCasedProperties<Value> = Value extends Function
|
|
25
|
+
? Value
|
|
26
|
+
: Value extends Array<infer U>
|
|
27
|
+
? Value
|
|
28
|
+
: { [K in keyof Value as PascalCase<K>]: Value[K] };
|
package/ts41/snake-case.d.ts
CHANGED
|
@@ -15,7 +15,7 @@ const someVariable: SnakeCase<'fooBar'> = 'foo_bar';
|
|
|
15
15
|
|
|
16
16
|
// Advanced
|
|
17
17
|
|
|
18
|
-
type
|
|
18
|
+
type SnakeCasedProperties<T> = {
|
|
19
19
|
[K in keyof T as SnakeCase<K>]: T[K]
|
|
20
20
|
};
|
|
21
21
|
|
|
@@ -25,7 +25,7 @@ interface ModelProps {
|
|
|
25
25
|
foo: number;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
const dbResult:
|
|
28
|
+
const dbResult: SnakeCasedProperties<ModelProps> = {
|
|
29
29
|
'is_happy': true,
|
|
30
30
|
'full_family_name': 'Carla Smith',
|
|
31
31
|
foo: 123
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {DelimiterCasedPropertiesDeep} from './delimiter-cased-properties-deep';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Convert object properties to snake case recursively.
|
|
5
|
+
|
|
6
|
+
This can be useful when, for example, converting some API types from a different style.
|
|
7
|
+
|
|
8
|
+
@see SnakeCase
|
|
9
|
+
@see SnakeCasedProperties
|
|
10
|
+
|
|
11
|
+
@example
|
|
12
|
+
```
|
|
13
|
+
interface User {
|
|
14
|
+
userId: number;
|
|
15
|
+
userName: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface UserWithFriends {
|
|
19
|
+
userInfo: User;
|
|
20
|
+
userFriends: User[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const result: SnakeCasedPropertiesDeep<UserWithFriends> = {
|
|
24
|
+
user_info: {
|
|
25
|
+
user_id: 1,
|
|
26
|
+
user_name: 'Tom',
|
|
27
|
+
},
|
|
28
|
+
user_friends: [
|
|
29
|
+
{
|
|
30
|
+
user_id: 2,
|
|
31
|
+
user_name: 'Jerry',
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
user_id: 3,
|
|
35
|
+
user_name: 'Spike',
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
};
|
|
39
|
+
```
|
|
40
|
+
*/
|
|
41
|
+
export type SnakeCasedPropertiesDeep<Value> = DelimiterCasedPropertiesDeep<Value, '_'>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import {DelimiterCasedProperties} from './delimiter-cased-properties';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Convert object properties to snake case but not recursively.
|
|
5
|
+
|
|
6
|
+
This can be useful when, for example, converting some API types from a different style.
|
|
7
|
+
|
|
8
|
+
@see SnakeCase
|
|
9
|
+
@see SnakeCasedPropertiesDeep
|
|
10
|
+
|
|
11
|
+
@example
|
|
12
|
+
```
|
|
13
|
+
interface User {
|
|
14
|
+
userId: number;
|
|
15
|
+
userName: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const result: SnakeCasedProperties<User> = {
|
|
19
|
+
user_id: 1,
|
|
20
|
+
user_name: 'Tom',
|
|
21
|
+
};
|
|
22
|
+
```
|
|
23
|
+
*/
|
|
24
|
+
export type SnakeCasedProperties<Value> = DelimiterCasedProperties<Value, '_'>;
|
package/ts41/split.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Represents an array of strings split using a given character or character set.
|
|
3
|
+
|
|
4
|
+
Use-case: Defining the return type of a method like `String.prototype.split`.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import {Split} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
declare function split<S extends string, D extends string>(string: S, separator: D): Split<S, D>;
|
|
11
|
+
|
|
12
|
+
type Item = 'foo' | 'bar' | 'baz' | 'waldo';
|
|
13
|
+
const items = 'foo,bar,baz,waldo';
|
|
14
|
+
let array: Item[];
|
|
15
|
+
|
|
16
|
+
array = split(items, ',');
|
|
17
|
+
```
|
|
18
|
+
*/
|
|
19
|
+
export type Split<S extends string, D extends string> =
|
|
20
|
+
S extends `${infer T}${D}${infer U}`
|
|
21
|
+
? [T, ...Split<U, D>]
|
|
22
|
+
: [S];
|
package/ts41/trim.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Remove spaces from the left side.
|
|
3
|
+
*/
|
|
4
|
+
type TrimLeft<V extends string> = V extends ` ${infer R}` ? TrimLeft<R> : V;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
Remove spaces from the right side.
|
|
8
|
+
*/
|
|
9
|
+
type TrimRight<V extends string> = V extends `${infer R} ` ? TrimRight<R> : V;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
Remove leading and trailing spaces from a string.
|
|
13
|
+
|
|
14
|
+
@example
|
|
15
|
+
```
|
|
16
|
+
import {Trim} from 'type-fest';
|
|
17
|
+
|
|
18
|
+
Trim<' foo '>
|
|
19
|
+
//=> 'foo'
|
|
20
|
+
```
|
|
21
|
+
*/
|
|
22
|
+
export type Trim<V extends string> = TrimLeft<TrimRight<V>>;
|