1 |
|
2 |
|
3 |
|
4 |
|
5 | 'use strict';
|
6 |
|
7 | const SchemaType = require('../schematype');
|
8 | const CastError = SchemaType.CastError;
|
9 | const Decimal128Type = require('../types/decimal128');
|
10 | const castDecimal128 = require('../cast/decimal128');
|
11 | const utils = require('../utils');
|
12 |
|
13 | const populateModelSymbol = require('../helpers/symbols').populateModelSymbol;
|
14 |
|
15 | let Document;
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function Decimal128(key, options) {
|
27 | SchemaType.call(this, key, options, 'Decimal128');
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | Decimal128.schemaName = 'Decimal128';
|
37 |
|
38 | Decimal128.defaultOptions = {};
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | Decimal128.prototype = Object.create(SchemaType.prototype);
|
44 | Decimal128.prototype.constructor = Decimal128;
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | Decimal128._cast = castDecimal128;
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | Decimal128.set = SchemaType.set;
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | Decimal128.cast = function cast(caster) {
|
96 | if (arguments.length === 0) {
|
97 | return this._cast;
|
98 | }
|
99 | if (caster === false) {
|
100 | caster = v => {
|
101 | if (v != null && !(v instanceof Decimal128Type)) {
|
102 | throw new Error();
|
103 | }
|
104 | return v;
|
105 | };
|
106 | }
|
107 | this._cast = caster;
|
108 |
|
109 | return this._cast;
|
110 | };
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | Decimal128._checkRequired = v => v instanceof Decimal128Type;
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | Decimal128.checkRequired = SchemaType.checkRequired;
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | Decimal128.prototype.checkRequired = function checkRequired(value, doc) {
|
141 | if (SchemaType._isRef(this, value, doc, true)) {
|
142 | return !!value;
|
143 | }
|
144 |
|
145 |
|
146 |
|
147 | const _checkRequired = typeof this.constructor.checkRequired == 'function' ?
|
148 | this.constructor.checkRequired() :
|
149 | Decimal128.checkRequired();
|
150 |
|
151 | return _checkRequired(value);
|
152 | };
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | Decimal128.prototype.cast = function(value, doc, init) {
|
164 | if (SchemaType._isRef(this, value, doc, init)) {
|
165 |
|
166 |
|
167 | if (value === null || value === undefined) {
|
168 | return value;
|
169 | }
|
170 |
|
171 |
|
172 | Document || (Document = require('./../document'));
|
173 |
|
174 | if (value instanceof Document) {
|
175 | value.$__.wasPopulated = true;
|
176 | return value;
|
177 | }
|
178 |
|
179 |
|
180 | if (value instanceof Decimal128Type) {
|
181 | return value;
|
182 | } else if (Buffer.isBuffer(value) || !utils.isObject(value)) {
|
183 | throw new CastError('Decimal128', value, this.path, null, this);
|
184 | }
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | const path = doc.$__fullPath(this.path);
|
190 | const owner = doc.ownerDocument ? doc.ownerDocument() : doc;
|
191 | const pop = owner.populated(path, true);
|
192 | let ret = value;
|
193 | if (!doc.$__.populated ||
|
194 | !doc.$__.populated[path] ||
|
195 | !doc.$__.populated[path].options ||
|
196 | !doc.$__.populated[path].options.options ||
|
197 | !doc.$__.populated[path].options.options.lean) {
|
198 | ret = new pop.options[populateModelSymbol](value);
|
199 | ret.$__.wasPopulated = true;
|
200 | }
|
201 |
|
202 | return ret;
|
203 | }
|
204 |
|
205 | const castDecimal128 = typeof this.constructor.cast === 'function' ?
|
206 | this.constructor.cast() :
|
207 | Decimal128.cast();
|
208 | try {
|
209 | return castDecimal128(value);
|
210 | } catch (error) {
|
211 | throw new CastError('Decimal128', value, this.path, error, this);
|
212 | }
|
213 | };
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 | function handleSingle(val) {
|
220 | return this.cast(val);
|
221 | }
|
222 |
|
223 | Decimal128.prototype.$conditionalHandlers =
|
224 | utils.options(SchemaType.prototype.$conditionalHandlers, {
|
225 | $gt: handleSingle,
|
226 | $gte: handleSingle,
|
227 | $lt: handleSingle,
|
228 | $lte: handleSingle
|
229 | });
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 | module.exports = Decimal128;
|