forked from Meteor-Community-Packages/meteor-simple-schema
-
Notifications
You must be signed in to change notification settings - Fork 2
/
simple-schema-utility.js
64 lines (63 loc) · 2.18 KB
/
simple-schema-utility.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* global Utility:true */
Utility = {
appendAffectedKey: function appendAffectedKey(affectedKey, key) {
if (key === "$each") {
return affectedKey;
} else {
return (affectedKey ? affectedKey + "." + key : key);
}
},
shouldCheck: function shouldCheck(key) {
if (key === "$pushAll") {
throw new Error("$pushAll is not supported; use $push + $each");
}
return !_.contains(["$pull", "$pullAll", "$pop", "$slice"], key);
},
errorObject: function errorObject(errorType, keyName, keyValue) {
return {name: keyName, type: errorType, value: keyValue};
},
// Tests whether it's an Object as opposed to something that inherits from Object
isBasicObject: function isBasicObject(obj) {
return _.isObject(obj) && Object.getPrototypeOf(obj) === Object.prototype;
},
// The latest Safari returns false for Uint8Array, etc. instanceof Function
// unlike other browsers.
safariBugFix: function safariBugFix(type) {
return (typeof Uint8Array !== "undefined" && type === Uint8Array) ||
(typeof Uint16Array !== "undefined" && type === Uint16Array) ||
(typeof Uint32Array !== "undefined" && type === Uint32Array) ||
(typeof Uint8ClampedArray !== "undefined" && type === Uint8ClampedArray);
},
isNotNullOrUndefined: function isNotNullOrUndefined(val) {
return val !== void 0 && val !== null;
},
// Extracts operator piece, if present, from position string
extractOp: function extractOp(position) {
var firstPositionPiece = position.slice(0, position.indexOf("["));
return (firstPositionPiece.substring(0, 1) === "$") ? firstPositionPiece : null;
},
deleteIfPresent: function deleteIfPresent(obj, key) {
if (key in obj) {
delete obj[key];
}
},
looksLikeModifier: function looksLikeModifier(obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key) && key.substring(0, 1) === "$") {
return true;
}
}
return false;
},
dateToDateString: function dateToDateString(date) {
var m = (date.getUTCMonth() + 1);
if (m < 10) {
m = "0" + m;
}
var d = date.getUTCDate();
if (d < 10) {
d = "0" + d;
}
return date.getUTCFullYear() + '-' + m + '-' + d;
}
};