|
| 1 | +export function getBlankObject(schema) { |
| 2 | + let keys = {}; |
| 3 | + |
| 4 | + for (let key in schema.keys) { |
| 5 | + let value = schema.keys[key]; |
| 6 | + let type = value.type; |
| 7 | + |
| 8 | + if (type === 'string') |
| 9 | + keys[key] = ''; |
| 10 | + else if (type === 'array') |
| 11 | + keys[key] = getBlankArray(value); |
| 12 | + else if (type === 'object') |
| 13 | + keys[key] = getBlankObject(value); |
| 14 | + } |
| 15 | + |
| 16 | + return keys; |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +export function getBlankArray(schema) { |
| 21 | + let items = []; |
| 22 | + let type = schema.items.type; |
| 23 | + |
| 24 | + if (type === 'string') |
| 25 | + items.push(''); |
| 26 | + else if (type === 'array') |
| 27 | + items.push(getBlankArray(schema.items)) |
| 28 | + else if (type === 'object') |
| 29 | + items.push(getBlankObject(schema.items)); |
| 30 | + |
| 31 | + return items; |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +export function getBlankData(schema) { |
| 36 | + if (schema.type === 'array') { |
| 37 | + return getBlankArray(schema); |
| 38 | + } |
| 39 | + else if (schema.type === 'object') { |
| 40 | + return getBlankObject(schema); |
| 41 | + } else if (schema.type === 'string') { |
| 42 | + return ''; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +export function getBlankItem(schema) { |
| 48 | + let dataObject = {}; |
| 49 | + |
| 50 | + for (let key in schema.fields) { |
| 51 | + if (!schema.fields.hasOwnProperty(key)) |
| 52 | + continue; |
| 53 | + |
| 54 | + let item = schema.fields[key]; |
| 55 | + |
| 56 | + dataObject[key] = ''; |
| 57 | + } |
| 58 | + |
| 59 | + return dataObject; |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +export function getSyncedData(data, schema) { |
| 64 | + // adds those keys to data which are in schema but not in data |
| 65 | + |
| 66 | + let blankItem = getBlankItem(schema); |
| 67 | + |
| 68 | + if (schema.type === 'object') { |
| 69 | + return {...blankItem, ...data}; |
| 70 | + } else if (schema.type === 'array') { |
| 71 | + for (let i = 0; i < data.length; i++) { |
| 72 | + data[i] = {...blankItem, ...data[i]}; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return data; |
| 77 | +} |
0 commit comments