@shaclmate/compiler 2.0.14 → 2.0.16

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.
Files changed (67) hide show
  1. package/_ShapesGraphToAstTransformer/transformNodeShapeToAstType.js +22 -7
  2. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.js +5 -3
  3. package/ast/ListType.d.ts +2 -2
  4. package/ast/ObjectType.d.ts +18 -12
  5. package/enums/IdentifierMintingStrategy.d.ts +5 -0
  6. package/enums/IdentifierMintingStrategy.js +2 -0
  7. package/enums/index.d.ts +1 -1
  8. package/enums/index.js +1 -1
  9. package/generators/json/AstJsonGenerator.js +5 -1
  10. package/generators/ts/DateTimeType.d.ts +3 -1
  11. package/generators/ts/DateTimeType.js +9 -1
  12. package/generators/ts/IdentifierType.d.ts +2 -0
  13. package/generators/ts/IdentifierType.js +11 -0
  14. package/generators/ts/Import.d.ts +0 -1
  15. package/generators/ts/Import.js +0 -5
  16. package/generators/ts/ListType.d.ts +4 -3
  17. package/generators/ts/ListType.js +27 -16
  18. package/generators/ts/ObjectType.d.ts +5 -4
  19. package/generators/ts/ObjectType.js +21 -9
  20. package/generators/ts/ObjectUnionType.d.ts +1 -1
  21. package/generators/ts/ObjectUnionType.js +22 -52
  22. package/generators/ts/OptionType.d.ts +3 -1
  23. package/generators/ts/OptionType.js +12 -11
  24. package/generators/ts/PrimitiveType.d.ts +2 -2
  25. package/generators/ts/PrimitiveType.js +9 -4
  26. package/generators/ts/SetType.d.ts +3 -1
  27. package/generators/ts/SetType.js +12 -8
  28. package/generators/ts/SnippetDeclarations.d.ts +11 -0
  29. package/generators/ts/SnippetDeclarations.js +215 -0
  30. package/generators/ts/TermType.d.ts +3 -1
  31. package/generators/ts/TermType.js +16 -8
  32. package/generators/ts/TsGenerator.js +15 -1
  33. package/generators/ts/Type.d.ts +15 -5
  34. package/generators/ts/Type.js +17 -0
  35. package/generators/ts/TypeFactory.js +18 -8
  36. package/generators/ts/UnionType.d.ts +2 -1
  37. package/generators/ts/UnionType.js +3 -3
  38. package/generators/ts/_ObjectType/IdentifierProperty.d.ts +7 -8
  39. package/generators/ts/_ObjectType/IdentifierProperty.js +99 -25
  40. package/generators/ts/_ObjectType/Property.d.ts +17 -3
  41. package/generators/ts/_ObjectType/Property.js +2 -1
  42. package/generators/ts/_ObjectType/ShaclProperty.d.ts +1 -0
  43. package/generators/ts/_ObjectType/ShaclProperty.js +4 -1
  44. package/generators/ts/_ObjectType/TypeDiscriminatorProperty.d.ts +3 -5
  45. package/generators/ts/_ObjectType/TypeDiscriminatorProperty.js +11 -4
  46. package/generators/ts/_ObjectType/equalsFunctionOrMethodDeclaration.js +1 -1
  47. package/generators/ts/_ObjectType/fromJsonFunctionDeclarations.js +1 -1
  48. package/generators/ts/_ObjectType/fromRdfFunctionDeclarations.js +1 -1
  49. package/generators/ts/_ObjectType/fromRdfTypeVariableStatement.d.ts +5 -0
  50. package/generators/ts/_ObjectType/fromRdfTypeVariableStatement.js +25 -0
  51. package/generators/ts/_ObjectType/index.d.ts +1 -0
  52. package/generators/ts/_ObjectType/index.js +1 -0
  53. package/generators/ts/_ObjectType/sparqlConstructQueryFunctionDeclaration.d.ts +5 -0
  54. package/generators/ts/_ObjectType/sparqlConstructQueryFunctionDeclaration.js +21 -0
  55. package/generators/ts/_ObjectType/sparqlConstructQueryStringFunctionDeclaration.d.ts +5 -0
  56. package/generators/ts/_ObjectType/sparqlConstructQueryStringFunctionDeclaration.js +20 -0
  57. package/generators/ts/_ObjectType/sparqlFunctionDeclarations.js +35 -58
  58. package/generators/ts/_ObjectType/toJsonFunctionOrMethodDeclaration.js +1 -1
  59. package/generators/ts/_ObjectType/toRdfFunctionOrMethodDeclaration.js +3 -3
  60. package/input/NodeShape.d.ts +2 -2
  61. package/input/generated.d.ts +24 -16
  62. package/input/generated.js +53 -8
  63. package/input/tsFeatures.d.ts +1 -1
  64. package/input/tsFeatures.js +21 -13
  65. package/package.json +4 -4
  66. package/enums/MintingStrategy.d.ts +0 -5
  67. package/enums/MintingStrategy.js +0 -2
@@ -11,6 +11,8 @@ import { StructureKind, } from "ts-morph";
11
11
  import { Memoize } from "typescript-memoize";
12
12
  import { DeclaredType } from "./DeclaredType.js";
13
13
  import { hasherTypeConstraint } from "./_ObjectType/hashFunctionOrMethodDeclaration.js";
