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

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 (153) hide show
  1. package/jest.config.js +10 -3
  2. package/lib/index.cjs +6893 -5884
  3. package/lib/index.js +6889 -5872
  4. package/lib/src/Ad4mClient.d.ts +9 -16
  5. package/lib/src/Ad4mClient.js +46 -25
  6. package/lib/src/Literal.d.ts +1 -2
  7. package/lib/src/Literal.js +38 -35
  8. package/lib/src/SmartLiteral.d.ts +1 -2
  9. package/lib/src/SmartLiteral.js +30 -15
  10. package/lib/src/agent/AgentClient.d.ts +1 -4
  11. package/lib/src/agent/AgentClient.js +79 -99
  12. package/lib/src/ai/AIClient.d.ts +1 -2
  13. package/lib/src/ai/AIClient.js +224 -220
  14. package/lib/src/expression/ExpressionClient.d.ts +1 -1
  15. package/lib/src/expression/ExpressionClient.js +73 -86
  16. package/lib/src/index.d.ts +1 -1
  17. package/lib/src/index.js +1 -1
  18. package/lib/src/language/LanguageClient.d.ts +1 -1
  19. package/lib/src/language/LanguageClient.js +52 -52
  20. package/lib/src/model/Ad4mModel.d.ts +1351 -144
  21. package/lib/src/model/Ad4mModel.js +3760 -228
  22. package/lib/src/model/Ad4mModel.test.js +522 -979
  23. package/lib/src/model/Subject.d.ts +3 -3
  24. package/lib/src/model/Subject.js +25 -25
  25. package/lib/src/model/decorators.d.ts +444 -100
  26. package/lib/src/model/decorators.js +686 -321
  27. package/lib/src/model/util.d.ts +6 -6
  28. package/lib/src/model/util.js +24 -24
  29. package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +1 -5
  30. package/lib/src/neighbourhood/NeighbourhoodClient.js +179 -242
  31. package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +1 -2
  32. package/lib/src/neighbourhood/NeighbourhoodProxy.js +28 -13
  33. package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +5 -1
  34. package/lib/src/perspectives/PerspectiveClient.d.ts +9 -9
  35. package/lib/src/perspectives/PerspectiveClient.js +240 -316
  36. package/lib/src/perspectives/PerspectiveProxy.d.ts +99 -61
  37. package/lib/src/perspectives/PerspectiveProxy.js +485 -832
  38. package/lib/src/runtime/RuntimeClient.d.ts +1 -5
  39. package/lib/src/runtime/RuntimeClient.js +238 -412
  40. package/lib/src/shacl/SHACLFlow.test.js +105 -164
  41. package/lib/src/shacl/SHACLShape.d.ts +12 -17
  42. package/lib/src/shacl/SHACLShape.js +115 -128
  43. package/lib/src/shacl/SHACLShape.test.js +176 -244
  44. package/lib/src/shims/reflect-metadata.d.ts +0 -0
  45. package/lib/src/shims/type-graphql.d.ts +32 -0
  46. package/lib/src/src/Ad4mClient.d.ts +28 -0
  47. package/lib/src/src/Address.d.ts +1 -0
  48. package/lib/src/src/DID.d.ts +1 -0
  49. package/lib/src/src/Exception.d.ts +7 -0
  50. package/lib/src/src/Literal.d.ts +7 -0
  51. package/lib/src/src/PubSub.d.ts +17 -0
  52. package/lib/src/src/SmartLiteral.d.ts +12 -0
  53. package/lib/src/src/agent/Agent.d.ts +110 -0
  54. package/lib/src/src/agent/AgentClient.d.ts +62 -0
  55. package/lib/src/src/agent/AgentResolver.d.ts +30 -0
  56. package/lib/src/src/agent/AgentStatus.d.ts +8 -0
  57. package/lib/src/src/ai/AIClient.d.ts +32 -0
  58. package/lib/src/src/ai/AIResolver.d.ts +75 -0
  59. package/lib/src/src/ai/Tasks.d.ts +37 -0
  60. package/lib/src/src/buildSchema.d.ts +1 -0
  61. package/lib/src/src/expression/Expression.d.ts +28 -0
  62. package/lib/src/src/expression/ExpressionClient.d.ts +13 -0
  63. package/lib/src/src/expression/ExpressionRef.d.ts +9 -0
  64. package/lib/src/src/expression/ExpressionResolver.d.ts +10 -0
  65. package/lib/src/src/index.d.ts +35 -0
  66. package/lib/src/src/jestSetup.d.ts +1 -0
  67. package/lib/src/src/language/Icon.d.ts +4 -0
  68. package/lib/src/src/language/Language.d.ts +206 -0
  69. package/lib/src/src/language/LanguageClient.d.ts +17 -0
  70. package/lib/src/src/language/LanguageContext.d.ts +37 -0
  71. package/lib/src/src/language/LanguageHandle.d.ts +9 -0
  72. package/lib/src/src/language/LanguageMeta.d.ts +35 -0
  73. package/lib/src/src/language/LanguageRef.d.ts +6 -0
  74. package/lib/src/src/language/LanguageResolver.d.ts +19 -0
  75. package/lib/src/src/links/Links.d.ts +39 -0
  76. package/lib/src/src/links/Links.test.d.ts +1 -0
  77. package/lib/src/src/model/Ad4mModel.d.ts +1438 -0
  78. package/lib/src/src/model/Ad4mModel.test.d.ts +1 -0
  79. package/lib/src/src/model/Subject.d.ts +25 -0
  80. package/lib/src/src/model/decorators.d.ts +545 -0
  81. package/lib/src/src/model/util.d.ts +12 -0
  82. package/lib/src/src/neighbourhood/Neighbourhood.d.ts +10 -0
  83. package/lib/src/src/neighbourhood/NeighbourhoodClient.d.ts +25 -0
  84. package/lib/src/src/neighbourhood/NeighbourhoodProxy.d.ts +19 -0
  85. package/lib/src/src/neighbourhood/NeighbourhoodProxy.test.d.ts +1 -0
  86. package/lib/src/src/neighbourhood/NeighbourhoodResolver.d.ts +24 -0
  87. package/lib/src/src/perspectives/LinkQuery.d.ts +11 -0
  88. package/lib/src/src/perspectives/Perspective.d.ts +38 -0
  89. package/lib/src/src/perspectives/Perspective.test.d.ts +1 -0
  90. package/lib/src/src/perspectives/PerspectiveClient.d.ts +97 -0
  91. package/lib/src/src/perspectives/PerspectiveDiff.d.ts +9 -0
  92. package/lib/src/src/perspectives/PerspectiveHandle.d.ts +18 -0
  93. package/lib/src/src/perspectives/PerspectiveProxy.d.ts +865 -0
  94. package/lib/src/src/perspectives/PerspectiveResolver.d.ts +57 -0
  95. package/lib/src/src/runtime/RuntimeClient.d.ts +56 -0
  96. package/lib/src/src/runtime/RuntimeResolver.d.ts +132 -0
  97. package/lib/src/src/shacl/SHACLFlow.d.ts +171 -0
  98. package/lib/src/src/shacl/SHACLFlow.test.d.ts +1 -0
  99. package/lib/src/src/shacl/SHACLShape.d.ts +117 -0
  100. package/lib/src/src/shacl/SHACLShape.test.d.ts +1 -0
  101. package/lib/src/src/typeDefs.d.ts +1 -0
  102. package/lib/src/src/unwrapApolloResult.d.ts +2 -0
  103. package/lib/src/src/utils.d.ts +25 -0
  104. package/lib/src/utils.d.ts +0 -8
  105. package/lib/src/utils.js +7 -21
  106. package/package.json +15 -14
  107. package/src/Ad4mClient.test.ts +1315 -1564
  108. package/src/Ad4mClient.ts +54 -55
  109. package/src/Literal.test.ts +26 -45
  110. package/src/Literal.ts +66 -82
  111. package/src/SmartLiteral.ts +58 -74
  112. package/src/agent/AgentClient.ts +99 -144
  113. package/src/ai/AIClient.ts +359 -409
  114. package/src/expression/ExpressionClient.ts +96 -142
  115. package/src/index.ts +1 -1
  116. package/src/language/LanguageClient.ts +113 -137
  117. package/src/model/Ad4mModel.test.ts +616 -1114
  118. package/src/model/Ad4mModel.ts +4386 -451
  119. package/src/model/Subject.ts +159 -0
  120. package/src/model/decorators.ts +1168 -667
  121. package/src/model/util.ts +54 -53
  122. package/src/neighbourhood/NeighbourhoodClient.ts +266 -392
  123. package/src/neighbourhood/NeighbourhoodProxy.test.ts +7 -3
  124. package/src/neighbourhood/NeighbourhoodProxy.ts +40 -58
  125. package/src/perspectives/PerspectiveClient.ts +540 -779
  126. package/src/perspectives/PerspectiveProxy.ts +2067 -2860
  127. package/src/runtime/RuntimeClient.ts +476 -775
  128. package/src/shacl/SHACLFlow.test.ts +129 -196
  129. package/src/shacl/SHACLShape.test.ts +190 -284
  130. package/src/shacl/SHACLShape.ts +200 -224
  131. package/src/utils.ts +17 -31
  132. package/tsconfig.json +10 -9
  133. package/src/model/IMPROVEMENTS.md +0 -482
  134. package/src/model/PREDICATE_COLLISION_FIX.md +0 -651
  135. package/src/model/README.md +0 -317
  136. package/src/model/SHACL_MUTATION_DECOUPLING.md +0 -356
  137. package/src/model/SUBSCRIPTION_STRATEGY.md +0 -317
  138. package/src/model/TC39_STAGE_3_DECORATORS.md +0 -609
  139. package/src/model/mutation.ts +0 -461
  140. package/src/model/parentUtils.ts +0 -91
  141. package/src/model/prolog/generatePrologFacts.ts +0 -176
  142. package/src/model/prolog/index.ts +0 -1
  143. package/src/model/query/ModelQueryBuilder.ts +0 -173
  144. package/src/model/query/fetchInstance.ts +0 -181
  145. package/src/model/query/hydration.ts +0 -273
  146. package/src/model/query/operations.ts +0 -510
  147. package/src/model/query/snapshot.ts +0 -126
  148. package/src/model/query/surrealCompiler.ts +0 -376
  149. package/src/model/schema/fromJSONSchema.ts +0 -411
  150. package/src/model/schema/metadata.ts +0 -148
  151. package/src/model/subscription.ts +0 -307
  152. package/src/model/transaction.ts +0 -60
  153. package/src/model/types.ts +0 -207
