@graphprotocol/hypergraph 0.1.0 → 0.2.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/Entity.d.ts +69 -0
- package/dist/Entity.d.ts.map +1 -0
- package/dist/Entity.js +174 -0
- package/dist/Entity.js.map +1 -0
- package/dist/identity/create-identity-keys.d.ts +3 -0
- package/dist/identity/create-identity-keys.d.ts.map +1 -0
- package/dist/identity/create-identity-keys.js +20 -0
- package/dist/identity/create-identity-keys.js.map +1 -0
- package/dist/identity/login.d.ts +38 -0
- package/dist/identity/login.d.ts.map +1 -0
- package/dist/identity/login.js +241 -0
- package/dist/identity/login.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/mapping/Mapping.d.ts +418 -0
- package/dist/mapping/Mapping.d.ts.map +1 -0
- package/dist/mapping/Mapping.js +554 -0
- package/dist/mapping/Mapping.js.map +1 -0
- package/dist/mapping/Utils.d.ts +74 -0
- package/dist/mapping/Utils.d.ts.map +1 -0
- package/dist/mapping/Utils.js +144 -0
- package/dist/mapping/Utils.js.map +1 -0
- package/dist/mapping/index.d.ts +3 -0
- package/dist/mapping/index.d.ts.map +1 -0
- package/dist/mapping/index.js +3 -0
- package/dist/mapping/index.js.map +1 -0
- package/dist/store.d.ts +1 -1
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js.map +1 -1
- package/dist/utils/hasArrayField.d.ts +2 -0
- package/dist/utils/hasArrayField.d.ts.map +1 -0
- package/dist/utils/hasArrayField.js +5 -0
- package/dist/utils/hasArrayField.js.map +1 -0
- package/package.json +3 -5
- package/src/index.ts +1 -0
- package/src/mapping/Mapping.ts +771 -0
- package/src/mapping/Utils.ts +156 -0
- package/src/mapping/index.ts +2 -0
- package/src/store.ts +1 -1
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { Data, String as EffectString } from 'effect';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Takes the input string and returns the camelCase equivalent
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { toCamelCase } from '@graphprotocol/hypergraph/mapping'
|
|
9
|
+
*
|
|
10
|
+
* expect(toCamelCase('Address line 1')).toEqual('addressLine1');
|
|
11
|
+
* expect(toCamelCase('AddressLine1')).toEqual('addressLine1');
|
|
12
|
+
* expect(toCamelCase('addressLine1')).toEqual('addressLine1');
|
|
13
|
+
* expect(toCamelCase('address_line_1')).toEqual('addressLine1');
|
|
14
|
+
* expect(toCamelCase('address-line-1')).toEqual('addressLine1');
|
|
15
|
+
* expect(toCamelCase('address-line_1')).toEqual('addressLine1');
|
|
16
|
+
* expect(toCamelCase('address-line 1')).toEqual('addressLine1');
|
|
17
|
+
* expect(toCamelCase('ADDRESS_LINE_1')).toEqual('addressLine1');
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @since 0.2.0
|
|
21
|
+
*
|
|
22
|
+
* @param str input string
|
|
23
|
+
* @returns camelCased value of the input string
|
|
24
|
+
*/
|
|
25
|
+
export function toCamelCase(str: string): string {
|
|
26
|
+
if (EffectString.isEmpty(str)) {
|
|
27
|
+
throw new InvalidInputError({ input: str, cause: 'Input is empty' });
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let result = '';
|
|
31
|
+
let capitalizeNext = false;
|
|
32
|
+
let i = 0;
|
|
33
|
+
|
|
34
|
+
// Skip leading non-alphanumeric characters
|
|
35
|
+
while (i < EffectString.length(str) && !/[a-zA-Z0-9]/.test(str[i])) {
|
|
36
|
+
i++;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
for (; i < EffectString.length(str); i++) {
|
|
40
|
+
const char = str[i];
|
|
41
|
+
|
|
42
|
+
if (/[a-zA-Z0-9]/.test(char)) {
|
|
43
|
+
if (capitalizeNext) {
|
|
44
|
+
result += EffectString.toUpperCase(char);
|
|
45
|
+
capitalizeNext = false;
|
|
46
|
+
} else if (EffectString.length(result) === 0) {
|
|
47
|
+
// First character should always be lowercase
|
|
48
|
+
result += EffectString.toLowerCase(char);
|
|
49
|
+
} else if (/[A-Z]/.test(char) && i > 0 && /[a-z0-9]/.test(str[i - 1])) {
|
|
50
|
+
// Capital letter following lowercase/number - this indicates a word boundary
|
|
51
|
+
// So we need to capitalize this letter (it starts a new word)
|
|
52
|
+
result += EffectString.toUpperCase(char);
|
|
53
|
+
} else {
|
|
54
|
+
result += EffectString.toLowerCase(char);
|
|
55
|
+
}
|
|
56
|
+
} else {
|
|
57
|
+
// Non-alphanumeric character - set flag to capitalize next letter
|
|
58
|
+
capitalizeNext = EffectString.length(result) > 0; // Only capitalize if we have existing content
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Takes the input string and returns the PascalCase equivalent
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* iimport { toPascalCase } from '@graphprotocol/hypergraph/mapping'
|
|
71
|
+
*
|
|
72
|
+
* expect(toPascalCase('Address line 1')).toEqual('AddressLine1');
|
|
73
|
+
* expect(toPascalCase('AddressLine1')).toEqual('AddressLine1');
|
|
74
|
+
* expect(toPascalCase('addressLine1')).toEqual('AddressLine1');
|
|
75
|
+
* expect(toPascalCase('address_line_1')).toEqual('AddressLine1');
|
|
76
|
+
* expect(toPascalCase('address-line-1')).toEqual('AddressLine1');
|
|
77
|
+
* expect(toPascalCase('address-line_1')).toEqual('AddressLine1');
|
|
78
|
+
* expect(toPascalCase('address-line 1')).toEqual('AddressLine1');
|
|
79
|
+
* expect(toPascalCase('ADDRESS_LINE_1')).toEqual('AddressLine1');
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* @since 0.2.0
|
|
83
|
+
*
|
|
84
|
+
* @param str input string
|
|
85
|
+
* @returns PascalCased value of the input string
|
|
86
|
+
*/
|
|
87
|
+
export function toPascalCase(str: string): string {
|
|
88
|
+
if (EffectString.isEmpty(str)) {
|
|
89
|
+
throw new InvalidInputError({ input: str, cause: 'Input is empty' });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
let result = '';
|
|
93
|
+
let capitalizeNext = true; // Start with true to capitalize the first letter
|
|
94
|
+
let i = 0;
|
|
95
|
+
|
|
96
|
+
// Skip leading non-alphanumeric characters
|
|
97
|
+
while (i < EffectString.length(str) && !/[a-zA-Z0-9]/.test(str[i])) {
|
|
98
|
+
i++;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
for (; i < EffectString.length(str); i++) {
|
|
102
|
+
const char = str[i];
|
|
103
|
+
|
|
104
|
+
if (/[a-zA-Z0-9]/.test(char)) {
|
|
105
|
+
if (capitalizeNext) {
|
|
106
|
+
result += EffectString.toUpperCase(char);
|
|
107
|
+
capitalizeNext = false;
|
|
108
|
+
} else if (/[A-Z]/.test(char) && i > 0 && /[a-z0-9]/.test(str[i - 1])) {
|
|
109
|
+
// Capital letter following lowercase/number - this indicates a word boundary
|
|
110
|
+
// So we need to capitalize this letter (it starts a new word)
|
|
111
|
+
result += EffectString.toUpperCase(char);
|
|
112
|
+
} else {
|
|
113
|
+
result += EffectString.toLowerCase(char);
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
// Non-alphanumeric character - set flag to capitalize next letter
|
|
117
|
+
capitalizeNext = true;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export class InvalidInputError extends Data.TaggedError('/typesync/errors/InvalidInputError')<{
|
|
125
|
+
readonly input: string;
|
|
126
|
+
readonly cause: unknown;
|
|
127
|
+
}> {}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Adds schema validation that the array of objects with property `name` only has unique names
|
|
131
|
+
*
|
|
132
|
+
* @example <caption>only unique names -> returns true</caption>
|
|
133
|
+
* ```ts
|
|
134
|
+
* const types = [{name:'Account'}, {name:'Event'}]
|
|
135
|
+
* expect(namesAreUnique(types)).toEqual(true)
|
|
136
|
+
* ```
|
|
137
|
+
*
|
|
138
|
+
* @example <caption>duplicate name -> returns false</caption>
|
|
139
|
+
* ```ts
|
|
140
|
+
* const types = [{name:'Account'}, {name:'Event'}, {name:'Account'}]
|
|
141
|
+
* expect(namesAreUnique(types)).toEqual(false)
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
export function namesAreUnique<T extends { readonly name: string }>(entries: ReadonlyArray<T>): boolean {
|
|
145
|
+
const names = new Set<string>();
|
|
146
|
+
|
|
147
|
+
for (const entry of entries) {
|
|
148
|
+
const name = EffectString.toLowerCase(entry.name);
|
|
149
|
+
if (names.has(name)) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
names.add(name);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return true;
|
|
156
|
+
}
|
package/src/store.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { AnyDocumentId, DocHandle, Repo } from '@automerge/automerge-repo';
|
|
2
|
-
import type { Mapping } from '@graphprotocol/typesync/Mapping';
|
|
3
2
|
import { createStore, type Store } from '@xstate/store';
|
|
4
3
|
import type { PrivateAppIdentity } from './connect/types.js';
|
|
5
4
|
import type { DocumentContent } from './entity/types.js';
|
|
6
5
|
import { mergeMessages } from './inboxes/merge-messages.js';
|
|
7
6
|
import type { InboxSenderAuthPolicy } from './inboxes/types.js';
|
|
7
|
+
import type { Mapping } from './mapping/Mapping.js';
|
|
8
8
|
import type { Invitation, Updates } from './messages/index.js';
|
|
9
9
|
import type { SpaceEvent, SpaceState } from './space-events/index.js';
|
|
10
10
|
import { idToAutomergeId } from './utils/automergeId.js';
|