~davewalker/etherpad/ubuntu-unlimited-max-users-and-revisions

« back to all changes in this revision

Viewing changes to infrastructure/framework-src/modules/jsutils.js

  • Committer: James Page
  • Date: 2011-04-13 08:00:43 UTC
  • Revision ID: james.page@canonical.com-20110413080043-eee2nq7y1v7cv2mp
* Refactoring to use native Ubuntu Java libraries. 
* debian/control:
  - use openjdk instead of sun's java
  - update maintainer
* debian/etherpad.init.orig, debian/etherpad.upstart:
  - move the init script out of the way
  - create a basic upstart script
  - note that the open office document conversion daemon was dropped
    from the upstart configuration; if this behavior is desired, please
    create a separate upstart job for it
* debian/rules:
  - just use basic dh_installinit, as it will pick up the new upstart job
* New release
* Changed maintainer to Packaging
* Fixed installation scripts
* Initial Release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2009 Google Inc.
 
3
 * 
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 * 
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 * 
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS-IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
/**
 
18
 * @fileOverview A collection of core JavaScript utilities.
 
19
 */
 
20
 
 
21
/**
 
22
 * Iterator convenience for JavaScript Objects.
 
23
 *
 
24
 * Note that if func returns false, the iteration will be immediately terminated.
 
25
 * (Returning undefined, or not specifying a return type, does not terminate the iteration).
 
26
 *
 
27
 * @example
 
28
var pastels = {
 
29
  red: "#fcc",
 
30
  green: "#cfc",
 
31
  blue: "#ccf"
 
32
};
 
33
eachProperty(pastels, function(key, value) {
 
34
  print(DIV({style: 'background: '+value+';'}, key));
 
35
});
 
36
 *
 
37
 * @param {object} obj The object over which to iterate.
 
38
 * @param {function} func The function to run on each [key,value] pair.
 
39
 */
 
40
function eachProperty(obj, func) {
 
41
  var r;
 
42
  for (k in obj) {
 
43
    if (!obj.hasOwnProperty || obj.hasOwnProperty(k)) {
 
44
      r = func(k,obj[k]);
 
45
      if (r === false) {
 
46
        break;
 
47
      }
 
48
    }
 
49
  }
 
50
}
 
51
 
 
52
/**
 
53
 * Douglas Crockford's "object" function for prototypal inheritance, taken from
 
54
 * http://javascript.crockford.com/prototypal.html
 
55
 *
 
56
 * @param {object} parent The parent object.
 
57
 * @return {object} A new object whose prototype is parent.
 
58
 */
 
59
function object(parent) {
 
60
  function f() {};
 
61
  f.prototype = parent;
 
62
  return new f();
 
63
}
 
64
 
 
65
/**
 
66
 * Creates an array of the properties of <code>obj</code>,
 
67
 * <em>not</em> including built-in or inherited properties.  If no
 
68
 * argument is given, applies to the global object.
 
69
 *
 
70
 * @example
 
71
// Prints "abc"
 
72
keys({a: 1, b: 2, c: 3}).forEach(function(k) {
 
73
  print(k);
 
74
}
 
75
 *
 
76
 * @example
 
77
// Prints all the functions and object members of the global "appjet" object,
 
78
// one per line.
 
79
print(keys(appjet).join('\n'));
 
80
 *
 
81
 * @param {object} obj
 
82
 */
 
83
function keys(obj) {
 
84
  var array = [];
 
85
  var o = obj;
 
86
  if (o == undefined) {
 
87
    o = this;
 
88
  }
 
89
  for(var k in o) {
 
90
    if (!obj.hasOwnProperty || o.hasOwnProperty(k)) {
 
91
      array.push(k);
 
92
    }
 
93
  }
 
94
  return array;
 
95
}
 
96
 
 
97
/**
 
98
 * Comparator that returns -1, +1, or 0 depending on whether a &lt; b, or a &gt; b, or
 
99
 * neither, respectively.
 
100
 * @param {object} a
 
101
 * @param {object} b
 
102
 * @return {number} -1, 0, or +1
 
103
 */
 
104
function cmp(a,b) {
 
105
  if (a < b) {
 
106
    return -1;
 
107
  }
 
108
  if (a > b) {
 
109
    return 1;
 
110
  }
 
111
  return 0;
 
112
}
 
113
 
 
114
function arrayToSet(arr) {
 
115
  var set = {};
 
116
  arr.forEach(function(x) {
 
117
    set[x] = true;
 
118
  });
 
119
  return set;
 
120
}
 
121
 
 
122
function mergeArrays(mergeFunction, a1, a2, etc) {
 
123
  var len = a1.length;
 
124
  var arrays = Array.prototype.slice.call(arguments, 1);
 
125
  for (var i = 0; i < arrays.length; ++i) {
 
126
    if (arrays[i].length != len) {
 
127
      return;
 
128
    }
 
129
  }
 
130
  out = [];
 
131
  for (var i = 0; i < a1.length; ++i) {
 
132
    out.push(mergeFunction.apply(this, arrays.map(function(array) { return array[i]; })));
 
133
  }
 
134
  return out;
 
135
}
 
136
 
 
137
function debug(obj) {
 
138
  if (typeof(obj) == 'object') {
 
139
    var ret = [];
 
140
    if (obj) {
 
141
      eachProperty(obj, function(k, v) {
 
142
        ret.push(k+" -> "+debug(v));
 
143
      });
 
144
      return '['+ret.join(", ")+']';
 
145
    } else {
 
146
      return String(obj);
 
147
    }
 
148
  } else {
 
149
    return String(obj);
 
150
  }
 
151
}
 
152
 
 
153
/**
 
154
 * Create a scala function out of the given JS function.
 
155
 */
 
156
function scalaFn(nargs, f) {
 
157
  if (typeof(f) == 'function') {
 
158
    return new Packages.scala['Function'+nargs]({
 
159
      apply: f
 
160
    });
 
161
  } else {
 
162
    return new Packages.scala['Function'+nargs]({
 
163
      apply: function() { return f; }
 
164
    })
 
165
  }
 
166
}
 
167
 
 
168
function scalaF0(f) {
 
169
  return scalaFn(0, f);
 
170
}
 
171
 
 
172
function scalaF1(f) {
 
173
  return scalaFn(1, f);
 
174
}
 
175
 
 
176
/** 
 
177
 * Some bonus functions for functional programming.
 
178
 */
 
179
function f_curry(thisPtr, f, arg1, arg2, etc) {
 
180
  var curriedArgs = Array.prototype.slice.call(arguments, 2);
 
181
  return function() {
 
182
    var args = Array.prototype.slice.call(arguments, 0);
 
183
    return f.apply(thisPtr, curriedArgs.concat(args));
 
184
  }
 
185
}
 
186
 
 
187
function f_limitArgs(thisPtr, f, n) {
 
188
  return function() {
 
189
    var args = Array.prototype.slice.call(arguments, 0, n);
 
190
    return f.apply(thisPtr, args);
 
191
  }
 
192
}
 
193
 
 
194
 
 
195