package/src/Ad4mClient.ts CHANGED
@@ -1,73 +1,72 @@
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
- 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;
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
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
- }
45
29
 
46
- get agent(): AgentClient {
47
- return this._agentClient;
48
- }
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
+ }
49
44
 
50
- get expression(): ExpressionClient {
51
- return this._expressionClient;
52
- }
45
+ get agent(): AgentClient {
46
+ return this.#agentClient
47
+ }
53
48
 
54
- get languages(): LanguageClient {
55
- return this._languageClient;
56
- }
49
+ get expression(): ExpressionClient {
50
+ return this.#expressionClient
51
+ }
57
52
 
58
- get neighbourhood(): NeighbourhoodClient {
59
- return this._neighbourhoodClient;
60
- }
53
+ get languages(): LanguageClient {
54
+ return this.#languageClient
55
+ }
61
56
 
62
- get perspective(): PerspectiveClient {
63
- return this._perspectiveClient;
64
- }
57
+ get neighbourhood(): NeighbourhoodClient {
58
+ return this.#neighbourhoodClient
59
+ }
65
60
 
66
- get runtime(): RuntimeClient {
67
- return this._runtimeClient;
68
- }
61
+ get perspective(): PerspectiveClient {
62
+ return this.#perspectiveClient
63
+ }
69
64
 
