| OLD | NEW |
| 1 // Copyright 2006 Google Inc. | 1 // Copyright 2006 Google Inc. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 try { | 48 try { |
| 49 // NOTE(mesch): An alternative idiom would be: | 49 // NOTE(mesch): An alternative idiom would be: |
| 50 // | 50 // |
| 51 // eval('(' + expr + ')'); | 51 // eval('(' + expr + ')'); |
| 52 // | 52 // |
| 53 // Note that using the square brackets as below, "" evals to undefined. | 53 // Note that using the square brackets as below, "" evals to undefined. |
| 54 // The alternative of using parentheses does not work when evaluating | 54 // The alternative of using parentheses does not work when evaluating |
| 55 // function literals in IE. | 55 // function literals in IE. |
| 56 // e.g. eval("(function() {})") returns undefined, and not a function | 56 // e.g. eval("(function() {})") returns undefined, and not a function |
| 57 // object, in IE. | 57 // object, in IE. |
| 58 return eval('[' + expr + '][0]'); | 58 var result = eval('[' + expr + '][0]'); |
| 59 if (typeof result != 'object') { |
| 60 throw new Error('expression of type Object expected, ' + |
| 61 typeof result + ' found'); |
| 62 } |
| 63 return /** @type {Object} */(result); |
| 59 } catch (e) { | 64 } catch (e) { |
| 60 log('EVAL FAILED ' + expr + ': ' + e); | 65 log('EVAL FAILED ' + expr + ': ' + e); |
| 61 return null; | 66 return null; |
| 62 } | 67 } |
| 63 } | 68 } |
| 64 | 69 |
| 65 function jsLength(obj) { | 70 function jsLength(obj) { |
| 66 return obj.length; | 71 return obj.length; |
| 67 } | 72 } |
| 68 | 73 |
| 69 function assert(obj) {} | |
| 70 | |
| 71 /** | 74 /** |
| 72 * Copies all properties from second object to the first. Modifies to. | 75 * Copies all properties from second object to the first. Modifies to. |
| 73 * | 76 * |
| 74 * @param {Object} to The target object. | 77 * @param {Object} to The target object. |
| 75 * @param {Object} from The source object. | 78 * @param {Object} from The source object. |
| 76 */ | 79 */ |
| 77 function copyProperties(to, from) { | 80 function copyProperties(to, from) { |
| 78 for (var p in from) { | 81 for (var p in from) { |
| 79 to[p] = from[p]; | 82 to[p] = from[p]; |
| 80 } | 83 } |
| 81 } | 84 } |
| 82 | 85 |
| 83 | 86 |
| 84 /** | 87 /** |
| 85 * @param {Object|null|undefined} value The possible value to use. | 88 * @param {*} value The possible value to use. |
| 86 * @param {Object} defaultValue The default if the value is not set. | 89 * @param {*} defaultValue The default if the value is not set. |
| 87 * @return {Object} The value, if it is | 90 * @return {*} The value, if it is defined and not null; otherwise the default. |
| 88 * defined and not null; otherwise the default | |
| 89 */ | 91 */ |
| 90 function getDefaultObject(value, defaultValue) { | 92 function getDefaultObject(value, defaultValue) { |
| 91 if (typeof value != TYPE_undefined && value != null) { | 93 if (typeof value != TYPE_undefined && value != null) { |
| 92 return /** @type Object */(value); | 94 return value; |
| 93 } else { | 95 } else { |
| 94 return defaultValue; | 96 return defaultValue; |
| 95 } | 97 } |
| 96 } | 98 } |
| 97 | 99 |
| 98 /** | 100 /** |
| 99 * Detect if an object looks like an Array. | 101 * Detect if an object looks like an Array. |
| 100 * Note that instanceof Array is not robust; for example an Array | 102 * Note that instanceof Array is not robust; for example an Array |
| 101 * created in another iframe fails instanceof Array. | 103 * created in another iframe fails instanceof Array. |
| 102 * @param {Object|null} value Object to interrogate | 104 * @param {Object|null} value Object to interrogate |
| 103 * @return {boolean} Is the object an array? | 105 * @return {boolean} Is the object an array? |
| 104 */ | 106 */ |
| 105 function isArray(value) { | 107 function isArray(value) { |
| 106 return value != null && | 108 return value != null && |
| 107 typeof value == TYPE_object && | 109 typeof value == TYPE_object && |
| 108 typeof value.length == TYPE_number; | 110 typeof value.length == TYPE_number; |
| 109 } | 111 } |
| 110 | 112 |
| 111 | 113 |
| 112 /** | 114 /** |
| 113 * Finds a slice of an array. | 115 * Finds a slice of an array. |
| 114 * | 116 * |
| 115 * @param {Array} array Array to be sliced. | 117 * @param {Array|Arguments} array Array to be sliced. |
| 116 * @param {number} start The start of the slice. | 118 * @param {number} start The start of the slice. |
| 117 * @param {number} opt_end The end of the slice (optional). | 119 * @param {number=} opt_end The end of the slice (optional). |
| 118 * @return {Array} array The slice of the array from start to end. | 120 * @return {Array} array The slice of the array from start to end. |
| 119 */ | 121 */ |
| 120 function arraySlice(array, start, opt_end) { | 122 function arraySlice(array, start, opt_end) { |
| 121 // Use | 123 // Use |
| 122 // return Function.prototype.call.apply(Array.prototype.slice, arguments); | 124 // return Function.prototype.call.apply(Array.prototype.slice, arguments); |
| 123 // instead of the simpler | 125 // instead of the simpler |
| 124 // return Array.prototype.slice.call(array, start, opt_end); | 126 // return Array.prototype.slice.call(array, start, opt_end); |
| 125 // here because of a bug in the FF and IE implementations of | 127 // here because of a bug in the FF and IE implementations of |
| 126 // Array.prototype.slice which causes this function to return an empty list | 128 // Array.prototype.slice which causes this function to return an empty list |
| 127 // if opt_end is not provided. | 129 // if opt_end is not provided. |
| 128 return Function.prototype.call.apply(Array.prototype.slice, arguments); | 130 return /** @type {Array} */( |
| 131 Function.prototype.call.apply(Array.prototype.slice, arguments)); |
| 129 } | 132 } |
| 130 | 133 |
| 131 | 134 |
| 132 /** | 135 /** |
| 133 * Jscompiler wrapper for parseInt() with base 10. | 136 * Jscompiler wrapper for parseInt() with base 10. |
| 134 * | 137 * |
| 135 * @param {string} s string repersentation of a number. | 138 * @param {string} s string repersentation of a number. |
| 136 * | 139 * |
| 137 * @return {number} The integer contained in s, converted on base 10. | 140 * @return {number} The integer contained in s, converted on base 10. |
| 138 */ | 141 */ |
| (...skipping 15 matching lines...) Expand all Loading... |
| 154 | 157 |
| 155 | 158 |
| 156 /** | 159 /** |
| 157 * Prebinds "this" within the given method to an object, but ignores all | 160 * Prebinds "this" within the given method to an object, but ignores all |
| 158 * arguments passed to the resulting function. | 161 * arguments passed to the resulting function. |
| 159 * I.e. var_args are all the arguments that method is invoked with when | 162 * I.e. var_args are all the arguments that method is invoked with when |
| 160 * invoking the bound function. | 163 * invoking the bound function. |
| 161 * | 164 * |
| 162 * @param {Object|null} object The object that the method call targets. | 165 * @param {Object|null} object The object that the method call targets. |
| 163 * @param {Function} method The target method. | 166 * @param {Function} method The target method. |
| 167 * @param {...*} var_args |
| 164 * @return {Function} Method with the target object bound to it and curried by | 168 * @return {Function} Method with the target object bound to it and curried by |
| 165 * the provided arguments. | 169 * the provided arguments. |
| 166 */ | 170 */ |
| 167 function bindFully(object, method, var_args) { | 171 function bindFully(object, method, var_args) { |
| 168 var args = arraySlice(arguments, 2); | 172 var args = arraySlice(arguments, 2); |
| 169 return function() { | 173 return function() { |
| 170 return method.apply(object, args); | 174 return method.apply(object, args); |
| 171 } | 175 } |
| 172 } | 176 } |
| 173 | 177 |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 * @param {Node} node The node to remove. | 417 * @param {Node} node The node to remove. |
| 414 * @return {Node} The removed node. | 418 * @return {Node} The removed node. |
| 415 */ | 419 */ |
| 416 function domRemoveNode(node) { | 420 function domRemoveNode(node) { |
| 417 return domRemoveChild(node.parentNode, node); | 421 return domRemoveChild(node.parentNode, node); |
| 418 } | 422 } |
| 419 | 423 |
| 420 /** | 424 /** |
| 421 * Remove a child from the specified (parent) node. | 425 * Remove a child from the specified (parent) node. |
| 422 * | 426 * |
| 423 * @param {Element} node Parent element. | 427 * @param {Node} node Parent element. |
| 424 * @param {Node} child Child node to remove. | 428 * @param {Node} child Child node to remove. |
| 425 * @return {Node} Removed node. | 429 * @return {Node} Removed node. |
| 426 */ | 430 */ |
| 427 function domRemoveChild(node, child) { | 431 function domRemoveChild(node, child) { |
| 428 return node.removeChild(child); | 432 return node.removeChild(child); |
| 429 } | 433 } |
| 430 | 434 |
| 431 | 435 |
| 432 /** | 436 /** |
| 433 * Trim whitespace from begin and end of string. | 437 * Trim whitespace from begin and end of string. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 457 * Trim whitespace from end of string. | 461 * Trim whitespace from end of string. |
| 458 * | 462 * |
| 459 * @see testStringTrimRight(); | 463 * @see testStringTrimRight(); |
| 460 * | 464 * |
| 461 * @param {string} str Input string. | 465 * @param {string} str Input string. |
| 462 * @return {string} Trimmed string. | 466 * @return {string} Trimmed string. |
| 463 */ | 467 */ |
| 464 function stringTrimRight(str) { | 468 function stringTrimRight(str) { |
| 465 return str.replace(/\s+$/, ""); | 469 return str.replace(/\s+$/, ""); |
| 466 } | 470 } |
| OLD | NEW |