ipld-schema-describer 1.0.8 → 2.0.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.
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * @typedef {import('ipld-schema/schema-schema').Schema} Schema
3
- * @typedef {import('ipld-schema/schema-schema').TypeLink} TypeLink
4
- * @typedef {import('ipld-schema/schema-schema').TypeList} TypeList
5
- * @typedef {import('ipld-schema/schema-schema').TypeMap} TypeMap
3
+ * @typedef {import('ipld-schema/schema-schema').TypeDefnLink} TypeDefnLink
4
+ * @typedef {import('ipld-schema/schema-schema').TypeDefnList} TypeDefnList
5
+ * @typedef {import('ipld-schema/schema-schema').TypeDefnMap} TypeDefnMap
6
6
  */
7
7
  /**
8
8
  * @param {any} obj
@@ -13,7 +13,7 @@ export function describe(obj: any): {
13
13
  root: string;
14
14
  };
15
15
  export type Schema = import('ipld-schema/schema-schema').Schema;
16
- export type TypeLink = import('ipld-schema/schema-schema').TypeLink;
17
- export type TypeList = import('ipld-schema/schema-schema').TypeList;
18
- export type TypeMap = import('ipld-schema/schema-schema').TypeMap;
16
+ export type TypeDefnLink = import('ipld-schema/schema-schema').TypeDefnLink;
17
+ export type TypeDefnList = import('ipld-schema/schema-schema').TypeDefnList;
18
+ export type TypeDefnMap = import('ipld-schema/schema-schema').TypeDefnMap;
19
19
  //# sourceMappingURL=ipld-schema-describer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ipld-schema-describer.d.ts","sourceRoot":"","sources":["../ipld-schema-describer.js"],"names":[],"mappings":"AAEA;;;;;GAKG;AAEH;;;GAGG;AACH,8BAHW,GAAG,GACD;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CA4B5C;qBApCY,OAAO,2BAA2B,EAAE,MAAM;uBAC1C,OAAO,2BAA2B,EAAE,QAAQ;uBAC5C,OAAO,2BAA2B,EAAE,QAAQ;sBAC5C,OAAO,2BAA2B,EAAE,OAAO"}
1
+ {"version":3,"file":"ipld-schema-describer.d.ts","sourceRoot":"","sources":["../ipld-schema-describer.js"],"names":[],"mappings":"AAEA;;;;;GAKG;AAEH;;;GAGG;AACH,8BAHW,GAAG,GACD;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CA4B5C;qBApCY,OAAO,2BAA2B,EAAE,MAAM;2BAC1C,OAAO,2BAA2B,EAAE,YAAY;2BAChD,OAAO,2BAA2B,EAAE,YAAY;0BAChD,OAAO,2BAA2B,EAAE,WAAW"}
@@ -1,297 +0,0 @@
1
- 'use strict';
2
-
3
- var ipldSchemaDescriber = require('../ipld-schema-describer.js');
4
- var chai = require('chai');
5
-
6
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
7
-
8
- var chai__default = /*#__PURE__*/_interopDefaultLegacy(chai);
9
-
10
- const {assert} = chai__default["default"];
11
- const fauxCID = {};
12
- fauxCID.asCID = fauxCID;
13
- function verify(obj, expected) {
14
- const description = ipldSchemaDescriber.describe(obj);
15
- assert.deepEqual(description, expected);
16
- }
17
- describe('Basics', () => {
18
- it('null', () => {
19
- const expected = {
20
- schema: { types: { Null: { kind: 'null' } } },
21
- root: 'Null'
22
- };
23
- verify(null, expected);
24
- });
25
- it('bool', () => {
26
- const expected = {
27
- schema: { types: { Bool: { kind: 'bool' } } },
28
- root: 'Bool'
29
- };
30
- verify(true, expected);
31
- verify(false, expected);
32
- });
33
- it('int', () => {
34
- const expected = {
35
- schema: { types: { Int: { kind: 'int' } } },
36
- root: 'Int'
37
- };
38
- verify(101, expected);
39
- verify(-101, expected);
40
- verify(0, expected);
41
- });
42
- it('float', () => {
43
- const expected = {
44
- schema: { types: { Float: { kind: 'float' } } },
45
- root: 'Float'
46
- };
47
- verify(1.01, expected);
48
- verify(-1.01, expected);
49
- });
50
- it('string', () => {
51
- const expected = {
52
- schema: { types: { String: { kind: 'string' } } },
53
- root: 'String'
54
- };
55
- verify('a string', expected);
56
- verify('', expected);
57
- });
58
- it('link', () => {
59
- const expected = {
60
- schema: { types: { Link: { kind: 'link' } } },
61
- root: 'Link'
62
- };
63
- verify(fauxCID, expected);
64
- });
65
- it('bytes', () => {
66
- const expected = {
67
- schema: { types: { Bytes: { kind: 'bytes' } } },
68
- root: 'Bytes'
69
- };
70
- verify(Uint8Array.from([
71
- 1,
72
- 3,
73
- 4,
74
- 5
75
- ]), expected);
76
- verify(Uint8Array.from([]), expected);
77
- });
78
- it('map', () => {
79
- const obj = {
80
- s1: 'a string',
81
- s2: 'second string',
82
- s3: 'third string'
83
- };
84
- const expected = {
85
- schema: {
86
- types: {
87
- Map_1: {
88
- kind: 'map',
89
- keyType: 'String',
90
- valueType: 'String'
91
- }
92
- }
93
- },
94
- root: 'Map_1'
95
- };
96
- verify(obj, expected);
97
- });
98
- it('empty map', () => {
99
- const obj = {};
100
- const expected = {
101
- schema: {
102
- types: {
103
- Map_1: {
104
- kind: 'map',
105
- keyType: 'String',
106
- valueType: 'Any'
107
- }
108
- }
109
- },
110
- root: 'Map_1'
111
- };
112
- verify(obj, expected);
113
- });
114
- it('struct (map repr)', () => {
115
- const obj = {
116
- s: 'a string',
117
- i: 101,
118
- l: fauxCID
119
- };
120
- const expected = {
121
- schema: {
122
- types: {
123
- Struct_1: {
124
- kind: 'struct',
125
- fields: {
126
- s: { type: 'String' },
127
- i: { type: 'Int' },
128
- l: { type: { kind: 'link' } }
129
- }
130
- }
131
- }
132
- },
133
- root: 'Struct_1'
134
- };
135
- verify(obj, expected);
136
- });
137
- it('list', () => {
138
- const obj = [
139
- 'a string',
140
- 'second string',
141
- 'third string'
142
- ];
143
- const expected = {
144
- schema: {
145
- types: {
146
- List_1: {
147
- kind: 'list',
148
- valueType: 'String'
149
- }
150
- }
151
- },
152
- root: 'List_1'
153
- };
154
- verify(obj, expected);
155
- });
156
- it('empty list', () => {
157
- const obj = [];
158
- const expected = {
159
- schema: {
160
- types: {
161
- List_1: {
162
- kind: 'list',
163
- valueType: 'Any'
164
- }
165
- }
166
- },
167
- root: 'List_1'
168
- };
169
- verify(obj, expected);
170
- });
171
- it('struct (list repr)', () => {
172
- const obj = [
173
- 'a string',
174
- 101,
175
- false,
176
- null
177
- ];
178
- const expected = {
179
- schema: {
180
- types: {
181
- Struct_1: {
182
- kind: 'struct',
183
- fields: {
184
- f0: { type: 'String' },
185
- f1: { type: 'Int' },
186
- f2: { type: 'Bool' },
187
- f3: { type: 'Null' }
188
- },
189
- representation: { tuple: {} }
190
- }
191
- }
192
- },
193
- root: 'Struct_1'
194
- };
195
- verify(obj, expected);
196
- });
197
- });
198
- describe('Complex', () => {
199
- it('list of lists', () => {
200
- const sa = [
201
- 'a string',
202
- 'second string',
203
- 'third string'
204
- ];
205
- const obj = [
206
- sa,
207
- sa.slice(),
208
- sa.slice()
209
- ];
210
- const expected = {
211
- schema: {
212
- types: {
213
- List_1: {
214
- kind: 'list',
215
- valueType: 'String'
216
- },
217
- List_2: {
218
- kind: 'list',
219
- valueType: 'List_1'
220
- }
221
- }
222
- },
223
- root: 'List_2'
224
- };
225
- verify(obj, expected);
226
- });
227
- it('map of maps', () => {
228
- const sm = {
229
- s1: 'a string',
230
- s2: 'second string',
231
- s3: 'third string'
232
- };
233
- const obj = {
234
- sm1: sm,
235
- sm2: Object.assign({}, sm),
236
- sm3: Object.assign({}, sm)
237
- };
238
- const expected = {
239
- schema: {
240
- types: {
241
- Map_1: {
242
- kind: 'map',
243
- keyType: 'String',
244
- valueType: 'String'
245
- },
246
- Map_2: {
247
- kind: 'map',
248
- keyType: 'String',
249
- valueType: 'Map_1'
250
- }
251
- }
252
- },
253
- root: 'Map_2'
254
- };
255
- verify(obj, expected);
256
- });
257
- it('struct of structs', () => {
258
- const obj = [
259
- 'a string',
260
- 101,
261
- [
262
- 's',
263
- false
264
- ],
265
- [
266
- '',
267
- true
268
- ]
269
- ];
270
- const expected = {
271
- schema: {
272
- types: {
273
- Struct_1: {
274
- kind: 'struct',
275
- fields: {
276
- f0: { type: 'String' },
277
- f1: { type: 'Bool' }
278
- },
279
- representation: { tuple: {} }
280
- },
281
- Struct_2: {
282
- kind: 'struct',
283
- fields: {
284
- f0: { type: 'String' },
285
- f1: { type: 'Int' },
286
- f2: { type: 'Struct_1' },
287
- f3: { type: 'Struct_1' }
288
- },
289
- representation: { tuple: {} }
290
- }
291
- }
292
- },
293
- root: 'Struct_2'
294
- };
295
- verify(obj, expected);
296
- });
297
- });
@@ -1,15 +0,0 @@
1
- 'use strict';
2
-
3
- var ipldSchemaDescriber = require('../ipld-schema-describer.js');
4
- var chai = require('chai');
5
-
6
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
7
-
8
- var chai__default = /*#__PURE__*/_interopDefaultLegacy(chai);
9
-
10
- const {assert} = chai__default["default"];
11
- describe('Errors', () => {
12
- it('bad kind', () => {
13
- assert.throws(() => ipldSchemaDescriber.describe(undefined), /Unknown IPLD kind for value: undefined/);
14
- });
15
- });
@@ -1,130 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var kind = require('./kind.js');
6
-
7
- function describe(obj) {
8
- const description = describeObject(obj, { types: {} });
9
- if (!Object.keys(description.schema.types).length) {
10
- if (typeof description.root === 'object' && description.root.kind === 'link') {
11
- const name = 'Link';
12
- description.schema.types[name] = { kind: 'link' };
13
- description.root = name;
14
- } else if (typeof description.root === 'string') {
15
- const name = `${ description.root }`;
16
- description.schema.types[name] = { kind: description.root.toLowerCase() };
17
- description.root = name;
18
- } else {
19
- throw new Error('internal error');
20
- }
21
- }
22
- if (typeof description.root !== 'string') {
23
- throw new Error('internal error');
24
- }
25
- return {
26
- schema: description.schema,
27
- root: description.root
28
- };
29
- }
30
- function describeObject(obj, schema) {
31
- const objKind = kind.kind(obj);
32
- let name = `${ objKind.charAt(0).toUpperCase() }${ objKind.substring(1) }`;
33
- if (objKind === 'null' || objKind === 'int' || objKind === 'bool' || objKind === 'float' || objKind === 'string' || objKind === 'bytes') {
34
- return {
35
- schema,
36
- root: name
37
- };
38
- }
39
- if (objKind === 'link') {
40
- return {
41
- schema,
42
- root: { kind: 'link' }
43
- };
44
- }
45
- const fieldNames = [];
46
- const entries = objKind === 'map' ? Object.entries(obj) : obj.map((e, i) => [
47
- `f${ i }`,
48
- e
49
- ]);
50
- for (const [fieldName, value] of entries) {
51
- fieldNames.push({
52
- fieldName,
53
- root: describeObject(value, schema).root
54
- });
55
- }
56
- let unique = true;
57
- for (let i = 1; i < fieldNames.length; i++) {
58
- if (fieldNames[i].root !== fieldNames[i - 1].root) {
59
- unique = false;
60
- break;
61
- }
62
- }
63
- name = `${ name }_1`;
64
- let type;
65
- if (unique) {
66
- type = { kind: objKind };
67
- if (type.kind === 'map') {
68
- type.keyType = 'String';
69
- }
70
- if (type.kind === 'map' || type.kind === 'list') {
71
- if (fieldNames.length) {
72
- type.valueType = fieldNames[0].root;
73
- } else {
74
- type.valueType = 'Any';
75
- }
76
- }
77
- } else {
78
- name = 'Struct_1';
79
- type = {
80
- kind: 'struct',
81
- fields: {}
82
- };
83
- for (const field of fieldNames) {
84
- type.fields[field.fieldName] = { type: field.root };
85
- }
86
- if (objKind === 'list') {
87
- type.representation = { tuple: {} };
88
- }
89
- }
90
- while (schema.types[name]) {
91
- if (deepEqual(schema.types[name], type)) {
92
- break;
93
- }
94
- name = name.split('_').map((s, i) => i ? parseInt(s, 10) + 1 : s).join('_');
95
- }
96
- schema.types[name] = type;
97
- return {
98
- schema,
99
- root: name
100
- };
101
- }
102
- function deepEqual(o1, o2) {
103
- const k1 = kind.kind(o1);
104
- const k2 = kind.kind(o2);
105
- if (k1 !== k2) {
106
- return false;
107
- }
108
- switch (k1) {
109
- case 'bool':
110
- case 'string':
111
- case 'int':
112
- case 'float':
113
- case 'null':
114
- return o1 === o2;
115
- case 'map':
116
- return deepEqual(Object.entries(o1), Object.entries(o2));
117
- case 'list':
118
- if (o1.length !== o2.length) {
119
- return false;
120
- }
121
- for (let i = 0; i < o1.length; i++) {
122
- if (!deepEqual(o1[i], o2[i])) {
123
- return false;
124
- }
125
- }
126
- }
127
- return true;
128
- }
129
-
130
- exports.describe = describe;
package/cjs/kind.js DELETED
@@ -1,36 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- function kind(obj) {
6
- if (typeof obj === 'number') {
7
- if (Number.isInteger(obj)) {
8
- return 'int';
9
- }
10
- return 'float';
11
- }
12
- if (typeof obj === 'string') {
13
- return 'string';
14
- }
15
- if (obj === null) {
16
- return 'null';
17
- }
18
- if (typeof obj === 'boolean') {
19
- return 'bool';
20
- }
21
- if (typeof obj === 'object' && obj.asCID === obj) {
22
- return 'link';
23
- }
24
- if (obj instanceof Uint8Array) {
25
- return 'bytes';
26
- }
27
- if (Array.isArray(obj)) {
28
- return 'list';
29
- }
30
- if (typeof obj === 'object') {
31
- return 'map';
32
- }
33
- throw new TypeError(`Unknown IPLD kind for value: ${ JSON.stringify(obj) }`);
34
- }
35
-
36
- exports.kind = kind;