4 print = function(msg) {
9 friendlyEqual = function( a , b ){
13 if ( tojson( a ) == tojson( b ) )
20 doassert = function( msg ){
21 print( "assert: " + msg );
25 assert = function( b , msg ){
26 if ( assert._debug && msg ) print( "in assert for: " + msg );
31 doassert( "assert failed : " + msg );
34 assert.eq = function( a , b , msg ){
35 if ( assert._debug && msg ) print( "in assert for: " + msg );
40 if ( ( a != null && b != null ) && friendlyEqual( a , b ) )
43 doassert( "[" + tojson( a ) + "] != [" + tojson( b ) + "] are not equal : " + msg );
46 assert.neq = function( a , b , msg ){
47 if ( assert._debug && msg ) print( "in assert for: " + msg );
51 doassert( "[" + a + "] != [" + b + "] are equal : " + msg );
54 assert.soon = function( f, msg, timeout, interval ) {
55 if ( assert._debug && msg ) print( "in assert for: " + msg );
57 var start = new Date();
58 timeout = timeout || 30000;
59 interval = interval || 200;
63 if ( typeof( f ) == "string" ){
72 if ( ( new Date() ).getTime() - start.getTime() > timeout )
73 doassert( "assert.soon failed: " + f + ", msg:" + msg );
78 assert.throws = function( func , params , msg ){
79 if ( assert._debug && msg ) print( "in assert for: " + msg );
81 func.apply( null , params );
87 doassert( "did not throw exception: " + msg );
90 assert.commandWorked = function( res , msg ){
91 if ( assert._debug && msg ) print( "in assert for: " + msg );
96 doassert( "command failed: " + tojson( res ) + " : " + msg );
99 assert.commandFailed = function( res , msg ){
100 if ( assert._debug && msg ) print( "in assert for: " + msg );
105 doassert( "command worked when it should have failed: " + tojson( res ) + " : " + msg );
108 assert.isnull = function( what , msg ){
109 if ( assert._debug && msg ) print( "in assert for: " + msg );
114 doassert( "supposed to null (" + ( msg || "" ) + ") was: " + tojson( what ) );
117 assert.lt = function( a , b , msg ){
118 if ( assert._debug && msg ) print( "in assert for: " + msg );
122 doassert( a + " is not less than " + b + " : " + msg );
125 assert.gt = function( a , b , msg ){
126 if ( assert._debug && msg ) print( "in assert for: " + msg );
130 doassert( a + " is not greater than " + b + " : " + msg );
133 Object.extend = function( dst , src , deep ){
134 for ( var k in src ){
136 if ( deep && typeof(v) == "object" ){
137 v = Object.extend( typeof ( v.length ) == "number" ? [] : {} , v , true );
144 argumentsToArray = function( a ){
146 for ( var i=0; i<a.length; i++ )
151 isString = function( x ){
152 return typeof( x ) == "string";
155 isNumber = function(x){
156 return typeof( x ) == "number";
159 isObject = function( x ){
160 return typeof( x ) == "object";
163 String.prototype.trim = function() {
164 return this.replace(/^\s+|\s+$/g,"");
166 String.prototype.ltrim = function() {
167 return this.replace(/^\s+/,"");
169 String.prototype.rtrim = function() {
170 return this.replace(/\s+$/,"");
173 Date.timeFunc = function( theFunc , numTimes ){
175 var start = new Date();
177 numTimes = numTimes || 1;
178 for ( var i=0; i<numTimes; i++ ){
179 theFunc.apply( null , argumentsToArray( arguments ).slice( 2 ) );
182 return (new Date()).getTime() - start.getTime();
185 Date.prototype.tojson = function(){
186 return "\"" + this.toString() + "\"";
189 RegExp.prototype.tojson = RegExp.prototype.toString;
191 Array.contains = function( a , x ){
192 for ( var i=0; i<a.length; i++ ){
199 Array.unique = function( a ){
201 for ( var i=0; i<a.length; i++){
203 if ( ! Array.contains( u , o ) ){
210 Array.shuffle = function( arr ){
211 for ( var i=0; i<arr.length-1; i++ ){
212 var pos = i+Math.floor(Math.random()*(arr.length-i));
221 Array.tojson = function( a , indent , x , html){
230 var s = spacer + "[ " + spacer;
232 for ( var i=0; i<a.length; i++){
233 s += indent + tojson( a[i], indent );
234 if ( i < a.length - 1 ){
238 if ( a.length == 0 ) {
242 indent = indent.substring(1);
243 s += spacer + " "+"]";
247 Array.fetchRefs = function( arr , coll ){
249 for ( var i=0; i<arr.length; i ++){
251 if ( coll && coll != z.getCollection() )
259 Array.sum = function( arr ){
260 if ( arr.length == 0 )
263 for ( var i=1; i<arr.length; i++ )
268 Array.avg = function( arr ){
269 if ( arr.length == 0 )
271 return Array.sum( arr ) / arr.length;
274 Array.stdDev = function( arr ){
275 var avg = Array.avg( arr );
278 for ( var i=0; i<arr.length; i++ ){
279 sum += Math.pow( arr[i] - avg , 2 );
282 return Math.sqrt( sum / arr.length );
285 if ( ! ObjectId.prototype )
286 ObjectId.prototype = {}
288 ObjectId.prototype.toString = function(){
292 ObjectId.prototype.tojson = function(){
293 return "ObjectId(\"" + this.str + "\")";
296 ObjectId.prototype.isObjectId = true;
298 tojson = function( x, indent , nolint , html){
302 if ( x == undefined )
312 for ( var i=0; i<x.length; i++ ){
327 var s = tojsonObject( x, indent , nolint , html);
328 if ( ( nolint == null || nolint == true ) && s.length < 80 && ( indent == null || indent.length == 0 ) ){
329 s = s.replace( /[\s\r\n ]+/gm , " " );
339 throw "tojson can't handle type " + ( typeof x );
344 tojsonObject = function( x, indent , nolint , html){
346 var lineEnding = "<br/>";
347 var tabSpace = " ";
350 var lineEnding = nolint ? " " : "\n";
351 var tabSpace = nolint ? "" : "\t";
354 assert.eq( ( typeof x ) , "object" , "tojsonObject needs object, not [" + ( typeof x ) + "]" );
359 if ( x.hasOwnProperty("__str__")) {
363 if ( typeof( x.tojson ) == "function" && x.tojson != tojson ) {
364 return x.tojson(indent,nolint,html);
367 if ( typeof( x.constructor.tojson ) == "function" && x.constructor.tojson != tojson ) {
368 return x.constructor.tojson( x, indent , nolint, html );
371 if ( x.toString() == "[object MaxKey]" )
372 return "{ $maxKey : 1 }";
373 if ( x.toString() == "[object MinKey]" )
374 return "{ $minKey : 1 }";
376 var s = "{" + lineEnding;
378 // push one level of indent
382 for ( var k in x ) total++;
384 s += indent + lineEnding;
388 if ( typeof( x._simpleKeys ) == "function" )
389 keys = x._simpleKeys();
391 for ( var k in keys ){
394 s += indent + "\"" + k + "\" : " + tojson( val, indent , nolint );
402 // pop one level of indent
403 indent = indent.substring(1);
404 return s + indent + "}";
407 shellPrint = function( x ){
409 if ( x != undefined )
410 shellPrintHelper( x );
413 printjson = function(x){
414 print( tojson( x ) );
417 shellPrintHelper = function( x ){
419 if ( typeof( x ) == "undefined" ){
429 if ( typeof x != "object" )
432 var p = x.shellPrint;
433 if ( typeof p == "function" )
434 return x.shellPrint();
437 if ( typeof p == "function" )
440 print( tojson( x ) );
443 shellHelper = function( command , rest , shouldPrint ){
444 command = command.trim();
445 var args = rest.trim().replace(/;$/,"").split( "\s+" );
447 if ( ! shellHelper[command] )
448 throw "no command [" + command + "]";
450 var res = shellHelper[command].apply( null , args );
452 shellPrintHelper( res );
457 help = shellHelper.help = function(){
459 print( "\t" + "show dbs show database names");
460 print( "\t" + "show collections show collections in current database");
461 print( "\t" + "show users show users in current database");
462 print( "\t" + "show profile show most recent system.profile entries with time >= 1ms");
463 print( "\t" + "use <db name> set curent database to <db name>" );
464 print( "\t" + "db.help() help on DB methods");
465 print( "\t" + "db.foo.help() help on collection methods");
466 print( "\t" + "db.foo.find() list objects in collection foo" );
467 print( "\t" + "db.foo.find( { a : 1 } ) list objects in foo where a == 1" );
468 print( "\t" + "it result of the last line evaluated; use to further iterate");
471 if ( typeof( Map ) == "undefined" ){
477 Map.hash = function( val ){
481 switch ( typeof( val ) ){
485 return val.toString();
489 for ( var k in val ){
495 throw "can't hash : " + typeof( val );
498 Map.prototype.put = function( key , value ){
499 var o = this._get( key );
505 Map.prototype.get = function( key ){
506 return this._get( key ).value;
509 Map.prototype._get = function( key ){
510 var h = Map.hash( key );
511 var a = this._data[h];
517 for ( var i=0; i<a.length; i++ ){
518 if ( friendlyEqual( key , a[i].key ) ){
522 var o = { key : key , value : null };
527 Map.prototype.values = function(){
529 for ( var k in this._data ){
530 this._data[k].forEach( function(z){ all.push( z.value ); } );
535 if ( typeof( gc ) == "undefined" ){
541 Math.sigFig = function( x , N ){
545 var p = Math.pow( 10, N - Math.ceil( Math.log( Math.abs(x) ) / Math.log( 10 )) );
546 return Math.round(x*p)/p;