aboutsummaryrefslogtreecommitdiffstats
path: root/infrastructure/framework-src/modules/stringutils.js
blob: 3fe56112e9243922fb65d1398c627d4e00751788 (plain) (blame)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/**
 * Copyright 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @fileOverview A collection of various string utilities.
 */

// TODO: uncomment with import works with *

import("funhtml.{TABLE,TR,TH,TD,OL,LI}");
import("jsutils.{object,eachProperty}");

//import("funhtml.*");

jimport("java.util.Random");
jimport("java.lang.System.currentTimeMillis");


/**
 * Removes leading and trailing whitespace from a string.
 * @param {string} str
 * @return {string} The trimmed string.
 */
function trim(str) {
  return str.replace(/^\s+|\s+$/g, "");
}

//----------------------------------------------------------------
// String prototype enhancements.
// TODO: should we move this to a new library "enhancedstring"?
//----------------------------------------------------------------
startsWith = function(s, prefix) {
  return (s.indexOf(prefix) == 0);
};
endsWith = function(s, suffix) {
  return (s.substr(s.length - suffix.length) == suffix);
};
contains = function(s, x) {
  return (s.indexOf(x) != -1);
};
makeTitle = function(s) {
  if (! s) return;
  return s.split(" ").map(function(x) { 
      return x[0].toUpperCase() + x.substr(1) 
    }).join(" ");
}
repeat = function(s, n) {
  var out = [];
  while (n-- > 0) {
    out.push(s);
  }
  return out.join('');
}

/*
 * Helper function that converts a raw string to an HTML string, with
 * character entities replaced by appropriate HTML codes, and newlines
 * rentered as BRs.
 *
 * <p>A more general version of this function is toHTML(), which can operate
 * on not just strings, but any object.
 *
 * @param {string} str the raw string
 * @return {string} HTML-formatted string
 */
function _stringToHTML(str) {
  return String(net.appjet.oui.Util.stringToHTML(str));
}

// used to convert an object to HTML when the object does not have a
// toHTML method.
//
function _coerceObjectToHTML(obj) {
  var t = TABLE({border: 1, cellpadding: 2, cellspacing: 0});
  eachProperty(obj, function(name, value) {
    t.push(TR(TH(String(name)), TD(String(value))));
  });
  return toHTML(t);
}

// Converts an array to an HTML list by listing its properties and
// recursively converting the values to HTML by calling toHTML() on
// each of them.
function _objectToOL(obj) {
  var l = OL();
  eachProperty(obj, function(name, value) {
      l.push(LI({value: name}, value));
    });
  return l;
}

function _sameProperties(obj1, obj2) {
  if (typeof(obj1) != 'object' || typeof(obj2) != 'object')
    return typeof(obj1) == typeof(obj2);

  var mismatch = 0;
  eachProperty(obj1, function(name) {
    if (! obj2.hasOwnProperty(name)) {
      mismatch++;
    }});
  eachProperty(obj2, function(name) {
    if (! obj1.hasOwnProperty(name)) {
      mismatch++;
    }});
  return mismatch < 2;
}

//
// for pretty-printing arrays.  needs a lot of work.
//
function _arrayToHTML(a) {
  if (a.length === 0) {
    return "";
  }
  if (typeof(a[0]) != 'object') {
    return toHTML(_objectToOL(a));
  } else if (! _sameProperties(a[0], a[1])) {
    return toHTML(_objectToOL(a));
  } else {
    return _likeObjectsToHTML(function (f) {
	a.forEach(function(value, i) {
	    f({index: i}, value, {});
	  });}, null);
  }
}

/** @ignore */

// a foreaching function that takes three arguments: properties to put first,
// properties to put in the middle, and properties to put at the end.
// and a table header (with large colspan)
function _likeObjectsToHTML(forEachFunction, tophead) {
  objs = [];
  prepnames = new StringSet();
  objpnames = new StringSet();
  postpnames = new StringSet();
  rows = [];

  var t = TABLE({border: 1, cellpadding: 2, cellspacing: 0});
  var head = TR();
  if (tophead)
    t.push(tophead);
  t.push(head);

  var butWaitTheresMore = false;
  var howManyMore = 0;

  forEachFunction(function(pre, o, post) {
    if (objs.length >= 10) {
      butWaitTheresMore = true;
      howManyMore++;
      return;
    }
    objs.push({pre: pre, o: o, post: post});
    var tr = TR();
    rows.push(tr);
    t.push(tr);

    eachProperty(pre, function(name) { prepnames.add(name); });
    eachProperty(o, function(name) { objpnames.add(name); });
    eachProperty(post, function(name) { postpnames.add(name); });
  });
  var numpnames = 0;
  var appendTDsForPropName = function (where) {
    return function(name) {
      numpnames++;
      head.push(TH(name));
      for (var j = 0; j < objs.length; ++j) {
	if (! (objs[j][where] === undefined) && ! (objs[j][where][name] === undefined))
	  rows[j].push(TD(String(objs[j][where][name])));
	else
	  rows[j].push(TD());
      }
    };
  };
  prepnames.forEach(appendTDsForPropName("pre"));
  objpnames.forEach(appendTDsForPropName("o"));
  postpnames.forEach(appendTDsForPropName("post"));
  if (butWaitTheresMore) {
    t.push(TR(TD({colspan: numpnames}, "..."+howManyMore+
		 " additional element"+(howManyMore == 1 ? "" : "s")+" omitted...")));
  }
  return toHTML(t);
}