14
+ import { sparqlConstructQueryFunctionDeclaration } from "./_ObjectType/sparqlConstructQueryFunctionDeclaration.js";
15
+ import { sparqlConstructQueryStringFunctionDeclaration } from "./_ObjectType/sparqlConstructQueryStringFunctionDeclaration.js";
14
16
  import { objectInitializer } from "./objectInitializer.js";
15
17
  import { tsComment } from "./tsComment.js";
16
18
  /**
@@ -53,7 +55,7 @@ export class ObjectUnionType extends DeclaredType {
53
55
  ];
54
56
  }
55
57
  get declarationImports() {
56
- return this.memberTypes.flatMap((memberType) => memberType.useImports);
58
+ return this.memberTypes.flatMap((memberType) => memberType.useImports(this.features));
57
59
  }
58
60
  get declarations() {
59
61
  const declarations = [this.typeAliasDeclaration];
@@ -91,9 +93,6 @@ export class ObjectUnionType extends DeclaredType {
91
93
  get mutable() {
92
94
  return this.memberTypes.some((memberType) => memberType.mutable);
93
95
  }
94
- get useImports() {
95
- return [];
96
- }
97
96
  get thisVariable() {
98
97
  return `_${camelCase(this.name)}`;
99
98
  }
@@ -127,9 +126,9 @@ export class ObjectUnionType extends DeclaredType {
127
126
  type: this.name,
128
127
  },
129
128
  ],
130
- returnType: "purifyHelpers.Equatable.EqualsResult",
129
+ returnType: "EqualsResult",
131
130
  statements: `\
132
- return purifyHelpers.Equatable.strictEquals(left.type, right.type).chain(() => {
131
+ return strictEquals(left.type, right.type).chain(() => {
133
132
  switch (left.${this._discriminatorProperty.name}) {
134
133
  ${caseBlocks.join(" ")}
135
134
  }
@@ -171,14 +170,14 @@ return purifyHelpers.Equatable.strictEquals(left.type, right.type).chain(() => {
171
170
  name: "fromRdf",
172
171
  parameters: [
173
172
  {
174
- name: "parameters",
173
+ name: "{ ignoreRdfType, resource, ...context }",
175
174
  type: `{ [_index: string]: any; ignoreRdfType?: boolean; resource: ${this.rdfjsResourceType().name}; }`,
176
175
  },
177
176
  ],
178
177
  returnType: `purify.Either<rdfjsResource.Resource.ValueError, ${this.name}>`,
179
178
  statements: [
180
179
  `return ${this.memberTypes.reduce((expression, memberType) => {
181
- const memberTypeExpression = `(${memberType.name}.fromRdf(parameters) as purify.Either<rdfjsResource.Resource.ValueError, ${this.name}>)`;
180
+ const memberTypeExpression = `(${memberType.name}.fromRdf({ ...context, resource }) as purify.Either<rdfjsResource.Resource.ValueError, ${this.name}>)`;
182
181
  return expression.length > 0
183
182
  ? `${expression}.altLazy(() => ${memberTypeExpression})`
184
183
  : memberTypeExpression;
@@ -244,54 +243,23 @@ return purifyHelpers.Equatable.strictEquals(left.type, right.type).chain(() => {
244
243
  return [];
245
244
  }
246
245
  return [
247
- {
248
- isExported: true,
249
- kind: StructureKind.Function,
250
- name: "sparqlConstructQuery",
251
- parameters: [
252
- {
253
- hasQuestionToken: true,
254
- name: "parameters",
255
- type: '{ prefixes?: { [prefix: string]: string }; subject: rdfjs.Variable } & Omit<sparqljs.ConstructQuery, "prefixes" | "queryType" | "template" | "where">',
256
- },
257
- ],
258
- returnType: "sparqljs.ConstructQuery",
259
- statements: [
260
- `const subject = parameters?.subject ?? ${this.dataFactoryVariable}.variable!("${camelCase(this.name)}");`,
261
- `return { ...parameters, prefixes: parameters?.prefixes ?? {}, queryType: "CONSTRUCT", template: ${this.name}.sparqlConstructTemplateTriples({ subject }).concat(), type: "query", where: ${this.name}.sparqlWherePatterns({ subject }).concat() };`,
262
- ],
263
- },
264
- {
265
- isExported: true,
266
- kind: StructureKind.Function,
267
- name: "sparqlConstructQueryString",
268
- parameters: [
269
- {
270
- hasQuestionToken: true,
271
- name: "parameters",
272
- type: '{ subject: rdfjs.Variable } & Omit<sparqljs.ConstructQuery, "prefixes" | "queryType" | "template" | "where"> & sparqljs.GeneratorOptions',
273
- },
274
- ],
275
- returnType: "string",
276
- statements: [
277
- `return new sparqljs.Generator(parameters).stringify(${this.name}.sparqlConstructQuery(parameters));`,
278
- ],
279
- },
246
+ sparqlConstructQueryFunctionDeclaration.bind(this)(),
247
+ sparqlConstructQueryStringFunctionDeclaration.bind(this)(),
280
248
  {
281
249
  isExported: true,
282
250
  kind: StructureKind.Function,
283
251
  name: "sparqlConstructTemplateTriples",
252
+ // Accept ignoreRdfType in order to reuse code but don't pass it through, since deserialization may depend on it
284
253
  parameters: [
285
254
  {
286
- name: "{ subject, variablePrefix: variablePrefixParameter }",
287
- type: "{ subject: rdfjs.Variable, variablePrefix?: string }",
255
+ name: "parameters",
256
+ type: '{ ignoreRdfType?: boolean, subject?: sparqljs.Triple["subject"], variablePrefix?: string }',
288
257
  },
289
258
  ],
290
259
  returnType: "readonly sparqljs.Triple[]",
291
260
  statements: [
292
- "const variablePrefix = variablePrefixParameter ?? subject.value;",
293
261
  `return [${this.memberTypes
294
- .map((memberType) => `...${memberType.name}.sparqlConstructTemplateTriples({ subject, variablePrefix: \`\${variablePrefix}${pascalCase(memberType.name)}\` }).concat()`)
262
+ .map((memberType) => `...${memberType.name}.sparqlConstructTemplateTriples({ subject: parameters.subject ?? ${this.dataFactoryVariable}.variable!("${camelCase(this.name)}${pascalCase(memberType.name)}"), variablePrefix: parameters?.variablePrefix ? \`\${parameters.variablePrefix}${pascalCase(memberType.name)}\` : "${camelCase(this.name)}${pascalCase(memberType.name)}" }).concat()`)
295
263
  .join(", ")}];`,
296
264
  ],
297
265
  },
@@ -299,18 +267,18 @@ return purifyHelpers.Equatable.strictEquals(left.type, right.type).chain(() => {
299
267
  isExported: true,
300
268
  kind: StructureKind.Function,
301
269
  name: "sparqlWherePatterns",
270
+ // Accept ignoreRdfType in order to reuse code but don't pass it through, since deserialization may depend on it
302
271
  parameters: [
303
272
  {
304
- name: "{ subject, variablePrefix: variablePrefixParameter }",
305
- type: "{ subject: rdfjs.Variable, variablePrefix?: string }",
273
+ name: "parameters",
274
+ type: '{ ignoreRdfType?: boolean; subject?: sparqljs.Triple["subject"], variablePrefix?: string }',
306
275
  },
307
276
  ],
308
277
  returnType: "readonly sparqljs.Pattern[]",
309
278
  statements: [
310
- "const variablePrefix = variablePrefixParameter ?? subject.value;",
311
279
  `return [{ patterns: [${this.memberTypes
312
280
  .map((memberType) => objectInitializer({
313
- patterns: `${memberType.name}.sparqlWherePatterns({ subject, variablePrefix: \`\${variablePrefix}${pascalCase(memberType.name)}\` }).concat()`,
281
+ patterns: `${memberType.name}.sparqlWherePatterns({ subject: parameters.subject ?? ${this.dataFactoryVariable}.variable!("${camelCase(this.name)}${pascalCase(memberType.name)}"), variablePrefix: parameters?.variablePrefix ? \`\${parameters.variablePrefix}${pascalCase(memberType.name)}\` : "${camelCase(this.name)}${pascalCase(memberType.name)}" }).concat()`,
314
282
  type: '"group"',
315
283
  }))
316
284
  .join(", ")}], type: "union" }];`,
@@ -397,7 +365,8 @@ return purifyHelpers.Equatable.strictEquals(left.type, right.type).chain(() => {
397
365
  return `${this.name}.fromJson(${variables.value}).unsafeCoerce()`;
398
366
  }
399
367
  fromRdfExpression({ variables, }) {
400
- return `${variables.resourceValues}.head().chain(value => value.to${this.rdfjsResourceType().named ? "Named" : ""}Resource()).chain(_resource => ${this.name}.fromRdf({ ...${variables.context}, resource: _resource }))`;
368
+ // Don't ignoreRdfType, we may need it to distinguish the union members
369
+ return `${variables.resourceValues}.head().chain(value => value.to${this.rdfjsResourceType().named ? "Named" : ""}Resource()).chain(_resource => ${this.name}.fromRdf({ ...${variables.context}, languageIn: ${variables.languageIn}, resource: _resource }))`;
401
370
  }
402
371
  hashStatements({ variables, }) {
403
372
  switch (this.memberTypes[0].declarationType) {
@@ -417,7 +386,6 @@ return purifyHelpers.Equatable.strictEquals(left.type, right.type).chain(() => {
417
386
  case "type":
418
387
  return [
419
388
  `...${this.name}.sparqlConstructTemplateTriples(${objectInitializer({
420
- ignoreRdfType: true,
421
389
  subject: variables.subject,
422
390
  variablePrefix: variables.variablePrefix,
423
391
  })})`,
@@ -431,7 +399,6 @@ return purifyHelpers.Equatable.strictEquals(left.type, right.type).chain(() => {
431
399
  case "type":
432
400
  return [
433
401
  `...${this.name}.sparqlWherePatterns(${objectInitializer({
434
- ignoreRdfType: true,
435
402
  subject: variables.subject,
436
403
  variablePrefix: variables.variablePrefix,
437
404
  })})`,
@@ -456,6 +423,9 @@ return purifyHelpers.Equatable.strictEquals(left.type, right.type).chain(() => {
456
423
  return `${this.name}.toRdf(${variables.value}, ${options})`;
457
424
  }
458
425
  }
426
+ useImports() {
427
+ return [];
428
+ }
459
429
  rdfjsResourceType(options) {
460
430
  const memberRdfjsResourceTypes = [];
461
431
  for (const memberType of this.memberTypes) {
@@ -1,3 +1,4 @@
1
+ import type { TsFeature } from "../../enums/index.js";
1
2
  import { Import } from "./Import.js";
2
3
  import { Type } from "./Type.js";
3
4
  export declare class OptionType extends Type {
@@ -11,15 +12,16 @@ export declare class OptionType extends Type {
11
12
  get jsonName(): string;
12
13
  get mutable(): boolean;
13
14
  get name(): string;
14
- get useImports(): readonly Import[];
15
15
  fromJsonExpression({ variables, }: Parameters<Type["fromJsonExpression"]>[0]): string;
16
16
  fromRdfExpression(parameters: Parameters<Type["fromRdfExpression"]>[0]): string;
17
17
  hashStatements({ depth, variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
18
18
  jsonUiSchemaElement(parameters: Parameters<Type["jsonUiSchemaElement"]>[0]): ReturnType<Type["jsonUiSchemaElement"]>;
19
19
  jsonZodSchema(parameters: Parameters<Type["jsonZodSchema"]>[0]): ReturnType<Type["jsonZodSchema"]>;
20
+ snippetDeclarations(features: Set<TsFeature>): readonly string[];
20
21
  sparqlConstructTemplateTriples({ context, variables, }: Parameters<Type["sparqlConstructTemplateTriples"]>[0]): readonly string[];
21
22
  sparqlWherePatterns({ context, variables, }: Parameters<Type["sparqlWherePatterns"]>[0]): readonly string[];
22
23
  toJsonExpression({ variables, }: Parameters<Type["toJsonExpression"]>[0]): string;
23
24
  toRdfExpression({ variables, }: Parameters<Type["toRdfExpression"]>[0]): string;
25
+ useImports(features: Set<TsFeature>): readonly Import[];
24
26
  }
25
27
  //# sourceMappingURL=OptionType.d.ts.map
@@ -6,6 +6,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
6
6
  };
7
7
  import { Memoize } from "typescript-memoize";
8
8
  import { Import } from "./Import.js";
9
+ import { SnippetDeclarations } from "./SnippetDeclarations.js";
9
10
  import { Type } from "./Type.js";
10
11
  export class OptionType extends Type {
11
12
  constructor({ itemType, ...superParameters }) {
@@ -36,14 +37,7 @@ export class OptionType extends Type {
36
37
  return conversions;
37
38
  }
38
39
  get equalsFunction() {
39
- const itemTypeEqualsFunction = this.itemType.equalsFunction;
40
- if (itemTypeEqualsFunction === "purifyHelpers.Equatable.equals") {
41
- return "purifyHelpers.Equatable.maybeEquals";
42
- }
43
- if (itemTypeEqualsFunction === "purifyHelpers.Equatable.strictEquals") {
44
- return "purifyHelpers.Equatable.booleanEquals"; // Use Maybe.equals
45
- }
46
- return `(left, right) => purifyHelpers.Maybes.equals(left, right, ${itemTypeEqualsFunction})`;
40
+ return `((left, right) => maybeEquals(left, right, ${this.itemType.equalsFunction}))`;
47
41
  }
48
42
  get jsonName() {
49
43
  return `(${this.itemType.jsonName}) | undefined`;
@@ -54,9 +48,6 @@ export class OptionType extends Type {
54
48
  get name() {
55
49
  return `purify.Maybe<${this.itemType.name}>`;
56
50
  }
57
- get useImports() {
58
- return [...this.itemType.useImports, Import.PURIFY];
59
- }
60
51
  fromJsonExpression({ variables, }) {
61
52
  const expression = `purify.Maybe.fromNullable(${variables.value})`;
62
53
  const itemFromJsonExpression = this.itemType.fromJsonExpression({
@@ -88,6 +79,13 @@ export class OptionType extends Type {
88
79
  jsonZodSchema(parameters) {
89
80
  return `${this.itemType.jsonZodSchema(parameters)}.optional()`;
90
81
  }
82
+ snippetDeclarations(features) {
83
+ const snippetDeclarations = [];
84
+ if (features.has("equals")) {
85
+ snippetDeclarations.push(SnippetDeclarations.maybeEquals);
86
+ }
87
+ return snippetDeclarations;
88
+ }
91
89
  sparqlConstructTemplateTriples({ context, variables, }) {
92
90
  switch (context) {
93
91
  case "property":
@@ -125,6 +123,9 @@ export class OptionType extends Type {
125
123
  }
126
124
  return `${variables.value}.map((_value) => ${itemTypeToRdfExpression})`;
127
125
  }
126
+ useImports(features) {
127
+ return [...this.itemType.useImports(features), Import.PURIFY];
128
+ }
128
129
  }
129
130
  __decorate([
130
131
  Memoize()
@@ -1,5 +1,5 @@
1
1
  import { Maybe } from "purify-ts";
2
- import type { Import } from "./Import.js";
2
+ import type { TsFeature } from "../../enums/index.js";
3
3
  import { LiteralType } from "./LiteralType.js";
4
4
  import type { Type } from "./Type.js";
5
5
  export declare abstract class PrimitiveType<ValueT extends boolean | Date | string | number> extends LiteralType {
@@ -12,9 +12,9 @@ export declare abstract class PrimitiveType<ValueT extends boolean | Date | stri
12
12
  } & ConstructorParameters<typeof LiteralType>[0]);
13
13
  get discriminatorProperty(): Maybe<Type.DiscriminatorProperty>;
14
14
  get jsonName(): string;
15
- get useImports(): readonly Import[];
16
15
  fromJsonExpression({ variables, }: Parameters<Type["fromJsonExpression"]>[0]): string;
17
16
  hashStatements({ variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
17
+ snippetDeclarations(features: Set<TsFeature>): readonly string[];
18
18
  toJsonExpression({ variables, }: Parameters<Type["toJsonExpression"]>[0]): string;
19
19
  protected propertyFilterRdfResourceValuesExpression({ variables, }: Parameters<LiteralType["propertyFilterRdfResourceValuesExpression"]>[0]): string;
20
20
  }
@@ -1,9 +1,10 @@
1
1
  import { Maybe } from "purify-ts";
2
2
  import { LiteralType } from "./LiteralType.js";
3
+ import { SnippetDeclarations } from "./SnippetDeclarations.js";
3
4
  export class PrimitiveType extends LiteralType {
4
5
  constructor({ primitiveDefaultValue, primitiveIn, ...superParameters }) {
5
6
  super(superParameters);
6
- this.equalsFunction = "purifyHelpers.Equatable.strictEquals";
7
+ this.equalsFunction = "strictEquals";
7
8
  this.primitiveDefaultValue = primitiveDefaultValue;
8
9
  this.primitiveIn = primitiveIn;
9
10
  }
@@ -13,15 +14,19 @@ export class PrimitiveType extends LiteralType {
13
14
  get jsonName() {
14
15
  return this.name;
15
16
  }
16
- get useImports() {
17
- return [];
18
- }
19
17
  fromJsonExpression({ variables, }) {
20
18
  return variables.value;
21
19
  }
22
20
  hashStatements({ variables, }) {
23
21
  return [`${variables.hasher}.update(${variables.value}.toString());`];
24
22
  }
23
+ snippetDeclarations(features) {
24
+ const snippetDeclarations = [];
25
+ if (features.has("equals")) {
26
+ snippetDeclarations.push(SnippetDeclarations.strictEquals);
27
+ }
28
+ return snippetDeclarations;
29
+ }
25
30
  toJsonExpression({ variables, }) {
26
31
  return variables.value;
27
32
  }
@@ -1,3 +1,4 @@
1
+ import type { TsFeature } from "../../enums/index.js";
1
2
  import type { Import } from "./Import.js";
2
3
  import { Type } from "./Type.js";
3
4
  export declare class SetType extends Type {
@@ -13,15 +14,16 @@ export declare class SetType extends Type {
13
14
  get jsonName(): string;
14
15
  get mutable(): boolean;
15
16
  get name(): string;
16
- get useImports(): readonly Import[];
17
17
  fromJsonExpression({ variables, }: Parameters<Type["fromJsonExpression"]>[0]): string;
18
18
  fromRdfExpression({ variables, }: Parameters<Type["fromRdfExpression"]>[0]): string;
19
19
  hashStatements({ depth, variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
20
20
  jsonUiSchemaElement(parameters: Parameters<Type["jsonUiSchemaElement"]>[0]): ReturnType<Type["jsonUiSchemaElement"]>;
21
21
  jsonZodSchema(parameters: Parameters<Type["jsonZodSchema"]>[0]): ReturnType<Type["jsonZodSchema"]>;
22
+ snippetDeclarations(features: Set<TsFeature>): readonly string[];
22
23
  sparqlConstructTemplateTriples({ context, variables, }: Parameters<Type["sparqlConstructTemplateTriples"]>[0]): readonly string[];
23
24
  sparqlWherePatterns({ context, variables, }: Parameters<Type["sparqlWherePatterns"]>[0]): readonly string[];
24
25
  toJsonExpression({ variables, }: Parameters<Type["toJsonExpression"]>[0]): string;
25
26
  toRdfExpression({ variables, }: Parameters<Type["toRdfExpression"]>[0]): string;
27
+ useImports(features: Set<TsFeature>): readonly Import[];
26
28
  }
27
29
  //# sourceMappingURL=SetType.d.ts.map
@@ -6,6 +6,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
6
6
  };
7
7
  import { invariant } from "ts-invariant";
8
8
  import { Memoize } from "typescript-memoize";
9
+ import { SnippetDeclarations } from "./SnippetDeclarations.js";
9
10
  import { Type } from "./Type.js";
10
11
  import { objectInitializer } from "./objectInitializer.js";
11
12
  export class SetType extends Type {
@@ -40,11 +41,7 @@ export class SetType extends Type {
40
41
  return conversions;
41
42
  }
42
43
  get equalsFunction() {
43
- const itemTypeEqualsFunction = this.itemType.equalsFunction;
44
- if (itemTypeEqualsFunction === "purifyHelpers.Equatable.equals") {
45
- return "purifyHelpers.Equatable.arrayEquals";
46
- }
47
- return `(left, right) => purifyHelpers.Arrays.equals(left, right, ${itemTypeEqualsFunction})`;
44
+ return `((left, right) => arrayEquals(left, right, ${this.itemType.equalsFunction}))`;
48
45
  }
49
46
  get jsonName() {
50
47
  if (this.minCount === 0) {
@@ -61,9 +58,6 @@ export class SetType extends Type {
61
58
  }
62
59
  return `purify.NonEmptyList<${this.itemType.name}>`;
63
60
  }
64
- get useImports() {
65
- return this.itemType.useImports;
66
- }
67
61
  fromJsonExpression({ variables, }) {
68
62
  let expression = variables.value;
69
63
  if (this.minCount > 0) {
@@ -108,6 +102,13 @@ export class SetType extends Type {
108
102
  }
109
103
  return schema;
110
104
  }
105
+ snippetDeclarations(features) {
106
+ const snippetDeclarations = [];
107
+ if (features.has("equals")) {
108
+ snippetDeclarations.push(SnippetDeclarations.arrayEquals);
109
+ }
110
+ return snippetDeclarations;
111
+ }
111
112
  sparqlConstructTemplateTriples({ context, variables, }) {
112
113
  switch (context) {
113
114
  case "property":
@@ -143,6 +144,9 @@ export class SetType extends Type {
143
144
  variables: { ...variables, value: "_item" },
144
145
  })})`;
145
146
  }
147
+ useImports(features) {
148
+ return this.itemType.useImports(features);
149
+ }
146
150
  }
147
151
  __decorate([
148
152
  Memoize()
@@ -0,0 +1,11 @@
1
+ export declare namespace SnippetDeclarations {
2
+ const arrayEquals = "export function arrayEquals<T>(\n leftArray: readonly T[],\n rightArray: readonly T[],\n elementEquals: (left: T, right: T) => boolean | EqualsResult,\n): EqualsResult {\n if (leftArray.length !== rightArray.length) {\n return purify.Left({\n left: leftArray,\n right: rightArray,\n type: \"ArrayLength\",\n });\n }\n\n for (\n let leftElementIndex = 0;\n leftElementIndex < leftArray.length;\n leftElementIndex++\n ) {\n const leftElement = leftArray[leftElementIndex];\n\n const rightUnequals: EqualsResult.Unequal[] = [];\n for (\n let rightElementIndex = 0;\n rightElementIndex < rightArray.length;\n rightElementIndex++\n ) {\n const rightElement = rightArray[rightElementIndex];\n\n const leftElementEqualsRightElement =\n EqualsResult.fromBooleanEqualsResult(\n leftElement,\n rightElement,\n elementEquals(leftElement, rightElement),\n );\n if (leftElementEqualsRightElement.isRight()) {\n break; // left element === right element, break out of the right iteration\n }\n rightUnequals.push(\n leftElementEqualsRightElement.extract() as EqualsResult.Unequal,\n );\n }\n\n if (rightUnequals.length === rightArray.length) {\n // All right elements were unequal to the left element\n return purify.Left({\n left: {\n array: leftArray,\n element: leftElement,\n elementIndex: leftElementIndex,\n },\n right: {\n array: rightArray,\n unequals: rightUnequals,\n },\n type: \"ArrayElement\",\n });\n }\n // Else there was a right element equal to the left element, continue to the next left element\n }\n\n return EqualsResult.Equal;\n}\n";
3
+ const booleanEquals = "/**\n * Compare two objects with equals(other: T): boolean methods and return an EqualsResult.\n */\nexport function booleanEquals<T extends { equals: (other: T) => boolean }>(\n left: T,\n right: T,\n): EqualsResult {\n return EqualsResult.fromBooleanEqualsResult(\n left,\n right,\n left.equals(right),\n );\n}";
4
+ const dateEquals = "/**\n * Compare two Dates and return an EqualsResult.\n */\nexport function dateEquals(left: Date, right: Date): EqualsResult {\n return EqualsResult.fromBooleanEqualsResult(\n left,\n right,\n left.getTime() === right.getTime(),\n );\n}";
5
+ const EqualsResult = "export type EqualsResult = purify.Either<EqualsResult.Unequal, true>;\n\nexport namespace EqualsResult {\n export const Equal: EqualsResult = purify.Either.of<Unequal, true>(true);\n\n export function fromBooleanEqualsResult(\n left: any,\n right: any,\n equalsResult: boolean | EqualsResult,\n ): EqualsResult {\n if (typeof equalsResult !== \"boolean\") {\n return equalsResult;\n }\n\n if (equalsResult) {\n return Equal;\n }\n return purify.Left({\n left,\n right,\n type: \"BooleanEquals\",\n });\n }\n\n export type Unequal =\n | {\n readonly left: {\n readonly array: readonly any[];\n readonly element: any;\n readonly elementIndex: number;\n };\n readonly right: {\n readonly array: readonly any[];\n readonly unequals: readonly Unequal[];\n };\n readonly type: \"ArrayElement\";\n }\n | {\n readonly left: readonly any[];\n readonly right: readonly any[];\n readonly type: \"ArrayLength\";\n }\n | {\n readonly left: any;\n readonly right: any;\n readonly type: \"BooleanEquals\";\n }\n | {\n readonly left: any;\n readonly right: any;\n readonly type: \"LeftError\";\n }\n | {\n readonly right: any;\n readonly type: \"LeftNull\";\n }\n | {\n readonly left: bigint | boolean | number | string;\n readonly right: bigint | boolean | number | string;\n readonly type: \"Primitive\";\n }\n | {\n readonly left: object;\n readonly right: object;\n readonly propertyName: string;\n readonly propertyValuesUnequal: Unequal;\n readonly type: \"Property\";\n }\n | {\n readonly left: any;\n readonly right: any;\n readonly type: \"RightError\";\n }\n | {\n readonly left: any;\n readonly type: \"RightNull\";\n };\n} \n";
6
+ const maybeEquals = "export function maybeEquals<T>(\n leftMaybe: purify.Maybe<T>,\n rightMaybe: purify.Maybe<T>,\n valueEquals: (left: T, right: T) => boolean | EqualsResult,\n): EqualsResult {\n if (leftMaybe.isJust()) {\n if (rightMaybe.isJust()) {\n return EqualsResult.fromBooleanEqualsResult(\n leftMaybe,\n rightMaybe,\n valueEquals(leftMaybe.unsafeCoerce(), rightMaybe.unsafeCoerce()),\n );\n }\n return purify.Left({\n left: leftMaybe.unsafeCoerce(),\n type: \"RightNull\",\n });\n }\n\n if (rightMaybe.isJust()) {\n return purify.Left({\n right: rightMaybe.unsafeCoerce(),\n type: \"LeftNull\",\n });\n }\n\n return EqualsResult.Equal;\n}\n";
7
+ const strictEquals = "/**\n * Compare two values for strict equality (===), returning an EqualsResult rather than a boolean.\n */\nexport function strictEquals<T extends bigint | boolean | number | string>(\n left: T,\n right: T,\n): EqualsResult {\n return EqualsResult.fromBooleanEqualsResult(left, right, left === right);\n}";
8
+ const UnwrapL = "type UnwrapL<T> = T extends purify.Either<infer L, any> ? L : never";
9
+ const UnwrapR = "type UnwrapR<T> = T extends purify.Either<any, infer R> ? R : never";
10
+ }
11
+ //# sourceMappingURL=SnippetDeclarations.d.ts.map
@@ -0,0 +1,215 @@
1
+ export var SnippetDeclarations;
2
+ (function (SnippetDeclarations) {
3
+ SnippetDeclarations.arrayEquals = `\
4
+ export function arrayEquals<T>(
5
+ leftArray: readonly T[],
6
+ rightArray: readonly T[],
7
+ elementEquals: (left: T, right: T) => boolean | EqualsResult,
8
+ ): EqualsResult {
9
+ if (leftArray.length !== rightArray.length) {
10
+ return purify.Left({
11
+ left: leftArray,
12
+ right: rightArray,
13
+ type: "ArrayLength",
14
+ });
15
+ }
16
+
17
+ for (
18
+ let leftElementIndex = 0;
19
+ leftElementIndex < leftArray.length;
20
+ leftElementIndex++
21
+ ) {
22
+ const leftElement = leftArray[leftElementIndex];
23
+
24
+ const rightUnequals: EqualsResult.Unequal[] = [];
25
+ for (
26
+ let rightElementIndex = 0;
27
+ rightElementIndex < rightArray.length;
28
+ rightElementIndex++
29
+ ) {
30
+ const rightElement = rightArray[rightElementIndex];
31
+
32
+ const leftElementEqualsRightElement =
33
+ EqualsResult.fromBooleanEqualsResult(
34
+ leftElement,
35
+ rightElement,
36
+ elementEquals(leftElement, rightElement),
37
+ );
38
+ if (leftElementEqualsRightElement.isRight()) {
39
+ break; // left element === right element, break out of the right iteration
40
+ }
41
+ rightUnequals.push(
42
+ leftElementEqualsRightElement.extract() as EqualsResult.Unequal,
43
+ );
44
+ }
45
+
46
+ if (rightUnequals.length === rightArray.length) {
47
+ // All right elements were unequal to the left element
48
+ return purify.Left({
49
+ left: {
50
+ array: leftArray,
51
+ element: leftElement,
52
+ elementIndex: leftElementIndex,
53
+ },
54
+ right: {
55
+ array: rightArray,
56
+ unequals: rightUnequals,
57
+ },
58
+ type: "ArrayElement",
59
+ });
60
+ }
61
+ // Else there was a right element equal to the left element, continue to the next left element
62
+ }
63
+
64
+ return EqualsResult.Equal;
65
+ }
66
+ `;
67
+ SnippetDeclarations.booleanEquals = `\
68
+ /**
69
+ * Compare two objects with equals(other: T): boolean methods and return an EqualsResult.
70
+ */
71
+ export function booleanEquals<T extends { equals: (other: T) => boolean }>(
72
+ left: T,
73
+ right: T,
74
+ ): EqualsResult {
75
+ return EqualsResult.fromBooleanEqualsResult(
76
+ left,
77
+ right,
78
+ left.equals(right),
79
+ );
80
+ }`;
81
+ SnippetDeclarations.dateEquals = `\
82
+ /**
83
+ * Compare two Dates and return an EqualsResult.
84
+ */
85
+ export function dateEquals(left: Date, right: Date): EqualsResult {
86
+ return EqualsResult.fromBooleanEqualsResult(
87
+ left,
88
+ right,
89
+ left.getTime() === right.getTime(),
90
+ );
91
+ }`;
92
+ SnippetDeclarations.EqualsResult = `\
93
+ export type EqualsResult = purify.Either<EqualsResult.Unequal, true>;
94
+
95
+ export namespace EqualsResult {
96
+ export const Equal: EqualsResult = purify.Either.of<Unequal, true>(true);
97
+
98
+ export function fromBooleanEqualsResult(
99
+ left: any,
100
+ right: any,
101
+ equalsResult: boolean | EqualsResult,
102
+ ): EqualsResult {
103
+ if (typeof equalsResult !== "boolean") {
104
+ return equalsResult;
105
+ }
106
+
107
+ if (equalsResult) {
108
+ return Equal;
109
+ }
110
+ return purify.Left({
111
+ left,
112
+ right,
113
+ type: "BooleanEquals",
114
+ });
115
+ }
116
+
117
+ export type Unequal =
118
+ | {
119
+ readonly left: {
120
+ readonly array: readonly any[];
121
+ readonly element: any;
122
+ readonly elementIndex: number;
123
+ };
124
+ readonly right: {
125
+ readonly array: readonly any[];
126
+ readonly unequals: readonly Unequal[];
127
+ };
128
+ readonly type: "ArrayElement";
129
+ }
130
+ | {
131
+ readonly left: readonly any[];
132
+ readonly right: readonly any[];
133
+ readonly type: "ArrayLength";
134
+ }
135
+ | {
136
+ readonly left: any;
137
+ readonly right: any;
138
+ readonly type: "BooleanEquals";
139
+ }
140
+ | {
141
+ readonly left: any;
142
+ readonly right: any;
143
+ readonly type: "LeftError";
144
+ }
145
+ | {
146
+ readonly right: any;
147
+ readonly type: "LeftNull";
148
+ }
149
+ | {
150
+ readonly left: bigint | boolean | number | string;
151
+ readonly right: bigint | boolean | number | string;
152
+ readonly type: "Primitive";
153
+ }
154
+ | {
155
+ readonly left: object;
156
+ readonly right: object;
157
+ readonly propertyName: string;
158
+ readonly propertyValuesUnequal: Unequal;
159
+ readonly type: "Property";
160
+ }
161
+ | {
162
+ readonly left: any;
163
+ readonly right: any;
164
+ readonly type: "RightError";
165
+ }
166
+ | {
167
+ readonly left: any;
168
+ readonly type: "RightNull";
169
+ };
170
+ }
171
+ `;
172
+ SnippetDeclarations.maybeEquals = `\
173
+ export function maybeEquals<T>(
174
+ leftMaybe: purify.Maybe<T>,
175
+ rightMaybe: purify.Maybe<T>,
176
+ valueEquals: (left: T, right: T) => boolean | EqualsResult,
177
+ ): EqualsResult {
178
+ if (leftMaybe.isJust()) {
179
+ if (rightMaybe.isJust()) {
180
+ return EqualsResult.fromBooleanEqualsResult(
181
+ leftMaybe,
182
+ rightMaybe,
183
+ valueEquals(leftMaybe.unsafeCoerce(), rightMaybe.unsafeCoerce()),
184
+ );
185
+ }
186
+ return purify.Left({
187
+ left: leftMaybe.unsafeCoerce(),
188
+ type: "RightNull",
189
+ });
190
+ }
191
+
192
+ if (rightMaybe.isJust()) {
193
+ return purify.Left({
194
+ right: rightMaybe.unsafeCoerce(),
195
+ type: "LeftNull",
196
+ });
197
+ }
198
+
199
+ return EqualsResult.Equal;
200
+ }
201
+ `;
202
+ SnippetDeclarations.strictEquals = `\
203
+ /**
204
+ * Compare two values for strict equality (===), returning an EqualsResult rather than a boolean.
205
+ */
206
+ export function strictEquals<T extends bigint | boolean | number | string>(
207
+ left: T,
208
+ right: T,
209
+ ): EqualsResult {
210
+ return EqualsResult.fromBooleanEqualsResult(left, right, left === right);
211
+ }`;
212
+ SnippetDeclarations.UnwrapL = "type UnwrapL<T> = T extends purify.Either<infer L, any> ? L : never";
213
+ SnippetDeclarations.UnwrapR = "type UnwrapR<T> = T extends purify.Either<any, infer R> ? R : never";
214
+ })(SnippetDeclarations || (SnippetDeclarations = {}));
215
+ //# sourceMappingURL=SnippetDeclarations.js.map
@@ -1,5 +1,6 @@
1
1
  import type { BlankNode, Literal, NamedNode } from "@rdfjs/types";
