|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
-
-
- 'use strict';
-
- const SchemaType = require('../schematype');
- const CastError = SchemaType.CastError;
- const Decimal128Type = require('../types/decimal128');
- const castDecimal128 = require('../cast/decimal128');
- const utils = require('../utils');
-
- let Document;
-
-
-
- function Decimal128(key, options) {
- SchemaType.call(this, key, options, 'Decimal128');
- }
-
-
- Decimal128.schemaName = 'Decimal128';
-
-
- Decimal128.prototype = Object.create(SchemaType.prototype);
- Decimal128.prototype.constructor = Decimal128;
-
-
-
- Decimal128._cast = castDecimal128;
-
-
-
- Decimal128.cast = function cast(caster) {
- if (arguments.length === 0) {
- return this._cast;
- }
- if (caster === false) {
- caster = v => {
- if (v != null && !(v instanceof Decimal128Type)) {
- throw new Error();
- }
- return v;
- };
- }
- this._cast = caster;
-
- return this._cast;
- };
-
-
-
- Decimal128._checkRequired = v => v instanceof Decimal128Type;
-
-
-
- Decimal128.checkRequired = SchemaType.checkRequired;
-
-
-
- Decimal128.prototype.checkRequired = function checkRequired(value, doc) {
- if (SchemaType._isRef(this, value, doc, true)) {
- return !!value;
- }
-
-
-
- const _checkRequired = typeof this.constructor.checkRequired == 'function' ?
- this.constructor.checkRequired() :
- Decimal128.checkRequired();
-
- return _checkRequired(value);
- };
-
-
-
- Decimal128.prototype.cast = function(value, doc, init) {
- if (SchemaType._isRef(this, value, doc, init)) {
-
-
- if (value === null || value === undefined) {
- return value;
- }
-
-
- Document || (Document = require('./../document'));
-
- if (value instanceof Document) {
- value.$__.wasPopulated = true;
- return value;
- }
-
-
- if (value instanceof Decimal128Type) {
- return value;
- } else if (Buffer.isBuffer(value) || !utils.isObject(value)) {
- throw new CastError('Decimal128', value, this.path);
- }
-
-
-
-
- const path = doc.$__fullPath(this.path);
- const owner = doc.ownerDocument ? doc.ownerDocument() : doc;
- const pop = owner.populated(path, true);
- let ret = value;
- if (!doc.$__.populated ||
- !doc.$__.populated[path] ||
- !doc.$__.populated[path].options ||
- !doc.$__.populated[path].options.options ||
- !doc.$__.populated[path].options.options.lean) {
- ret = new pop.options.model(value);
- ret.$__.wasPopulated = true;
- }
-
- return ret;
- }
-
- const castDecimal128 = typeof this.constructor.cast === 'function' ?
- this.constructor.cast() :
- Decimal128.cast();
- try {
- return castDecimal128(value);
- } catch (error) {
- throw new CastError('Decimal128', value, this.path);
- }
- };
-
-
-
- function handleSingle(val) {
- return this.cast(val);
- }
-
- Decimal128.prototype.$conditionalHandlers =
- utils.options(SchemaType.prototype.$conditionalHandlers, {
- $gt: handleSingle,
- $gte: handleSingle,
- $lt: handleSingle,
- $lte: handleSingle
- });
-
-
-
- module.exports = Decimal128;
|