metano-runtime 0.1.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/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/system/collections/hash-set.d.ts +29 -0
- package/dist/system/collections/hash-set.js +138 -0
- package/dist/system/collections/immutable-collection.d.ts +31 -0
- package/dist/system/collections/immutable-collection.js +58 -0
- package/dist/system/collections/index.d.ts +3 -0
- package/dist/system/collections/index.js +3 -0
- package/dist/system/collections/list-helpers.d.ts +43 -0
- package/dist/system/collections/list-helpers.js +58 -0
- package/dist/system/equality.d.ts +38 -0
- package/dist/system/equality.js +178 -0
- package/dist/system/hash-code.d.ts +35 -0
- package/dist/system/hash-code.js +177 -0
- package/dist/system/index.d.ts +6 -0
- package/dist/system/index.js +6 -0
- package/dist/system/json/converters.d.ts +22 -0
- package/dist/system/json/converters.js +261 -0
- package/dist/system/json/index.d.ts +5 -0
- package/dist/system/json/index.js +4 -0
- package/dist/system/json/json-serializer.d.ts +39 -0
- package/dist/system/json/json-serializer.js +57 -0
- package/dist/system/json/property-naming-policy.d.ts +14 -0
- package/dist/system/json/property-naming-policy.js +116 -0
- package/dist/system/json/serializer-context.d.ts +30 -0
- package/dist/system/json/serializer-context.js +38 -0
- package/dist/system/json/types.d.ts +85 -0
- package/dist/system/json/types.js +1 -0
- package/dist/system/linq/append-enumerable.d.ts +8 -0
- package/dist/system/linq/append-enumerable.js +15 -0
- package/dist/system/linq/compare-keys.d.ts +7 -0
- package/dist/system/linq/compare-keys.js +31 -0
- package/dist/system/linq/concat-enumerable.d.ts +8 -0
- package/dist/system/linq/concat-enumerable.js +15 -0
- package/dist/system/linq/distinct-by-enumerable.d.ts +8 -0
- package/dist/system/linq/distinct-by-enumerable.js +21 -0
- package/dist/system/linq/distinct-enumerable.d.ts +7 -0
- package/dist/system/linq/distinct-enumerable.js +19 -0
- package/dist/system/linq/enumerable-base.d.ts +98 -0
- package/dist/system/linq/enumerable-base.js +306 -0
- package/dist/system/linq/enumerable.d.ts +16 -0
- package/dist/system/linq/enumerable.js +33 -0
- package/dist/system/linq/except-enumerable.d.ts +8 -0
- package/dist/system/linq/except-enumerable.js +22 -0
- package/dist/system/linq/group-by-enumerable.d.ts +8 -0
- package/dist/system/linq/group-by-enumerable.js +31 -0
- package/dist/system/linq/index.d.ts +23 -0
- package/dist/system/linq/index.js +70 -0
- package/dist/system/linq/intersect-enumerable.d.ts +8 -0
- package/dist/system/linq/intersect-enumerable.js +22 -0
- package/dist/system/linq/order-by-enumerable.d.ts +9 -0
- package/dist/system/linq/order-by-enumerable.js +22 -0
- package/dist/system/linq/prepend-enumerable.d.ts +8 -0
- package/dist/system/linq/prepend-enumerable.js +15 -0
- package/dist/system/linq/reverse-enumerable.d.ts +7 -0
- package/dist/system/linq/reverse-enumerable.js +15 -0
- package/dist/system/linq/select-enumerable.d.ts +8 -0
- package/dist/system/linq/select-enumerable.js +17 -0
- package/dist/system/linq/select-many-enumerable.d.ts +8 -0
- package/dist/system/linq/select-many-enumerable.js +17 -0
- package/dist/system/linq/skip-enumerable.d.ts +8 -0
- package/dist/system/linq/skip-enumerable.js +21 -0
- package/dist/system/linq/skip-while-enumerable.d.ts +8 -0
- package/dist/system/linq/skip-while-enumerable.js +21 -0
- package/dist/system/linq/source-enumerable.d.ts +7 -0
- package/dist/system/linq/source-enumerable.js +12 -0
- package/dist/system/linq/take-enumerable.d.ts +8 -0
- package/dist/system/linq/take-enumerable.js +20 -0
- package/dist/system/linq/take-while-enumerable.d.ts +8 -0
- package/dist/system/linq/take-while-enumerable.js +19 -0
- package/dist/system/linq/then-by-enumerable.d.ts +9 -0
- package/dist/system/linq/then-by-enumerable.js +61 -0
- package/dist/system/linq/union-enumerable.d.ts +8 -0
- package/dist/system/linq/union-enumerable.js +27 -0
- package/dist/system/linq/where-enumerable.d.ts +8 -0
- package/dist/system/linq/where-enumerable.js +18 -0
- package/dist/system/linq/zip-enumerable.d.ts +9 -0
- package/dist/system/linq/zip-enumerable.js +24 -0
- package/dist/system/temporal-helpers.d.ts +10 -0
- package/dist/system/temporal-helpers.js +12 -0
- package/dist/type-checking/index.d.ts +1 -0
- package/dist/type-checking/index.js +1 -0
- package/dist/type-checking/primitive-type-checks.d.ts +33 -0
- package/dist/type-checking/primitive-type-checks.js +74 -0
- package/package.json +44 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Naming policy for converting C# PascalCase property names to JSON wire names.
|
|
3
|
+
*
|
|
4
|
+
* The transpiler pre-computes all JSON names at compile time, so this class is
|
|
5
|
+
* only needed when constructing a SerializerContext manually at runtime.
|
|
6
|
+
*/
|
|
7
|
+
export class PropertyNamingPolicy {
|
|
8
|
+
static camelCase;
|
|
9
|
+
static snakeCaseLower;
|
|
10
|
+
static snakeCaseUpper;
|
|
11
|
+
static kebabCaseLower;
|
|
12
|
+
static kebabCaseUpper;
|
|
13
|
+
}
|
|
14
|
+
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
15
|
+
/**
|
|
16
|
+
* Splits a PascalCase or camelCase name into words. Handles:
|
|
17
|
+
* - Uppercase transitions: "FirstName" → ["First", "Name"]
|
|
18
|
+
* - Acronyms: "HTMLParser" → ["HTML", "Parser"]
|
|
19
|
+
* - Numbers: "Item2Count" → ["Item", "2", "Count"]
|
|
20
|
+
*/
|
|
21
|
+
function splitWords(name) {
|
|
22
|
+
const words = [];
|
|
23
|
+
let current = "";
|
|
24
|
+
for (let i = 0; i < name.length; i++) {
|
|
25
|
+
const ch = name[i];
|
|
26
|
+
const next = name[i + 1];
|
|
27
|
+
if (i > 0 && isUpperCase(ch)) {
|
|
28
|
+
if (!isUpperCase(name[i - 1])) {
|
|
29
|
+
words.push(current);
|
|
30
|
+
current = ch;
|
|
31
|
+
}
|
|
32
|
+
else if (next && !isUpperCase(next) && !isDigit(next)) {
|
|
33
|
+
words.push(current);
|
|
34
|
+
current = ch;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
current += ch;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else if (i > 0 && isDigit(ch) && !isDigit(name[i - 1])) {
|
|
41
|
+
words.push(current);
|
|
42
|
+
current = ch;
|
|
43
|
+
}
|
|
44
|
+
else if (i > 0 && !isDigit(ch) && isDigit(name[i - 1])) {
|
|
45
|
+
words.push(current);
|
|
46
|
+
current = ch;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
current += ch;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (current) {
|
|
53
|
+
words.push(current);
|
|
54
|
+
}
|
|
55
|
+
return words;
|
|
56
|
+
}
|
|
57
|
+
function isUpperCase(ch) {
|
|
58
|
+
return ch >= "A" && ch <= "Z";
|
|
59
|
+
}
|
|
60
|
+
function isDigit(ch) {
|
|
61
|
+
return ch >= "0" && ch <= "9";
|
|
62
|
+
}
|
|
63
|
+
// ─── Policy Implementations ─────────────────────────────────────────────────
|
|
64
|
+
class CamelCasePolicy extends PropertyNamingPolicy {
|
|
65
|
+
convert(name) {
|
|
66
|
+
if (!name)
|
|
67
|
+
return name;
|
|
68
|
+
let i = 0;
|
|
69
|
+
while (i < name.length && isUpperCase(name[i])) {
|
|
70
|
+
i++;
|
|
71
|
+
}
|
|
72
|
+
if (i === 0)
|
|
73
|
+
return name;
|
|
74
|
+
if (i === 1)
|
|
75
|
+
return name[0].toLowerCase() + name.slice(1);
|
|
76
|
+
if (i === name.length)
|
|
77
|
+
return name.toLowerCase(); // all uppercase: "ID" → "id"
|
|
78
|
+
return name.slice(0, i - 1).toLowerCase() + name.slice(i - 1);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
class SnakeCaseLowerPolicy extends PropertyNamingPolicy {
|
|
82
|
+
convert(name) {
|
|
83
|
+
return splitWords(name)
|
|
84
|
+
.map((w) => w.toLowerCase())
|
|
85
|
+
.join("_");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
class SnakeCaseUpperPolicy extends PropertyNamingPolicy {
|
|
89
|
+
convert(name) {
|
|
90
|
+
return splitWords(name)
|
|
91
|
+
.map((w) => w.toUpperCase())
|
|
92
|
+
.join("_");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
class KebabCaseLowerPolicy extends PropertyNamingPolicy {
|
|
96
|
+
convert(name) {
|
|
97
|
+
return splitWords(name)
|
|
98
|
+
.map((w) => w.toLowerCase())
|
|
99
|
+
.join("-");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
class KebabCaseUpperPolicy extends PropertyNamingPolicy {
|
|
103
|
+
convert(name) {
|
|
104
|
+
return splitWords(name)
|
|
105
|
+
.map((w) => w.toUpperCase())
|
|
106
|
+
.join("-");
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// ─── Static Initialization ──────────────────────────────────────────────────
|
|
110
|
+
// Assigned after subclasses are defined to avoid TDZ issues.
|
|
111
|
+
const pnp = PropertyNamingPolicy;
|
|
112
|
+
pnp.camelCase = new CamelCasePolicy();
|
|
113
|
+
pnp.snakeCaseLower = new SnakeCaseLowerPolicy();
|
|
114
|
+
pnp.snakeCaseUpper = new SnakeCaseUpperPolicy();
|
|
115
|
+
pnp.kebabCaseLower = new KebabCaseLowerPolicy();
|
|
116
|
+
pnp.kebabCaseUpper = new KebabCaseUpperPolicy();
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { TypeSpec, SerializerContextOptions, JsonConverter } from "./types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Base class for generated serializer contexts. Each context holds options
|
|
4
|
+
* (including custom converters) and a registry of type specs for lookup.
|
|
5
|
+
*
|
|
6
|
+
* Generated subclasses expose lazy getters per type (e.g., `get order()`).
|
|
7
|
+
*/
|
|
8
|
+
export declare abstract class SerializerContext {
|
|
9
|
+
readonly options: SerializerContextOptions;
|
|
10
|
+
private readonly _registry;
|
|
11
|
+
constructor(options?: SerializerContextOptions);
|
|
12
|
+
/** The custom converters from the context options, if any. */
|
|
13
|
+
get converters(): readonly JsonConverter[] | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Registers a spec in the internal registry and returns it. Intended to
|
|
16
|
+
* be called from lazy getters in generated subclasses:
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* get order(): TypeSpec<Order> {
|
|
20
|
+
* return this._order ??= this.createSpec({ type: Order, ... });
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
protected createSpec<T>(spec: TypeSpec<T>): TypeSpec<T>;
|
|
25
|
+
/**
|
|
26
|
+
* Looks up a spec by constructor reference. Returns undefined if the type
|
|
27
|
+
* was not registered via createSpec().
|
|
28
|
+
*/
|
|
29
|
+
resolve<T>(type: abstract new (...args: any[]) => T): TypeSpec<T> | undefined;
|
|
30
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base class for generated serializer contexts. Each context holds options
|
|
3
|
+
* (including custom converters) and a registry of type specs for lookup.
|
|
4
|
+
*
|
|
5
|
+
* Generated subclasses expose lazy getters per type (e.g., `get order()`).
|
|
6
|
+
*/
|
|
7
|
+
export class SerializerContext {
|
|
8
|
+
options;
|
|
9
|
+
_registry = new Map();
|
|
10
|
+
constructor(options) {
|
|
11
|
+
this.options = options ?? {};
|
|
12
|
+
}
|
|
13
|
+
/** The custom converters from the context options, if any. */
|
|
14
|
+
get converters() {
|
|
15
|
+
return this.options.converters;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Registers a spec in the internal registry and returns it. Intended to
|
|
19
|
+
* be called from lazy getters in generated subclasses:
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* get order(): TypeSpec<Order> {
|
|
23
|
+
* return this._order ??= this.createSpec({ type: Order, ... });
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
createSpec(spec) {
|
|
28
|
+
this._registry.set(spec.type, spec);
|
|
29
|
+
return spec;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Looks up a spec by constructor reference. Returns undefined if the type
|
|
33
|
+
* was not registered via createSpec().
|
|
34
|
+
*/
|
|
35
|
+
resolve(type) {
|
|
36
|
+
return this._registry.get(type);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/** Primitives that JSON.stringify handles natively (string, number, boolean) */
|
|
2
|
+
export type PrimitiveDescriptor = {
|
|
3
|
+
readonly kind: "primitive";
|
|
4
|
+
};
|
|
5
|
+
/** Another spec — recursive serialize/deserialize */
|
|
6
|
+
export type RefDescriptor<T = unknown> = {
|
|
7
|
+
readonly kind: "ref";
|
|
8
|
+
readonly spec: () => TypeSpec<T>;
|
|
9
|
+
};
|
|
10
|
+
/** Temporal types — toString() to serialize, parse() to deserialize */
|
|
11
|
+
export type TemporalDescriptor = {
|
|
12
|
+
readonly kind: "temporal";
|
|
13
|
+
readonly parse: (iso: string) => unknown;
|
|
14
|
+
};
|
|
15
|
+
/** decimal.js Decimal — toNumber() by default, overridable via custom converter */
|
|
16
|
+
export type DecimalDescriptor = {
|
|
17
|
+
readonly kind: "decimal";
|
|
18
|
+
};
|
|
19
|
+
/** Map<K,V> — Object.fromEntries on serialize, new Map(Object.entries) on deserialize */
|
|
20
|
+
export type MapDescriptor = {
|
|
21
|
+
readonly kind: "map";
|
|
22
|
+
readonly key: TypeDescriptor;
|
|
23
|
+
readonly value: TypeDescriptor;
|
|
24
|
+
};
|
|
25
|
+
/** Array<T> */
|
|
26
|
+
export type ArrayDescriptor = {
|
|
27
|
+
readonly kind: "array";
|
|
28
|
+
readonly element: TypeDescriptor;
|
|
29
|
+
};
|
|
30
|
+
/** HashSet<T> — spread to array on serialize, reconstruct on deserialize */
|
|
31
|
+
export type HashSetDescriptor = {
|
|
32
|
+
readonly kind: "hashSet";
|
|
33
|
+
readonly element: TypeDescriptor;
|
|
34
|
+
};
|
|
35
|
+
/** Branded type ([InlineWrapper]) — passthrough on serialize, create() on deserialize */
|
|
36
|
+
export type BrandedDescriptor = {
|
|
37
|
+
readonly kind: "branded";
|
|
38
|
+
readonly create: (value: unknown) => unknown;
|
|
39
|
+
};
|
|
40
|
+
/** String enum — passthrough on serialize, validate on deserialize */
|
|
41
|
+
export type EnumDescriptor = {
|
|
42
|
+
readonly kind: "enum";
|
|
43
|
+
readonly values: Record<string, string>;
|
|
44
|
+
};
|
|
45
|
+
/** Numeric enum — passthrough both ways */
|
|
46
|
+
export type NumericEnumDescriptor = {
|
|
47
|
+
readonly kind: "numericEnum";
|
|
48
|
+
readonly values: Record<string, number>;
|
|
49
|
+
};
|
|
50
|
+
/** Nullable wrapper — delegates to inner descriptor, passes null through */
|
|
51
|
+
export type NullableDescriptor = {
|
|
52
|
+
readonly kind: "nullable";
|
|
53
|
+
readonly inner: TypeDescriptor;
|
|
54
|
+
};
|
|
55
|
+
export type TypeDescriptor = PrimitiveDescriptor | RefDescriptor | TemporalDescriptor | DecimalDescriptor | MapDescriptor | ArrayDescriptor | HashSetDescriptor | BrandedDescriptor | EnumDescriptor | NumericEnumDescriptor | NullableDescriptor;
|
|
56
|
+
export interface PropertySpec {
|
|
57
|
+
/** TypeScript field name (camelCase) */
|
|
58
|
+
readonly ts: string;
|
|
59
|
+
/** JSON wire name (pre-computed by transpiler) */
|
|
60
|
+
readonly json: string;
|
|
61
|
+
/** How to convert between TS and JSON */
|
|
62
|
+
readonly type: TypeDescriptor;
|
|
63
|
+
/** Property is optional — null is accepted on deserialize */
|
|
64
|
+
readonly optional?: boolean;
|
|
65
|
+
}
|
|
66
|
+
export interface TypeSpec<T = unknown> {
|
|
67
|
+
/** Constructor reference (for instanceof checks and context lookups) */
|
|
68
|
+
readonly type: abstract new (...args: any[]) => T;
|
|
69
|
+
/** Parent type's spec — properties are inherited */
|
|
70
|
+
readonly base?: TypeSpec;
|
|
71
|
+
/** Constructs T from a Record<ts-field-name, deserialized-value> */
|
|
72
|
+
readonly factory: (props: Record<string, unknown>) => T;
|
|
73
|
+
/** Own properties only (base properties come from base spec) */
|
|
74
|
+
readonly properties: readonly PropertySpec[];
|
|
75
|
+
}
|
|
76
|
+
/** Custom converter — overrides built-in handling for a type descriptor kind */
|
|
77
|
+
export interface JsonConverter {
|
|
78
|
+
readonly kind: TypeDescriptor["kind"];
|
|
79
|
+
readonly serialize: (value: unknown) => unknown;
|
|
80
|
+
readonly deserialize: (value: unknown) => unknown;
|
|
81
|
+
}
|
|
82
|
+
export interface SerializerContextOptions {
|
|
83
|
+
/** Custom converters that override built-in type handling */
|
|
84
|
+
readonly converters?: readonly JsonConverter[];
|
|
85
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// ─── Type Descriptors ────────────────────────────────────────────────────────
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { EnumerableBase } from "./enumerable-base.ts";
|
|
2
|
+
/** Appends a single element to the end of a sequence. Equivalent to C# .Append() */
|
|
3
|
+
export declare class AppendEnumerable<T> extends EnumerableBase<T> {
|
|
4
|
+
readonly source: EnumerableBase<T>;
|
|
5
|
+
readonly element: T;
|
|
6
|
+
constructor(source: EnumerableBase<T>, element: T);
|
|
7
|
+
[Symbol.iterator](): Iterator<T>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { EnumerableBase } from "./enumerable-base.js";
|
|
2
|
+
/** Appends a single element to the end of a sequence. Equivalent to C# .Append() */
|
|
3
|
+
export class AppendEnumerable extends EnumerableBase {
|
|
4
|
+
source;
|
|
5
|
+
element;
|
|
6
|
+
constructor(source, element) {
|
|
7
|
+
super();
|
|
8
|
+
this.source = source;
|
|
9
|
+
this.element = element;
|
|
10
|
+
}
|
|
11
|
+
*[Symbol.iterator]() {
|
|
12
|
+
yield* this.source;
|
|
13
|
+
yield this.element;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic key comparator that handles primitives, Temporal types, and
|
|
3
|
+
* objects with a custom compareTo() method.
|
|
4
|
+
*
|
|
5
|
+
* Returns negative if a < b, positive if a > b, 0 if equal.
|
|
6
|
+
*/
|
|
7
|
+
export function compareKeys(a, b) {
|
|
8
|
+
if (a === b)
|
|
9
|
+
return 0;
|
|
10
|
+
if (a == null)
|
|
11
|
+
return b == null ? 0 : -1;
|
|
12
|
+
if (b == null)
|
|
13
|
+
return 1;
|
|
14
|
+
// Objects with compareTo() (e.g., Temporal types)
|
|
15
|
+
if (typeof a === "object" && a !== null && typeof a.compare === "function") {
|
|
16
|
+
return a.compare(b);
|
|
17
|
+
}
|
|
18
|
+
// Temporal types expose static compare() — try to detect via constructor
|
|
19
|
+
if (typeof a === "object" && a !== null) {
|
|
20
|
+
const ctor = a.constructor;
|
|
21
|
+
if (ctor && typeof ctor.compare === "function") {
|
|
22
|
+
return ctor.compare(a, b);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
// Primitives (string, number, bigint, boolean): use < / >
|
|
26
|
+
if (a < b)
|
|
27
|
+
return -1;
|
|
28
|
+
if (a > b)
|
|
29
|
+
return 1;
|
|
30
|
+
return 0;
|
|
31
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { EnumerableBase } from "./enumerable-base.ts";
|
|
2
|
+
/** Concatenates two sequences. */
|
|
3
|
+
export declare class ConcatEnumerable<T> extends EnumerableBase<T> {
|
|
4
|
+
readonly firstSource: EnumerableBase<T>;
|
|
5
|
+
readonly secondSource: Iterable<T>;
|
|
6
|
+
constructor(firstSource: EnumerableBase<T>, secondSource: Iterable<T>);
|
|
7
|
+
[Symbol.iterator](): Iterator<T>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { EnumerableBase } from "./enumerable-base.js";
|
|
2
|
+
/** Concatenates two sequences. */
|
|
3
|
+
export class ConcatEnumerable extends EnumerableBase {
|
|
4
|
+
firstSource;
|
|
5
|
+
secondSource;
|
|
6
|
+
constructor(firstSource, secondSource) {
|
|
7
|
+
super();
|
|
8
|
+
this.firstSource = firstSource;
|
|
9
|
+
this.secondSource = secondSource;
|
|
10
|
+
}
|
|
11
|
+
*[Symbol.iterator]() {
|
|
12
|
+
yield* this.firstSource;
|
|
13
|
+
yield* this.secondSource;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { EnumerableBase } from "./enumerable-base.ts";
|
|
2
|
+
/** Returns distinct elements by a key selector. Equivalent to C# .DistinctBy() */
|
|
3
|
+
export declare class DistinctByEnumerable<T, K> extends EnumerableBase<T> {
|
|
4
|
+
readonly source: EnumerableBase<T>;
|
|
5
|
+
readonly keySelector: (item: T) => K;
|
|
6
|
+
constructor(source: EnumerableBase<T>, keySelector: (item: T) => K);
|
|
7
|
+
[Symbol.iterator](): Iterator<T>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { EnumerableBase } from "./enumerable-base.js";
|
|
2
|
+
/** Returns distinct elements by a key selector. Equivalent to C# .DistinctBy() */
|
|
3
|
+
export class DistinctByEnumerable extends EnumerableBase {
|
|
4
|
+
source;
|
|
5
|
+
keySelector;
|
|
6
|
+
constructor(source, keySelector) {
|
|
7
|
+
super();
|
|
8
|
+
this.source = source;
|
|
9
|
+
this.keySelector = keySelector;
|
|
10
|
+
}
|
|
11
|
+
*[Symbol.iterator]() {
|
|
12
|
+
const seen = new Set();
|
|
13
|
+
for (const item of this.source) {
|
|
14
|
+
const key = this.keySelector(item);
|
|
15
|
+
if (!seen.has(key)) {
|
|
16
|
+
seen.add(key);
|
|
17
|
+
yield item;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { EnumerableBase } from "./enumerable-base.ts";
|
|
2
|
+
/** Returns distinct elements using HashSet-based deduplication (respects equals/hashCode). */
|
|
3
|
+
export declare class DistinctEnumerable<T> extends EnumerableBase<T> {
|
|
4
|
+
readonly source: EnumerableBase<T>;
|
|
5
|
+
constructor(source: EnumerableBase<T>);
|
|
6
|
+
[Symbol.iterator](): Iterator<T>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { EnumerableBase } from "./enumerable-base.js";
|
|
2
|
+
import { HashSet } from "../collections/hash-set.js";
|
|
3
|
+
/** Returns distinct elements using HashSet-based deduplication (respects equals/hashCode). */
|
|
4
|
+
export class DistinctEnumerable extends EnumerableBase {
|
|
5
|
+
source;
|
|
6
|
+
constructor(source) {
|
|
7
|
+
super();
|
|
8
|
+
this.source = source;
|
|
9
|
+
}
|
|
10
|
+
*[Symbol.iterator]() {
|
|
11
|
+
const seen = new HashSet();
|
|
12
|
+
for (const item of this.source) {
|
|
13
|
+
if (!seen.has(item)) {
|
|
14
|
+
seen.add(item);
|
|
15
|
+
yield item;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { HashSet } from "../collections/hash-set.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Abstract base for all LINQ enumerable operations.
|
|
4
|
+
* Each subclass represents a node in the query composition tree.
|
|
5
|
+
* Lazy: operations are deferred until iteration (via Symbol.iterator).
|
|
6
|
+
*/
|
|
7
|
+
export declare abstract class EnumerableBase<T> implements Iterable<T> {
|
|
8
|
+
abstract [Symbol.iterator](): Iterator<T>;
|
|
9
|
+
where(predicate: (item: T, index: number) => boolean): EnumerableBase<T>;
|
|
10
|
+
select<R>(selector: (item: T, index: number) => R): EnumerableBase<R>;
|
|
11
|
+
selectMany<R>(selector: (item: T, index: number) => Iterable<R>): EnumerableBase<R>;
|
|
12
|
+
orderBy<K>(keySelector: (item: T) => K): EnumerableBase<T>;
|
|
13
|
+
orderByDescending<K>(keySelector: (item: T) => K): EnumerableBase<T>;
|
|
14
|
+
thenBy<K>(keySelector: (item: T) => K): EnumerableBase<T>;
|
|
15
|
+
thenByDescending<K>(keySelector: (item: T) => K): EnumerableBase<T>;
|
|
16
|
+
take(count: number): EnumerableBase<T>;
|
|
17
|
+
skip(count: number): EnumerableBase<T>;
|
|
18
|
+
distinct(): EnumerableBase<T>;
|
|
19
|
+
groupBy<K>(keySelector: (item: T) => K): EnumerableBase<Grouping<K, T>>;
|
|
20
|
+
concat(other: Iterable<T>): EnumerableBase<T>;
|
|
21
|
+
takeWhile(predicate: (item: T, index: number) => boolean): EnumerableBase<T>;
|
|
22
|
+
skipWhile(predicate: (item: T, index: number) => boolean): EnumerableBase<T>;
|
|
23
|
+
distinctBy<K>(keySelector: (item: T) => K): EnumerableBase<T>;
|
|
24
|
+
reverse(): EnumerableBase<T>;
|
|
25
|
+
zip<U, R>(other: Iterable<U>, resultSelector: (first: T, second: U) => R): EnumerableBase<R>;
|
|
26
|
+
append(element: T): EnumerableBase<T>;
|
|
27
|
+
prepend(element: T): EnumerableBase<T>;
|
|
28
|
+
union(other: Iterable<T>): EnumerableBase<T>;
|
|
29
|
+
intersect(other: Iterable<T>): EnumerableBase<T>;
|
|
30
|
+
except(other: Iterable<T>): EnumerableBase<T>;
|
|
31
|
+
toArray(): T[];
|
|
32
|
+
toMap<K, V>(keySelector: (item: T) => K, valueSelector: (item: T) => V): Map<K, V>;
|
|
33
|
+
toSet(): HashSet<T>;
|
|
34
|
+
first(predicate?: (item: T) => boolean): T;
|
|
35
|
+
firstOrDefault(predicate?: (item: T) => boolean, defaultValue?: T | null): T | null;
|
|
36
|
+
last(predicate?: (item: T) => boolean): T;
|
|
37
|
+
lastOrDefault(predicate?: (item: T) => boolean, defaultValue?: T | null): T | null;
|
|
38
|
+
single(predicate?: (item: T) => boolean): T;
|
|
39
|
+
singleOrDefault(predicate?: (item: T) => boolean, defaultValue?: T | null): T | null;
|
|
40
|
+
any(predicate?: (item: T) => boolean): boolean;
|
|
41
|
+
all(predicate: (item: T) => boolean): boolean;
|
|
42
|
+
count(predicate?: (item: T) => boolean): number;
|
|
43
|
+
sum(selector?: (item: T) => number): number;
|
|
44
|
+
min(selector?: (item: T) => number): number;
|
|
45
|
+
max(selector?: (item: T) => number): number;
|
|
46
|
+
average(selector?: (item: T) => number): number;
|
|
47
|
+
minBy<K>(selector: (item: T) => K): T;
|
|
48
|
+
maxBy<K>(selector: (item: T) => K): T;
|
|
49
|
+
contains(item: T): boolean;
|
|
50
|
+
aggregate<A>(seed: A, accumulator: (acc: A, item: T) => A): A;
|
|
51
|
+
forEach(action: (item: T, index: number) => void): void;
|
|
52
|
+
}
|
|
53
|
+
/** Represents a group of elements that share a common key. */
|
|
54
|
+
export interface Grouping<K, T> extends Iterable<T> {
|
|
55
|
+
readonly key: K;
|
|
56
|
+
}
|
|
57
|
+
export declare let createWhere: <T>(source: EnumerableBase<T>, predicate: (item: T, index: number) => boolean) => EnumerableBase<T>;
|
|
58
|
+
export declare let createSelect: <T, R>(source: EnumerableBase<T>, selector: (item: T, index: number) => R) => EnumerableBase<R>;
|
|
59
|
+
export declare let createSelectMany: <T, R>(source: EnumerableBase<T>, selector: (item: T, index: number) => Iterable<R>) => EnumerableBase<R>;
|
|
60
|
+
export declare let createOrderBy: <T, K>(source: EnumerableBase<T>, keySelector: (item: T) => K, descending: boolean) => EnumerableBase<T>;
|
|
61
|
+
export declare let createTake: <T>(source: EnumerableBase<T>, count: number) => EnumerableBase<T>;
|
|
62
|
+
export declare let createSkip: <T>(source: EnumerableBase<T>, count: number) => EnumerableBase<T>;
|
|
63
|
+
export declare let createDistinct: <T>(source: EnumerableBase<T>) => EnumerableBase<T>;
|
|
64
|
+
export declare let createGroupBy: <T, K>(source: EnumerableBase<T>, keySelector: (item: T) => K) => EnumerableBase<Grouping<K, T>>;
|
|
65
|
+
export declare let createConcat: <T>(first: EnumerableBase<T>, second: Iterable<T>) => EnumerableBase<T>;
|
|
66
|
+
export declare let createThenBy: <T, K>(source: EnumerableBase<T>, keySelector: (item: T) => K, descending: boolean) => EnumerableBase<T>;
|
|
67
|
+
export declare let createTakeWhile: <T>(source: EnumerableBase<T>, predicate: (item: T, index: number) => boolean) => EnumerableBase<T>;
|
|
68
|
+
export declare let createSkipWhile: <T>(source: EnumerableBase<T>, predicate: (item: T, index: number) => boolean) => EnumerableBase<T>;
|
|
69
|
+
export declare let createDistinctBy: <T, K>(source: EnumerableBase<T>, keySelector: (item: T) => K) => EnumerableBase<T>;
|
|
70
|
+
export declare let createReverse: <T>(source: EnumerableBase<T>) => EnumerableBase<T>;
|
|
71
|
+
export declare let createZip: <T, U, R>(first: EnumerableBase<T>, second: Iterable<U>, resultSelector: (first: T, second: U) => R) => EnumerableBase<R>;
|
|
72
|
+
export declare let createAppend: <T>(source: EnumerableBase<T>, element: T) => EnumerableBase<T>;
|
|
73
|
+
export declare let createPrepend: <T>(source: EnumerableBase<T>, element: T) => EnumerableBase<T>;
|
|
74
|
+
export declare let createUnion: <T>(first: EnumerableBase<T>, second: Iterable<T>) => EnumerableBase<T>;
|
|
75
|
+
export declare let createIntersect: <T>(first: EnumerableBase<T>, second: Iterable<T>) => EnumerableBase<T>;
|
|
76
|
+
export declare let createExcept: <T>(first: EnumerableBase<T>, second: Iterable<T>) => EnumerableBase<T>;
|
|
77
|
+
export declare function _registerFactories(factories: {
|
|
78
|
+
where: typeof createWhere;
|
|
79
|
+
select: typeof createSelect;
|
|
80
|
+
selectMany: typeof createSelectMany;
|
|
81
|
+
orderBy: typeof createOrderBy;
|
|
82
|
+
take: typeof createTake;
|
|
83
|
+
skip: typeof createSkip;
|
|
84
|
+
distinct: typeof createDistinct;
|
|
85
|
+
groupBy: typeof createGroupBy;
|
|
86
|
+
concat: typeof createConcat;
|
|
87
|
+
thenBy: typeof createThenBy;
|
|
88
|
+
takeWhile: typeof createTakeWhile;
|
|
89
|
+
skipWhile: typeof createSkipWhile;
|
|
90
|
+
distinctBy: typeof createDistinctBy;
|
|
91
|
+
reverse: typeof createReverse;
|
|
92
|
+
zip: typeof createZip;
|
|
93
|
+
append: typeof createAppend;
|
|
94
|
+
prepend: typeof createPrepend;
|
|
95
|
+
union: typeof createUnion;
|
|
96
|
+
intersect: typeof createIntersect;
|
|
97
|
+
except: typeof createExcept;
|
|
98
|
+
}): void;
|