2
2
  import { Maybe } from "purify-ts";
3
+ import type { TsFeature } from "../../enums/index.js";
3
4
  import { Import } from "./Import.js";
4
5
  import { Type } from "./Type.js";
5
6
  /**
@@ -22,14 +23,15 @@ export declare class TermType<TermT extends BlankNode | Literal | NamedNode> ext
22
23
  get discriminatorProperty(): Maybe<Type.DiscriminatorProperty>;
23
24
  get jsonName(): string;
24
25
  get name(): string;
25
- get useImports(): readonly Import[];
26
26
  fromJsonExpression({ variables, }: Parameters<Type["fromJsonExpression"]>[0]): string;
27
27
  fromRdfExpression({ variables, }: Parameters<Type["fromRdfExpression"]>[0]): string;
28
28
  hashStatements({ variables, }: Parameters<Type["hashStatements"]>[0]): readonly string[];
29
29
  jsonZodSchema({ variables, }: Parameters<Type["jsonZodSchema"]>[0]): ReturnType<Type["jsonZodSchema"]>;
30
+ snippetDeclarations(features: Set<TsFeature>): readonly string[];
30
31
  sparqlWherePatterns(parameters: Parameters<Type["sparqlWherePatterns"]>[0]): readonly string[];
31
32
  toJsonExpression({ variables, }: Parameters<Type["toJsonExpression"]>[0]): string;
32
33
  toRdfExpression({ variables, }: Parameters<Type["toRdfExpression"]>[0]): string;
34
+ useImports(): readonly Import[];
33
35
  /**
34
36
  * Filter the rdfjsResource.Resource.Values to those that are relevant to the type.
35
37
  *