@based/schema 2.2.10 → 2.2.12
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/{display → src/display}/index.d.ts +1 -1
- package/dist/src/display/index.js +16 -0
- package/dist/{display → src/display}/number.js +1 -3
- package/dist/{display → src/display}/string.js +1 -3
- package/dist/{display → src/display}/timestamp.js +1 -3
- package/dist/{error.js → src/error.js} +2 -5
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.js +6 -0
- package/dist/{languages.js → src/languages.js} +1 -4
- package/dist/src/set/fields/array.d.ts +2 -0
- package/dist/{set → src/set}/fields/array.js +10 -14
- package/dist/src/set/fields/index.d.ts +3 -0
- package/dist/src/set/fields/index.js +71 -0
- package/dist/{set → src/set}/fields/number.d.ts +1 -1
- package/dist/{set → src/set}/fields/number.js +15 -21
- package/dist/{set → src/set}/fields/object.d.ts +1 -1
- package/dist/src/set/fields/object.js +28 -0
- package/dist/{set → src/set}/fields/references.d.ts +1 -1
- package/dist/{set → src/set}/fields/references.js +13 -18
- package/dist/src/set/fields/set.d.ts +2 -0
- package/dist/{set → src/set}/fields/set.js +4 -8
- package/dist/{set → src/set}/fields/string.d.ts +1 -1
- package/dist/{set → src/set}/fields/string.js +87 -95
- package/dist/{set → src/set}/index.d.ts +2 -2
- package/dist/{set → src/set}/index.js +21 -25
- package/dist/{set → src/set}/isValidId.d.ts +1 -1
- package/dist/{set → src/set}/isValidId.js +1 -5
- package/dist/{types.d.ts → src/types.d.ts} +7 -6
- package/dist/src/types.js +23 -0
- package/dist/src/updateSchema.d.ts +2 -0
- package/dist/src/updateSchema.js +12 -0
- package/dist/src/validateSchema.d.ts +4 -0
- package/dist/{validateSchema.js → src/validateSchema.js} +5 -11
- package/dist/{walker → src/walker}/args.d.ts +4 -4
- package/dist/{walker → src/walker}/args.js +8 -12
- package/dist/src/walker/index.d.ts +6 -0
- package/dist/src/walker/index.js +31 -0
- package/dist/{walker → src/walker}/parse.d.ts +1 -1
- package/dist/{walker → src/walker}/parse.js +7 -11
- package/dist/{walker → src/walker}/types.d.ts +3 -3
- package/dist/{walker → src/walker}/types.js +2 -5
- package/dist/test/array.d.ts +1 -0
- package/dist/test/array.js +342 -0
- package/dist/test/display.d.ts +1 -0
- package/dist/test/display.js +103 -0
- package/dist/test/number.d.ts +1 -0
- package/dist/test/number.js +374 -0
- package/dist/test/reference.d.ts +1 -0
- package/dist/test/reference.js +186 -0
- package/dist/test/rest.d.ts +1 -0
- package/dist/test/rest.js +168 -0
- package/dist/test/set.d.ts +1 -0
- package/dist/test/set.js +95 -0
- package/dist/test/string.d.ts +1 -0
- package/dist/test/string.js +120 -0
- package/dist/test/text.d.ts +1 -0
- package/dist/test/text.js +324 -0
- package/dist/test/utils/index.d.ts +11 -0
- package/dist/test/utils/index.js +17 -0
- package/dist/test/validateSchema.d.ts +1 -0
- package/dist/test/validateSchema.js +38 -0
- package/dist/test/walker.d.ts +1 -0
- package/dist/test/walker.js +289 -0
- package/package.json +8 -13
- package/dist/display/dateString copy.d.ts +0 -3
- package/dist/display/dateString copy.js +0 -127
- package/dist/display/dateString.d.ts +0 -3
- package/dist/display/dateString.js +0 -127
- package/dist/display/display.d.ts +0 -1
- package/dist/display/display.js +0 -8
- package/dist/display/index.js +0 -26
- package/dist/display/number copy.d.ts +0 -3
- package/dist/display/number copy.js +0 -81
- package/dist/display.d.ts +0 -2
- package/dist/display.js +0 -8
- package/dist/index.d.ts +0 -5
- package/dist/index.js +0 -22
- package/dist/set/fields/array.d.ts +0 -2
- package/dist/set/fields/index.d.ts +0 -3
- package/dist/set/fields/index.js +0 -74
- package/dist/set/fields/object.js +0 -33
- package/dist/set/fields/set.d.ts +0 -2
- package/dist/set/types.d.ts +0 -0
- package/dist/set/types.js +0 -1
- package/dist/types.js +0 -27
- package/dist/updateSchema.d.ts +0 -2
- package/dist/updateSchema.js +0 -16
- package/dist/validateSchema.d.ts +0 -4
- package/dist/walker/index.d.ts +0 -6
- package/dist/walker/index.js +0 -49
- /package/dist/{display → src/display}/number.d.ts +0 -0
- /package/dist/{display → src/display}/string.d.ts +0 -0
- /package/dist/{display → src/display}/timestamp.d.ts +0 -0
- /package/dist/{error.d.ts → src/error.d.ts} +0 -0
- /package/dist/{languages.d.ts → src/languages.d.ts} +0 -0
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { BasedSchema } from '../types.js';
|
|
2
|
+
import { ArgsClass } from './args.js';
|
|
3
|
+
import { AsyncOperation, Opts } from './types.js';
|
|
4
|
+
export declare const walk: <T>(schema: BasedSchema, opts: Opts<T>, value: any, asyncOperationHandler?: AsyncOperation<T>) => Promise<T>;
|
|
5
|
+
export { ArgsClass };
|
|
6
|
+
export * from './types.js';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ArgsClass } from './args.js';
|
|
2
|
+
export const walk = async (schema, opts, value, asyncOperationHandler) => {
|
|
3
|
+
if (!('collect' in opts)) {
|
|
4
|
+
opts.collect = () => { };
|
|
5
|
+
}
|
|
6
|
+
if (!('error' in opts)) {
|
|
7
|
+
opts.error = () => { };
|
|
8
|
+
}
|
|
9
|
+
if (asyncOperationHandler) {
|
|
10
|
+
opts = {
|
|
11
|
+
...opts,
|
|
12
|
+
asyncOperationHandler,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
const argsOpts = await opts.init(value, schema, opts.error);
|
|
16
|
+
if (!argsOpts) {
|
|
17
|
+
return {};
|
|
18
|
+
}
|
|
19
|
+
if (!argsOpts.value) {
|
|
20
|
+
argsOpts.value = value;
|
|
21
|
+
}
|
|
22
|
+
const args = new ArgsClass(argsOpts);
|
|
23
|
+
args.root = args;
|
|
24
|
+
args._opts = opts;
|
|
25
|
+
args._schema = schema;
|
|
26
|
+
await args.parse();
|
|
27
|
+
return args.target;
|
|
28
|
+
};
|
|
29
|
+
export { ArgsClass };
|
|
30
|
+
export * from './types.js';
|
|
31
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.parse = void 0;
|
|
4
|
-
const args_1 = require("./args");
|
|
5
|
-
const types_1 = require("./types");
|
|
1
|
+
import { ArgsClass } from './args.js';
|
|
2
|
+
import { Stopped } from './types.js';
|
|
6
3
|
// TODO needs cleaning
|
|
7
4
|
function createOrUseArgs(from, newArgs) {
|
|
8
5
|
if (!newArgs) {
|
|
9
6
|
return;
|
|
10
7
|
}
|
|
11
|
-
if (newArgs instanceof
|
|
8
|
+
if (newArgs instanceof ArgsClass) {
|
|
12
9
|
return newArgs;
|
|
13
10
|
}
|
|
14
11
|
const x = from.create(newArgs);
|
|
@@ -17,7 +14,7 @@ function createOrUseArgs(from, newArgs) {
|
|
|
17
14
|
return x;
|
|
18
15
|
}
|
|
19
16
|
async function parseKey(from, key, parser) {
|
|
20
|
-
const keyArgs = new
|
|
17
|
+
const keyArgs = new ArgsClass({
|
|
21
18
|
key,
|
|
22
19
|
value: from.value[key],
|
|
23
20
|
fieldSchema: from.fieldSchema,
|
|
@@ -29,7 +26,7 @@ async function parseKey(from, key, parser) {
|
|
|
29
26
|
}
|
|
30
27
|
}
|
|
31
28
|
function createFieldArgs(from, key, fieldSchema) {
|
|
32
|
-
return new
|
|
29
|
+
return new ArgsClass({
|
|
33
30
|
key,
|
|
34
31
|
value: from.value[key],
|
|
35
32
|
// @ts-ignore needs key
|
|
@@ -42,7 +39,7 @@ function getFieldParser(args) {
|
|
|
42
39
|
: args.root._opts.parsers.fields[args.fieldSchema.type];
|
|
43
40
|
return fieldParser;
|
|
44
41
|
}
|
|
45
|
-
async function parse(args) {
|
|
42
|
+
export async function parse(args) {
|
|
46
43
|
const opts = args.root._opts;
|
|
47
44
|
if (args.parseTopLevel) {
|
|
48
45
|
const parser = opts.parsers.any;
|
|
@@ -126,7 +123,7 @@ async function parse(args) {
|
|
|
126
123
|
return;
|
|
127
124
|
}
|
|
128
125
|
// any
|
|
129
|
-
if (args.stopped !==
|
|
126
|
+
if (args.stopped !== Stopped.stopAll) {
|
|
130
127
|
const parser = opts.parsers.any || opts.parsers.catch;
|
|
131
128
|
if (parser) {
|
|
132
129
|
const q = [];
|
|
@@ -182,5 +179,4 @@ async function parse(args) {
|
|
|
182
179
|
}
|
|
183
180
|
}
|
|
184
181
|
}
|
|
185
|
-
exports.parse = parse;
|
|
186
182
|
//# sourceMappingURL=parse.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ParseError } from '../error';
|
|
2
|
-
import { BasedSchemaType, BasedSchemaFields, BasedSchemaField, BasedSchema } from '../types';
|
|
3
|
-
import { ArgsClass } from './args';
|
|
1
|
+
import { ParseError } from '../error.js';
|
|
2
|
+
import { BasedSchemaType, BasedSchemaFields, BasedSchemaField, BasedSchema } from '../types.js';
|
|
3
|
+
import { ArgsClass } from './args.js';
|
|
4
4
|
export type Path = (string | number)[];
|
|
5
5
|
export type ErrorHandler<T> = (code: ParseError, args: ArgsClass<T> | ArgsOpts<T>) => void;
|
|
6
6
|
export type Collect<T> = (args: ArgsClass<T>) => any;
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Stopped = void 0;
|
|
4
|
-
var Stopped;
|
|
1
|
+
export var Stopped;
|
|
5
2
|
(function (Stopped) {
|
|
6
3
|
Stopped[Stopped["onlyStopFieldParser"] = 0] = "onlyStopFieldParser";
|
|
7
4
|
Stopped[Stopped["stopAll"] = 1] = "stopAll";
|
|
8
|
-
})(Stopped || (
|
|
5
|
+
})(Stopped || (Stopped = {}));
|
|
9
6
|
// Add asyncOperations // requiresAsyncValidation -> asyncOperation: () => {}
|
|
10
7
|
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import test from 'ava';
|
|
2
|
+
import { setWalker } from '../src/index.js';
|
|
3
|
+
import { errorCollect, resultCollect } from './utils/index.js';
|
|
4
|
+
const schema = {
|
|
5
|
+
types: {
|
|
6
|
+
bla: {
|
|
7
|
+
prefix: 'bl',
|
|
8
|
+
fields: {
|
|
9
|
+
arrNum: {
|
|
10
|
+
type: 'array',
|
|
11
|
+
values: {
|
|
12
|
+
type: 'number',
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
objArray: {
|
|
16
|
+
type: 'array',
|
|
17
|
+
values: {
|
|
18
|
+
type: 'object',
|
|
19
|
+
properties: {
|
|
20
|
+
snurp: {
|
|
21
|
+
type: 'string',
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
arrStr: {
|
|
27
|
+
type: 'array',
|
|
28
|
+
values: {
|
|
29
|
+
type: 'string',
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
intarray: {
|
|
33
|
+
type: 'array',
|
|
34
|
+
values: {
|
|
35
|
+
type: 'integer',
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
$defs: {},
|
|
42
|
+
language: 'en',
|
|
43
|
+
root: {
|
|
44
|
+
fields: {},
|
|
45
|
+
},
|
|
46
|
+
prefixToTypeMapping: {
|
|
47
|
+
bl: 'bla',
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
test('arrayNum', async (t) => {
|
|
51
|
+
const err = await setWalker(schema, {
|
|
52
|
+
$id: 'bl1',
|
|
53
|
+
arrNum: ['1', '2'],
|
|
54
|
+
});
|
|
55
|
+
const err1 = await setWalker(schema, {
|
|
56
|
+
$id: 'bla',
|
|
57
|
+
ref: 1,
|
|
58
|
+
});
|
|
59
|
+
const res = await setWalker(schema, {
|
|
60
|
+
$id: 'bla',
|
|
61
|
+
arrNum: [1, 2],
|
|
62
|
+
});
|
|
63
|
+
t.true(errorCollect(err, err1).length > 0);
|
|
64
|
+
t.deepEqual(resultCollect(res), [
|
|
65
|
+
{ path: ['arrNum'], value: { $delete: true } },
|
|
66
|
+
{ path: ['arrNum', 0], value: 1 },
|
|
67
|
+
{ path: ['arrNum', 1], value: 2 },
|
|
68
|
+
]);
|
|
69
|
+
});
|
|
70
|
+
test('value arr', async (t) => {
|
|
71
|
+
const err = await setWalker(schema, {
|
|
72
|
+
$id: 'bl1',
|
|
73
|
+
arrNum: { $value: ['1', '2'] },
|
|
74
|
+
});
|
|
75
|
+
t.true(err.errors.length > 1);
|
|
76
|
+
const res = await setWalker(schema, {
|
|
77
|
+
$id: 'bla',
|
|
78
|
+
arrNum: [{ $value: 1 }, { $value: 2 }],
|
|
79
|
+
});
|
|
80
|
+
const res1 = await setWalker(schema, {
|
|
81
|
+
$id: 'bla',
|
|
82
|
+
arrNum: { $value: [1, 2] },
|
|
83
|
+
});
|
|
84
|
+
t.deepEqual(resultCollect(res, res1), [
|
|
85
|
+
{ path: ['arrNum'], value: { $delete: true } },
|
|
86
|
+
{ path: ['arrNum', 0], value: 1 },
|
|
87
|
+
{ path: ['arrNum', 1], value: 2 },
|
|
88
|
+
{ path: ['arrNum'], value: { $delete: true } },
|
|
89
|
+
{ path: ['arrNum', 0], value: 1 },
|
|
90
|
+
{ path: ['arrNum', 1], value: 2 },
|
|
91
|
+
]);
|
|
92
|
+
});
|
|
93
|
+
test.only('default arr', async (t) => {
|
|
94
|
+
const err = await setWalker(schema, {
|
|
95
|
+
$id: 'bl1',
|
|
96
|
+
arrNum: ['1', '2'],
|
|
97
|
+
});
|
|
98
|
+
const err1 = await setWalker(schema, {
|
|
99
|
+
$id: 'bla',
|
|
100
|
+
ref: 1,
|
|
101
|
+
});
|
|
102
|
+
const res = await setWalker(schema, {
|
|
103
|
+
$id: 'bla',
|
|
104
|
+
arrNum: [{ $default: 1 }, { $default: 2 }],
|
|
105
|
+
});
|
|
106
|
+
const res1 = await setWalker(schema, {
|
|
107
|
+
$id: 'bla',
|
|
108
|
+
arrNum: { $default: [1, 2] },
|
|
109
|
+
});
|
|
110
|
+
t.true(errorCollect(err, err1).length > 0);
|
|
111
|
+
t.deepEqual(resultCollect(res, res1), [
|
|
112
|
+
{ path: ['arrNum'], value: { $delete: true } },
|
|
113
|
+
{ path: ['arrNum', 0], value: { $default: 1 } },
|
|
114
|
+
{ path: ['arrNum', 1], value: { $default: 2 } },
|
|
115
|
+
// TODO bit sketchy needs some work
|
|
116
|
+
{ path: ['arrNum'], value: { $default: [1, 2] } },
|
|
117
|
+
]);
|
|
118
|
+
});
|
|
119
|
+
let r;
|
|
120
|
+
test('assign idx value', async (t) => {
|
|
121
|
+
r = await setWalker(schema, {
|
|
122
|
+
$id: 'bl120',
|
|
123
|
+
intarray: {
|
|
124
|
+
$assign: {
|
|
125
|
+
$idx: 0,
|
|
126
|
+
$value: 6,
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
t.deepEqual(resultCollect(r), [{ path: ['intarray', 0], value: 6 }]);
|
|
131
|
+
});
|
|
132
|
+
test('push ints', async (t) => {
|
|
133
|
+
r = await setWalker(schema, {
|
|
134
|
+
$id: 'bl120',
|
|
135
|
+
intarray: {
|
|
136
|
+
$push: [1, 2, 3, 4, 5],
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
t.deepEqual(resultCollect(r), [
|
|
140
|
+
{ path: ['intarray'], value: { $push: [1, 2, 3, 4, 5] } },
|
|
141
|
+
]);
|
|
142
|
+
});
|
|
143
|
+
test('push objs', async (t) => {
|
|
144
|
+
r = await setWalker(schema, {
|
|
145
|
+
$id: 'bl120',
|
|
146
|
+
objArray: {
|
|
147
|
+
$push: [{ snurp: 'a' }, { snurp: 'b' }, { snurp: 'c' }],
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
t.deepEqual(resultCollect(r), [
|
|
151
|
+
{
|
|
152
|
+
path: ['objArray'],
|
|
153
|
+
value: {
|
|
154
|
+
$push: [{ snurp: 'a' }, { snurp: 'b' }, { snurp: 'c' }],
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
{ path: ['objArray', -3, 'snurp'], value: 'a' },
|
|
158
|
+
{ path: ['objArray', -2, 'snurp'], value: 'b' },
|
|
159
|
+
{ path: ['objArray', -1, 'snurp'], value: 'c' },
|
|
160
|
+
{ path: ['objArray', -3], value: { snurp: 'a' } },
|
|
161
|
+
{ path: ['objArray', -2], value: { snurp: 'b' } },
|
|
162
|
+
{ path: ['objArray', -1], value: { snurp: 'c' } },
|
|
163
|
+
]);
|
|
164
|
+
});
|
|
165
|
+
test('unshift ints', async (t) => {
|
|
166
|
+
r = await setWalker(schema, {
|
|
167
|
+
$id: 'bl120',
|
|
168
|
+
intarray: {
|
|
169
|
+
$unshift: [1, 2, 3, 4, 5],
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
t.deepEqual(resultCollect(r), [
|
|
173
|
+
{ path: ['intarray'], value: { $unshift: [1, 2, 3, 4, 5] } },
|
|
174
|
+
]);
|
|
175
|
+
t.true(true);
|
|
176
|
+
});
|
|
177
|
+
test('nested default unshift', async (t) => {
|
|
178
|
+
r = await setWalker(schema, {
|
|
179
|
+
$id: 'bl120',
|
|
180
|
+
intarray: {
|
|
181
|
+
$unshift: [{ $value: 1 }, { $default: 2 }, 3, 4, 5],
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
t.is(r.errors.length, 0);
|
|
185
|
+
t.deepEqual(resultCollect(r), [
|
|
186
|
+
{
|
|
187
|
+
path: ['intarray'],
|
|
188
|
+
value: { $unshift: [1, { $default: 2 }, 3, 4, 5] },
|
|
189
|
+
},
|
|
190
|
+
]);
|
|
191
|
+
});
|
|
192
|
+
test('nested default in push', async (t) => {
|
|
193
|
+
r = await setWalker(schema, {
|
|
194
|
+
$id: 'bl120',
|
|
195
|
+
intarray: {
|
|
196
|
+
$push: [{ $value: 1 }, { $default: 2 }, 3, 4, 5],
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
t.is(r.errors.length, 0);
|
|
200
|
+
t.deepEqual(resultCollect(r), [
|
|
201
|
+
{
|
|
202
|
+
path: ['intarray'],
|
|
203
|
+
value: { $push: [1, { $default: 2 }, 3, 4, 5] },
|
|
204
|
+
},
|
|
205
|
+
]);
|
|
206
|
+
});
|
|
207
|
+
test('assign idx default value error', async (t) => {
|
|
208
|
+
r = await setWalker(schema, {
|
|
209
|
+
$id: 'bl120',
|
|
210
|
+
intarray: {
|
|
211
|
+
$assign: {
|
|
212
|
+
$idx: { $default: 0 },
|
|
213
|
+
$value: 6,
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
});
|
|
217
|
+
t.true(r.errors.length > 0);
|
|
218
|
+
});
|
|
219
|
+
test('assign idx no value', async (t) => {
|
|
220
|
+
r = await setWalker(schema, {
|
|
221
|
+
$id: 'bl120',
|
|
222
|
+
intarray: {
|
|
223
|
+
$assign: {
|
|
224
|
+
$idx: { $default: 0 },
|
|
225
|
+
},
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
t.true(r.errors.length > 0);
|
|
229
|
+
});
|
|
230
|
+
test('assign idx value spelled wrong', async (t) => {
|
|
231
|
+
r = await setWalker(schema, {
|
|
232
|
+
$id: 'bl120',
|
|
233
|
+
intarray: {
|
|
234
|
+
$assign: {
|
|
235
|
+
$idx: 0,
|
|
236
|
+
value: 5,
|
|
237
|
+
},
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
t.true(r.errors.length > 0);
|
|
241
|
+
});
|
|
242
|
+
test('assign idx value wrong type', async (t) => {
|
|
243
|
+
r = await setWalker(schema, {
|
|
244
|
+
$id: 'bl120',
|
|
245
|
+
intarray: {
|
|
246
|
+
$assign: {
|
|
247
|
+
$idx: 0,
|
|
248
|
+
$value: 5.6,
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
});
|
|
252
|
+
t.true(r.errors.length > 0);
|
|
253
|
+
});
|
|
254
|
+
test('assign idx value value wrong type', async (t) => {
|
|
255
|
+
r = await setWalker(schema, {
|
|
256
|
+
$id: 'bl120',
|
|
257
|
+
intarray: {
|
|
258
|
+
$assign: {
|
|
259
|
+
$idx: 0,
|
|
260
|
+
$value: { $value: 5.6 },
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
});
|
|
264
|
+
t.true(r.errors.length > 0);
|
|
265
|
+
});
|
|
266
|
+
test('assign idx default value', async (t) => {
|
|
267
|
+
r = await setWalker(schema, {
|
|
268
|
+
$id: 'bl120',
|
|
269
|
+
intarray: {
|
|
270
|
+
$assign: {
|
|
271
|
+
$idx: 0,
|
|
272
|
+
$value: { $default: 5 },
|
|
273
|
+
},
|
|
274
|
+
},
|
|
275
|
+
});
|
|
276
|
+
t.deepEqual(resultCollect(r), [
|
|
277
|
+
{ path: ['intarray', 0], value: { $default: 5 } },
|
|
278
|
+
]);
|
|
279
|
+
});
|
|
280
|
+
test('assign idx value value error', async (t) => {
|
|
281
|
+
r = await setWalker(schema, {
|
|
282
|
+
$id: 'bl120',
|
|
283
|
+
intarray: {
|
|
284
|
+
$assign: {
|
|
285
|
+
$idx: { $value: 0 },
|
|
286
|
+
$value: 6,
|
|
287
|
+
},
|
|
288
|
+
},
|
|
289
|
+
});
|
|
290
|
+
t.true(r.errors.length > 0);
|
|
291
|
+
});
|
|
292
|
+
test('insert idx intarray', async (t) => {
|
|
293
|
+
r = await setWalker(schema, {
|
|
294
|
+
$id: 'bl120',
|
|
295
|
+
intarray: {
|
|
296
|
+
$insert: {
|
|
297
|
+
$idx: 10,
|
|
298
|
+
$value: 1212,
|
|
299
|
+
},
|
|
300
|
+
},
|
|
301
|
+
});
|
|
302
|
+
t.true(r.errors.length === 0);
|
|
303
|
+
t.deepEqual(resultCollect(r), [
|
|
304
|
+
{
|
|
305
|
+
path: ['intarray'],
|
|
306
|
+
value: { $insert: { $idx: 10, $value: [1212] } },
|
|
307
|
+
},
|
|
308
|
+
]);
|
|
309
|
+
});
|
|
310
|
+
test('unshift array', async (t) => {
|
|
311
|
+
r = await setWalker(schema, {
|
|
312
|
+
$id: 'bl120',
|
|
313
|
+
intarray: {
|
|
314
|
+
$unshift: { $value: [-10, -20, -30] },
|
|
315
|
+
},
|
|
316
|
+
});
|
|
317
|
+
t.true(r.errors.length === 0);
|
|
318
|
+
t.deepEqual(resultCollect(r), [
|
|
319
|
+
{ path: ['intarray'], value: { $unshift: [-10, -20, -30] } },
|
|
320
|
+
]);
|
|
321
|
+
});
|
|
322
|
+
test('assign + $delete', async (t) => {
|
|
323
|
+
r = await setWalker(schema, {
|
|
324
|
+
$id: 'bl120',
|
|
325
|
+
objArray: {
|
|
326
|
+
$assign: {
|
|
327
|
+
$idx: 3,
|
|
328
|
+
$value: {
|
|
329
|
+
snurp: {
|
|
330
|
+
$delete: true,
|
|
331
|
+
},
|
|
332
|
+
},
|
|
333
|
+
},
|
|
334
|
+
},
|
|
335
|
+
});
|
|
336
|
+
t.is(r.errors.length, 0);
|
|
337
|
+
t.deepEqual(resultCollect(r), [
|
|
338
|
+
{ path: ['objArray', 3, 'snurp'], value: { $delete: true } },
|
|
339
|
+
{ path: ['objArray', 3], value: { snurp: { $delete: true } } },
|
|
340
|
+
]);
|
|
341
|
+
});
|
|
342
|
+
//# sourceMappingURL=array.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import test from 'ava';
|
|
2
|
+
import { display } from '../src/index.js';
|
|
3
|
+
const schema = {
|
|
4
|
+
types: {
|
|
5
|
+
thing: {
|
|
6
|
+
prefix: 'ti',
|
|
7
|
+
fields: {
|
|
8
|
+
dateHuman: { type: 'timestamp', display: 'human' },
|
|
9
|
+
dateTime: { type: 'timestamp', display: 'date-time' },
|
|
10
|
+
dateTimeText: { type: 'timestamp', display: 'date-time-text' },
|
|
11
|
+
time: { type: 'timestamp', display: 'time' },
|
|
12
|
+
timePrecise: { type: 'timestamp', display: 'time-precise' },
|
|
13
|
+
capitalize: {
|
|
14
|
+
type: 'string',
|
|
15
|
+
display: 'capitalize',
|
|
16
|
+
format: 'lowercase',
|
|
17
|
+
},
|
|
18
|
+
upperCase: { type: 'string', display: 'uppercase' },
|
|
19
|
+
lowerCase: { type: 'string', display: 'lowercase' },
|
|
20
|
+
euros: { type: 'number', display: 'euro' },
|
|
21
|
+
dollars: { type: 'number', display: 'dollar' },
|
|
22
|
+
pounds: { type: 'number', display: 'pound' },
|
|
23
|
+
bytes: { type: 'number', display: 'bytes' },
|
|
24
|
+
humanNumber: { type: 'number', display: 'human' },
|
|
25
|
+
ratio: { type: 'number', display: 'ratio' },
|
|
26
|
+
short: { type: 'number', display: 'short' },
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
$defs: {},
|
|
31
|
+
language: 'en',
|
|
32
|
+
root: {
|
|
33
|
+
fields: {},
|
|
34
|
+
},
|
|
35
|
+
prefixToTypeMapping: {
|
|
36
|
+
bl: 'bla',
|
|
37
|
+
ti: 'thing',
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
test('date display', async (t) => {
|
|
41
|
+
const now = 1701695869765;
|
|
42
|
+
const parsed = [
|
|
43
|
+
display('bla', schema.types.thing.fields.capitalize),
|
|
44
|
+
display('b', schema.types.thing.fields.capitalize),
|
|
45
|
+
display(undefined, schema.types.thing.fields.capitalize),
|
|
46
|
+
display('', schema.types.thing.fields.capitalize),
|
|
47
|
+
display('bla', schema.types.thing.fields.upperCase),
|
|
48
|
+
display('BLA', schema.types.thing.fields.lowerCase),
|
|
49
|
+
display(100000, schema.types.thing.fields.euros),
|
|
50
|
+
display(100000, schema.types.thing.fields.dollars),
|
|
51
|
+
display(10.21, schema.types.thing.fields.euros),
|
|
52
|
+
display(10.2312234342, schema.types.thing.fields.dollars),
|
|
53
|
+
display(1001.21212, schema.types.thing.fields.euros),
|
|
54
|
+
display(1001.21212, schema.types.thing.fields.dollars),
|
|
55
|
+
display(10000.21212, schema.types.thing.fields.euros),
|
|
56
|
+
display(10000.21212, schema.types.thing.fields.dollars),
|
|
57
|
+
display(100e6, schema.types.thing.fields.euros),
|
|
58
|
+
display(100e6, schema.types.thing.fields.dollars),
|
|
59
|
+
display(100e6, schema.types.thing.fields.bytes),
|
|
60
|
+
display(100e9, schema.types.thing.fields.bytes),
|
|
61
|
+
display(100e9, schema.types.thing.fields.short),
|
|
62
|
+
];
|
|
63
|
+
t.deepEqual(parsed, [
|
|
64
|
+
'Bla',
|
|
65
|
+
'B',
|
|
66
|
+
undefined,
|
|
67
|
+
'',
|
|
68
|
+
'BLA',
|
|
69
|
+
'bla',
|
|
70
|
+
'€100k',
|
|
71
|
+
'$100k',
|
|
72
|
+
'€10,21',
|
|
73
|
+
'$10.23',
|
|
74
|
+
'€1001,21',
|
|
75
|
+
'$1001.21',
|
|
76
|
+
'€10k',
|
|
77
|
+
'$10k',
|
|
78
|
+
'€100m',
|
|
79
|
+
'$100m',
|
|
80
|
+
'95.37 mb',
|
|
81
|
+
'93.13 gb',
|
|
82
|
+
'100b',
|
|
83
|
+
]);
|
|
84
|
+
const dates = [
|
|
85
|
+
display(now + 20e3, schema.types.thing.fields.dateTimeText),
|
|
86
|
+
display(now + 20e3, schema.types.thing.fields.timePrecise),
|
|
87
|
+
display(now + 20e3, schema.types.thing.fields.dateTime),
|
|
88
|
+
display(now + 20e3, {
|
|
89
|
+
type: 'timestamp',
|
|
90
|
+
display: 'date-time-text',
|
|
91
|
+
}),
|
|
92
|
+
];
|
|
93
|
+
for (const d of dates) {
|
|
94
|
+
if (!d) {
|
|
95
|
+
t.fail('date is not parsed');
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
t.regex(String(display(now + 20e3, schema.types.thing.fields.dateTimeText)), /^December 4, 2023, *./);
|
|
99
|
+
t.regex(String(display(now + 20e3, schema.types.thing.fields.timePrecise)), /^[0-9]{2}:18:09$/);
|
|
100
|
+
t.regex(String(display(now + 20e3, schema.types.thing.fields.dateTime)), /^[0-9]{2}:18 4\/12\/2023$/);
|
|
101
|
+
t.regex(String(display(now + 20e3, schema.types.thing.fields.time)), /^[0-9]{2}:18$/);
|
|
102
|
+
});
|
|
103
|
+
//# sourceMappingURL=display.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|