summaryrefslogtreecommitdiffstats
path: root/chrome/third_party/jstemplate/util.js
diff options
context:
space:
mode:
authorinitial.commit <initial.commit@0039d316-1c4b-4281-b951-d872f2087c98>2008-07-26 23:55:29 +0000
committerinitial.commit <initial.commit@0039d316-1c4b-4281-b951-d872f2087c98>2008-07-26 23:55:29 +0000
commit09911bf300f1a419907a9412154760efd0b7abc3 (patch)
treef131325fb4e2ad12c6d3504ab75b16dd92facfed /chrome/third_party/jstemplate/util.js
parent586acc5fe142f498261f52c66862fa417c3d52d2 (diff)
downloadchromium_src-09911bf300f1a419907a9412154760efd0b7abc3.zip
chromium_src-09911bf300f1a419907a9412154760efd0b7abc3.tar.gz
chromium_src-09911bf300f1a419907a9412154760efd0b7abc3.tar.bz2
Add chrome to the repository.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@15 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/third_party/jstemplate/util.js')
-rw-r--r--chrome/third_party/jstemplate/util.js137
1 files changed, 137 insertions, 0 deletions
diff --git a/chrome/third_party/jstemplate/util.js b/chrome/third_party/jstemplate/util.js
new file mode 100644
index 0000000..f9ae5e2
--- /dev/null
+++ b/chrome/third_party/jstemplate/util.js
@@ -0,0 +1,137 @@
+// Copyright 2005-2006 Google Inc. All Rights Reserved.
+/**
+ * @fileoverview This file contains javascript utility functions which
+ * do not depend on anything defined elsewhere, so that this file can
+ * be used completely independent of the rest of the maps codebase.
+ *
+ * @author Multiple authors, see g4 blame.
+ */
+
+/**
+ * Returns the value of the length property of the given object. Used
+ * to reduce compiled code size.
+ *
+ * @param {Array | String} a The string or array to interrogate.
+ * @return {Number} The value of the length property.
+ */
+function jsLength(a) {
+ return a.length;
+}
+
+// Wrappers for Math functions
+var min = Math.min;
+var max = Math.max;
+var ceil = Math.ceil;
+var floor = Math.floor;
+var round = Math.round;
+var abs = Math.abs;
+
+/**
+ * Copies all properties from second object to the first. Modifies to.
+ *
+ * @param {Object} to The target object.
+ * @param {Object} from The source object.
+ */
+function copyProperties(to, from) {
+ foreachin(from, function(p) {
+ to[p] = from[p];
+ });
+}
+
+/**
+ * Iterates over the array, calling the given function for each
+ * element.
+ *
+ * @param {Array} array
+ * @param {Function} fn
+ */
+function foreach(array, fn) {
+ var I = jsLength(array);
+ for (var i = 0; i < I; ++i) {
+ fn(array[i], i);
+ }
+}
+
+/**
+ * Safely iterates over all properties of the given object, calling
+ * the given function for each property. If opt_all isn't true, uses
+ * hasOwnProperty() to assure the property is on the object, not on
+ * its prototype.
+ *
+ * @param {Object} object
+ * @param {Function} fn
+ * @param {Boolean} opt_all If true, also iterates over inherited properties.
+ */
+function foreachin(object, fn, opt_all) {
+ for (var i in object) {
+ // NOTE: Safari/1.3 doesn't have hasOwnProperty(). In that
+ // case, we iterate over all properties as a very lame workaround.
+ if (opt_all || !object.hasOwnProperty || object.hasOwnProperty(i)) {
+ fn(i, object[i]);
+ }
+ }
+}
+
+/**
+ * Appends the second array to the first, copying its elements.
+ * Optionally only a slice of the second array is copied.
+ *
+ * @param {Array} a1 Target array (modified).
+ * @param {Array} a2 Source array.
+ * @param {Number} opt_begin Begin of slice of second array (optional).
+ * @param {Number} opt_end End (exclusive) of slice of second array (optional).
+ */
+function arrayAppend(a1, a2, opt_begin, opt_end) {
+ var i0 = opt_begin || 0;
+ var i1 = opt_end || jsLength(a2);
+ for (var i = i0; i < i1; ++i) {
+ a1.push(a2[i]);
+ }
+}
+
+/**
+ * Trim whitespace from begin and end of string.
+ *
+ * @see testStringTrim();
+ *
+ * @param {String} str Input string.
+ * @return {String} Trimmed string.
+ */
+function stringTrim(str) {
+ return stringTrimRight(stringTrimLeft(str));
+}
+
+/**
+ * Trim whitespace from beginning of string.
+ *
+ * @see testStringTrimLeft();
+ *
+ * @param {String} str Input string.
+ * @return {String} Trimmed string.
+ */
+function stringTrimLeft(str) {
+ return str.replace(/^\s+/, "");
+}
+
+/**
+ * Trim whitespace from end of string.
+ *
+ * @see testStringTrimRight();
+ *
+ * @param {String} str Input string.
+ * @return {String} Trimmed string.
+ */
+function stringTrimRight(str) {
+ return str.replace(/\s+$/, "");
+}
+
+/**
+ * Jscompiler wrapper for parseInt() with base 10.
+ *
+ * @param {String} s String repersentation of a number.
+ *
+ * @return {Number} The integer contained in s, converted on base 10.
+ */
+function parseInt10(s) {
+ return parseInt(s, 10);
+}