Skip to content

Commit c837e98

Browse files
committed
Added keyOfClass.
1 parent 72c75cb commit c837e98

File tree

10 files changed

+87
-172
lines changed

10 files changed

+87
-172
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
### 1.3.2
4+
5+
#### Added
6+
7+
- `keyOfClass`
8+
39
### 1.3.1
410

511
#### Update

README.md

Lines changed: 9 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -8,173 +8,11 @@ TypeScript common types.
88
[![NPM Downloads][npm-downloads-img]][npm-downloads-url]
99
[![TypeScript Typings][ts-img]][ts-url]
1010

11-
# Reference
11+
---
1212

13-
## Class
13+
Documentation [https://corefunc.github.io/type](https://corefunc.github.io/type).
1414

15-
### Complete
16-
17-
```typescript
18-
import type { CompleteType } from "@corefunc/type/class/complete.type";
19-
interface UserInterface {
20-
name: string;
21-
zip: string;
22-
}
23-
// Forces class to implement all interface properties.
24-
class User implements CompleteType<UserInterface> {
25-
public name: string;
26-
public zip: string;
27-
}
28-
```
29-
30-
### Concrete
31-
32-
```typescript
33-
import type { ConcreteType } from "@corefunc/type/class/concrete.type";
34-
interface UserInterface {
35-
email: string;
36-
nickname?: string;
37-
}
38-
// Disallow 'optional' properties from inherited type.
39-
class User implements ConcreteType<UserInterface> {
40-
email: string;
41-
nickname: string | undefined;
42-
}
43-
```
44-
45-
### Constructor
46-
47-
```typescript
48-
import type { ConstructorType } from "@corefunc/type/class/constructor.type";
49-
50-
class ExchangeClient {}
51-
52-
function mixinApiPostUser<T extends ConstructorType<ExchangeClient>>(superClass: T) {
53-
return class extends superClass {
54-
public postUser(data: any) {
55-
return this.post(`/users/`, data);
56-
}
57-
};
58-
}
59-
60-
function mixinApiGetUser<T extends ConstructorType<ExchangeClient>>(superClass: T) {
61-
return class extends superClass {
62-
public getUser(id: string) {
63-
return this.get(`/users/${id}`);
64-
}
65-
};
66-
}
67-
68-
class ApiClient extends mixinApiPostUser(mixinApiGetUser(ExchangeClient)) {}
69-
```
70-
71-
## Number
72-
73-
### Float
74-
75-
```typescript
76-
import { isFloatRange, assertFloatRange } from "@corefunc/type";
77-
import { TypeFloatRange, FloatRangeType, FloatRange } from "@corefunc/type";
78-
const value: TypeFloatRange<0.01, 99.99> = 10.5;
79-
```
80-
81-
### Integer 16
82-
83-
```typescript
84-
import { isInteger16, assertInteger16, INT16, INT16_MAX, INT16_MIN } from "@corefunc/type";
85-
import type { TypeInteger16, Integer16Type, Integer16 } from "@corefunc/type";
86-
const value: TypeInteger16 = 32_767;
87-
```
88-
89-
### Integer 32
90-
91-
```typescript
92-
import { isInteger32, assertInteger32, INT32, INT32_MAX, INT32_MIN } from "@corefunc/type";
93-
import type { TypeInteger32, Integer32Type, Integer32 } from "@corefunc/type";
94-
const value: TypeInteger32 = 2_147_483_647;
95-
```
96-
97-
### Integer 64
98-
99-
```typescript
100-
import { isInteger64, assertInteger64, INT64, INT64_MAX, INT64_MIN } from "@corefunc/type";
101-
import type { TypeInteger64, Integer64Type, Integer64 } from "@corefunc/type";
102-
const value: TypeInteger64 = 9_223_372_036_854_775_807;
103-
```
104-
105-
### Integer Range
106-
107-
```typescript
108-
import { isIntegerRange, assertIntegerRange } from "@corefunc/type";
109-
import type { TypeIntegerRange, IntegerRangeType, IntegerRange } from "@corefunc/type";
110-
const value: TypeIntegerRange<0, 100> = 50;
111-
```
112-
113-
### Numeric
114-
115-
```typescript
116-
import { isNumeric, assertNumeric } from "@corefunc/type";
117-
import type { TypeNumeric, NumericType, Numeric } from "@corefunc/type";
118-
const value: TypeNumeric<5, 2> = 999.99;
119-
```
120-
121-
## String
122-
123-
### String Date
124-
125-
```typescript
126-
import { isStringDate, assertStringDate } from "@corefunc/type";
127-
import type { TypeStringDate, StringDateType, StringDate } from "@corefunc/type";
128-
const value: StringDate = "2020-01-01";
129-
```
130-
131-
### String Fixed
132-
133-
```typescript
134-
import { isStringFixed, assertStringFixed } from "@corefunc/type";
135-
import type { TypeStringFixed, StringFixedType, StringFixed } from "@corefunc/type";
136-
const value: StringFixed<3> = "USD";
137-
```
138-
139-
### String Of Length
140-
141-
```typescript
142-
import { isStringOfLength, assertStringOfLength } from "@corefunc/type";
143-
import type { TypeStringOfLength, StringOfLengthType, StringOfLength } from "@corefunc/type";
144-
const value: StringOfLength<2, 99> = "Billy";
145-
```
146-
147-
### String Time
148-
149-
```typescript
150-
import { isStringTime, assertStringTime } from "@corefunc/type";
151-
import type { TypeStringTime, StringTimeType, StringTime } from "@corefunc/type";
152-
const value: StringTime = "12:59";
153-
```
154-
155-
### String Varying
156-
157-
```typescript
158-
import { isStringVarying, assertStringVarying } from "@corefunc/type";
159-
import type { TypeStringVarying, StringVaryingType, StringVarying } from "@corefunc/type";
160-
const value: StringVarying<254> = "user+inbox@domain";
161-
```
162-
163-
### UUID
164-
165-
```typescript
166-
import { isUuid, assertUuid } from "@corefunc/type";
167-
import type { TypeUUID, UUIDType, UUID } from "@corefunc/type";
168-
const value: UUID = "0b0bc0f0-db42-11eb-8d19-0242ac130003";
169-
```
170-
171-
### UUID 4
172-
173-
```typescript
174-
import { isUuid4, assertUuid4 } from "@corefunc/type";
175-
import type { TypeUUID4, UUID4Type, UUID4 } from "@corefunc/type";
176-
const value: UUID4 = "155fbaaf-09de-4141-80df-94696eed4cb6";
177-
```
15+
---
17816

17917
## See also
18018

@@ -184,9 +22,9 @@ const value: UUID4 = "155fbaaf-09de-4141-80df-94696eed4cb6";
18422

18523
<!-- Badges -->
18624

187-
[npm-version-url]: https://npmjs.com/package/@corefunc/type
188-
[npm-version-img]: https://badgen.net/npm/v/@corefunc/type?&icon=npm&label=npm&color=DD3636
189-
[npm-downloads-url]: https://npmjs.com/package/@corefunc/type
190-
[npm-downloads-img]: https://badgen.net/npm/dt/@corefunc/type?&icon=terminal&label=downloads&color=009688
191-
[ts-url]: https://github.com/corefunc/type/blob/master/index.d.ts
192-
[ts-img]: https://badgen.net/npm/types/@corefunc/type?&icon=typescript&label=types&color=1E90FF
25+
[npm-version-url]: https://npmjs.com/package/@nestjsi/class-validator
26+
[npm-version-img]: https://badgen.net/npm/v/@nestjsi/class-validator?&icon=npm&label=npm&color=DD3636
27+
[npm-downloads-url]: https://npmjs.com/package/@nestjsi/class-validator
28+
[npm-downloads-img]: https://badgen.net/npm/dt/@nestjsi/class-validator?&icon=terminal&label=downloads&color=009688
29+
[ts-url]: https://github.com/nestjsi/class-validator/blob/main/dist/index.d.ts
30+
[ts-img]: https://badgen.net/npm/types/@nestjsi/class-validator?&icon=typescript&label=types&color=1E90FF

class/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./complete.type";
22
export * from "./concrete.type";
33
export * from "./constructor.type";
4+
export * from "./key-of-class";

class/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3+
if (k2 === undefined) k2 = k;
4+
var desc = Object.getOwnPropertyDescriptor(m, k);
5+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6+
desc = { enumerable: true, get: function() { return m[k]; } };
7+
}
8+
Object.defineProperty(o, k2, desc);
9+
}) : (function(o, m, k, k2) {
10+
if (k2 === undefined) k2 = k;
11+
o[k2] = m[k];
12+
}));
13+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
14+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15+
};
16+
exports.__esModule = true;
17+
__exportStar(require("./complete.type"), exports);
18+
__exportStar(require("./concrete.type"), exports);
19+
__exportStar(require("./constructor.type"), exports);
20+
__exportStar(require("./key-of-class"), exports);

