@coasys/ad4m 0.12.0-rc1-dev → 0.12.0-rc1-dev.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/jest.config.js +4 -4
  2. package/lib/index.cjs +5999 -6044
  3. package/lib/index.js +5977 -6038
  4. package/lib/src/Ad4mClient.d.ts +16 -9
  5. package/lib/src/Ad4mClient.js +25 -46
  6. package/lib/src/Ad4mClient.test.d.ts +1 -0
  7. package/lib/src/Ad4mClient.test.js +1204 -0
  8. package/lib/src/Literal.d.ts +2 -1
  9. package/lib/src/Literal.js +35 -38
  10. package/lib/src/Literal.test.d.ts +1 -0
  11. package/lib/src/Literal.test.js +41 -0
  12. package/lib/src/SmartLiteral.d.ts +2 -1
  13. package/lib/src/SmartLiteral.js +15 -30
  14. package/lib/src/agent/AgentClient.d.ts +4 -1
  15. package/lib/src/agent/AgentClient.js +99 -79
  16. package/lib/src/ai/AIClient.d.ts +2 -1
  17. package/lib/src/ai/AIClient.js +220 -224
  18. package/lib/src/expression/ExpressionClient.d.ts +1 -1
  19. package/lib/src/expression/ExpressionClient.js +86 -73
  20. package/lib/src/index.d.ts +1 -1
  21. package/lib/src/index.js +1 -1
  22. package/lib/src/language/LanguageClient.d.ts +1 -1
  23. package/lib/src/language/LanguageClient.js +52 -52
  24. package/lib/src/model/Ad4mModel.d.ts +157 -937
  25. package/lib/src/model/Ad4mModel.js +212 -2797
  26. package/lib/src/model/Ad4mModel.test.js +1002 -561
  27. package/lib/src/model/decorators.d.ts +94 -426
  28. package/lib/src/model/decorators.js +333 -741
  29. package/lib/src/model/mutation.d.ts +81 -0
  30. package/lib/src/model/mutation.js +317 -0
  31. package/lib/src/model/parentUtils.d.ts +32 -0
  32. package/lib/src/model/parentUtils.js +64 -0
  33. package/lib/src/model/prolog/generatePrologFacts.d.ts +36 -0
  34. package/lib/src/model/prolog/generatePrologFacts.js +139 -0
  35. package/lib/src/model/prolog/index.d.ts +1 -0
  36. package/lib/src/model/prolog/index.js +1 -0
  37. package/lib/src/model/query/ModelQueryBuilder.d.ts +82 -0
  38. package/lib/src/model/query/ModelQueryBuilder.js +115 -0
  39. package/lib/src/model/query/QueryBuilder.d.ts +101 -0
  40. package/lib/src/model/query/QueryBuilder.js +193 -0
  41. package/lib/src/model/query/SurrealQueryBuilder.d.ts +48 -0
  42. package/lib/src/model/query/SurrealQueryBuilder.js +371 -0
  43. package/lib/src/model/query/fetchInstance.d.ts +24 -0
  44. package/lib/src/model/query/fetchInstance.js +144 -0
  45. package/lib/src/model/query/hydration.d.ts +54 -0
  46. package/lib/src/model/query/hydration.js +214 -0
  47. package/lib/src/model/query/operations.d.ts +59 -0
  48. package/lib/src/model/query/operations.js +384 -0
  49. package/lib/src/model/query/snapshot.d.ts +59 -0
  50. package/lib/src/model/query/snapshot.js +106 -0
  51. package/lib/src/model/query/surrealCompiler.d.ts +42 -0
  52. package/lib/src/model/query/surrealCompiler.js +304 -0
  53. package/lib/src/model/schema/fromJSONSchema.d.ts +67 -0
  54. package/lib/src/model/schema/fromJSONSchema.js +264 -0
  55. package/lib/src/model/schema/metadata.d.ts +15 -0
  56. package/lib/src/model/schema/metadata.js +112 -0
  57. package/lib/src/model/subscription.d.ts +47 -0
  58. package/lib/src/model/subscription.js +231 -0
  59. package/lib/src/model/transaction.d.ts +27 -0
  60. package/lib/src/model/transaction.js +32 -0
  61. package/lib/src/model/types.d.ts +195 -0
  62. package/lib/src/model/types.js +8 -0
  63. package/lib/src/model/util.js +7 -7
  64. package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +5 -1
  65. package/lib/src/neighbourhood/NeighbourhoodClient.js +263 -181
  66. package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +2 -1
  67. package/lib/src/neighbourhood/NeighbourhoodProxy.js +13 -28
  68. package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +1 -5
  69. package/lib/src/perspectives/PerspectiveClient.d.ts +9 -9
  70. package/lib/src/perspectives/PerspectiveClient.js +316 -240
  71. package/lib/src/perspectives/PerspectiveProxy.d.ts +54 -92
  72. package/lib/src/perspectives/PerspectiveProxy.js +803 -444
  73. package/lib/src/runtime/RuntimeClient.d.ts +5 -1
  74. package/lib/src/runtime/RuntimeClient.js +412 -238
  75. package/lib/src/shacl/SHACLFlow.test.js +164 -105
  76. package/lib/src/shacl/SHACLShape.d.ts +17 -3
  77. package/lib/src/shacl/SHACLShape.js +127 -90
  78. package/lib/src/shacl/SHACLShape.test.js +244 -176
  79. package/lib/src/utils.d.ts +8 -0
  80. package/lib/src/utils.js +21 -7
  81. package/package.json +8 -13
  82. package/src/Ad4mClient.test.ts +1564 -1315
  83. package/src/Ad4mClient.ts +55 -54
  84. package/src/Literal.test.ts +45 -26
  85. package/src/Literal.ts +82 -66
  86. package/src/SmartLiteral.ts +74 -58
  87. package/src/agent/AgentClient.ts +144 -99
  88. package/src/ai/AIClient.ts +409 -359
  89. package/src/expression/ExpressionClient.ts +142 -96
  90. package/src/index.ts +1 -1
  91. package/src/language/LanguageClient.ts +137 -113
  92. package/src/model/Ad4mModel.test.ts +1128 -647
  93. package/src/model/Ad4mModel.ts +485 -3247
  94. package/src/model/IMPROVEMENTS.md +482 -0
  95. package/src/model/PREDICATE_COLLISION_FIX.md +651 -0
  96. package/src/model/README.md +317 -0
  97. package/src/model/SHACL_MUTATION_DECOUPLING.md +356 -0
  98. package/src/model/SUBSCRIPTION_STRATEGY.md +317 -0
  99. package/src/model/TC39_STAGE_3_DECORATORS.md +609 -0
  100. package/src/model/decorators.ts +689 -1046
  101. package/src/model/mutation.ts +461 -0
  102. package/src/model/parentUtils.ts +91 -0
  103. package/src/model/prolog/generatePrologFacts.ts +176 -0
  104. package/src/model/prolog/index.ts +1 -0
  105. package/src/model/query/ModelQueryBuilder.ts +173 -0
  106. package/src/model/query/fetchInstance.ts +181 -0
  107. package/src/model/query/hydration.ts +273 -0
  108. package/src/model/query/operations.ts +510 -0
  109. package/src/model/query/snapshot.ts +126 -0
  110. package/src/model/query/surrealCompiler.ts +376 -0
  111. package/src/model/schema/fromJSONSchema.ts +411 -0
  112. package/src/model/schema/metadata.ts +148 -0
  113. package/src/model/subscription.ts +307 -0
  114. package/src/model/transaction.ts +60 -0
  115. package/src/model/types.ts +207 -0
  116. package/src/model/util.ts +47 -48
  117. package/src/neighbourhood/NeighbourhoodClient.ts +393 -251
  118. package/src/neighbourhood/NeighbourhoodProxy.test.ts +3 -7
  119. package/src/neighbourhood/NeighbourhoodProxy.ts +58 -40
  120. package/src/perspectives/PerspectiveClient.ts +779 -540
  121. package/src/perspectives/PerspectiveProxy.ts +2860 -2053
  122. package/src/runtime/RuntimeClient.ts +775 -476
  123. package/src/shacl/SHACLFlow.test.ts +196 -129
  124. package/src/shacl/SHACLShape.test.ts +284 -190
  125. package/src/shacl/SHACLShape.ts +225 -172
  126. package/src/utils.ts +31 -17
  127. package/tsconfig.json +9 -10
  128. package/src/model/Subject.ts +0 -159
