UNPKG

hydrate-mongodb

Version:
89 lines (88 loc) 3.25 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var 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 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 mappingBase_1 = require("./mappingBase"); var mappingModel_1 = require("./mappingModel"); var EnumMapping = (function (_super) { __extends(EnumMapping, _super); function EnumMapping(members, ignoreCase) { var _this = _super.call(this, 16) || this; _this.members = members; _this.type = 0; _this._values = []; _this.ignoreCase = ignoreCase || false; for (var name in members) { if (typeof name === "string" && members.hasOwnProperty(name)) { var value = members[name]; _this._values[value] = name; } } return _this; } EnumMapping.prototype.read = function (context, value) { if (value == null) return null; if (typeof value === "number") { return value; } if (typeof value === "string") { if (!this.ignoreCase) { var result = this.members[value]; } else { value = value.toLowerCase(); for (var name in this.members) { if (this.members.hasOwnProperty(name) && typeof name === "string") { if (name.toLowerCase() === value) { var result = this.members[name]; break; } } } } if (result === undefined) { context.addError("'" + value + "' is not a valid enum value."); } return result; } context.addError("Enum value must be a string or number."); }; EnumMapping.prototype.write = function (context, value) { if (value == null) return null; if (typeof value !== "number") { context.addError("Expected enum value to be a number."); return; } switch (this.type) { case 1: return value; case 0: var name = this._values[value]; if (!name) { context.addError("Could not find enum member name for value '" + value + "'."); return; } return name; default: if (this.type == null) { context.addError("Enum type not specified." + this.type); } else { context.addError("Unknown enum type '" + this.type + "'."); } return; } }; return EnumMapping; }(mappingBase_1.MappingBase)); exports.EnumMapping = EnumMapping;