UNPKG

pinia-orm

Version:

The Pinia plugin to enable Object-Relational Mapping access to the Pinia Store.

131 lines (123 loc) 2.79 kB
'use strict'; const CastAttribute = require('./shared/pinia-orm.DWjxIbAJ.cjs'); class ArrayCast extends CastAttribute.CastAttribute { /** * Create a new String attribute instance. */ constructor(attributes) { super(attributes); } get(value) { return typeof value !== "string" ? value : JSON.parse(value); } /** * Make the value for the attribute. */ set(value) { return JSON.stringify(value); } } class StringCast extends CastAttribute.CastAttribute { /** * Create a new String attribute instance. */ constructor(attributes) { super(attributes); } get(value) { return typeof value === "string" || value === void 0 || value === null ? value : `${value}`; } /** * Make the value for the attribute. */ set(value) { return this.get(value); } } class BooleanCast extends CastAttribute.CastAttribute { /** * Create a new String attribute instance. */ constructor(attributes) { super(attributes); } get(value) { if (typeof value === "boolean" || value === void 0 || value === null) { return value; } if (typeof value === "string") { if (value.length === 0) { return false; } const int = Number.parseInt(value, 0); return Number.isNaN(int) ? true : !!int; } if (typeof value === "number") { return !!value; } return false; } /** * Make the value for the attribute. */ set(value) { return this.get(value); } } class NumberCast extends CastAttribute.CastAttribute { /** * Create a new String attribute instance. */ constructor(attributes) { super(attributes); } get(value) { if (typeof value === "number" || value === void 0 || value === null) { return value; } if (typeof value === "string") { return Number.parseFloat(value); } if (typeof value === "boolean") { return value ? 1 : 0; } return 0; } /** * Make the value for the attribute. */ set(value) { return this.get(value); } } class DateCast extends CastAttribute.CastAttribute { /** * Create a new String attribute instance. */ constructor(attributes) { super(attributes); } get(value) { return value === null ? null : new Date(value); } /** * Make the value for the attribute. */ set(value) { if (value === null) { return null; } if (typeof value === "number") { return new Date(value).toISOString(); } if (typeof value === "string") { return new Date(Date.parse(value)).toISOString(); } return value.toISOString(); } } exports.ArrayCast = ArrayCast; exports.BooleanCast = BooleanCast; exports.DateCast = DateCast; exports.NumberCast = NumberCast; exports.StringCast = StringCast;