package/src/Ad4mClient.ts CHANGED
@@ -1,72 +1,73 @@
1
- import { ApolloClient } from '@apollo/client/core'
2
- import { AgentClient } from './agent/AgentClient'
3
- import { LanguageClient } from './language/LanguageClient'
4
- import { NeighbourhoodClient } from './neighbourhood/NeighbourhoodClient'
5
- import { PerspectiveClient } from './perspectives/PerspectiveClient'
6
- import { RuntimeClient } from './runtime/RuntimeClient'
7
- import { ExpressionClient } from './expression/ExpressionClient'
8
- import { AIClient } from './ai/AIClient'
1
+ import { ApolloClient } from "@apollo/client/core";
2
+ import { AgentClient } from "./agent/AgentClient";
3
+ import { LanguageClient } from "./language/LanguageClient";
4
+ import { NeighbourhoodClient } from "./neighbourhood/NeighbourhoodClient";
5
+ import { PerspectiveClient } from "./perspectives/PerspectiveClient";
6
+ import { RuntimeClient } from "./runtime/RuntimeClient";
7
+ import { ExpressionClient } from "./expression/ExpressionClient";
8
+ import { AIClient } from "./ai/AIClient";
9
9
 
10
10
  /**
11
11
  * Client for the Ad4m interface wrapping GraphQL queryies
12
12
  * for convenient use in user facing code.
13
- *
13
+ *
14
14
  * Aggregates the six sub-clients:
15
15
  * AgentClient, ExpressionClient, LanguageClient,
16
16
  * NeighbourhoodClient, PerspectiveClient and RuntimeClient
17
17
  * for the respective functionality.
18
18
  */