70
- get ai(): AIClient {
71
- return this._aiClient;
72
- }
73
- }
65
+ get runtime(): RuntimeClient {
66
+ return this.#runtimeClient
67
+ }
68
+
69
+ get ai(): AIClient {
70
+ return this.#aiClient
71
+ }
72
+ }
@@ -1,50 +1,31 @@
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 =
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
- });
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
+ })
25
24
 
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
- });
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
+ })
package/src/Literal.ts CHANGED
@@ -1,100 +1,84 @@
1
1
  function encodeRFC3986URIComponent(str) {
2
- return encodeURIComponent(str).replace(
3
- /[!'()*]/g,
4
- (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`,
5
- );
2
+ return encodeURIComponent(str)
3
+ .replace(
4
+ /[!'()*]/g,
5
+ (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`
6
+ );
6
7
  }
7
8
 
8
9
  export class Literal {
9
- private _literal?: any;
10
- private _url?: string;
11
-
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
- }
19
-
20
- public static from(literal: any) {
21
- const l = new Literal();
22
- l._literal = literal;
23
- return l;
24
- }
10
+ #literal?: any
11
+ #url?: string
25
12
 
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");
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
+ }
35
20
 
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
- );
21
+ public static from(literal: any) {
22
+ const l = new Literal()
23
+ l.#literal = literal
24
+ return l
54
25
  }
