schemind 0.1.1 → 0.2.1
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/README.md +141 -5
- package/dist/schemind.cjs +1 -1
- package/dist/schemind.d.ts +13 -7
- package/dist/schemind.iife.js +1 -1
- package/dist/schemind.js +61 -61
- package/dist/schemind.umd.cjs +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@ In formats like JSON, a message normally looks something like this:
|
|
|
27
27
|
```
|
|
28
28
|
*I'm using JSON as an example here, but schemind is essentially protocol-agnostic. I use it with MessagePack.*
|
|
29
29
|
|
|
30
|
-
If you desperately need to make this message more compact, you could alternatively serialize it
|
|
30
|
+
If you desperately need to make this message more compact, you could alternatively serialize it like so:
|
|
31
31
|
```json
|
|
32
32
|
[
|
|
33
33
|
1,
|
|
@@ -49,10 +49,11 @@ If you desperately need to make this message more compact, you could alternative
|
|
|
49
49
|
|
|
50
50
|
This is sometimes referred to as a message with *indexed keys*.
|
|
51
51
|
|
|
52
|
-
*Note that this obviously has some drawbacks: [recommended reading about the pros and cons of this format](https://github.com/MessagePack-CSharp/MessagePack-CSharp#use-indexed-keys-instead-of-string-keys-contractless).*
|
|
53
|
-
|
|
54
52
|
**Schemind** helps you create and read such messages, if your (de)serializer doesn't support this technique.
|
|
55
53
|
|
|
54
|
+
|
|
55
|
+
*Note that this format obviously has some drawbacks: [recommended reading about the pros and cons](https://github.com/MessagePack-CSharp/MessagePack-CSharp#use-indexed-keys-instead-of-string-keys-contractless).*
|
|
56
|
+
|
|
56
57
|
## Installation
|
|
57
58
|
|
|
58
59
|
```shell
|
|
@@ -60,7 +61,142 @@ npm install schemind
|
|
|
60
61
|
```
|
|
61
62
|
|
|
62
63
|
## Usage
|
|
63
|
-
|
|
64
|
+
### Defining a schema
|
|
65
|
+
```typescript
|
|
66
|
+
import { buildSchema, withIndex as i } from "schemind";
|
|
67
|
+
|
|
68
|
+
const personSchema = buildSchema({
|
|
69
|
+
id: i(0)<number>(),
|
|
70
|
+
fullName: i(1)<string>(),
|
|
71
|
+
email: i(2)<string>(),
|
|
72
|
+
birthDate: i(3)<Date>(),
|
|
73
|
+
address: i(4)({
|
|
74
|
+
street: i(0)<string>(),
|
|
75
|
+
city: i(1)<string>(),
|
|
76
|
+
zipcode: i(2)<string>(),
|
|
77
|
+
geo: i(3)({
|
|
78
|
+
lat: i(0)<number>(),
|
|
79
|
+
lng: i(1)<number>()
|
|
80
|
+
})
|
|
81
|
+
}),
|
|
82
|
+
website: i(5)<string>()
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
Every field needs to have its index in the message specified using `withIndex`.
|
|
86
|
+
Note that this also goes for nested objects, such as `address`.
|
|
87
|
+
|
|
88
|
+
* If you accidentally pass the same index twice, or you forget to call `withIndex` on any nested object, `buildSchema` will throw an `InvalidSchemaError`.
|
|
89
|
+
* If you forget to call `buildSchema` on your object, you'll get a type error when trying to use your schema.
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
### Reading from a message
|
|
93
|
+
Say you have an incoming message (from network/storage/whatever) like this:
|
|
94
|
+
```typescript
|
|
95
|
+
const incomingMessage = JSON.parse(`
|
|
96
|
+
[
|
|
97
|
+
1,
|
|
98
|
+
"John Doe",
|
|
99
|
+
"johndoe@example.com",
|
|
100
|
+
"1973-01-22",
|
|
101
|
+
[
|
|
102
|
+
"123 Main Street",
|
|
103
|
+
"Anytown",
|
|
104
|
+
"12345-6789",
|
|
105
|
+
[
|
|
106
|
+
42.1234,
|
|
107
|
+
-71.2345
|
|
108
|
+
]
|
|
109
|
+
],
|
|
110
|
+
"www.johndoe.com"
|
|
111
|
+
]`);
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
There are 2 ways to read this message:
|
|
115
|
+
#### • `toPlainObject`
|
|
116
|
+
This is the more convenient option.
|
|
117
|
+
```typescript
|
|
118
|
+
import { toPlainObject } from "schemind";
|
|
119
|
+
|
|
120
|
+
const messageAsObject = toPlainObject(incomingMessage, personSchema);
|
|
121
|
+
// ^ this has the following type:
|
|
122
|
+
// {
|
|
123
|
+
// id: number,
|
|
124
|
+
// fullName: string,
|
|
125
|
+
// email: string,
|
|
126
|
+
// birthDate: Date,
|
|
127
|
+
// address: {
|
|
128
|
+
// street: string,
|
|
129
|
+
// city: string,
|
|
130
|
+
// zipcode: string,
|
|
131
|
+
// geo: {
|
|
132
|
+
// lat: number,
|
|
133
|
+
// lng: number
|
|
134
|
+
// }
|
|
135
|
+
// },
|
|
136
|
+
// website: string
|
|
137
|
+
// }
|
|
138
|
+
```
|
|
139
|
+
#### • `get`
|
|
140
|
+
This is the more performant option – it doesn't allocate on the heap.
|
|
141
|
+
```typescript
|
|
142
|
+
const fullName = get(incomingMessage, personSchema.fullName);
|
|
143
|
+
// ^ this is of type string
|
|
144
|
+
|
|
145
|
+
const latitude = get(incomingMessage, personSchema.address.geo.lat);
|
|
146
|
+
// ^ this is of type number
|
|
147
|
+
```
|
|
148
|
+
Alternatively, you can use the method "get". It works in the exact same way.
|
|
149
|
+
```typescript
|
|
150
|
+
const fullName = personSchema.fullName.get(incomingMessage);
|
|
151
|
+
const latitude = personSchema.address.geo.lat.get(incomingMessage);
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Writing
|
|
155
|
+
There are 2 ways to write a message.
|
|
156
|
+
|
|
157
|
+
#### • `toIndexedKeysMessage`
|
|
158
|
+
```typescript
|
|
159
|
+
import { toIndexedKeysMessage } from "schemind";
|
|
160
|
+
|
|
161
|
+
const objectToSerialize = {
|
|
162
|
+
id: 1,
|
|
163
|
+
fullName: "John Doe",
|
|
164
|
+
email: "johndoe@example.com",
|
|
165
|
+
birthDate: new Date(),
|
|
166
|
+
address: {
|
|
167
|
+
street: "123 Main Street",
|
|
168
|
+
city: "Anytown",
|
|
169
|
+
zipcode: "12345-6789",
|
|
170
|
+
geo: {
|
|
171
|
+
lat: 42.1234,
|
|
172
|
+
lng: -71.2345
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
website: "www.johndoe.com"
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
const message = toIndexedKeysMessage(objectToSerialize, personSchema);
|
|
179
|
+
// ^ this is an array that's the same as the "incomingMessage" in the previous section
|
|
180
|
+
|
|
181
|
+
// JSON.stringify(message) or whatever
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### • `set`
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
import { set } from "schemind";
|
|
188
|
+
|
|
189
|
+
const newMessage: unknown[] = [];
|
|
190
|
+
set(newMessage, personSchema.fullName, "John Doe");
|
|
191
|
+
set(newMessage, personSchema.address.geo.lat, 42.1234);
|
|
192
|
+
// ^ this is type-checked
|
|
193
|
+
// etc
|
|
194
|
+
```
|
|
195
|
+
Alternatively, you can use the method "set". It works in the exact same way.
|
|
196
|
+
```typescript
|
|
197
|
+
personSchema.fullName.set(newMessage, "John Doe");
|
|
198
|
+
personSchema.address.geo.lat.set(newMessage, 42.1234);
|
|
199
|
+
```
|
|
64
200
|
|
|
65
201
|
## FAQ
|
|
66
202
|
### Shouldn't this be an extension of a serializer?
|
|
@@ -71,7 +207,7 @@ Possibly. But if you're already using JSON / MessagePack / CBOR etc. in your app
|
|
|
71
207
|
|
|
72
208
|
Additionally, in some languages (backend or frontend) there's a MessagePack or JSON implementation that's faster, or allocates less memory, than protobuf.
|
|
73
209
|
|
|
74
|
-
### Why
|
|
210
|
+
### Why would I use `get` if it's inconvenient?
|
|
75
211
|
The `get` function prioritizes performance over convenience. The main goal here is to avoid any heap allocations (beyond what your deserializer allocates). I use *schemind* in performance-critical scenarios, where avoiding GC pauses is crucial.
|
|
76
212
|
Use the `toPlainObject` function instead, if you don't mind some extra allocations.
|
|
77
213
|
|
package/dist/schemind.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=Symbol("isSchemaLeaf");class
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function h(e,t){const o=t[i];let s=e;for(let c=t[i].length-1;c>=1;c--)s=s[o[c]];const r=o[0];return s[r]}function f(e,t,o){const s=t[i];let r=e;for(let n=t[i].length-1;n>=1;n--)r[s[n]]===void 0&&(r[s[n]]=[]),r=r[s[n]];const c=s[0];r[c]=o}const i=Symbol("indexesPathReversed"),g=Symbol("fieldType"),u=Symbol("isSchemaLeaf");class l extends Error{constructor(){super("Invalid schema. Make sure there are no duplicate indexes, and that nested objects are also wrapped with withIndex."),this.name="InvalidSchemaError"}}function m(e){return S(e,e,[],0),e}function S(e,t,o,s){for(const[r,c]of Object.entries(t)){const n=c;d(n)?(p(n,o,s),n.get=a=>h(a,n),n.set=(a,x)=>f(a,n,x)):S(e,n,o,s+1)}}function p(e,t,o){if(t.some(c=>c.length===e[i].length&&c.every((n,a)=>n===e[i][a])))throw new l;if(t.push(e[i]),e[i].length!==o+1)throw new l}function y(e){return t=>t?(b(t,e),t):{[i]:[e],[g]:void 0,[u]:!0}}function d(e){return Object.hasOwn(e,u)}function b(e,t){for(const[o,s]of Object.entries(e)){const r=s;d(r)?r[i].push(t):b(r,t)}}function v(e,t){const o={};for(const[s,r]of Object.entries(t)){const c=r;let n;d(c)?n=h(e,c):n=v(e,c),o[s]=n}return o}function O(e,t){const o=[];return I(o,e,t),o}function I(e,t,o){for(const[s,r]of Object.entries(o)){const c=r,n=t[s];d(c)?f(e,c,n):I(e,n,c)}}exports.InvalidSchemaError=l;exports.buildSchema=m;exports.get=h;exports.set=f;exports.toIndexedKeysMessage=O;exports.toPlainObject=v;exports.withIndex=y;
|
package/dist/schemind.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { NonNegativeInteger } from 'type-fest';
|
|
2
2
|
|
|
3
|
+
export declare function buildSchema<TSchema extends IndexedKeysMessageSchema<TSchemaInner>, TSchemaInner>(schema: TSchema): ToValidIndexedKeysMessageSchema<TSchema>;
|
|
4
|
+
|
|
5
|
+
declare const fieldType: unique symbol;
|
|
6
|
+
|
|
3
7
|
export declare function get<const TField>(message: readonly unknown[], schemaField: ValidSchemaLeaf<TField>): TField;
|
|
4
8
|
|
|
5
9
|
declare type IndexedKeysMessageSchema<TSchema> = {
|
|
@@ -8,6 +12,8 @@ declare type IndexedKeysMessageSchema<TSchema> = {
|
|
|
8
12
|
|
|
9
13
|
declare type IndexesPath = number[];
|
|
10
14
|
|
|
15
|
+
declare const indexesPathReversed: unique symbol;
|
|
16
|
+
|
|
11
17
|
declare type Invalid<T extends string> = {
|
|
12
18
|
[invalid]: T;
|
|
13
19
|
};
|
|
@@ -20,7 +26,7 @@ export declare class InvalidSchemaError extends Error {
|
|
|
20
26
|
|
|
21
27
|
declare const isSchemaLeafTag: unique symbol;
|
|
22
28
|
|
|
23
|
-
declare const
|
|
29
|
+
declare const isValidSchemaLeafTag: unique symbol;
|
|
24
30
|
|
|
25
31
|
declare type PlainObjectOfSchema<TSchema> = TSchema extends ValidIndexedKeysMessageSchema<unknown> ? {
|
|
26
32
|
[K in keyof TSchema]: TSchema[K] extends ValidSchemaLeaf<infer TField> ? TField : TSchema[K] extends SchemaLeaf<unknown> ? Invalid<"Schema needs to be validated before you use it!"> : PlainObjectOfSchema<TSchema[K]>;
|
|
@@ -29,8 +35,8 @@ declare type PlainObjectOfSchema<TSchema> = TSchema extends ValidIndexedKeysMess
|
|
|
29
35
|
declare type ReturnedSchemaNode<TField, TNestedSchema> = TNestedSchema extends undefined ? SchemaLeaf<TField> : TNestedSchema;
|
|
30
36
|
|
|
31
37
|
declare type SchemaLeaf<TField> = {
|
|
32
|
-
indexesPathReversed: IndexesPath;
|
|
33
|
-
fieldType: TField;
|
|
38
|
+
[indexesPathReversed]: IndexesPath;
|
|
39
|
+
[fieldType]: TField;
|
|
34
40
|
[isSchemaLeafTag]: true;
|
|
35
41
|
};
|
|
36
42
|
|
|
@@ -44,14 +50,14 @@ declare type ToValidIndexedKeysMessageSchema<TSchema> = {
|
|
|
44
50
|
[K in keyof TSchema]: TSchema[K] extends SchemaLeaf<infer TField> ? ValidSchemaLeaf<TField> : ToValidIndexedKeysMessageSchema<TSchema[K]>;
|
|
45
51
|
};
|
|
46
52
|
|
|
47
|
-
export declare function validateSchema<TSchema extends IndexedKeysMessageSchema<TSchemaInner>, TSchemaInner>(schema: TSchema): ToValidIndexedKeysMessageSchema<TSchema>;
|
|
48
|
-
|
|
49
53
|
export declare type ValidIndexedKeysMessageSchema<TSchema> = {
|
|
50
|
-
[K in keyof TSchema]: TSchema[K] extends ValidSchemaLeaf<infer TField> ? ValidSchemaLeaf<TField> : TSchema[K] extends SchemaLeaf<unknown> ? Invalid<"Schema needs to be
|
|
54
|
+
[K in keyof TSchema]: TSchema[K] extends ValidSchemaLeaf<infer TField> ? ValidSchemaLeaf<TField> : TSchema[K] extends SchemaLeaf<unknown> ? Invalid<"Schema needs to be built before you use it, did you forget to call buildSchema()?"> : ToValidIndexedKeysMessageSchema<TSchema[K]>;
|
|
51
55
|
};
|
|
52
56
|
|
|
53
57
|
declare type ValidSchemaLeaf<TField> = SchemaLeaf<TField> & {
|
|
54
|
-
[
|
|
58
|
+
[isValidSchemaLeafTag]: true;
|
|
59
|
+
get: (message: unknown[]) => TField;
|
|
60
|
+
set: (message: unknown[], value: TField) => void;
|
|
55
61
|
};
|
|
56
62
|
|
|
57
63
|
export declare function withIndex<const TIndex extends number>(index: NonNegativeInteger<TIndex>): <const TField = undefined, TNestedSchema extends IndexedKeysMessageSchema<TNestedSchema> | undefined = undefined>(nestedSchema?: TNestedSchema) => ReturnedSchemaNode<TField, TNestedSchema>;
|
package/dist/schemind.iife.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var Schemind=function(
|
|
1
|
+
var Schemind=function(d){"use strict";function h(e,t){const o=t[a];let s=e;for(let c=t[a].length-1;c>=1;c--)s=s[o[c]];const i=o[0];return s[i]}function u(e,t,o){const s=t[a];let i=e;for(let n=t[a].length-1;n>=1;n--)i[s[n]]===void 0&&(i[s[n]]=[]),i=i[s[n]];const c=s[0];i[c]=o}const a=Symbol("indexesPathReversed"),m=Symbol("fieldType"),S=Symbol("isSchemaLeaf");class f extends Error{constructor(){super("Invalid schema. Make sure there are no duplicate indexes, and that nested objects are also wrapped with withIndex."),this.name="InvalidSchemaError"}}function y(e){return v(e,e,[],0),e}function v(e,t,o,s){for(const[i,c]of Object.entries(t)){const n=c;l(n)?(O(n,o,s),n.get=r=>h(r,n),n.set=(r,P)=>u(r,n,P)):v(e,n,o,s+1)}}function O(e,t,o){if(t.some(c=>c.length===e[a].length&&c.every((n,r)=>n===e[a][r])))throw new f;if(t.push(e[a]),e[a].length!==o+1)throw new f}function j(e){return t=>t?(b(t,e),t):{[a]:[e],[m]:void 0,[S]:!0}}function l(e){return Object.hasOwn(e,S)}function b(e,t){for(const[o,s]of Object.entries(e)){const i=s;l(i)?i[a].push(t):b(i,t)}}function I(e,t){const o={};for(const[s,i]of Object.entries(t)){const c=i;let n;l(c)?n=h(e,c):n=I(e,c),o[s]=n}return o}function w(e,t){const o=[];return g(o,e,t),o}function g(e,t,o){for(const[s,i]of Object.entries(o)){const c=i,n=t[s];l(c)?u(e,c,n):g(e,n,c)}}return d.InvalidSchemaError=f,d.buildSchema=y,d.get=h,d.set=u,d.toIndexedKeysMessage=w,d.toPlainObject=I,d.withIndex=j,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"}),d}({});
|
package/dist/schemind.js
CHANGED
|
@@ -1,81 +1,81 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
function f(e, t) {
|
|
2
|
+
const o = t[i];
|
|
3
|
+
let s = e;
|
|
4
|
+
for (let c = t[i].length - 1; c >= 1; c--)
|
|
5
|
+
s = s[o[c]];
|
|
6
|
+
const r = o[0];
|
|
7
|
+
return s[r];
|
|
8
|
+
}
|
|
9
|
+
function h(e, t, o) {
|
|
10
|
+
const s = t[i];
|
|
11
|
+
let r = e;
|
|
12
|
+
for (let n = t[i].length - 1; n >= 1; n--)
|
|
13
|
+
r[s[n]] === void 0 && (r[s[n]] = []), r = r[s[n]];
|
|
14
|
+
const c = s[0];
|
|
15
|
+
r[c] = o;
|
|
16
|
+
}
|
|
17
|
+
const i = Symbol("indexesPathReversed"), b = Symbol("fieldType"), u = Symbol("isSchemaLeaf");
|
|
18
|
+
class l extends Error {
|
|
3
19
|
constructor() {
|
|
4
20
|
super("Invalid schema. Make sure there are no duplicate indexes, and that nested objects are also wrapped with withIndex."), this.name = "InvalidSchemaError";
|
|
5
21
|
}
|
|
6
22
|
}
|
|
7
|
-
function
|
|
8
|
-
return
|
|
23
|
+
function m(e) {
|
|
24
|
+
return v(e, e, [], 0), e;
|
|
9
25
|
}
|
|
10
|
-
function
|
|
11
|
-
for (const [
|
|
12
|
-
const n =
|
|
13
|
-
|
|
26
|
+
function v(e, t, o, s) {
|
|
27
|
+
for (const [r, c] of Object.entries(t)) {
|
|
28
|
+
const n = c;
|
|
29
|
+
d(n) ? (p(n, o, s), n.get = (a) => f(a, n), n.set = (a, I) => h(a, n, I)) : v(e, n, o, s + 1);
|
|
14
30
|
}
|
|
15
31
|
}
|
|
16
|
-
function
|
|
17
|
-
if (t.some((
|
|
18
|
-
throw new
|
|
19
|
-
if (t.push(e
|
|
20
|
-
throw new
|
|
32
|
+
function p(e, t, o) {
|
|
33
|
+
if (t.some((c) => c.length === e[i].length && c.every((n, a) => n === e[i][a])))
|
|
34
|
+
throw new l();
|
|
35
|
+
if (t.push(e[i]), e[i].length !== o + 1)
|
|
36
|
+
throw new l();
|
|
21
37
|
}
|
|
22
|
-
function
|
|
23
|
-
return (t) => t ? (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
[
|
|
38
|
+
function y(e) {
|
|
39
|
+
return (t) => t ? (S(t, e), t) : {
|
|
40
|
+
[i]: [e],
|
|
41
|
+
[b]: void 0,
|
|
42
|
+
[u]: !0
|
|
27
43
|
};
|
|
28
44
|
}
|
|
29
|
-
function
|
|
30
|
-
return Object.hasOwn(e,
|
|
45
|
+
function d(e) {
|
|
46
|
+
return Object.hasOwn(e, u);
|
|
31
47
|
}
|
|
32
|
-
function
|
|
33
|
-
for (const [
|
|
34
|
-
const
|
|
35
|
-
r
|
|
48
|
+
function S(e, t) {
|
|
49
|
+
for (const [o, s] of Object.entries(e)) {
|
|
50
|
+
const r = s;
|
|
51
|
+
d(r) ? r[i].push(t) : S(r, t);
|
|
36
52
|
}
|
|
37
53
|
}
|
|
38
|
-
function
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return o[d];
|
|
45
|
-
}
|
|
46
|
-
function P(e, t, s) {
|
|
47
|
-
const o = t.indexesPathReversed;
|
|
48
|
-
let d = e;
|
|
49
|
-
for (let i = t.indexesPathReversed.length - 1; i >= 1; i--)
|
|
50
|
-
d[o[i]] === void 0 && (d[o[i]] = []), d = d[o[i]];
|
|
51
|
-
const n = o[0];
|
|
52
|
-
d[n] = s;
|
|
53
|
-
}
|
|
54
|
-
function S(e, t) {
|
|
55
|
-
const s = {};
|
|
56
|
-
for (const [o, d] of Object.entries(t)) {
|
|
57
|
-
const n = d;
|
|
58
|
-
let i;
|
|
59
|
-
r(n) ? i = x(e, n) : i = S(e, n), s[o] = i;
|
|
54
|
+
function g(e, t) {
|
|
55
|
+
const o = {};
|
|
56
|
+
for (const [s, r] of Object.entries(t)) {
|
|
57
|
+
const c = r;
|
|
58
|
+
let n;
|
|
59
|
+
d(c) ? n = f(e, c) : n = g(e, c), o[s] = n;
|
|
60
60
|
}
|
|
61
|
-
return
|
|
61
|
+
return o;
|
|
62
62
|
}
|
|
63
|
-
function
|
|
64
|
-
const
|
|
65
|
-
return
|
|
63
|
+
function O(e, t) {
|
|
64
|
+
const o = [];
|
|
65
|
+
return x(o, e, t), o;
|
|
66
66
|
}
|
|
67
|
-
function
|
|
68
|
-
for (const [
|
|
69
|
-
const
|
|
70
|
-
|
|
67
|
+
function x(e, t, o) {
|
|
68
|
+
for (const [s, r] of Object.entries(o)) {
|
|
69
|
+
const c = r, n = t[s];
|
|
70
|
+
d(c) ? h(e, c, n) : x(e, n, c);
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
export {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
74
|
+
l as InvalidSchemaError,
|
|
75
|
+
m as buildSchema,
|
|
76
|
+
f as get,
|
|
77
|
+
h as set,
|
|
78
|
+
O as toIndexedKeysMessage,
|
|
79
|
+
g as toPlainObject,
|
|
80
|
+
y as withIndex
|
|
81
81
|
};
|
package/dist/schemind.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(c,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(c=typeof globalThis<"u"?globalThis:c||self,r(c.Schemind={}))})(this,function(c){"use strict";function r(e,t){const o=t[a];let s=e;for(let i=t[a].length-1;i>=1;i--)s=s[o[i]];const d=o[0];return s[d]}function u(e,t,o){const s=t[a];let d=e;for(let n=t[a].length-1;n>=1;n--)d[s[n]]===void 0&&(d[s[n]]=[]),d=d[s[n]];const i=s[0];d[i]=o}const a=Symbol("indexesPathReversed"),g=Symbol("fieldType"),S=Symbol("isSchemaLeaf");class h extends Error{constructor(){super("Invalid schema. Make sure there are no duplicate indexes, and that nested objects are also wrapped with withIndex."),this.name="InvalidSchemaError"}}function y(e){return b(e,e,[],0),e}function b(e,t,o,s){for(const[d,i]of Object.entries(t)){const n=i;f(n)?(p(n,o,s),n.get=l=>r(l,n),n.set=(l,O)=>u(l,n,O)):b(e,n,o,s+1)}}function p(e,t,o){if(t.some(i=>i.length===e[a].length&&i.every((n,l)=>n===e[a][l])))throw new h;if(t.push(e[a]),e[a].length!==o+1)throw new h}function x(e){return t=>t?(v(t,e),t):{[a]:[e],[g]:void 0,[S]:!0}}function f(e){return Object.hasOwn(e,S)}function v(e,t){for(const[o,s]of Object.entries(e)){const d=s;f(d)?d[a].push(t):v(d,t)}}function I(e,t){const o={};for(const[s,d]of Object.entries(t)){const i=d;let n;f(i)?n=r(e,i):n=I(e,i),o[s]=n}return o}function j(e,t){const o=[];return m(o,e,t),o}function m(e,t,o){for(const[s,d]of Object.entries(o)){const i=d,n=t[s];f(i)?u(e,i,n):m(e,n,i)}}c.InvalidSchemaError=h,c.buildSchema=y,c.get=r,c.set=u,c.toIndexedKeysMessage=j,c.toPlainObject=I,c.withIndex=x,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "schemind",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Read and write to messages serialized as arrays (indexed keys) by defining a schema, \nenabling smaller message size when using protocols such as msgpack or JSON.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"serialization",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@commitlint/cli": "^19.6.1",
|
|
40
40
|
"@commitlint/config-conventional": "^19.6.0",
|
|
41
|
-
"@types/node": "^22.10.
|
|
41
|
+
"@types/node": "^22.10.3",
|
|
42
42
|
"camelcase": "^8.0.0",
|
|
43
43
|
"changelogen": "^0.5.7",
|
|
44
44
|
"husky": "^9.1.7",
|