UNPKG

sequelize-store

Version:
63 lines (62 loc) 1.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isValueOptions = exports.validateType = exports.parseType = exports.getType = exports.isType = void 0; const errors_1 = require("./errors"); /// ///////////////////////////////////////////////////////////////////////////////////// // Helpers function function isType(t) { if (typeof t !== 'string' && !(t instanceof String)) { return false; } // @ts-ignore return ['int', 'float', 'bool', 'string', 'json'].includes(t); } exports.isType = isType; function getType(t) { if (isType(t)) { return t; } return t.type; } exports.getType = getType; function parseType(data, type) { switch (type) { case 'bool': return data === 'true'; case 'int': return parseInt(data); case 'float': return parseFloat(data); case 'json': return JSON.parse(data); case 'string': return data; default: throw new errors_1.ParseError(`Unknown type ${type}`); } } exports.parseType = parseType; function validateType(data, type) { switch (type) { case 'bool': return typeof data === 'boolean'; case 'int': return typeof data === 'number'; case 'float': return typeof data === 'number'; case 'json': return typeof data === 'object'; case 'string': return typeof data === 'string'; default: throw new errors_1.ParseError(`Unknown type ${type}`); } } exports.validateType = validateType; function isValueOptions(obj) { if (typeof obj !== 'object' || !('type' in obj)) { return false; } return isType(obj.type); } exports.isValueOptions = isValueOptions;