19
19
  export class Ad4mClient {
20
- #apolloClient: ApolloClient<any>
21
- #agentClient: AgentClient
22
- #expressionClient: ExpressionClient
23
- #languageClient: LanguageClient
24
- #neighbourhoodClient: NeighbourhoodClient
25
- #perspectiveClient: PerspectiveClient
26
- #runtimeClient: RuntimeClient
27
- #aiClient: AIClient
20
+ private _apolloClient: ApolloClient<any>;
21
+ private _agentClient: AgentClient;
22
+ private _expressionClient: ExpressionClient;
23
+ private _languageClient: LanguageClient;
24
+ private _neighbourhoodClient: NeighbourhoodClient;
25
+ private _perspectiveClient: PerspectiveClient;
26
+ private _runtimeClient: RuntimeClient;
27
+ private _aiClient: AIClient;
28
28
 
29
+ constructor(client: ApolloClient<any>, subscribe: boolean = true) {
30
+ this._apolloClient = client;
31
+ this._agentClient = new AgentClient(this._apolloClient, subscribe);
32
+ this._expressionClient = new ExpressionClient(this._apolloClient);
33
+ this._languageClient = new LanguageClient(this._apolloClient);
34
+ this._neighbourhoodClient = new NeighbourhoodClient(this._apolloClient);
35
+ this._aiClient = new AIClient(this._apolloClient, subscribe);
36
+ this._perspectiveClient = new PerspectiveClient(
37
+ this._apolloClient,
38
+ subscribe,
39
+ );
40
+ this._perspectiveClient.setExpressionClient(this._expressionClient);
41
+ this._perspectiveClient.setNeighbourhoodClient(this._neighbourhoodClient);
42
+ this._perspectiveClient.setAIClient(this._aiClient);
43
+ this._runtimeClient = new RuntimeClient(this._apolloClient, subscribe);
44
+ }
29
45
 
30
- constructor(client: ApolloClient<any>, subscribe: boolean = true) {
31
- this.#apolloClient = client
32
- this.#agentClient = new AgentClient(this.#apolloClient, subscribe)
33
- this.#expressionClient = new ExpressionClient(this.#apolloClient)
34
- this.#languageClient = new LanguageClient(this.#apolloClient)
35
- this.#neighbourhoodClient = new NeighbourhoodClient(this.#apolloClient)
36
- this.#aiClient = new AIClient(this.#apolloClient, subscribe)
37
- this.#perspectiveClient = new PerspectiveClient(this.#apolloClient, subscribe)
38
- this.#perspectiveClient.setExpressionClient(this.#expressionClient)
39
- this.#perspectiveClient.setNeighbourhoodClient(this.#neighbourhoodClient)
40
- this.#perspectiveClient.setAIClient(this.#aiClient)
41
- this.#runtimeClient = new RuntimeClient(this.#apolloClient, subscribe)
42
-
43
- }
46
+ get agent(): AgentClient {
47
+ return this._agentClient;
48
+ }
44
49
 
45
- get agent(): AgentClient {
46
- return this.#agentClient
47
- }
50
+ get expression(): ExpressionClient {
51
+ return this._expressionClient;
52
+ }
48
53
 
49
- get expression(): ExpressionClient {
50
- return this.#expressionClient
51
- }
54
+ get languages(): LanguageClient {
55
+ return this._languageClient;
56
+ }
52
57
 
53
- get languages(): LanguageClient {
54
- return this.#languageClient
55
- }
58
+ get neighbourhood(): NeighbourhoodClient {
59
+ return this._neighbourhoodClient;
60
+ }
56
61
 
57
- get neighbourhood(): NeighbourhoodClient {
58
- return this.#neighbourhoodClient
59
- }
62
+ get perspective(): PerspectiveClient {
63
+ return this._perspectiveClient;
64
+ }
60
65
 
61
- get perspective(): PerspectiveClient {
62
- return this.#perspectiveClient
63
- }
66
+ get runtime(): RuntimeClient {
67
+ return this._runtimeClient;
68
+ }
64
69
 
65
- get runtime(): RuntimeClient {
66
- return this.#runtimeClient
67
- }
68
-
69
- get ai(): AIClient {
70
- return this.#aiClient
71
- }
72
- }
70
+ get ai(): AIClient {
71
+ return this._aiClient;
72
+ }
73
+ }
@@ -1,31 +1,50 @@
1
- import { Literal } from './Literal'
1
+ import { Literal } from "./Literal";
2
2
 
