
mongodb.types.js Maven / Gradle / Ivy
// Date and time types
if (typeof(Timestamp) != "undefined"){
Timestamp.prototype.tojson = function() {
return this.toString();
}
Timestamp.prototype.getTime = function() {
return this.t;
}
Timestamp.prototype.getInc = function() {
return this.i;
}
Timestamp.prototype.toString = function() {
return "Timestamp(" + this.t + ", " + this.i + ")";
}
}
else {
print("warning: no Timestamp class");
}
Date.timeFunc = function(theFunc, numTimes){
var start = new Date();
numTimes = numTimes || 1;
for (var i=0; i 0 ? '-' : '+'; // This is correct
// ofs += (ofsmin/60).zeroPad(2)
// ofs += (ofsmin%60).zeroPad(2)
// }
return 'ISODate("'+year+'-'+month+'-'+date+'T'+hour+':'+minute+':'+sec+ofs+'")';
}
ISODate = function(isoDateStr){
if (!isoDateStr)
return new Date();
var isoDateRegex = /(\d{4})-?(\d{2})-?(\d{2})([T ](\d{2})(:?(\d{2})(:?(\d{2}(\.\d+)?))?)?(Z|([+-])(\d{2}):?(\d{2})?)?)?/;
var res = isoDateRegex.exec(isoDateStr);
if (!res)
throw "invalid ISO date";
var year = parseInt(res[1],10) || 1970; // this should always be present
var month = (parseInt(res[2],10) || 1) - 1;
var date = parseInt(res[3],10) || 0;
var hour = parseInt(res[5],10) || 0;
var min = parseInt(res[7],10) || 0;
var sec = parseFloat(res[9]) || 0;
var ms = Math.round((sec%1) * 1000)
sec -= ms/1000
var time = Date.UTC(year, month, date, hour, min, sec, ms);
if (res[11] && res[11] != 'Z'){
var ofs = 0;
ofs += (parseInt(res[13],10) || 0) * 60*60*1000; // hours
ofs += (parseInt(res[14],10) || 0) * 60*1000; // mins
if (res[12] == '+') // if ahead subtract
ofs *= -1;
time += ofs
}
return new Date(time);
}
// Regular Expression
RegExp.escape = function(text){
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
RegExp.prototype.tojson = RegExp.prototype.toString;
// Array
Array.contains = function(a, x){
for (var i=0; i 0; i--) {
if (right) {
str = str + chr;
} else {
str = chr + str;
}
}
return str;
}
// Number
Number.prototype.toPercentStr = function() {
return (this * 100).toFixed(2) + "%";
}
Number.prototype.zeroPad = function(width) {
return ('' + this).pad(width, false, '0');
}
// NumberLong
if (! NumberLong.prototype) {
NumberLong.prototype = {}
}
NumberLong.prototype.tojson = function() {
return this.toString();
}
// NumberInt
if (! NumberInt.prototype) {
NumberInt.prototype = {}
}
NumberInt.prototype.tojson = function() {
return this.toString();
}
// ObjectId
if (! ObjectId.prototype)
ObjectId.prototype = {}
ObjectId.prototype.toString = function(){
return "ObjectId(" + tojson(this.str) + ")";
}
ObjectId.prototype.tojson = function(){
return this.toString();
}
ObjectId.prototype.valueOf = function(){
return this.str;
}
ObjectId.prototype.isObjectId = true;
ObjectId.prototype.getTimestamp = function(){
return new Date(parseInt(this.valueOf().slice(0,8), 16)*1000);
}
ObjectId.prototype.equals = function(other){
return this.str == other.str;
}
// DBPointer
if (typeof(DBPointer) != "undefined"){
DBPointer.prototype.fetch = function(){
assert(this.ns, "need a ns");
assert(this.id, "need an id");
return db[ this.ns ].findOne({ _id : this.id });
}
DBPointer.prototype.tojson = function(indent){
return this.toString();
}
DBPointer.prototype.getCollection = function(){
return this.ns;
}
DBPointer.prototype.getId = function(){
return this.id;
}
DBPointer.prototype.toString = function(){
return "DBPointer(" + tojson(this.ns) + ", " + tojson(this.id) + ")";
}
}
else {
print("warning: no DBPointer");
}
// DBRef
if (typeof(DBRef) != "undefined"){
DBRef.prototype.fetch = function(){
assert(this.$ref, "need a ns");
assert(this.$id, "need an id");
return db[ this.$ref ].findOne({ _id : this.$id });
}
DBRef.prototype.tojson = function(indent){
return this.toString();
}
DBRef.prototype.getCollection = function(){
return this.$ref;
}
DBRef.prototype.getRef = function(){
return this.$ref;
}
DBRef.prototype.getId = function(){
return this.$id;
}
DBRef.prototype.toString = function(){
return "DBRef(" + tojson(this.$ref) + ", " + tojson(this.$id) + ")";
}
}
else {
print("warning: no DBRef");
}
// BinData
if (typeof(BinData) != "undefined"){
BinData.prototype.tojson = function() {
return this.toString();
}
BinData.prototype.subtype = function() {
return this.type;
}
BinData.prototype.length = function() {
return this.len;
}
}
else {
print("warning: no BinData class");
}
// Map
if (typeof(Map) == "undefined"){
Map = function(){
this._data = {};
}
}
Map.hash = function(val){
if (! val)
return val;
switch (typeof(val)){
case 'string':
case 'number':
case 'date':
return val.toString();
case 'object':
case 'array':
var s = "";
for (var k in val){
s += k + val[k];
}
return s;
}
throw "can't hash : " + typeof(val);
}
Map.prototype.put = function(key, value){
var o = this._get(key);
var old = o.value;
o.value = value;
return old;
}
Map.prototype.get = function(key){
return this._get(key).value;
}
Map.prototype._get = function(key){
var h = Map.hash(key);
var a = this._data[h];
if (! a){
a = [];
this._data[h] = a;
}
for (var i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy