xoslib wip
[plstackapi.git] / planetstack / core / xoslib / static / xsh / utils.js
1 // Try Mongo
2 //
3 // Copyright (c) 2009 Kyle Banker
4 // Licensed under the MIT licence.
5 // http://www.opensource.org/licenses/mit-license.php
6
7 // extending array like this is breaking datatables
8
9 /*Array.prototype.include = function(value) {
10   for(var i=0; i < this.length; i++) {
11     if(this[i] == value) {
12       return this[i];
13     }
14   }
15   return false;
16 };
17
18 Array.prototype.empty = function() {
19   return (this.length == 0);
20 };*/
21
22 function ArrayInclude(arr,value) {
23   for(var i=0; i < arr.length; i++) {
24     if(arr[i] == value) {
25       return arr[i];
26     }
27   }
28   return false;
29 };
30
31 Function.prototype.bind = function() {
32   var __method = this, object = arguments[0], args = [];
33
34   for(i = 1; i < arguments.length; i++) {
35    args.push(arguments[i]);
36   }
37
38  return function() {
39  return __method.apply(object, args);
40  };
41 }; 
42
43 String.prototype.trim = function() {
44   return this.replace(/^\s+|\s+$/g,"");
45 };
46
47 // Prints javascript types as readable strings.
48 Inspect = function(obj) {
49   if(typeof(obj) != 'object') {
50     return obj;
51   }
52
53   else if (obj instanceof Array) {
54     var objRep = [];
55     for(var prop in obj) { 
56       if(obj.hasOwnProperty(prop)) {
57         objRep.push(obj[prop]); 
58       }
59     }
60     return '[' + objRep.join(', ') + ']';
61   }
62
63   else {
64     var objRep = [];
65     for(var prop in obj) {
66       if(obj.hasOwnProperty(prop)) {
67         objRep.push(prop + ': ' + ((typeof(obj[prop]) == 'object') ? Inspect(obj[prop]) : obj[prop]));
68       }
69     }
70     return '{' + objRep.join(', ') + '}';
71   }
72 };
73
74 // Prints an array of javascript objects.
75 CollectionInspect = function(coll) {
76   var str = '';
77   for(var i=0; i<coll.length; i++) {
78     str += Inspect(coll[i]) + '<br />'; 
79   }
80   return str;
81 };