3
3
  describe("Literal", () => {
4
- it("can handle strings", () => {
5
- const testString = "test string"
6
- const testUrl = "literal://string:test%20string"
7
- expect(Literal.from(testString).toUrl()).toBe(testUrl)
8
- expect(Literal.fromUrl(testUrl).get()).toBe(testString)
9
- })
4
+ it("can handle strings", () => {
5
+ const testString = "test string";
6
+ const testUrl = "literal://string:test%20string";
7
+ expect(Literal.from(testString).toUrl()).toBe(testUrl);
8
+ expect(Literal.fromUrl(testUrl).get()).toBe(testString);
9
+ });
10
10
 
11
- it("can handle numbers", () => {
12
- const testNumber = 3.1415
13
- const testUrl = "literal://number:3.1415"
14
- expect(Literal.from(testNumber).toUrl()).toBe(testUrl)
15
- expect(Literal.fromUrl(testUrl).get()).toBe(testNumber)
16
- })
11
+ it("can handle numbers", () => {
12
+ const testNumber = 3.1415;
13
+ const testUrl = "literal://number:3.1415";
14
+ expect(Literal.from(testNumber).toUrl()).toBe(testUrl);
15
+ expect(Literal.fromUrl(testUrl).get()).toBe(testNumber);
16
+ });
17
17
 
18
- it("can handle objects", () => {
19
- const testObject = {testNumber: "1337", testString: "test"}
20
- const testUrl = "literal://json:%7B%22testNumber%22%3A%221337%22%2C%22testString%22%3A%22test%22%7D"
21
- expect(Literal.from(testObject).toUrl()).toBe(testUrl)
22
- expect(Literal.fromUrl(testUrl).get()).toStrictEqual(testObject)
23
- })
18
+ it("can handle objects", () => {
19
+ const testObject = { testNumber: "1337", testString: "test" };
20
+ const testUrl =
21
+ "literal://json:%7B%22testNumber%22%3A%221337%22%2C%22testString%22%3A%22test%22%7D";
22
+ expect(Literal.from(testObject).toUrl()).toBe(testUrl);
23
+ expect(Literal.fromUrl(testUrl).get()).toStrictEqual(testObject);
24
+ });
24
25
 
25
- it("can handle special characters", () => {
26
- const testString = "message(X) :- triple('ad4m://self', _, X)."
27
- const testUrl = "literal://string:message%28X%29%20%3A-%20triple%28%27ad4m%3A%2F%2Fself%27%2C%20_%2C%20X%29."
28
- expect(Literal.from(testString).toUrl()).toBe(testUrl)
29
- expect(Literal.fromUrl(testUrl).get()).toBe(testString)
30
- })
31
- })
26
+ it("can handle special characters", () => {
27
+ const testString = "message(X) :- triple('ad4m://self', _, X).";
28
+ const testUrl =
29
+ "literal://string:message%28X%29%20%3A-%20triple%28%27ad4m%3A%2F%2Fself%27%2C%20_%2C%20X%29.";
30
+ expect(Literal.from(testString).toUrl()).toBe(testUrl);
31
+ expect(Literal.fromUrl(testUrl).get()).toBe(testString);
32
+ });
33
+
34
+ it("can handle boolean true", () => {
35
+ const testUrl = "literal://boolean:true";
36
+ expect(Literal.from(true).toUrl()).toBe(testUrl);
37
+ expect(Literal.fromUrl(testUrl).get()).toBe(true);
38
+ });
39
+
40
+ it("can handle boolean false", () => {
41
+ const testUrl = "literal://boolean:false";
42
+ expect(Literal.from(false).toUrl()).toBe(testUrl);
43
+ expect(Literal.fromUrl(testUrl).get()).toBe(false);
44
+ });
45
+
46
+ it("can handle zero", () => {
47
+ expect(Literal.from(0).toUrl()).toBe("literal://number:0");
48
+ expect(Literal.fromUrl("literal://number:0").get()).toBe(0);
49
+ });
50
+ });
package/src/Literal.ts CHANGED
@@ -1,84 +1,100 @@
1
1
  function encodeRFC3986URIComponent(str) {
2
- return encodeURIComponent(str)
3
- .replace(
4
- /[!'()*]/g,
5
- (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`
6
- );
2
+ return encodeURIComponent(str).replace(
3
+ /[!'()*]/g,
4
+ (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`,
5
+ );
7
6
  }
8
7
 
9
8
  export class Literal {
10
- #literal?: any
11
- #url?: string
12
-
13
- public static fromUrl(url: string) {
14
- if(!url || !url.startsWith("literal://"))
15
- throw new Error("Can't create Literal from non-literal URL")
16
- const l = new Literal()
17
- l.#url = url
18
- return l
19
- }
9
+ private _literal?: any;
10
+ private _url?: string;
20
11
 
21
- public static from(literal: any) {
22
- const l = new Literal()
23
- l.#literal = literal
24
- return l
25
- }
12
+ public static fromUrl(url: string) {
13
+ if (!url || !url.startsWith("literal://"))
14
+ throw new Error("Can't create Literal from non-literal URL");
15
+ const l = new Literal();
16
+ l._url = url;
17
+ return l;
18
+ }
26
19
 
27
- toUrl(): string {
28
- if(this.#url && !this.#literal)
29
- return this.#url
30
- if(!this.#url && (this.#literal === undefined || this.#literal === "" || this.#literal === null))
31
- throw new Error("Can't turn empty Literal into URL")
20
+ public static from(literal: any) {
21
+ const l = new Literal();
22
+ l._literal = literal;
23
+ return l;
24
+ }
32
25
 
33
- let encoded
34
- switch(typeof this.#literal) {
35
- case 'string':
36
- encoded = `string:${encodeRFC3986URIComponent(this.#literal)}`
37
- break;
38
- case 'number':
39
- encoded = `number:${encodeRFC3986URIComponent(this.#literal)}`
40
- break;
41
- case 'boolean':
42
- encoded = `boolean:${encodeRFC3986URIComponent(this.#literal)}`
43
- break;
44
- case 'object':
45
- encoded = `json:${encodeRFC3986URIComponent(JSON.stringify(this.#literal))}`
46
- break;
47
- }
26
+ toUrl(): string {
27
+ if (this._url && !this._literal) return this._url;
28
+ if (
29
+ !this._url &&
30
+ (this._literal === undefined ||
31
+ this._literal === "" ||
32
+ this._literal === null)
33
+ )
34
+ throw new Error("Can't turn empty Literal into URL");
48
35
 
49
- return `literal://${encoded}`
36
+ let encoded;
37
+ switch (typeof this._literal) {
38
+ case "string":
39
+ encoded = `string:${encodeRFC3986URIComponent(this._literal)}`;
40
+ break;
41
+ case "number":
42
+ encoded = `number:${encodeRFC3986URIComponent(this._literal)}`;
43
+ break;
44
+ case "boolean":
45
+ encoded = `boolean:${encodeRFC3986URIComponent(this._literal)}`;
46
+ break;
47
+ case "object":
48
+ encoded = `json:${encodeRFC3986URIComponent(JSON.stringify(this._literal))}`;
49
+ break;
50
+ default:
51
+ throw new Error(
52
+ `Literal.toUrl(): unsupported type "${typeof this._literal}" (value: ${String(this._literal)})`,
53
+ );
50
54
  }
51
55
 
52
- get(): any {
53
- if(this.#literal)
54
- return this.#literal
55
-
56
- if(!this.#url)
57
- throw new Error("Can't render empty Literal")
56
+ return `literal://${encoded}`;
57
+ }
58
58
 
59
- if(!this.#url.startsWith("literal://"))
60
- throw new Error("Can't render Literal from non-literal URL")
61
-
62
- // get rid of "literal://"
63
- const body = this.#url.substring(10)
64
-
59
+ get(): any {
60
+ if (
61
+ this._literal !== undefined &&
62
+ this._literal !== null &&
63
+ this._literal !== ""
64
+ )
65
+ return this._literal;
65
66
 
66
- if(body.startsWith("string:")) {
67
- return decodeURIComponent(body.substring(7))
68
- }
67
+ if (!this._url) throw new Error("Can't render empty Literal");
69
68
 
70
- if(body.startsWith("number:")) {
71
- const numberString = body.substring(7)
72
- return parseFloat(numberString)
73
- }
69
+ if (!this._url.startsWith("literal://"))
70
+ throw new Error("Can't render Literal from non-literal URL");
74
71
 
75
- if(body.startsWith("json:")) {
76
- const json = body.substring(5)
77
- return JSON.parse(decodeURIComponent(json))
78
- }
72
+ // get rid of "literal://"
73
+ const body = this._url.substring(10);
79
74
 
80
- throw new Error(`Can't parse unknown literal: ${body}`)
75
+ if (body.startsWith("string:")) {
76
+ return decodeURIComponent(body.substring(7));
81
77
  }
82
78
 
79
+ if (body.startsWith("number:")) {
80
+ const numberString = body.substring(7);
81
+ return parseFloat(numberString);
82
+ }
83
83
 
84
- }
84
+ if (body.startsWith("boolean:")) {
85
+ const boolStr = body.substring(8).trim();
86
+ if (boolStr === "true") return true;
87
+ if (boolStr === "false") return false;
88
+ throw new Error(
89
+ `Literal.get(): malformed boolean payload "${boolStr}" — expected "true" or "false"`,
90
+ );
91
+ }
92
+
93
+ if (body.startsWith("json:")) {
94
+ const json = body.substring(5);
95
+ return JSON.parse(decodeURIComponent(json));
96
+ }
97
+
98
+ throw new Error(`Can't parse unknown literal: ${body}`);
99
+ }
100
+ }
@@ -3,73 +3,89 @@ import { Literal } from "./Literal";
3
3
  import { LinkQuery } from "./perspectives/LinkQuery";
4
4
  import { PerspectiveProxy } from "./perspectives/PerspectiveProxy";
5
5
 
6
- export const SMART_LITERAL_CONTENT_PREDICATE = "smart_literal://content"
7
-
6
+ export const SMART_LITERAL_CONTENT_PREDICATE = "smart_literal://content";
8
7
 
9
8
  function makeRandomStringID(length: number): string {
10
- let result = '';
11
- let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
12
- let charactersLength = characters.length;
13
- for (let i = 0; i < length; i++) {
14
- result += characters.charAt(Math.floor(Math.random() * charactersLength));
15
- }
16
- return result;
17
- }
9
+ let result = "";
10
+ let characters =
11
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
12
+ let charactersLength = characters.length;
13
+ for (let i = 0; i < length; i++) {
14
+ result += characters.charAt(Math.floor(Math.random() * charactersLength));
15
+ }
16
+ return result;
17
+ }
18
18
 
19
19
  export class SmartLiteral {
20
- #perspective: PerspectiveProxy
21
- #base: string
20
+ private _perspective: PerspectiveProxy;
21
+ private _base: string;
22
22
 
23
- constructor(perspective: PerspectiveProxy, base: string) {
24
- this.#perspective = perspective
25
- this.#base = base
26
- }
23
+ constructor(perspective: PerspectiveProxy, base: string) {
24
+ this._perspective = perspective;
25
+ this._base = base;
26
+ }
27
27
 
28
- get base() {
29
- return this.#base
30
- }
28
+ get base() {
29
+ return this._base;
30
+ }
31
31
 
32
- public static async create(perspective: PerspectiveProxy, literal: any): Promise<SmartLiteral> {
33
- const base = Literal.from(makeRandomStringID(10)).toUrl()
34
- const smartLiteral = new SmartLiteral(perspective, base)
35
- await smartLiteral.set(literal)
36
- return smartLiteral
37
- }
32
+ public static async create(
33
+ perspective: PerspectiveProxy,
34
+ literal: any,
35
+ ): Promise<SmartLiteral> {
36
+ const base = Literal.from(makeRandomStringID(10)).toUrl();
37
+ const smartLiteral = new SmartLiteral(perspective, base);
38
+ await smartLiteral.set(literal);
39
+ return smartLiteral;
40
+ }
38
41
 
39
- public static async isSmartLiteralBase(perspective: PerspectiveProxy, base: string): Promise<boolean> {
40
- let links = await perspective.get(new LinkQuery({
41
- source: base,
42
- predicate: SMART_LITERAL_CONTENT_PREDICATE
43
- }))
44
- return links.length > 0
45
- }
42
+ public static async isSmartLiteralBase(
43
+ perspective: PerspectiveProxy,
44
+ base: string,
45
+ ): Promise<boolean> {
46
+ let links = await perspective.get(
47
+ new LinkQuery({
48
+ source: base,
49
+ predicate: SMART_LITERAL_CONTENT_PREDICATE,
50
+ }),
51
+ );
52
+ return links.length > 0;
53
+ }
46
54
 
47
- public static async getAllSmartLiterals(perspective: PerspectiveProxy): Promise<SmartLiteral[]> {
48
- let links = await perspective.get(new LinkQuery({
49
- predicate: SMART_LITERAL_CONTENT_PREDICATE
50
- }))
51
- return links.map(link => new SmartLiteral(perspective, link.data.source))
52
- }
53
-
54
- async get(): Promise<any> {
55
- let link = await this.#perspective.getSingleTarget(new LinkQuery({
56
- source: this.#base,
57
- predicate: SMART_LITERAL_CONTENT_PREDICATE
58
- }))
55
+ public static async getAllSmartLiterals(
56
+ perspective: PerspectiveProxy,
57
+ ): Promise<SmartLiteral[]> {
58
+ let links = await perspective.get(
59
+ new LinkQuery({
60
+ predicate: SMART_LITERAL_CONTENT_PREDICATE,
61
+ }),
62
+ );
63
+ return links.map((link) => new SmartLiteral(perspective, link.data.source));
64
+ }
59
65
 
60
- if(!link) {
61
- throw `No content for smart literal ${this.#base}`
62
- }
66
+ async get(): Promise<any> {
67
+ let link = await this._perspective.getSingleTarget(
68
+ new LinkQuery({
69
+ source: this._base,
70
+ predicate: SMART_LITERAL_CONTENT_PREDICATE,
71
+ }),
72
+ );
63
73
 
64
- return Literal.fromUrl(link).get()
65
- }
66
-
67
- async set(content: any) {
68
- let literal = Literal.from(content)
69
- await this.#perspective.setSingleTarget(new Link({
70
- source: this.#base,
71
- predicate: SMART_LITERAL_CONTENT_PREDICATE,
72
- target: literal.toUrl()
73
- }))
74
+ if (!link) {
75
+ throw `No content for smart literal ${this._base}`;
74
76
  }
75
- }
77
+
78
+ return Literal.fromUrl(link).get();
79
+ }
80
+
81
+ async set(content: any) {
82
+ let literal = Literal.from(content);
83
+ await this._perspective.setSingleTarget(
84
+ new Link({
85
+ source: this._base,
86
+ predicate: SMART_LITERAL_CONTENT_PREDICATE,
87
+ target: literal.toUrl(),
88
+ }),
89
+ );
90
+ }
91
+ }