class/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./complete.type";
22
export * from "./concrete.type";
33
export * from "./constructor.type";
4+
export * from "./key-of-class";

class/key-of-class.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @category Class
3+
* @name keyOfClass
4+
* @description Checks if name is a key of class. String only, no Symbols or Numbers.
5+
* @summary ```import type { keyOfClass } from "@corefunc/type/class/key-of-class";```
6+
* @returns {(propertyName: string) => string}
7+
* @since 1.3.2
8+
*/
9+
export declare function keyOfClass<ClassName>(propertyName: keyof ClassName): string;

class/key-of-class.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
exports.keyOfClass = void 0;
4+
/**
5+
* @category Class
6+
* @name keyOfClass
7+
* @description Checks if name is a key of class. String only, no Symbols or Numbers.
8+
* @summary ```import type { keyOfClass } from "@corefunc/type/class/key-of-class";```
9+
* @returns {(propertyName: string) => string}
10+
* @since 1.3.2
11+
*/
12+
function keyOfClass(propertyName) {
13+
return String(propertyName);
14+
}
15+
exports.keyOfClass = keyOfClass;

class/key-of-class.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @category Class
3+
* @name keyOfClass
4+
* @description Checks if name is a key of class. String only, no Symbols or Numbers.
5+
* @summary ```import type { keyOfClass } from "@corefunc/type/class/key-of-class";```
6+
* @returns {(propertyName: string) => string}
7+
* @since 1.3.2
8+
*/
9+
export function keyOfClass<ClassName>(propertyName: keyof ClassName): string {
10+
return String(propertyName);
11+
}

docs/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ function mixinApiGetUser<T extends ConstructorType<ExchangeClient>>(superClass:
6868
class ApiClient extends mixinApiPostUser(mixinApiGetUser(ExchangeClient)) {}
6969
```
7070

71+
### Key of class
72+
73+
```typescript
74+
import { keyOfClass } from '@corefunc/type/class/key-of-class';
75+
class User {
76+
public static readonly name: string = keyOfClass<User>('name');
77+
public readonly name: string;
78+
}
79+
```
80+
7181
## Number
7282

7383
### Float

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
"class/concrete.type.d.ts",
2727
"class/constructor.type.ts",
2828
"class/index.d.ts",
29+
"class/index.js",
30+
"class/key-of-class.d.ts",
31+
"class/key-of-class.js",
32+
"class/key-of-class.ts",
2933
"number/float-range.type.js",
3034
"number/float-range.type.d.ts",
3135
"number/index.js",
@@ -78,5 +82,5 @@
7882
"test": "echo \"Error: no test specified\" && exit 1"
7983
},
8084
"types": "./index.d.ts",
81-
"version": "1.3.1"
85+
"version": "1.3.2"
8286
}

0 commit comments

Comments
 (0)