@tstdl/base 0.91.0-beta8 → 0.91.0
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/authentication/authentication.api.d.ts +1 -1
- package/examples/api/basic-overview.js +2 -2
- package/image-service/image-service.js +4 -4
- package/intl/number-parser.d.ts +1 -1
- package/mail/mail.client.js +2 -2
- package/object-storage/s3/s3.object-storage-provider.d.ts +6 -6
- package/object-storage/s3/s3.object-storage-provider.js +6 -6
- package/orm/entity.js +2 -2
- package/package.json +5 -5
- package/schema/schema.d.ts +3 -2
- package/schema/schemas/array.js +10 -8
- package/schema/schemas/bigint.d.ts +1 -1
- package/schema/schemas/bigint.js +1 -1
- package/schema/schemas/boolean.d.ts +0 -4
- package/schema/schemas/boolean.js +1 -7
- package/schema/schemas/date.d.ts +1 -1
- package/schema/schemas/date.js +1 -1
- package/schema/schemas/function.d.ts +1 -1
- package/schema/schemas/function.js +1 -1
- package/schema/schemas/number.d.ts +1 -5
- package/schema/schemas/number.js +1 -7
- package/schema/schemas/object.d.ts +19 -11
- package/schema/schemas/object.js +1 -1
- package/schema/schemas/optional.d.ts +3 -2
- package/schema/schemas/readable-stream.d.ts +1 -1
- package/schema/schemas/readable-stream.js +1 -1
- package/schema/schemas/regexp.d.ts +1 -1
- package/schema/schemas/regexp.js +1 -1
- package/schema/schemas/string.d.ts +0 -4
- package/schema/schemas/string.js +1 -7
- package/schema/schemas/symbol.d.ts +0 -4
- package/schema/schemas/symbol.js +1 -7
- package/schema/schemas/uint8-array.d.ts +1 -1
- package/schema/schemas/uint8-array.js +3 -3
- package/utils/async-iterable-helpers/group-single.d.ts +2 -1
- package/utils/async-iterable-helpers/group-single.js +2 -2
- package/utils/async-iterable-helpers/group-to-map.d.ts +2 -1
- package/utils/async-iterable-helpers/group-to-map.js +44 -17
- package/utils/async-iterable-helpers/group-to-single-map.d.ts +2 -1
- package/utils/async-iterable-helpers/group-to-single-map.js +44 -17
- package/utils/async-iterable-helpers/group.d.ts +2 -1
- package/utils/async-iterable-helpers/group.js +2 -2
- package/utils/async-iterable-helpers/types.d.ts +3 -0
- package/utils/iterable-helpers/group-single.d.ts +2 -1
- package/utils/iterable-helpers/group-single.js +2 -2
- package/utils/iterable-helpers/group-to-map.d.ts +2 -1
- package/utils/iterable-helpers/group-to-map.js +21 -7
- package/utils/iterable-helpers/group-to-single-map.d.ts +2 -1
- package/utils/iterable-helpers/group-to-single-map.js +21 -7
- package/utils/iterable-helpers/group.d.ts +2 -1
- package/utils/iterable-helpers/group.js +2 -2
- package/utils/iterable-helpers/types.d.ts +3 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type ApiDefinition } from '../api/types.js';
|
|
2
|
-
import {
|
|
2
|
+
import { ObjectSchema, type ObjectSchemaOrType } from '../schema/index.js';
|
|
3
3
|
import type { SchemaTestable } from '../schema/schema.js';
|
|
4
4
|
import type { Record } from '../types.js';
|
|
5
5
|
import type { TokenPayload } from './index.js';
|
|
@@ -18,7 +18,7 @@ import { configureHttpClient } from '../../http/client/module.js';
|
|
|
18
18
|
import { configureNodeHttpServer } from '../../http/server/node/module.js';
|
|
19
19
|
import { inject } from '../../injector/inject.js';
|
|
20
20
|
import { WebServerModule } from '../../module/modules/web-server.module.js';
|
|
21
|
-
import {
|
|
21
|
+
import { NumberProperty, Property, array, boolean, number, object } from '../../schema/index.js';
|
|
22
22
|
import { timeout } from '../../utils/timing.js';
|
|
23
23
|
import { Agent } from 'undici';
|
|
24
24
|
class User {
|
|
@@ -26,7 +26,7 @@ class User {
|
|
|
26
26
|
name;
|
|
27
27
|
}
|
|
28
28
|
__decorate([
|
|
29
|
-
|
|
29
|
+
NumberProperty({ coerce: true }),
|
|
30
30
|
__metadata("design:type", Number)
|
|
31
31
|
], User.prototype, "id", void 0);
|
|
32
32
|
__decorate([
|
|
@@ -7,7 +7,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
7
7
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
9
|
};
|
|
10
|
-
import { Enumeration,
|
|
10
|
+
import { Enumeration, NumberProperty, Optional } from '../schema/index.js';
|
|
11
11
|
export var ImageResizeMode;
|
|
12
12
|
(function (ImageResizeMode) {
|
|
13
13
|
ImageResizeMode["Fit"] = "fit";
|
|
@@ -49,11 +49,11 @@ __decorate([
|
|
|
49
49
|
__metadata("design:type", String)
|
|
50
50
|
], ImageOptions.prototype, "resizeMode", void 0);
|
|
51
51
|
__decorate([
|
|
52
|
-
|
|
52
|
+
NumberProperty({ optional: true, coerce: true }),
|
|
53
53
|
__metadata("design:type", Number)
|
|
54
54
|
], ImageOptions.prototype, "width", void 0);
|
|
55
55
|
__decorate([
|
|
56
|
-
|
|
56
|
+
NumberProperty({ optional: true, coerce: true }),
|
|
57
57
|
__metadata("design:type", Number)
|
|
58
58
|
], ImageOptions.prototype, "height", void 0);
|
|
59
59
|
__decorate([
|
|
@@ -62,7 +62,7 @@ __decorate([
|
|
|
62
62
|
__metadata("design:type", String)
|
|
63
63
|
], ImageOptions.prototype, "origin", void 0);
|
|
64
64
|
__decorate([
|
|
65
|
-
|
|
65
|
+
NumberProperty({ optional: true, coerce: true }),
|
|
66
66
|
__metadata("design:type", Number)
|
|
67
67
|
], ImageOptions.prototype, "quality", void 0);
|
|
68
68
|
__decorate([
|
package/intl/number-parser.d.ts
CHANGED
|
@@ -14,5 +14,5 @@ export declare class NumberParser {
|
|
|
14
14
|
*/
|
|
15
15
|
parse(value: string, loose?: boolean): number;
|
|
16
16
|
}
|
|
17
|
-
export declare const getNumberParser: (locale: string) =>
|
|
17
|
+
export declare const getNumberParser: (locale: string) => NumberParser;
|
|
18
18
|
export declare function parseNumber(locale: string, value: string, loose?: boolean): number;
|
package/mail/mail.client.js
CHANGED
|
@@ -8,7 +8,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
8
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
9
|
};
|
|
10
10
|
import { Property } from '../schema/decorators/property.js';
|
|
11
|
-
import {
|
|
11
|
+
import { BooleanProperty } from '../schema/index.js';
|
|
12
12
|
import { Optional } from '../schema/schemas/optional.js';
|
|
13
13
|
export class MailClientAuthConfig {
|
|
14
14
|
user;
|
|
@@ -38,7 +38,7 @@ __decorate([
|
|
|
38
38
|
__metadata("design:type", Number)
|
|
39
39
|
], MailClientConfig.prototype, "port", void 0);
|
|
40
40
|
__decorate([
|
|
41
|
-
|
|
41
|
+
BooleanProperty({ optional: true }),
|
|
42
42
|
__metadata("design:type", Boolean)
|
|
43
43
|
], MailClientConfig.prototype, "secure", void 0);
|
|
44
44
|
__decorate([
|
|
@@ -2,27 +2,27 @@ import { ObjectStorageProvider } from '../../object-storage/index.js';
|
|
|
2
2
|
import { S3ObjectStorage } from './s3.object-storage.js';
|
|
3
3
|
export declare class S3ObjectStorageProviderConfig {
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* S3 endpoint
|
|
6
6
|
*/
|
|
7
7
|
endpoint: string;
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* S3 bucket, use a single bucket for all modules (which will become transparent key prefixes)
|
|
10
10
|
*
|
|
11
11
|
* mutually exclusive with bucketPerModule
|
|
12
12
|
*/
|
|
13
13
|
bucket?: string;
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
15
|
+
* Use an own bucket for every module
|
|
16
16
|
*
|
|
17
17
|
* mutually exclusive with bucket
|
|
18
18
|
*/
|
|
19
19
|
bucketPerModule?: boolean;
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
21
|
+
* S3 access key
|
|
22
22
|
*/
|
|
23
23
|
accessKey: string;
|
|
24
24
|
/**
|
|
25
|
-
*
|
|
25
|
+
* S3 secret key
|
|
26
26
|
*/
|
|
27
27
|
secretKey: string;
|
|
28
28
|
}
|
|
@@ -34,7 +34,7 @@ export declare class S3ObjectStorageProvider extends ObjectStorageProvider<S3Obj
|
|
|
34
34
|
get(module: string): S3ObjectStorage;
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
37
|
-
*
|
|
37
|
+
* Configure s3 object storage provider
|
|
38
38
|
* @param config s3 config
|
|
39
39
|
* @param register whether to register for {@link ObjectStorage} and {@link ObjectStorageProvider}
|
|
40
40
|
*/
|
|
@@ -15,27 +15,27 @@ import { Client } from 'minio';
|
|
|
15
15
|
import { S3ObjectStorage } from './s3.object-storage.js';
|
|
16
16
|
export class S3ObjectStorageProviderConfig {
|
|
17
17
|
/**
|
|
18
|
-
*
|
|
18
|
+
* S3 endpoint
|
|
19
19
|
*/
|
|
20
20
|
endpoint;
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
22
|
+
* S3 bucket, use a single bucket for all modules (which will become transparent key prefixes)
|
|
23
23
|
*
|
|
24
24
|
* mutually exclusive with bucketPerModule
|
|
25
25
|
*/
|
|
26
26
|
bucket;
|
|
27
27
|
/**
|
|
28
|
-
*
|
|
28
|
+
* Use an own bucket for every module
|
|
29
29
|
*
|
|
30
30
|
* mutually exclusive with bucket
|
|
31
31
|
*/
|
|
32
32
|
bucketPerModule;
|
|
33
33
|
/**
|
|
34
|
-
*
|
|
34
|
+
* S3 access key
|
|
35
35
|
*/
|
|
36
36
|
accessKey;
|
|
37
37
|
/**
|
|
38
|
-
*
|
|
38
|
+
* S3 secret key
|
|
39
39
|
*/
|
|
40
40
|
secretKey;
|
|
41
41
|
}
|
|
@@ -70,7 +70,7 @@ S3ObjectStorageProvider = __decorate([
|
|
|
70
70
|
], S3ObjectStorageProvider);
|
|
71
71
|
export { S3ObjectStorageProvider };
|
|
72
72
|
/**
|
|
73
|
-
*
|
|
73
|
+
* Configure s3 object storage provider
|
|
74
74
|
* @param config s3 config
|
|
75
75
|
* @param register whether to register for {@link ObjectStorage} and {@link ObjectStorageProvider}
|
|
76
76
|
*/
|
package/orm/entity.js
CHANGED
|
@@ -7,7 +7,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
7
7
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
9
|
};
|
|
10
|
-
import {
|
|
10
|
+
import { NumberProperty, Property, any, record } from '../schema/index.js';
|
|
11
11
|
export class EntityMetadata {
|
|
12
12
|
revision;
|
|
13
13
|
revisionTimestamp;
|
|
@@ -28,7 +28,7 @@ __decorate([
|
|
|
28
28
|
__metadata("design:type", Number)
|
|
29
29
|
], EntityMetadata.prototype, "createTimestamp", void 0);
|
|
30
30
|
__decorate([
|
|
31
|
-
|
|
31
|
+
NumberProperty({ nullable: true }),
|
|
32
32
|
__metadata("design:type", Object)
|
|
33
33
|
], EntityMetadata.prototype, "deleteTimestamp", void 0);
|
|
34
34
|
__decorate([
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tstdl/base",
|
|
3
|
-
"version": "0.91.0
|
|
3
|
+
"version": "0.91.0",
|
|
4
4
|
"author": "Patrick Hein",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -111,7 +111,7 @@
|
|
|
111
111
|
"luxon": "^3.4",
|
|
112
112
|
"reflect-metadata": "^0.2",
|
|
113
113
|
"rxjs": "^7.8",
|
|
114
|
-
"type-fest": "4.
|
|
114
|
+
"type-fest": "4.23"
|
|
115
115
|
},
|
|
116
116
|
"devDependencies": {
|
|
117
117
|
"@mxssfd/typedoc-theme": "1.1",
|
|
@@ -123,7 +123,7 @@
|
|
|
123
123
|
"@types/mjml": "4.7",
|
|
124
124
|
"@types/node": "20",
|
|
125
125
|
"@types/nodemailer": "6.4",
|
|
126
|
-
"@typescript-eslint/eslint-plugin": "7.
|
|
126
|
+
"@typescript-eslint/eslint-plugin": "7.17",
|
|
127
127
|
"concurrently": "8.2",
|
|
128
128
|
"eslint": "8.57",
|
|
129
129
|
"eslint-import-resolver-typescript": "3.6",
|
|
@@ -147,11 +147,11 @@
|
|
|
147
147
|
"koa": "^2.15",
|
|
148
148
|
"minio": "^8.0",
|
|
149
149
|
"mjml": "^4.15",
|
|
150
|
-
"mongodb": "^6.
|
|
150
|
+
"mongodb": "^6.8",
|
|
151
151
|
"nodemailer": "^6.9",
|
|
152
152
|
"pg": "^8.12",
|
|
153
153
|
"playwright": "^1.45",
|
|
154
|
-
"preact": "^10.
|
|
154
|
+
"preact": "^10.23",
|
|
155
155
|
"preact-render-to-string": "^6.5",
|
|
156
156
|
"undici": "^6.19",
|
|
157
157
|
"urlpattern-polyfill": "^10.0"
|
package/schema/schema.d.ts
CHANGED
|
@@ -19,10 +19,11 @@ export type SchemaTestResult<T> = {
|
|
|
19
19
|
error: SchemaError;
|
|
20
20
|
};
|
|
21
21
|
type NormalizePrimitiveToConstructor<T> = Or<IsEqual<T, string>, IsEqual<T, String>> extends true ? typeof String : Or<IsEqual<T, number>, IsEqual<T, Number>> extends true ? typeof Number : Or<IsEqual<T, boolean>, IsEqual<T, Boolean>> extends true ? typeof Boolean : Or<IsEqual<T, bigint>, IsEqual<T, BigInt>> extends true ? typeof BigInt : Or<IsEqual<T, symbol>, IsEqual<T, Symbol>> extends true ? typeof Symbol : never;
|
|
22
|
-
type NormalizeConstructorToPrimitve<T> = T extends String ? string : T extends Number ? number : T extends Boolean ? boolean : T extends BigInt ? bigint : T extends Symbol ? symbol : T;
|
|
23
22
|
export type SchemaTestable<T = unknown> = Schema<T> | AbstractConstructor<T> | NormalizePrimitiveToConstructor<T>;
|
|
24
|
-
export type SchemaOutput<T extends SchemaTestable> = T extends SchemaTestable<infer U> ?
|
|
23
|
+
export type SchemaOutput<T extends SchemaTestable> = T extends SchemaTestable<infer U> ? U : never;
|
|
24
|
+
export declare const OPTIONAL: unique symbol;
|
|
25
25
|
export declare abstract class Schema<T = unknown> {
|
|
26
|
+
readonly [OPTIONAL]: boolean;
|
|
26
27
|
/**
|
|
27
28
|
* Test an unknown value to see whether it corresponds to the schema.
|
|
28
29
|
* @param schema schema to test against
|
package/schema/schemas/array.js
CHANGED
|
@@ -11,16 +11,18 @@ export class ArraySchema extends Schema {
|
|
|
11
11
|
this.itemSchema = schemaTestableToSchema(itemSchema);
|
|
12
12
|
}
|
|
13
13
|
_test(value, path, options) {
|
|
14
|
-
if (isArray(value)) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
14
|
+
if (!isArray(value)) {
|
|
15
|
+
return { valid: false, error: SchemaError.expectedButGot('array', typeOf(value), path) };
|
|
16
|
+
}
|
|
17
|
+
const values = [];
|
|
18
|
+
for (let i = 0; i < value.length; i++) {
|
|
19
|
+
const result = this.itemSchema._test(value[i], path.add(i), options);
|
|
20
|
+
if (!result.valid) {
|
|
21
|
+
return result;
|
|
20
22
|
}
|
|
21
|
-
|
|
23
|
+
values.push(result.value);
|
|
22
24
|
}
|
|
23
|
-
return { valid:
|
|
25
|
+
return { valid: true, value: values };
|
|
24
26
|
}
|
|
25
27
|
}
|
|
26
28
|
export function array(schema) {
|
|
@@ -6,4 +6,4 @@ export declare class BigIntSchema extends Schema<bigint> {
|
|
|
6
6
|
_test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<bigint>;
|
|
7
7
|
}
|
|
8
8
|
export declare function bigint(): BigIntSchema;
|
|
9
|
-
export declare function
|
|
9
|
+
export declare function BigIntProperty(options?: SchemaPropertyDecoratorOptions): SchemaPropertyDecorator;
|
package/schema/schemas/bigint.js
CHANGED
|
@@ -8,8 +8,4 @@ export declare class BooleanSchema extends SimpleSchema<boolean> {
|
|
|
8
8
|
_test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<boolean>;
|
|
9
9
|
}
|
|
10
10
|
export declare function boolean(options?: BooleanSchemaOptions): BooleanSchema;
|
|
11
|
-
export declare function Boolean(options?: BooleanSchemaOptions & SchemaPropertyDecoratorOptions): SchemaPropertyDecorator;
|
|
12
|
-
/**
|
|
13
|
-
* @deprecated use {@link Boolean()} instead
|
|
14
|
-
*/
|
|
15
11
|
export declare function BooleanProperty(options?: BooleanSchemaOptions & SchemaPropertyDecoratorOptions): SchemaPropertyDecorator;
|
|
@@ -39,12 +39,6 @@ export class BooleanSchema extends SimpleSchema {
|
|
|
39
39
|
export function boolean(options) {
|
|
40
40
|
return new BooleanSchema(options);
|
|
41
41
|
}
|
|
42
|
-
export function Boolean(options) {
|
|
43
|
-
return Property(boolean(options), options);
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* @deprecated use {@link Boolean()} instead
|
|
47
|
-
*/
|
|
48
42
|
export function BooleanProperty(options) {
|
|
49
|
-
return
|
|
43
|
+
return Property(boolean(options), options);
|
|
50
44
|
}
|
package/schema/schemas/date.d.ts
CHANGED
|
@@ -7,4 +7,4 @@ export declare class DateSchema extends SimpleSchema<globalThis.Date> {
|
|
|
7
7
|
constructor(options?: DateSchemaOptions);
|
|
8
8
|
}
|
|
9
9
|
export declare function date(options?: DateSchemaOptions): DateSchema;
|
|
10
|
-
export declare function
|
|
10
|
+
export declare function DateProperty(options?: SchemaPropertyDecoratorOptions & DateSchemaOptions): SchemaPropertyDecorator;
|
package/schema/schemas/date.js
CHANGED
|
@@ -5,4 +5,4 @@ export declare class FunctionSchema extends SimpleSchema<Function> {
|
|
|
5
5
|
constructor(options?: FunctionSchemaOptions);
|
|
6
6
|
}
|
|
7
7
|
export declare function func(): FunctionSchema;
|
|
8
|
-
export declare function
|
|
8
|
+
export declare function FunctionProperty(options?: SchemaPropertyDecoratorOptions): SchemaPropertyDecorator;
|
|
@@ -7,9 +7,5 @@ export declare class NumberSchema extends SimpleSchema<number> {
|
|
|
7
7
|
constructor(options?: NumberSchemaOptions);
|
|
8
8
|
}
|
|
9
9
|
export declare function number(options?: NumberSchemaOptions): NumberSchema;
|
|
10
|
-
export declare function Number(options?: SchemaPropertyDecoratorOptions & NumberSchemaOptions): SchemaPropertyDecorator;
|
|
11
|
-
export declare function Integer(options?: SchemaPropertyDecoratorOptions & NumberSchemaOptions): SchemaPropertyDecorator;
|
|
12
|
-
/**
|
|
13
|
-
* @deprecated use {@link Number()} instead
|
|
14
|
-
*/
|
|
15
10
|
export declare function NumberProperty(options?: SchemaPropertyDecoratorOptions & NumberSchemaOptions): SchemaPropertyDecorator;
|
|
11
|
+
export declare function Integer(options?: SchemaPropertyDecoratorOptions & NumberSchemaOptions): SchemaPropertyDecorator;
|
package/schema/schemas/number.js
CHANGED
|
@@ -24,15 +24,9 @@ export class NumberSchema extends SimpleSchema {
|
|
|
24
24
|
export function number(options) {
|
|
25
25
|
return new NumberSchema(options);
|
|
26
26
|
}
|
|
27
|
-
export function
|
|
27
|
+
export function NumberProperty(options) {
|
|
28
28
|
return Property(number(options), options);
|
|
29
29
|
}
|
|
30
30
|
export function Integer(options) {
|
|
31
31
|
return Property(number({ ...options, integer: true }), options);
|
|
32
32
|
}
|
|
33
|
-
/**
|
|
34
|
-
* @deprecated use {@link Number()} instead
|
|
35
|
-
*/
|
|
36
|
-
export function NumberProperty(options) {
|
|
37
|
-
return Number(options);
|
|
38
|
-
}
|
|
@@ -3,7 +3,7 @@ import type { JsonPath } from '../../json-path/json-path.js';
|
|
|
3
3
|
import type { AbstractConstructor, OneOrMany, PartialProperty, Record as RecordType, SimplifyObject, Type, TypedOmit } from '../../types.js';
|
|
4
4
|
import { type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
|
|
5
5
|
import type { SchemaPropertyDecorator } from '../decorators/types.js';
|
|
6
|
-
import { Schema, type SchemaTestable, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
|
|
6
|
+
import { type OPTIONAL, Schema, type SchemaOutput, type SchemaTestable, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
|
|
7
7
|
export type Record<K extends PropertyKey = PropertyKey, V = any> = RecordType<K, V>;
|
|
8
8
|
export type ObjectSchemaFactoryFunction<T> = (data: T) => T;
|
|
9
9
|
export type ObjectSchemaFactory<T> = {
|
|
@@ -22,6 +22,14 @@ export type ObjectSchemaOptions<T extends Record = Record, K extends PropertyKey
|
|
|
22
22
|
factory?: ObjectSchemaFactory<T> | null;
|
|
23
23
|
};
|
|
24
24
|
export type ObjectSchemaOrType<T extends Record = any> = ObjectSchema<T> | AbstractConstructor<T>;
|
|
25
|
+
export type OptionalProperties<T> = {
|
|
26
|
+
[P in keyof T]: T[P] extends {
|
|
27
|
+
[OPTIONAL]: true;
|
|
28
|
+
} ? P : never;
|
|
29
|
+
}[keyof T];
|
|
30
|
+
export type ObjectSchemaPropertiesType<TP extends ObjectSchemaProperties> = SimplifyObject<{
|
|
31
|
+
[P in keyof PartialProperty<TP, OptionalProperties<TP>>]: SchemaOutput<TP[P]>;
|
|
32
|
+
}>;
|
|
25
33
|
export declare const tryGetSchemaFromReflection: typeof _tryGetSchemaFromReflection;
|
|
26
34
|
export declare class ObjectSchema<T extends Record = Record> extends Schema<T> {
|
|
27
35
|
private readonly propertyKeys;
|
|
@@ -46,21 +54,21 @@ export declare function object<const V>(properties: Record<never>, options: Obje
|
|
|
46
54
|
unknownProperties: SchemaTestable<V>;
|
|
47
55
|
unknownPropertiesKey?: undefined;
|
|
48
56
|
}): ObjectSchema<Record<PropertyKey, V>>;
|
|
49
|
-
export declare function object<const
|
|
57
|
+
export declare function object<const TP extends ObjectSchemaProperties, const K extends PropertyKey, const V>(properties: TP, options: ObjectSchemaOptions<ObjectSchemaPropertiesType<TP> & Record<K, V>> & {
|
|
50
58
|
unknownProperties: SchemaTestable<V>;
|
|
51
59
|
unknownPropertiesKey: SchemaTestable<K>;
|
|
52
|
-
}): ObjectSchema<
|
|
53
|
-
export declare function object<const
|
|
60
|
+
}): ObjectSchema<ObjectSchemaPropertiesType<TP> & Record<K, V>>;
|
|
61
|
+
export declare function object<const TP extends ObjectSchemaProperties, const K extends PropertyKey>(properties: TP, options: ObjectSchemaOptions<ObjectSchemaPropertiesType<TP> & Record<K, unknown>> & {
|
|
54
62
|
unknownPropertiesKey: SchemaTestable<K>;
|
|
55
|
-
}): ObjectSchema<
|
|
56
|
-
export declare function object<const
|
|
63
|
+
}): ObjectSchema<ObjectSchemaPropertiesType<TP> & Record<K, unknown>>;
|
|
64
|
+
export declare function object<const TP extends ObjectSchemaProperties, const V>(properties: TP, options: ObjectSchemaOptions<ObjectSchemaPropertiesType<TP> & Record<PropertyKey, V>> & {
|
|
57
65
|
unknownProperties: SchemaTestable<V>;
|
|
58
|
-
}): ObjectSchema<
|
|
59
|
-
export declare function object<const
|
|
66
|
+
}): ObjectSchema<ObjectSchemaPropertiesType<TP> & Record<PropertyKey, V>>;
|
|
67
|
+
export declare function object<const TP extends ObjectSchemaProperties>(properties: TP, options?: ObjectSchemaOptions<ObjectSchemaPropertiesType<TP>> & {
|
|
60
68
|
unknownProperties?: undefined;
|
|
61
69
|
unknownPropertiesKey?: undefined;
|
|
62
|
-
}): ObjectSchema<
|
|
63
|
-
export declare function object<const
|
|
70
|
+
}): ObjectSchema<ObjectSchemaPropertiesType<TP>>;
|
|
71
|
+
export declare function object<const TP extends ObjectSchemaProperties, const K extends PropertyKey, const V>(properties: TP, options?: ObjectSchemaOptions<ObjectSchemaPropertiesType<TP>, K, V>): ObjectSchema<ObjectSchemaPropertiesType<TP> & Record<K, V>>;
|
|
64
72
|
export declare function explicitObject<const T extends Record>(properties: ObjectSchemaProperties<T>, options?: ObjectSchemaOptions<T>): ObjectSchema<T>;
|
|
65
73
|
export declare function record<const K extends PropertyKey, const V>(key: Schema<K>, value: Schema<V>, options?: TypedOmit<ObjectSchemaOptions<Record<K, V>>, 'unknownProperties' | 'unknownPropertiesKey'>): ObjectSchema<Record<K, V>>;
|
|
66
74
|
export declare function assign<const T1 extends Record, const T2 extends Record>(a: ObjectSchemaOrType<T1>, b: ObjectSchemaOrType<T2>): ObjectSchema<Merge<T1, T2>>;
|
|
@@ -73,7 +81,7 @@ export declare function partial<const T extends Record, const K extends keyof T>
|
|
|
73
81
|
export declare function pick<const T extends Record, const K extends keyof T>(schemaOrType: ObjectSchemaOrType<T>, keys: OneOrMany<K>): ObjectSchema<SimplifyObject<Pick<T, K>>>;
|
|
74
82
|
export declare function omit<const T extends Record, const K extends keyof T>(schemaOrType: ObjectSchemaOrType<T>, keys: OneOrMany<K>): ObjectSchema<SimplifyObject<Omit<T, K>>>;
|
|
75
83
|
export declare function getSchemaFromReflection<T extends Record>(type: AbstractConstructor<T>): Schema<T>;
|
|
76
|
-
declare function _tryGetSchemaFromReflection<T extends Record>(type: AbstractConstructor<T>): Schema<T> | null;
|
|
84
|
+
declare function _tryGetSchemaFromReflection<T extends Record>(type: AbstractConstructor<T>): Schema<T> | ObjectSchema<T> | null;
|
|
77
85
|
export declare function getObjectSchema<T extends Record>(schemaOrType: SchemaTestable<T>): ObjectSchema<T>;
|
|
78
86
|
export declare function Record<K extends PropertyKey, V>(key: Schema<K>, value: Schema<V>, options?: TypedOmit<ObjectSchemaOptions<Record<K, V>>, 'unknownProperties' | 'unknownPropertiesKey'> & SchemaPropertyDecoratorOptions): SchemaPropertyDecorator;
|
|
79
87
|
export declare const emptyObjectSchema: ObjectSchema<EmptyObject>;
|
package/schema/schemas/object.js
CHANGED
|
@@ -155,7 +155,7 @@ function getObjectSchemaPropertiesFromReflection(metadata, type) {
|
|
|
155
155
|
for (const [key, propertyMetadata] of metadata.properties) {
|
|
156
156
|
const reflectionData = propertyMetadata.data.tryGet('schema');
|
|
157
157
|
if (isUndefined(reflectionData?.schema) && (propertyMetadata.type == Object)) {
|
|
158
|
-
throw new Error(`Schema of property "${String(key)}" on type ${type.name} is inferred as Object. This is most likely unwanted and happens when the property is defined as partial or the type is an union. Use an explicit @Property(Object) if wanted.`);
|
|
158
|
+
throw new Error(`Schema of property "${String(key)}" on type ${type.name} is inferred as Object. This is most likely unwanted and happens when the property is defined as partial or the type is an union. Use an explicit @Property(Object) if this is wanted.`);
|
|
159
159
|
}
|
|
160
160
|
let propertySchema = reflectionData?.schema ?? propertyMetadata.type;
|
|
161
161
|
if (reflectionData?.array == true) {
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { JsonPath } from '../../json-path/json-path.js';
|
|
2
|
-
import { TypedOmit } from '../../types.js';
|
|
2
|
+
import type { TypedOmit } from '../../types.js';
|
|
3
3
|
import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
|
|
4
|
-
import { Schema, type SchemaTestable, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
|
|
4
|
+
import { type OPTIONAL, Schema, type SchemaTestable, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
|
|
5
5
|
export declare class OptionalSchema<T> extends Schema<T | undefined> {
|
|
6
|
+
readonly [OPTIONAL]: true;
|
|
6
7
|
readonly schema: Schema<T>;
|
|
7
8
|
constructor(schema: SchemaTestable<T>);
|
|
8
9
|
_test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<T | undefined>;
|
|
@@ -5,4 +5,4 @@ export declare class ReadableStreamSchema extends SimpleSchema<ReadableStream> {
|
|
|
5
5
|
constructor(options?: ReadableStreamSchemaOptions);
|
|
6
6
|
}
|
|
7
7
|
export declare function readableStream(options?: ReadableStreamSchemaOptions): ReadableStreamSchema;
|
|
8
|
-
export declare function
|
|
8
|
+
export declare function ReadableStreamProperty(options?: SchemaPropertyDecoratorOptions & ReadableStreamSchemaOptions): SchemaPropertyDecorator;
|
|
@@ -9,6 +9,6 @@ export class ReadableStreamSchema extends SimpleSchema {
|
|
|
9
9
|
export function readableStream(options) {
|
|
10
10
|
return new ReadableStreamSchema(options);
|
|
11
11
|
}
|
|
12
|
-
export function
|
|
12
|
+
export function ReadableStreamProperty(options) {
|
|
13
13
|
return Property(readableStream(options), options);
|
|
14
14
|
}
|
|
@@ -5,4 +5,4 @@ export declare class RegExpSchema extends SimpleSchema<RegExp> {
|
|
|
5
5
|
constructor(options?: RegExpSchemaOptions);
|
|
6
6
|
}
|
|
7
7
|
export declare function regExp(options?: RegExpSchemaOptions): RegExpSchema;
|
|
8
|
-
export declare function
|
|
8
|
+
export declare function RegExpProperty(options?: SchemaPropertyDecoratorOptions & RegExpSchemaOptions): SchemaPropertyDecorator;
|
package/schema/schemas/regexp.js
CHANGED
|
@@ -7,8 +7,4 @@ export declare class StringSchema extends SimpleSchema<string> {
|
|
|
7
7
|
constructor(options?: StringSchemaOptions);
|
|
8
8
|
}
|
|
9
9
|
export declare function string(options?: StringSchemaOptions): StringSchema;
|
|
10
|
-
export declare function String(options?: SchemaPropertyDecoratorOptions & StringSchemaOptions): SchemaPropertyDecorator;
|
|
11
|
-
/**
|
|
12
|
-
* @deprecated use {@link String()} instead
|
|
13
|
-
*/
|
|
14
10
|
export declare function StringProperty(options?: SchemaPropertyDecoratorOptions & StringSchemaOptions): SchemaPropertyDecorator;
|
package/schema/schemas/string.js
CHANGED
|
@@ -19,12 +19,6 @@ export class StringSchema extends SimpleSchema {
|
|
|
19
19
|
export function string(options) {
|
|
20
20
|
return new StringSchema(options);
|
|
21
21
|
}
|
|
22
|
-
export function String(options) {
|
|
23
|
-
return Property(string(options), options);
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* @deprecated use {@link String()} instead
|
|
27
|
-
*/
|
|
28
22
|
export function StringProperty(options) {
|
|
29
|
-
return
|
|
23
|
+
return Property(string(options), options);
|
|
30
24
|
}
|
|
@@ -5,8 +5,4 @@ export declare class SymbolSchema extends Schema<symbol> {
|
|
|
5
5
|
_test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<symbol>;
|
|
6
6
|
}
|
|
7
7
|
export declare function symbol(): SymbolSchema;
|
|
8
|
-
export declare function Symbol(options?: SchemaPropertyDecoratorOptions): SchemaPropertyDecorator;
|
|
9
|
-
/**
|
|
10
|
-
* @deprecated use {@link Symbol()} instead
|
|
11
|
-
*/
|
|
12
8
|
export declare function SymbolProperty(options?: SchemaPropertyDecoratorOptions): SchemaPropertyDecorator;
|
package/schema/schemas/symbol.js
CHANGED
|
@@ -14,12 +14,6 @@ export class SymbolSchema extends Schema {
|
|
|
14
14
|
export function symbol() {
|
|
15
15
|
return new SymbolSchema();
|
|
16
16
|
}
|
|
17
|
-
export function Symbol(options) {
|
|
18
|
-
return Property(symbol(), options);
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* @deprecated use {@link Symbol()} instead
|
|
22
|
-
*/
|
|
23
17
|
export function SymbolProperty(options) {
|
|
24
|
-
return
|
|
18
|
+
return Property(symbol(), options);
|
|
25
19
|
}
|
|
@@ -11,4 +11,4 @@ export declare class Uint8ArraySchema extends SimpleSchema<Uint8Array> {
|
|
|
11
11
|
constructor(options?: Uint8ArraySchemaOptions);
|
|
12
12
|
}
|
|
13
13
|
export declare function uint8Array(options?: Uint8ArraySchemaOptions): Uint8ArraySchema;
|
|
14
|
-
export declare function
|
|
14
|
+
export declare function Uint8ArrayProperty(schemaOptions?: Uint8ArraySchemaOptions, options?: SchemaPropertyDecoratorOptions): SchemaPropertyDecorator;
|
|
@@ -6,8 +6,8 @@ export class Uint8ArraySchema extends SimpleSchema {
|
|
|
6
6
|
constructor(options) {
|
|
7
7
|
super('Uint8Array', isUint8Array, options, {
|
|
8
8
|
constraints: [
|
|
9
|
-
isDefined(options?.minimumLength) ? (value) => (value.byteLength >= options.minimumLength) ? ({ success: true }) : ({ success: false, error: `
|
|
10
|
-
isDefined(options?.maximumLength) ? (value) => (value.byteLength <= options.maximumLength) ? ({ success: true }) : ({ success: false, error: `
|
|
9
|
+
isDefined(options?.minimumLength) ? (value) => (value.byteLength >= options.minimumLength) ? ({ success: true }) : ({ success: false, error: `Size must be at least ${options.minimumLength} bytes.` }) : null,
|
|
10
|
+
isDefined(options?.maximumLength) ? (value) => (value.byteLength <= options.maximumLength) ? ({ success: true }) : ({ success: false, error: `Size must be at most ${options.maximumLength} bytes.` }) : null
|
|
11
11
|
]
|
|
12
12
|
});
|
|
13
13
|
}
|
|
@@ -15,6 +15,6 @@ export class Uint8ArraySchema extends SimpleSchema {
|
|
|
15
15
|
export function uint8Array(options) {
|
|
16
16
|
return new Uint8ArraySchema(options);
|
|
17
17
|
}
|
|
18
|
-
export function
|
|
18
|
+
export function Uint8ArrayProperty(schemaOptions, options) {
|
|
19
19
|
return Property(uint8Array(schemaOptions), options);
|
|
20
20
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import type { AnyIterable } from '../any-iterable-iterator.js';
|
|
2
|
-
import type { AsyncIteratorFunction } from './types.js';
|
|
2
|
+
import type { AsyncGroupSelectors, AsyncIteratorFunction } from './types.js';
|
|
3
3
|
export declare function groupSingleAsync<TIn, TGroup>(iterable: AnyIterable<TIn>, selector: AsyncIteratorFunction<TIn, TGroup>): AsyncIterableIterator<[TGroup, TIn]>;
|
|
4
|
+
export declare function groupSingleAsync<TIn, TGroup extends [any, ...any[]]>(iterable: AnyIterable<TIn>, ...selectors: AsyncGroupSelectors<TIn, TGroup>): AsyncIterableIterator<[TGroup['length'] extends 1 ? TGroup[0] : TGroup, TIn]>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { groupToSingleMapAsync } from './group-to-single-map.js';
|
|
2
|
-
export async function* groupSingleAsync(iterable,
|
|
3
|
-
yield* await groupToSingleMapAsync(iterable,
|
|
2
|
+
export async function* groupSingleAsync(iterable, ...selectors) {
|
|
3
|
+
yield* await groupToSingleMapAsync(iterable, ...selectors);
|
|
4
4
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import type { AnyIterable } from '../any-iterable-iterator.js';
|
|
2
|
-
import type { AsyncIteratorFunction } from './types.js';
|
|
2
|
+
import type { AsyncGroupSelectors, AsyncIteratorFunction } from './types.js';
|
|
3
3
|
export declare function groupToMapAsync<TIn, TGroup>(iterable: AnyIterable<TIn>, selector: AsyncIteratorFunction<TIn, TGroup>): Promise<Map<TGroup, TIn[]>>;
|
|
4
|
+
export declare function groupToMapAsync<TIn, TGroup extends [any, ...any[]]>(iterable: AnyIterable<TIn>, ...selectors: AsyncGroupSelectors<TIn, TGroup>): Promise<Map<TGroup['length'] extends 1 ? TGroup[0] : TGroup, TIn[]>>;
|
|
@@ -1,30 +1,57 @@
|
|
|
1
|
+
import { MultiKeyMap } from '../../data-structures/multi-key-map.js';
|
|
1
2
|
import { isAsyncIterable } from './is-async-iterable.js';
|
|
2
|
-
export async function groupToMapAsync(iterable,
|
|
3
|
+
export async function groupToMapAsync(iterable, ...selectors) {
|
|
3
4
|
return isAsyncIterable(iterable)
|
|
4
|
-
? async(iterable,
|
|
5
|
-
: sync(iterable,
|
|
5
|
+
? async(iterable, ...selectors)
|
|
6
|
+
: sync(iterable, ...selectors);
|
|
6
7
|
}
|
|
7
|
-
async function async(iterable,
|
|
8
|
-
const map = new Map();
|
|
8
|
+
async function async(iterable, ...selectors) {
|
|
9
9
|
let index = 0;
|
|
10
|
+
if (selectors.length == 1) {
|
|
11
|
+
const selector = selectors[0];
|
|
12
|
+
const map = new Map();
|
|
13
|
+
for await (const item of iterable) {
|
|
14
|
+
const groupKey = await selector(item, index++);
|
|
15
|
+
if (!map.has(groupKey)) {
|
|
16
|
+
map.set(groupKey, []);
|
|
17
|
+
}
|
|
18
|
+
map.get(groupKey).push(item);
|
|
19
|
+
}
|
|
20
|
+
return map;
|
|
21
|
+
}
|
|
22
|
+
const map = new MultiKeyMap();
|
|
10
23
|
for await (const item of iterable) {
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
24
|
+
const groupKeys = await Promise.all(selectors.map(async (selector) => selector(item, index))); // eslint-disable-line @typescript-eslint/no-loop-func
|
|
25
|
+
index++;
|
|
26
|
+
if (!map.has(groupKeys)) {
|
|
27
|
+
map.set(groupKeys, []);
|
|
14
28
|
}
|
|
15
|
-
map.get(
|
|
29
|
+
map.get(groupKeys).push(item);
|
|
16
30
|
}
|
|
17
|
-
return map;
|
|
31
|
+
return map.toMap();
|
|
18
32
|
}
|
|
19
|
-
async function sync(iterable,
|
|
20
|
-
const map = new Map();
|
|
33
|
+
async function sync(iterable, ...selectors) {
|
|
21
34
|
let index = 0;
|
|
35
|
+
if (selectors.length == 1) {
|
|
36
|
+
const selector = selectors[0];
|
|
37
|
+
const map = new Map();
|
|
38
|
+
for (const item of iterable) {
|
|
39
|
+
const groupKey = await selector(item, index++);
|
|
40
|
+
if (!map.has(groupKey)) {
|
|
41
|
+
map.set(groupKey, []);
|
|
42
|
+
}
|
|
43
|
+
map.get(groupKey).push(item);
|
|
44
|
+
}
|
|
45
|
+
return map;
|
|
46
|
+
}
|
|
47
|
+
const map = new MultiKeyMap();
|
|
22
48
|
for (const item of iterable) {
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
49
|
+
const groupKeys = await Promise.all(selectors.map(async (selector) => selector(item, index))); // eslint-disable-line @typescript-eslint/no-loop-func
|
|
50
|
+
index++;
|
|
51
|
+
if (!map.has(groupKeys)) {
|
|
52
|
+
map.set(groupKeys, []);
|
|
26
53
|
}
|
|
27
|
-
map.get(
|
|
54
|
+
map.get(groupKeys).push(item);
|
|
28
55
|
}
|
|
29
|
-
return map;
|
|
56
|
+
return map.toMap();
|
|
30
57
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import type { AnyIterable } from '../any-iterable-iterator.js';
|
|
2
|
-
import type { AsyncIteratorFunction } from './types.js';
|
|
2
|
+
import type { AsyncGroupSelectors, AsyncIteratorFunction } from './types.js';
|
|
3
3
|
export declare function groupToSingleMapAsync<TIn, TGroup>(iterable: AnyIterable<TIn>, selector: AsyncIteratorFunction<TIn, TGroup>): Promise<Map<TGroup, TIn>>;
|
|
4
|
+
export declare function groupToSingleMapAsync<TIn, TGroup extends [any, ...any[]]>(iterable: AnyIterable<TIn>, ...selectors: AsyncGroupSelectors<TIn, TGroup>): Promise<Map<TGroup['length'] extends 1 ? TGroup[0] : TGroup, TIn>>;
|
|
@@ -1,30 +1,57 @@
|
|
|
1
|
+
import { MultiKeyMap } from '../../data-structures/multi-key-map.js';
|
|
1
2
|
import { isAsyncIterable } from './is-async-iterable.js';
|
|
2
|
-
export async function groupToSingleMapAsync(iterable,
|
|
3
|
+
export async function groupToSingleMapAsync(iterable, ...selectors) {
|
|
3
4
|
return isAsyncIterable(iterable)
|
|
4
|
-
? async(iterable,
|
|
5
|
-
: sync(iterable,
|
|
5
|
+
? async(iterable, ...selectors)
|
|
6
|
+
: sync(iterable, ...selectors);
|
|
6
7
|
}
|
|
7
|
-
async function async(iterable,
|
|
8
|
-
const map = new Map();
|
|
8
|
+
async function async(iterable, ...selectors) {
|
|
9
9
|
let index = 0;
|
|
10
|
+
if (selectors.length == 1) {
|
|
11
|
+
const selector = selectors[0];
|
|
12
|
+
const map = new Map();
|
|
13
|
+
for await (const item of iterable) {
|
|
14
|
+
const groupKey = await selector(item, index++);
|
|
15
|
+
if (map.has(groupKey)) {
|
|
16
|
+
throw new Error(`Group ${String(groupKey)} has more than one item.`);
|
|
17
|
+
}
|
|
18
|
+
map.set(groupKey, item);
|
|
19
|
+
}
|
|
20
|
+
return map;
|
|
21
|
+
}
|
|
22
|
+
const map = new MultiKeyMap();
|
|
10
23
|
for await (const item of iterable) {
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
24
|
+
const groupKeys = await Promise.all(selectors.map(async (selector) => selector(item, index))); // eslint-disable-line @typescript-eslint/no-loop-func
|
|
25
|
+
index++;
|
|
26
|
+
if (map.has(groupKeys)) {
|
|
27
|
+
throw new Error(`Group [${groupKeys.map((key) => String(key)).join(', ')}] has more than one item.`);
|
|
14
28
|
}
|
|
15
|
-
map.set(
|
|
29
|
+
map.set(groupKeys, item);
|
|
16
30
|
}
|
|
17
|
-
return map;
|
|
31
|
+
return map.toMap();
|
|
18
32
|
}
|
|
19
|
-
async function sync(iterable,
|
|
20
|
-
const map = new Map();
|
|
33
|
+
async function sync(iterable, ...selectors) {
|
|
21
34
|
let index = 0;
|
|
35
|
+
if (selectors.length == 1) {
|
|
36
|
+
const selector = selectors[0];
|
|
37
|
+
const map = new Map();
|
|
38
|
+
for (const item of iterable) {
|
|
39
|
+
const groupKey = await selector(item, index++);
|
|
40
|
+
if (map.has(groupKey)) {
|
|
41
|
+
throw new Error(`Group ${String(groupKey)} has more than one item.`);
|
|
42
|
+
}
|
|
43
|
+
map.set(groupKey, item);
|
|
44
|
+
}
|
|
45
|
+
return map;
|
|
46
|
+
}
|
|
47
|
+
const map = new MultiKeyMap();
|
|
22
48
|
for (const item of iterable) {
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
49
|
+
const groupKeys = await Promise.all(selectors.map(async (selector) => selector(item, index))); // eslint-disable-line @typescript-eslint/no-loop-func
|
|
50
|
+
index++;
|
|
51
|
+
if (map.has(groupKeys)) {
|
|
52
|
+
throw new Error(`Group [${groupKeys.map((key) => String(key)).join(', ')}] has more than one item.`);
|
|
26
53
|
}
|
|
27
|
-
map.set(
|
|
54
|
+
map.set(groupKeys, item);
|
|
28
55
|
}
|
|
29
|
-
return map;
|
|
56
|
+
return map.toMap();
|
|
30
57
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import type { AnyIterable } from '../any-iterable-iterator.js';
|
|
2
|
-
import type { AsyncIteratorFunction } from './types.js';
|
|
2
|
+
import type { AsyncGroupSelectors, AsyncIteratorFunction } from './types.js';
|
|
3
3
|
export declare function groupAsync<TIn, TGroup>(iterable: AnyIterable<TIn>, selector: AsyncIteratorFunction<TIn, TGroup>): AsyncIterableIterator<[TGroup, TIn[]]>;
|
|
4
|
+
export declare function groupAsync<TIn, TGroup extends [any, ...any[]]>(iterable: AnyIterable<TIn>, ...selectors: AsyncGroupSelectors<TIn, TGroup>): AsyncIterableIterator<[TGroup['length'] extends 1 ? TGroup[0] : TGroup, TIn[]]>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { groupToMapAsync } from './group-to-map.js';
|
|
2
|
-
export async function* groupAsync(iterable,
|
|
3
|
-
yield* await groupToMapAsync(iterable,
|
|
2
|
+
export async function* groupAsync(iterable, ...selectors) {
|
|
3
|
+
yield* await groupToMapAsync(iterable, ...selectors);
|
|
4
4
|
}
|
|
@@ -3,6 +3,9 @@ export type AsyncIteratorFunction<TIn, TOut> = (item: TIn, index: number) => TOu
|
|
|
3
3
|
export type AsyncPredicate<T> = (item: T, index: number) => boolean | Promise<boolean>;
|
|
4
4
|
export type AsyncRetryPredicate<T> = (error: Error, item: T, index: number) => boolean | Promise<boolean>;
|
|
5
5
|
export type AsyncReducer<T, U> = (previous: U, current: T, index: number) => U | Promise<U>;
|
|
6
|
+
export type AsyncGroupSelectors<T, TGroup extends any[]> = {
|
|
7
|
+
[P in keyof TGroup]: AsyncIteratorFunction<T, TGroup[P]>;
|
|
8
|
+
};
|
|
6
9
|
export type ParallelizableIteratorFunction<TIn, TOut> = (item: TIn, index: number) => Promise<TOut>;
|
|
7
10
|
export type ParallelizablePredicate<T> = ParallelizableIteratorFunction<T, boolean>;
|
|
8
11
|
export type ThrottleFunction = () => Promise<void>;
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import type { IteratorFunction } from './types.js';
|
|
1
|
+
import type { GroupSelectors, IteratorFunction } from './types.js';
|
|
2
2
|
export declare function groupSingle<TIn, TGroup>(iterable: Iterable<TIn>, selector: IteratorFunction<TIn, TGroup>): IterableIterator<[TGroup, TIn]>;
|
|
3
|
+
export declare function groupSingle<TIn, TGroup extends [any, ...any[]]>(iterable: Iterable<TIn>, ...selectors: GroupSelectors<TIn, TGroup>): IterableIterator<[TGroup['length'] extends 1 ? TGroup[0] : TGroup, TIn]>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { groupToSingleMap } from './group-to-single-map.js';
|
|
2
|
-
export function groupSingle(iterable,
|
|
3
|
-
return groupToSingleMap(iterable,
|
|
2
|
+
export function groupSingle(iterable, ...selectors) {
|
|
3
|
+
return groupToSingleMap(iterable, ...selectors)[Symbol.iterator]();
|
|
4
4
|
}
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import type { IteratorFunction } from './types.js';
|
|
1
|
+
import type { GroupSelectors, IteratorFunction } from './types.js';
|
|
2
2
|
export declare function groupToMap<TIn, TGroup>(iterable: Iterable<TIn>, selector: IteratorFunction<TIn, TGroup>): Map<TGroup, TIn[]>;
|
|
3
|
+
export declare function groupToMap<TIn, TGroup extends [any, ...any[]]>(iterable: Iterable<TIn>, ...selectors: GroupSelectors<TIn, TGroup>): Map<TGroup['length'] extends 1 ? TGroup[0] : TGroup, TIn[]>;
|
|
@@ -1,12 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { MultiKeyMap } from '../../data-structures/multi-key-map.js';
|
|
2
|
+
export function groupToMap(iterable, ...selectors) {
|
|
3
3
|
let index = 0;
|
|
4
|
+
if (selectors.length == 1) {
|
|
5
|
+
const selector = selectors[0];
|
|
6
|
+
const map = new Map();
|
|
7
|
+
for (const item of iterable) {
|
|
8
|
+
const groupKey = selector(item, index++);
|
|
9
|
+
if (!map.has(groupKey)) {
|
|
10
|
+
map.set(groupKey, []);
|
|
11
|
+
}
|
|
12
|
+
map.get(groupKey).push(item);
|
|
13
|
+
}
|
|
14
|
+
return map;
|
|
15
|
+
}
|
|
16
|
+
const map = new MultiKeyMap();
|
|
4
17
|
for (const item of iterable) {
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
18
|
+
const groupKeys = selectors.map((selector) => selector(item, index)); // eslint-disable-line @typescript-eslint/no-loop-func
|
|
19
|
+
index++;
|
|
20
|
+
if (!map.has(groupKeys)) {
|
|
21
|
+
map.set(groupKeys, []);
|
|
8
22
|
}
|
|
9
|
-
map.get(
|
|
23
|
+
map.get(groupKeys).push(item);
|
|
10
24
|
}
|
|
11
|
-
return map;
|
|
25
|
+
return map.toMap();
|
|
12
26
|
}
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import type { IteratorFunction } from './types.js';
|
|
1
|
+
import type { GroupSelectors, IteratorFunction } from './types.js';
|
|
2
2
|
export declare function groupToSingleMap<TIn, TGroup>(iterable: Iterable<TIn>, selector: IteratorFunction<TIn, TGroup>): Map<TGroup, TIn>;
|
|
3
|
+
export declare function groupToSingleMap<TIn, TGroup extends [any, ...any[]]>(iterable: Iterable<TIn>, ...selectors: GroupSelectors<TIn, TGroup>): Map<TGroup['length'] extends 1 ? TGroup[0] : TGroup, TIn>;
|
|
@@ -1,12 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { MultiKeyMap } from '../../data-structures/multi-key-map.js';
|
|
2
|
+
export function groupToSingleMap(iterable, ...selectors) {
|
|
3
3
|
let index = 0;
|
|
4
|
+
if (selectors.length == 1) {
|
|
5
|
+
const selector = selectors[0];
|
|
6
|
+
const map = new Map();
|
|
7
|
+
for (const item of iterable) {
|
|
8
|
+
const groupKey = selector(item, index++);
|
|
9
|
+
if (map.has(groupKey)) {
|
|
10
|
+
throw new Error(`Group ${String(groupKey)} has more than one item.`);
|
|
11
|
+
}
|
|
12
|
+
map.set(groupKey, item);
|
|
13
|
+
}
|
|
14
|
+
return map;
|
|
15
|
+
}
|
|
16
|
+
const map = new MultiKeyMap();
|
|
4
17
|
for (const item of iterable) {
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
18
|
+
const groupKeys = selectors.map((selector) => selector(item, index)); // eslint-disable-line @typescript-eslint/no-loop-func
|
|
19
|
+
index++;
|
|
20
|
+
if (map.has(groupKeys)) {
|
|
21
|
+
throw new Error(`Group [${groupKeys.map((key) => String(key)).join(', ')}] has more than one item.`);
|
|
8
22
|
}
|
|
9
|
-
map.set(
|
|
23
|
+
map.set(groupKeys, item);
|
|
10
24
|
}
|
|
11
|
-
return map;
|
|
25
|
+
return map.toMap();
|
|
12
26
|
}
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import type { IteratorFunction } from './types.js';
|
|
1
|
+
import type { GroupSelectors, IteratorFunction } from './types.js';
|
|
2
2
|
export declare function group<TIn, TGroup>(iterable: Iterable<TIn>, selector: IteratorFunction<TIn, TGroup>): IterableIterator<[TGroup, TIn[]]>;
|
|
3
|
+
export declare function group<TIn, TGroup extends [any, ...any[]]>(iterable: Iterable<TIn>, ...selectors: GroupSelectors<TIn, TGroup>): IterableIterator<[TGroup['length'] extends 1 ? TGroup[0] : TGroup, TIn[]]>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { groupToMap } from './group-to-map.js';
|
|
2
|
-
export function group(iterable,
|
|
3
|
-
return groupToMap(iterable,
|
|
2
|
+
export function group(iterable, ...selectors) {
|
|
3
|
+
return groupToMap(iterable, ...selectors)[Symbol.iterator]();
|
|
4
4
|
}
|