thoughts/node_modules/mongoose/lib/schema/mixed.js
node_modules/mongoose/lib/schema/mixed.jsRaw
1/*!
2 * Module dependencies.
3 */
4
5var SchemaType = require('../schematype');
6var utils = require('../utils');
7
8/**
9 * Mixed SchemaType constructor.
10 *
11 * @param {String} path
12 * @param {Object} options
13 * @inherits SchemaType
14 * @api public
15 */
16
17function Mixed(path, options) {
18 if (options && options.default) {
19 var def = options.default;
20 if (Array.isArray(def) && def.length === 0) {
21 // make sure empty array defaults are handled
22 options.default = Array;
23 } else if (!options.shared && utils.isObject(def) && Object.keys(def).length === 0) {
24 // prevent odd "shared" objects between documents
25 options.default = function() {
26 return {};
27 };
28 }
29 }
30
31 SchemaType.call(this, path, options, 'Mixed');
32}
33
34/**
35 * This schema type's name, to defend against minifiers that mangle
36 * function names.
37 *
38 * @api public
39 */
40Mixed.schemaName = 'Mixed';
41
42/*!
43 * Inherits from SchemaType.
44 */
45Mixed.prototype = Object.create(SchemaType.prototype);
46Mixed.prototype.constructor = Mixed;
47
48/**
49 * Casts `val` for Mixed.
50 *
51 * _this is a no-op_
52 *
53 * @param {Object} value to cast
54 * @api private
55 */
56
57Mixed.prototype.cast = function(val) {
58 return val;
59};
60
61/**
62 * Casts contents for queries.
63 *
64 * @param {String} $cond
65 * @param {any} [val]
66 * @api private
67 */
68
69Mixed.prototype.castForQuery = function($cond, val) {
70 if (arguments.length === 2) {
71 return val;
72 }
73 return $cond;
74};
75
76/*!
77 * Module exports.
78 */
79
80module.exports = Mixed;