UNPKG

data-mapping

Version:

Mapping the response to the defined data type.

122 lines (121 loc) 4.72 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var mapping_error_1 = require("./mapping.error"); var mapping_utils_1 = require("./mapping.utils"); function tryParseString(rs) { if (typeof rs === 'string') { try { return JSON.parse(rs); } catch (e) { } } return rs; } function getParser(mapper) { if (typeof mapper === 'function') { var mapperClass = void 0; if (mapping_utils_1.subclassOf(mapper, DataMapper)) { mapperClass = mapper; } else { mapperClass = /** @class */ (function (_super) { __extends(class_1, _super); function class_1() { return _super !== null && _super.apply(this, arguments) || this; } class_1.prototype.map = function (rs, index, data) { var mapperFunction = mapper; return mapperFunction(rs, index, data); }; return class_1; }(DataMapper)); } return new mapperClass(); } else throw new mapping_error_1.MappingError(mapping_error_1.ErrorConstant.PROVIDER_MAPPER, 2); } function checkIgnore(ignore) { if (ignore) { if (ignore === 'null') return function (result) { return result == null; }; else if (typeof ignore === 'function') return ignore; } return function () { return false; }; } var DataMapper = /** @class */ (function () { function DataMapper() { } DataMapper.parseWith = function (mapper, rs, options) { var parser = getParser(mapper); if (rs == null) throw new mapping_error_1.MappingError(mapping_error_1.ErrorConstant.NULL, 3); var result = parser.parse(tryParseString(rs), undefined, options); var isIgnore = checkIgnore(options && options.ignore || undefined); if (isIgnore(result)) throw new mapping_error_1.MappingError(mapping_error_1.ErrorConstant.IGNORED, 4); return result; }; DataMapper.parseArrayWith = function (mapper, rs, options) { var parser = getParser(mapper); var result = []; if (rs == null) throw new mapping_error_1.MappingError(mapping_error_1.ErrorConstant.NULL, 3); rs = tryParseString(rs); if (rs instanceof Array) { var isIgnore = checkIgnore(options && options.ignore || undefined); for (var i = 0, len = rs.length; i < len; i++) { var item = parser.parse(rs[i], i, options); if (isIgnore(item, i)) continue; result.push(item); } } else throw new mapping_error_1.MappingError(mapping_error_1.ErrorConstant.NOT_ARRAY, 4); return result; }; DataMapper.parseMapWith = function (mapper, rs, options) { var parser = getParser(mapper); var result = {}; if (rs == null) throw new mapping_error_1.MappingError(mapping_error_1.ErrorConstant.NULL, 3); rs = tryParseString(rs); var isObj = typeof rs === 'object'; var keys = Object.getOwnPropertyNames(rs); if (keys && keys.length > 0) { var key = void 0; var isIgnore = checkIgnore(options && options.ignore || undefined); for (var i = 0, len = keys.length; i < len; i++) { key = keys[i]; if (isObj || key !== 'length' || typeof rs[key] !== 'number') { var item = parser.parse(rs[key], i, options); if (isIgnore(item, i)) continue; result[key] = item; } } } return result; }; DataMapper.prototype.parse = function (rs, index, data) { return this.map(rs, index, data); }; return DataMapper; }()); exports.DataMapper = DataMapper;