1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | var _utils = require("./utils");
|
9 |
|
10 | function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
11 |
|
12 |
|
13 | function getDefaultValue(_ref) {
|
14 | var def = _ref.default,
|
15 | _ref$enum = _ref.enum,
|
16 | enums = _ref$enum === void 0 ? [] : _ref$enum,
|
17 | type = _ref.type;
|
18 | var defaultValue = {
|
19 | array: [],
|
20 | boolean: false,
|
21 | integer: '',
|
22 | null: null,
|
23 | number: '',
|
24 | object: {},
|
25 | string: '',
|
26 | range: null
|
27 | };
|
28 |
|
29 | if ((0, _utils.isFunction)(def)) {
|
30 | return defaultValue[type];
|
31 | }
|
32 |
|
33 | if ((0, _utils.isFunction)(enums)) {
|
34 | if (type === 'array') {
|
35 | return [];
|
36 | }
|
37 |
|
38 | if (type === 'string' || type === 'number') {
|
39 | return '';
|
40 | }
|
41 | }
|
42 |
|
43 |
|
44 | if (typeof def !== 'undefined') {
|
45 | return def;
|
46 | }
|
47 |
|
48 |
|
49 | if (type === 'array' && enums.length) {
|
50 | return [];
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 | if (Array.isArray(enums) && enums[0] && typeof enums[0] !== 'undefined') {
|
56 | return enums[0];
|
57 | }
|
58 |
|
59 |
|
60 | return defaultValue[type];
|
61 | }
|
62 |
|
63 | function resolve(schema, data) {
|
64 | var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
65 | var type = schema.type,
|
66 | properties = schema.properties,
|
67 | items = schema.items,
|
68 | def = schema.default,
|
69 | _schema$required = schema.required,
|
70 | required = _schema$required === void 0 ? [] : _schema$required,
|
71 | widget = schema['ui:widget'];
|
72 | var _options$checkRequire = options.checkRequired,
|
73 | checkRequired = _options$checkRequire === void 0 ? false : _options$checkRequire;
|
74 | var value = typeof data === 'undefined' ? getDefaultValue(schema) : (0, _utils.clone)(data);
|
75 |
|
76 | if (type === 'object') {
|
77 |
|
78 | if (widget) {
|
79 | if (def && _typeof(def) === 'object') {
|
80 | return def;
|
81 | }
|
82 |
|
83 | return value;
|
84 | }
|
85 |
|
86 | var subs = properties || {};
|
87 | var ret = {};
|
88 | Object.keys(subs).forEach(function (name) {
|
89 | var checkAndPass = checkRequired && [].concat(required).indexOf(name) !== -1;
|
90 |
|
91 | if (!checkRequired || checkAndPass) {
|
92 | ret[name] = resolve(subs[name], value[name], options);
|
93 | }
|
94 | });
|
95 | return ret;
|
96 | }
|
97 |
|
98 | if (type === 'array') {
|
99 |
|
100 | if (def && Array.isArray(def) && !value) {
|
101 | return def;
|
102 | }
|
103 |
|
104 |
|
105 | if (widget) return value;
|
106 |
|
107 | var _subs = [].concat(items || []);
|
108 |
|
109 | var _ret = [];
|
110 | value.forEach && value.forEach(function (item, idx) {
|
111 | _ret[idx] = resolve(_subs[idx] || _subs[0], item, options);
|
112 | });
|
113 | return _ret;
|
114 | }
|
115 |
|
116 | return value;
|
117 | }
|
118 |
|
119 | var _default = resolve;
|
120 | exports.default = _default; |
\ | No newline at end of file |