thoughts/node_modules/mongoose/node_modules/mongodb/lib/utils.js
node_modules/mongoose/node_modules/mongodb/lib/utils.jsRaw
1"use strict";
2
3var MongoError = require('mongodb-core').MongoError,
4 f = require('util').format;
5
6var shallowClone = function(obj) {
7 var copy = {};
8 for(var name in obj) copy[name] = obj[name];
9 return copy;
10}
11
12// Set simple property
13var getSingleProperty = function(obj, name, value) {
14 Object.defineProperty(obj, name, {
15 enumerable:true,
16 get: function() {
17 return value
18 }
19 });
20}
21
22var formatSortValue = exports.formatSortValue = function(sortDirection) {
23 var value = ("" + sortDirection).toLowerCase();
24
25 switch (value) {
26 case 'ascending':
27 case 'asc':
28 case '1':
29 return 1;
30 case 'descending':
31 case 'desc':
32 case '-1':
33 return -1;
34 default:
35 throw new Error("Illegal sort clause, must be of the form "
36 + "[['field1', '(ascending|descending)'], "
37 + "['field2', '(ascending|descending)']]");
38 }
39};
40
41var formattedOrderClause = exports.formattedOrderClause = function(sortValue) {
42 var orderBy = {};
43 if(sortValue == null) return null;
44 if (Array.isArray(sortValue)) {
45 if(sortValue.length === 0) {
46 return null;
47 }
48
49 for(var i = 0; i < sortValue.length; i++) {
50 if(sortValue[i].constructor == String) {
51 orderBy[sortValue[i]] = 1;
52 } else {
53 orderBy[sortValue[i][0]] = formatSortValue(sortValue[i][1]);
54 }
55 }
56 } else if(sortValue != null && typeof sortValue == 'object') {
57 orderBy = sortValue;
58 } else if (typeof sortValue == 'string') {
59 orderBy[sortValue] = 1;
60 } else {
61 throw new Error("Illegal sort clause, must be of the form " +
62 "[['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]");
63 }
64
65 return orderBy;
66};
67
68var checkCollectionName = function checkCollectionName (collectionName) {
69 if('string' !== typeof collectionName) {
70 throw Error("collection name must be a String");
71 }
72
73 if(!collectionName || collectionName.indexOf('..') != -1) {
74 throw Error("collection names cannot be empty");
75 }
76
77 if(collectionName.indexOf('$') != -1 &&
78 collectionName.match(/((^\$cmd)|(oplog\.\$main))/) == null) {
79 throw Error("collection names must not contain '$'");
80 }
81
82 if(collectionName.match(/^\.|\.$/) != null) {
83 throw Error("collection names must not start or end with '.'");
84 }
85
86 // Validate that we are not passing 0x00 in the colletion name
87 if(!!~collectionName.indexOf("\x00")) {
88 throw new Error("collection names cannot contain a null character");
89 }
90};
91
92var handleCallback = function(callback, err, value1, value2) {
93 try {
94 if(callback == null) return;
95 if(value2) return callback(err, value1, value2);
96 return callback(err, value1);
97 } catch(err) {
98 process.nextTick(function() { throw err; });
99 return false;
100 }
101
102 return true;
103}
104
105/**
106 * Wrap a Mongo error document in an Error instance
107 * @ignore
108 * @api private
109 */
110var toError = function(error) {
111 if (error instanceof Error) return error;
112
113 var msg = error.err || error.errmsg || error.errMessage || error;
114 var e = MongoError.create({message: msg, driver:true});
115
116 // Get all object keys
117 var keys = typeof error == 'object'
118 ? Object.keys(error)
119 : [];
120
121 for(var i = 0; i < keys.length; i++) {
122 try {
123 e[keys[i]] = error[keys[i]];
124 } catch(err) {
125 // continue
126 }
127 }
128
129 return e;
130}
131
132/**
133 * @ignore
134 */
135var normalizeHintField = function normalizeHintField(hint) {
136 var finalHint = null;
137
138 if(typeof hint == 'string') {
139 finalHint = hint;
140 } else if(Array.isArray(hint)) {
141 finalHint = {};
142
143 hint.forEach(function(param) {
144 finalHint[param] = 1;
145 });
146 } else if(hint != null && typeof hint == 'object') {
147 finalHint = {};
148 for (var name in hint) {
149 finalHint[name] = hint[name];
150 }
151 }
152
153 return finalHint;
154};
155
156/**
157 * Create index name based on field spec
158 *
159 * @ignore
160 * @api private
161 */
162var parseIndexOptions = function(fieldOrSpec) {
163 var fieldHash = {};
164 var indexes = [];
165 var keys;
166
167 // Get all the fields accordingly
168 if('string' == typeof fieldOrSpec) {
169 // 'type'
170 indexes.push(fieldOrSpec + '_' + 1);
171 fieldHash[fieldOrSpec] = 1;
172 } else if(Array.isArray(fieldOrSpec)) {
173 fieldOrSpec.forEach(function(f) {
174 if('string' == typeof f) {
175 // [{location:'2d'}, 'type']
176 indexes.push(f + '_' + 1);
177 fieldHash[f] = 1;
178 } else if(Array.isArray(f)) {
179 // [['location', '2d'],['type', 1]]
180 indexes.push(f[0] + '_' + (f[1] || 1));
181 fieldHash[f[0]] = f[1] || 1;
182 } else if(isObject(f)) {
183 // [{location:'2d'}, {type:1}]
184 keys = Object.keys(f);
185 keys.forEach(function(k) {
186 indexes.push(k + '_' + f[k]);
187 fieldHash[k] = f[k];
188 });
189 } else {
190 // undefined (ignore)
191 }
192 });
193 } else if(isObject(fieldOrSpec)) {
194 // {location:'2d', type:1}
195 keys = Object.keys(fieldOrSpec);
196 keys.forEach(function(key) {
197 indexes.push(key + '_' + fieldOrSpec[key]);
198 fieldHash[key] = fieldOrSpec[key];
199 });
200 }
201
202 return {
203 name: indexes.join("_"), keys: keys, fieldHash: fieldHash
204 }
205}
206
207var isObject = exports.isObject = function (arg) {
208 return '[object Object]' == toString.call(arg)
209}
210
211var debugOptions = function(debugFields, options) {
212 var finaloptions = {};
213 debugFields.forEach(function(n) {
214 finaloptions[n] = options[n];
215 });
216
217 return finaloptions;
218}
219
220var decorateCommand = function(command, options, exclude) {
221 for(var name in options) {
222 if(exclude[name] == null) command[name] = options[name];
223 }
224
225 return command;
226}
227
228exports.shallowClone = shallowClone;
229exports.getSingleProperty = getSingleProperty;
230exports.checkCollectionName = checkCollectionName;
231exports.toError = toError;
232exports.formattedOrderClause = formattedOrderClause;
233exports.parseIndexOptions = parseIndexOptions;
234exports.normalizeHintField = normalizeHintField;
235exports.handleCallback = handleCallback;
236exports.decorateCommand = decorateCommand;
237exports.isObject = isObject;
238exports.debugOptions = debugOptions;
239exports.MAX_JS_INT = 0x20000000000000;