UNPKG

jswagger

Version:
54 lines (53 loc) 2.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function leafConvertToClassValue(propInfo, input) { if (propInfo.type === 'string' && propInfo.format === 'date-time') { return new Date(input); } else if (propInfo.type === 'string' && propInfo.format === 'byte') { return Buffer.from(input, 'binary'); } return input; } exports.leafConvertToClassValue = leafConvertToClassValue; function leafConvertToJsonValue(propInfo, input) { if (propInfo.type === 'string' && propInfo.format === 'date-time' && input instanceof Date) { return input.toISOString(); } else if (propInfo.type === 'string' && propInfo.format === 'byte' && Buffer.isBuffer(input)) { return input.toString('binary'); } return input; } exports.leafConvertToJsonValue = leafConvertToJsonValue; function toClassValue(metadata, property, key, value) { const lastKey = key.split('/').reduce((prev, cur) => cur, ''); const propInfo = ((tmp) => { return tmp.$ref ? metadata.definitions[tmp.$ref] : tmp; })(property[lastKey]); if (typeof value === 'object' && propInfo.type === 'object') { if (propInfo.properties) { const _properties = propInfo.properties; return Object.entries(propInfo.properties).reduce((results, [curKey, curProp]) => { results[curKey] = toClassValue(metadata, _properties, key + '/' + curKey, value[curKey]); return results; }, {}); } } return leafConvertToClassValue(propInfo, value); } exports.toClassValue = toClassValue; function toJsonObject(metadata, property, key, value) { const lastKey = key.split('/').reduce((prev, cur) => cur, ''); const propInfo = ((tmp) => { return tmp.$ref ? metadata.definitions[tmp.$ref] : tmp; })(property[lastKey]); if (typeof value === 'object' && propInfo.type === 'object') { return Object.entries(propInfo.properties).reduce((results, [curKey, curProp]) => { results[curKey] = toJsonObject(metadata, propInfo.properties, key + '/' + curKey, value[curKey]); return results; }, {}); } return leafConvertToJsonValue(propInfo, value); } exports.toJsonObject = toJsonObject;