/**
 * Returns a string with any number of variables substituted in, as
 * popularized by C's function of the same name.  Some common substitutions:
 *
 * <ul><li>%d - an integer</li><li>%f - a floating-point number</li><li>%b - a boolean</li>
 * <li>%s - a string</li></ul>
 *
 * <p>Each time one of these "slot" appears in your format string, the next argument is displayed
 * according to the type of slot you specified.
 *
 * <p>AppJet supports <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html">
 * Java's specification of printf</a>, which has a ton of features, including selecting
 * arguments out of order, formatting dates and times, and specifying how many characters
 * wide each slot should be.
 *
 * @example
var x = 5;
response.write(sprintf("an integer: %d", x));
response.write(sprintf("Two strings: [%s] and [%s].", "string one", "string two"));
 *
 * @param {string} formatString
 * @param {*} arg1
 * @param {*} arg2
 * @param {*} arg3 ...
 */
function sprintf(formatString, arg1, arg2, etc) {
  if (typeof(formatString) != 'string') {
    throw new Error('printf takes a string as the first argument.');
  }
  var argList = java.lang.reflect.Array.newInstance(java.lang.Object, arguments.length-1);
  for (var i = 1; i < arguments.length; i++) {
    if (arguments[i] instanceof Date)
      argList[i-1] = arguments[i].getTime();
    else
      argList[i-1] = arguments[i];
  }
  return String(net.appjet.ajstdlib.printf.printf(formatString, argList));
};

/**
 * Replaces keys of data found in string with their corresponding values.
 *
 * <p>(Inspired by http://javascript.crockford.com/remedial.html)
 *
 * @example
var data = {name: "Aaron", age: 25, today: new Date()};
print(supplant(data, """

{name}'s age is {age} years, as of {today}.

"""));

 * @param {object} data dictionary of values
 * @param {string} str
 * @return {string} str with keys of data replaced by their values
 */
function supplant(data, str) {
  var s = str;
  var o = data;
  function rep(a, b) {
    var r = o[b];
    if (typeof(r) != 'undefined') {
      return r;
    } else {
      return a;
    }
  }
  return s.replace(/{([^{}]*)}/g, rep);
};

//----------------------------------------------------------------
// raw printing
//----------------------------------------------------------------
var _raw_prototype;

/**
 * Used for printing un-escaped HTML, such as your own HTML tags.
 *
 * <p>Normally, printing a string will cause it to be translated
 * so that it appears the same on the screen as it did in your code.
 * If you're writing your own HTML, you don't want it to be processed
 * this way. Wrapping a string in html(...) by-passes normal printing behavior,
 * so that print(html(" -- html goes here ---")) will write the HTML
 * directly to the page.
 *
 * <p>If you want to mix your own HTML code with HTML code generated from a
 * tag object, you can get the HTML for the tag by calling its toHTML(...) method.
 *
 * <p>Multiple arguments to html(...) will be concatenated into one string.
 *
 * @example
print(html("""
&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;&lt;p&gt;Here is some text inside a P inside a DIV.&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
"""));
 *
 * @param {string} text the raw text
 * @return {object} an object which, when printed, prints the raw html text
 */
function html(text) {
  if (!_raw_prototype) {
    _raw_prototype = object(Object.prototype);
    _raw_prototype.toString = function() { return this._text; };
    _raw_prototype.toHTML = function() { return this._text; };
  }
  var rawObj = object(_raw_prototype);
  rawObj._text = Array.prototype.map.call(arguments, String).join('');
  return rawObj;
}

/**
 * This function is used by print(...) to convert a string or object
 * into nice-looking printable HTML.  It may be useful in conjunction
 * with html(...) if you wish to work directly with HTML.
 *
 * <p>You can control how toHTML(...) (and therefore print(...)) behave on an object
 * by giving that object a .toHTML() function.
 *
 * @param {*} x any javascript variable
 * @return {string} html-formatted string
 */
function toHTML(x) {
  if (typeof(x) == 'undefined') {
    return 'undefined';
  }
  if (x === null) {
    return 'null';
  }
  if (typeof x == "string" || (x instanceof java.lang.String)) {
    return _stringToHTML(x);
  }
  if (typeof(x.toHTML) == "function") {
    return x.toHTML();
  }
  if (typeof(x) == "xml") {
    return _stringToHTML(x.toSource());
  }
  if (x instanceof Array) {
    return _arrayToHTML(x);
  }
  if (x instanceof Date) {
    var pieces = x.toString().split(" ");
    return pieces.slice(0, 5).join(' ') + ' ' + pieces[6];
  }
  if (typeof(x) == "object") {
    return _coerceObjectToHTML(x);
  }
  // TODO: add more types to auto-printing, such as functions,
  // numbers, what else?
  return _stringToHTML(""+x);
}


/**
 * Generates a random string of specified length using upper-case letters, lower-case letters, and numbers.
 */

var _jrand = new Random(currentTimeMillis());

function randomString(nchars) {
  var result = '';

  // 48-58  or  65-91  or  97-123    (inclusive-exclusive)
  // 0-10   or  0-26   or  0-26
  // 0-62
  
  for (var i = 0; i < nchars; i++) {
    var x = _jrand.nextInt(62);
    var code;
    if (x < 10) { code = x + 48; }
    if (x >= 10 && x < 36) { code = x - 10 + 65/*a*/; }
    if (x >= 36) { code = x - 36 + 97/*A*/; }
    result += String.fromCharCode(code);
  }
  return result;
}

function md5(x) {
  return net.appjet.ajstdlib.md5.md5(x);
}

function randomHash(len) {
  var x = md5(""+_jrand.nextDouble()*1e12+_jrand.nextDouble()*1e12);
  if (len) {
    return String(x).substr(0,len);
  } else {
    return x;
  }
}

function gzip(x) {
  return net.appjet.oui.Util.gzip(x)
}

function isNumeric(x) {
  return !!(/^\d+$/.test(x));
}