55
26
 
56
- return `literal://${encoded}`;
57
- }
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")
58
32
 
59
- get(): any {
60
- if (
61
- this._literal !== undefined &&
62
- this._literal !== null &&
63
- this._literal !== ""
64
- )
65
- return this._literal;
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
+ }
66
48
 
67
- if (!this._url) throw new Error("Can't render empty Literal");
49
+ return `literal://${encoded}`
50
+ }
68
51
 
69
- if (!this._url.startsWith("literal://"))
70
- throw new Error("Can't render Literal from non-literal URL");
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")
71
58
 
72
- // get rid of "literal://"
73
- const body = this._url.substring(10);
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
+
74
65
 
75
- if (body.startsWith("string:")) {
76
- return decodeURIComponent(body.substring(7));
77
- }
66
+ if(body.startsWith("string:")) {
67
+ return decodeURIComponent(body.substring(7))
68
+ }
78
69
 
79
- if (body.startsWith("number:")) {
80
- const numberString = body.substring(7);
81
- return parseFloat(numberString);
82
- }
70
+ if(body.startsWith("number:")) {
71
+ const numberString = body.substring(7)
72
+ return parseFloat(numberString)
73
+ }
83
74
 
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
- }
75
+ if(body.startsWith("json:")) {
76
+ const json = body.substring(5)
77
+ return JSON.parse(decodeURIComponent(json))
78
+ }
92
79
 
93
- if (body.startsWith("json:")) {
94
- const json = body.substring(5);
95
- return JSON.parse(decodeURIComponent(json));
80
+ throw new Error(`Can't parse unknown literal: ${body}`)
96
81
  }
97
82
 
98
- throw new Error(`Can't parse unknown literal: ${body}`);
99
- }
100
- }
83
+
84
+ }
@@ -3,89 +3,73 @@ 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";
6
+ export const SMART_LITERAL_CONTENT_PREDICATE = "smart_literal://content"
7
+
7
8
 
8
9
  function makeRandomStringID(length: number): string {
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
- }
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
+ }
18
18
 
19
19
  export class SmartLiteral {
20
- private _perspective: PerspectiveProxy;
21
- private _base: string;
22
-
23
- constructor(perspective: PerspectiveProxy, base: string) {
24
- this._perspective = perspective;
25
- this._base = base;
26
- }
20
+ #perspective: PerspectiveProxy
21
+ #base: string
27
22
 
28
- get base() {
29
- return this._base;
30
- }
31
-
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
- }
23
+ constructor(perspective: PerspectiveProxy, base: string) {
24
+ this.#perspective = perspective
25
+ this.#base = base
26
+ }
41
27
 
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
- }
28
+ get base() {
29
+ return this.#base
30
+ }
54
31
 
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
- }
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
+ }
65
38
 
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
- );
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
+ }
73
46
 
74
- if (!link) {
75
- throw `No content for smart literal ${this._base}`;
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))
76
52
  }
77
53
 
78
- return Literal.fromUrl(link).get();
79
- }
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
+ }))
59
+
60
+ if(!link) {
61
+ throw `No content for smart literal ${this.#base}`
62
+ }
80
63
 
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
- }
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
+ }
75
+ }