@naturalcycles/js-lib 14.42.1 → 14.46.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/CHANGELOG.md +28 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +7 -3
- package/dist/json-schema/from-data/generateJsonSchemaFromData.d.ts +7 -0
- package/dist/json-schema/from-data/generateJsonSchemaFromData.js +90 -0
- package/dist/json-schema/jsonSchema.model.d.ts +7 -0
- package/dist/json-schema/jsonSchemaBuilder.d.ts +3 -0
- package/dist/json-schema/jsonSchemaBuilder.js +9 -0
- package/dist/string/stringifyAny.d.ts +1 -1
- package/dist/string/stringifyAny.js +1 -1
- package/dist/typeFest.d.ts +16 -0
- package/dist/vendor/is.d.ts +224 -0
- package/dist/vendor/is.js +466 -0
- package/dist-esm/array/array.util.js +0 -1
- package/dist-esm/array/range.js +0 -1
- package/dist-esm/decorators/createPromiseDecorator.js +0 -1
- package/dist-esm/decorators/debounce.decorator.js +0 -1
- package/dist-esm/decorators/debounce.js +0 -1
- package/dist-esm/decorators/decorator.util.js +0 -1
- package/dist-esm/decorators/logMethod.decorator.js +0 -1
- package/dist-esm/decorators/memo.decorator.js +0 -1
- package/dist-esm/decorators/memo.util.js +0 -1
- package/dist-esm/decorators/memoFn.js +0 -1
- package/dist-esm/decorators/memoSimple.decorator.js +0 -1
- package/dist-esm/decorators/retry.decorator.js +0 -1
- package/dist-esm/decorators/timeout.decorator.js +0 -1
- package/dist-esm/error/app.error.js +0 -1
- package/dist-esm/error/assert.js +0 -1
- package/dist-esm/error/error.model.js +0 -1
- package/dist-esm/error/error.util.js +0 -1
- package/dist-esm/error/errorMode.js +0 -1
- package/dist-esm/error/http.error.js +0 -1
- package/dist-esm/error/try.js +0 -1
- package/dist-esm/error/tryCatch.js +0 -1
- package/dist-esm/index.js +3 -2
- package/dist-esm/json-schema/from-data/generateJsonSchemaFromData.js +86 -0
- package/dist-esm/json-schema/jsonSchema.cnst.js +0 -1
- package/dist-esm/json-schema/jsonSchema.model.js +0 -1
- package/dist-esm/json-schema/jsonSchema.util.js +0 -1
- package/dist-esm/json-schema/jsonSchemaBuilder.js +9 -1
- package/dist-esm/json-schema/jsonSchemas.js +0 -1
- package/dist-esm/lodash.types.js +0 -1
- package/dist-esm/math/math.util.js +0 -1
- package/dist-esm/math/sma.js +0 -1
- package/dist-esm/number/createDeterministicRandom.js +0 -1
- package/dist-esm/number/number.util.js +0 -1
- package/dist-esm/object/deepEquals.js +0 -1
- package/dist-esm/object/object.util.js +0 -1
- package/dist-esm/object/sortObject.js +0 -1
- package/dist-esm/object/sortObjectDeep.js +0 -1
- package/dist-esm/promise/AggregatedError.js +0 -1
- package/dist-esm/promise/pBatch.js +0 -1
- package/dist-esm/promise/pDefer.js +0 -1
- package/dist-esm/promise/pDelay.js +0 -1
- package/dist-esm/promise/pFilter.js +0 -1
- package/dist-esm/promise/pHang.js +0 -1
- package/dist-esm/promise/pMap.js +0 -1
- package/dist-esm/promise/pProps.js +0 -1
- package/dist-esm/promise/pRetry.js +0 -1
- package/dist-esm/promise/pState.js +0 -1
- package/dist-esm/promise/pTimeout.js +0 -1
- package/dist-esm/promise/pTuple.js +0 -1
- package/dist-esm/string/case.js +0 -1
- package/dist-esm/string/json.util.js +0 -1
- package/dist-esm/string/lodash/unicodeWords.js +0 -1
- package/dist-esm/string/lodash/words.js +0 -1
- package/dist-esm/string/string.util.js +0 -1
- package/dist-esm/string/stringifyAny.js +1 -2
- package/dist-esm/time/time.util.js +0 -1
- package/dist-esm/typeFest.js +0 -1
- package/dist-esm/types.js +0 -1
- package/dist-esm/unit/size.util.js +0 -1
- package/dist-esm/vendor/is.js +463 -0
- package/package.json +2 -1
- package/src/index.ts +4 -0
- package/src/json-schema/from-data/generateJsonSchemaFromData.ts +120 -0
- package/src/json-schema/jsonSchema.model.ts +8 -0
- package/src/json-schema/jsonSchemaBuilder.ts +16 -10
- package/src/string/stringifyAny.ts +2 -2
- package/src/typeFest.ts +30 -0
- package/src/vendor/is.ts +837 -0
- package/dist-esm/array/array.util.js.map +0 -1
- package/dist-esm/array/range.js.map +0 -1
- package/dist-esm/decorators/createPromiseDecorator.js.map +0 -1
- package/dist-esm/decorators/debounce.decorator.js.map +0 -1
- package/dist-esm/decorators/debounce.js.map +0 -1
- package/dist-esm/decorators/decorator.util.js.map +0 -1
- package/dist-esm/decorators/logMethod.decorator.js.map +0 -1
- package/dist-esm/decorators/memo.decorator.js.map +0 -1
- package/dist-esm/decorators/memo.util.js.map +0 -1
- package/dist-esm/decorators/memoFn.js.map +0 -1
- package/dist-esm/decorators/memoSimple.decorator.js.map +0 -1
- package/dist-esm/decorators/retry.decorator.js.map +0 -1
- package/dist-esm/decorators/timeout.decorator.js.map +0 -1
- package/dist-esm/error/app.error.js.map +0 -1
- package/dist-esm/error/assert.js.map +0 -1
- package/dist-esm/error/error.model.js.map +0 -1
- package/dist-esm/error/error.util.js.map +0 -1
- package/dist-esm/error/errorMode.js.map +0 -1
- package/dist-esm/error/http.error.js.map +0 -1
- package/dist-esm/error/try.js.map +0 -1
- package/dist-esm/error/tryCatch.js.map +0 -1
- package/dist-esm/index.js.map +0 -1
- package/dist-esm/json-schema/jsonSchema.cnst.js.map +0 -1
- package/dist-esm/json-schema/jsonSchema.model.js.map +0 -1
- package/dist-esm/json-schema/jsonSchema.util.js.map +0 -1
- package/dist-esm/json-schema/jsonSchemaBuilder.js.map +0 -1
- package/dist-esm/json-schema/jsonSchemas.js.map +0 -1
- package/dist-esm/lodash.types.js.map +0 -1
- package/dist-esm/math/math.util.js.map +0 -1
- package/dist-esm/math/sma.js.map +0 -1
- package/dist-esm/number/createDeterministicRandom.js.map +0 -1
- package/dist-esm/number/number.util.js.map +0 -1
- package/dist-esm/object/deepEquals.js.map +0 -1
- package/dist-esm/object/object.util.js.map +0 -1
- package/dist-esm/object/sortObject.js.map +0 -1
- package/dist-esm/object/sortObjectDeep.js.map +0 -1
- package/dist-esm/promise/AggregatedError.js.map +0 -1
- package/dist-esm/promise/pBatch.js.map +0 -1
- package/dist-esm/promise/pDefer.js.map +0 -1
- package/dist-esm/promise/pDelay.js.map +0 -1
- package/dist-esm/promise/pFilter.js.map +0 -1
- package/dist-esm/promise/pHang.js.map +0 -1
- package/dist-esm/promise/pMap.js.map +0 -1
- package/dist-esm/promise/pProps.js.map +0 -1
- package/dist-esm/promise/pRetry.js.map +0 -1
- package/dist-esm/promise/pState.js.map +0 -1
- package/dist-esm/promise/pTimeout.js.map +0 -1
- package/dist-esm/promise/pTuple.js.map +0 -1
- package/dist-esm/string/case.js.map +0 -1
- package/dist-esm/string/json.util.js.map +0 -1
- package/dist-esm/string/lodash/unicodeWords.js.map +0 -1
- package/dist-esm/string/lodash/words.js.map +0 -1
- package/dist-esm/string/string.util.js.map +0 -1
- package/dist-esm/string/stringifyAny.js.map +0 -1
- package/dist-esm/time/time.util.js.map +0 -1
- package/dist-esm/typeFest.js.map +0 -1
- package/dist-esm/types.js.map +0 -1
- package/dist-esm/unit/size.util.js.map +0 -1
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { _stringMapEntries, _uniq, } from '../..';
|
|
2
|
+
/**
|
|
3
|
+
* Each row must be an object (current limitation).
|
|
4
|
+
*
|
|
5
|
+
* `additionalProperties` is set to `true`, cause it's safer.
|
|
6
|
+
*/
|
|
7
|
+
export function generateJsonSchemaFromData(rows) {
|
|
8
|
+
return objectToJsonSchema(rows);
|
|
9
|
+
}
|
|
10
|
+
function objectToJsonSchema(rows) {
|
|
11
|
+
const typesByKey = {};
|
|
12
|
+
rows.forEach(r => {
|
|
13
|
+
Object.keys(r).forEach(key => {
|
|
14
|
+
typesByKey[key] || (typesByKey[key] = new Set());
|
|
15
|
+
typesByKey[key].add(getTypeOfValue(r[key]));
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
const s = {
|
|
19
|
+
type: 'object',
|
|
20
|
+
properties: {},
|
|
21
|
+
required: [],
|
|
22
|
+
additionalProperties: true,
|
|
23
|
+
};
|
|
24
|
+
_stringMapEntries(typesByKey).forEach(([key, types]) => {
|
|
25
|
+
const schema = mergeTypes([...types], rows.map(r => r[key]));
|
|
26
|
+
if (!schema)
|
|
27
|
+
return;
|
|
28
|
+
s.properties[key] = schema;
|
|
29
|
+
});
|
|
30
|
+
// console.log(typesByKey)
|
|
31
|
+
return s;
|
|
32
|
+
}
|
|
33
|
+
function mergeTypes(types, samples) {
|
|
34
|
+
// skip "undefined" types
|
|
35
|
+
types = types.filter(t => t !== 'undefined');
|
|
36
|
+
if (!types.length)
|
|
37
|
+
return undefined;
|
|
38
|
+
if (types.length > 1) {
|
|
39
|
+
// oneOf
|
|
40
|
+
const s = {
|
|
41
|
+
oneOf: types.map(type => mergeTypes([type], samples)),
|
|
42
|
+
};
|
|
43
|
+
return s;
|
|
44
|
+
}
|
|
45
|
+
const type = types[0];
|
|
46
|
+
if (type === 'null') {
|
|
47
|
+
return {
|
|
48
|
+
type: 'null',
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
if (type === 'boolean') {
|
|
52
|
+
return {
|
|
53
|
+
type: 'boolean',
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (type === 'string') {
|
|
57
|
+
return {
|
|
58
|
+
type: 'string',
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
if (type === 'number') {
|
|
62
|
+
return {
|
|
63
|
+
type: 'number',
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
if (type === 'object') {
|
|
67
|
+
return objectToJsonSchema(samples.filter((r) => r && typeof r === 'object'));
|
|
68
|
+
}
|
|
69
|
+
if (type === 'array') {
|
|
70
|
+
// possible feature: detect if it's a tuple
|
|
71
|
+
// currently assume no-tuple
|
|
72
|
+
const items = samples.filter(r => Array.isArray(r)).flat(1);
|
|
73
|
+
const itemTypes = _uniq(items.map(i => getTypeOfValue(i)));
|
|
74
|
+
return {
|
|
75
|
+
type: 'array',
|
|
76
|
+
items: mergeTypes(itemTypes, items),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function getTypeOfValue(v) {
|
|
81
|
+
if (v === null)
|
|
82
|
+
return 'null';
|
|
83
|
+
if (Array.isArray(v))
|
|
84
|
+
return 'array';
|
|
85
|
+
return typeof v;
|
|
86
|
+
}
|
|
@@ -32,6 +32,11 @@ export const jsonSchema = {
|
|
|
32
32
|
type: 'boolean',
|
|
33
33
|
});
|
|
34
34
|
},
|
|
35
|
+
buffer() {
|
|
36
|
+
return new JsonSchemaAnyBuilder({
|
|
37
|
+
instanceof: 'Buffer',
|
|
38
|
+
});
|
|
39
|
+
},
|
|
35
40
|
// number types
|
|
36
41
|
number() {
|
|
37
42
|
return new JsonSchemaNumberBuilder();
|
|
@@ -124,6 +129,10 @@ export class JsonSchemaAnyBuilder {
|
|
|
124
129
|
Object.assign(this.schema, { allOf: schemas });
|
|
125
130
|
return this;
|
|
126
131
|
}
|
|
132
|
+
instanceof(of) {
|
|
133
|
+
this.schema.instanceof = of;
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
127
136
|
optional(optional = true) {
|
|
128
137
|
if (optional) {
|
|
129
138
|
this.schema.optionalField = true;
|
|
@@ -340,4 +349,3 @@ export class JsonSchemaTupleBuilder extends JsonSchemaAnyBuilder {
|
|
|
340
349
|
});
|
|
341
350
|
}
|
|
342
351
|
}
|
|
343
|
-
//# sourceMappingURL=jsonSchemaBuilder.js.map
|
package/dist-esm/lodash.types.js
CHANGED
package/dist-esm/math/sma.js
CHANGED
|
@@ -3,4 +3,3 @@ export async function pFilter(iterable, filterFn, opt) {
|
|
|
3
3
|
const values = await pMap(iterable, async (item, index) => await Promise.all([filterFn(item, index), item]), opt);
|
|
4
4
|
return values.filter(value => Boolean(value[0])).map(value => value[1]);
|
|
5
5
|
}
|
|
6
|
-
//# sourceMappingURL=pFilter.js.map
|
package/dist-esm/promise/pMap.js
CHANGED
package/dist-esm/string/case.js
CHANGED
|
@@ -91,10 +91,9 @@ export function _stringifyAny(obj, opt = {}) {
|
|
|
91
91
|
if (s === undefined)
|
|
92
92
|
return 'undefined';
|
|
93
93
|
// Handle maxLen
|
|
94
|
-
const { maxLen =
|
|
94
|
+
const { maxLen = 10000 } = opt;
|
|
95
95
|
if (maxLen && s.length > maxLen) {
|
|
96
96
|
s = s.slice(0, maxLen) + `... ${Math.ceil(s.length / 1024)} KB message truncated`;
|
|
97
97
|
}
|
|
98
98
|
return s;
|
|
99
99
|
}
|
|
100
|
-
//# sourceMappingURL=stringifyAny.js.map
|
package/dist-esm/typeFest.js
CHANGED
package/dist-esm/types.js
CHANGED