Last change
on this file since 6a80231 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
910 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = builder;
|
---|
| 7 |
|
---|
| 8 | var _definitions = require("../definitions");
|
---|
| 9 |
|
---|
| 10 | var _validate = require("../validators/validate");
|
---|
| 11 |
|
---|
| 12 | function builder(type, ...args) {
|
---|
| 13 | const keys = _definitions.BUILDER_KEYS[type];
|
---|
| 14 | const countArgs = args.length;
|
---|
| 15 |
|
---|
| 16 | if (countArgs > keys.length) {
|
---|
| 17 | throw new Error(`${type}: Too many arguments passed. Received ${countArgs} but can receive no more than ${keys.length}`);
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | const node = {
|
---|
| 21 | type
|
---|
| 22 | };
|
---|
| 23 | let i = 0;
|
---|
| 24 | keys.forEach(key => {
|
---|
| 25 | const field = _definitions.NODE_FIELDS[type][key];
|
---|
| 26 | let arg;
|
---|
| 27 | if (i < countArgs) arg = args[i];
|
---|
| 28 |
|
---|
| 29 | if (arg === undefined) {
|
---|
| 30 | arg = Array.isArray(field.default) ? [] : field.default;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | node[key] = arg;
|
---|
| 34 | i++;
|
---|
| 35 | });
|
---|
| 36 |
|
---|
| 37 | for (const key of Object.keys(node)) {
|
---|
| 38 | (0, _validate.default)(node, key, node[key]);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | return node;
|
---|
| 42 | } |
---|
Note:
See
TracBrowser
for help on using the repository browser.