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 object = eval('[' + expr + '][0]'); |
59 if (typeof object != 'object') { | |
60 throw new Error('expression of type Object expected, ' + | |
61 typeof object + ' found'); | |
62 } | |
63 return /** @type {Object} */(object); | |
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) {} | 74 function assert(obj) {} |
70 | 75 |
71 /** | 76 /** |
72 * Copies all properties from second object to the first. Modifies to. | 77 * Copies all properties from second object to the first. Modifies to. |
73 * | 78 * |
74 * @param {Object} to The target object. | 79 * @param {Object} to The target object. |
75 * @param {Object} from The source object. | 80 * @param {Object} from The source object. |
76 */ | 81 */ |
77 function copyProperties(to, from) { | 82 function copyProperties(to, from) { |
78 for (var p in from) { | 83 for (var p in from) { |
79 to[p] = from[p]; | 84 to[p] = from[p]; |
80 } | 85 } |
81 } | 86 } |
82 | 87 |
83 | 88 |
84 /** | 89 /** |
85 * @param {Object|null|undefined} value The possible value to use. | 90 * @param {Object|string|null|undefined} value The possible value to use. |
arv (Not doing code reviews)
2014/09/25 18:46:53
any?
Vitaly Pavlenko
2014/09/25 20:24:42
Done.
| |
86 * @param {Object} defaultValue The default if the value is not set. | 91 * @param {Object|string} defaultValue The default if the value is not set. |
87 * @return {Object} The value, if it is | 92 * @return {Object|string} The value, if it is |
88 * defined and not null; otherwise the default | 93 * defined and not null; otherwise the default |
89 */ | 94 */ |
90 function getDefaultObject(value, defaultValue) { | 95 function getDefaultObject(value, defaultValue) { |
91 if (typeof value != TYPE_undefined && value != null) { | 96 if (typeof value != TYPE_undefined && value != null) { |
92 return /** @type Object */(value); | 97 return /** @type Object */(value); |
93 } else { | 98 } else { |
94 return defaultValue; | 99 return defaultValue; |
95 } | 100 } |
96 } | 101 } |
97 | 102 |
98 /** | 103 /** |
99 * Detect if an object looks like an Array. | 104 * Detect if an object looks like an Array. |
100 * Note that instanceof Array is not robust; for example an Array | 105 * Note that instanceof Array is not robust; for example an Array |
101 * created in another iframe fails instanceof Array. | 106 * created in another iframe fails instanceof Array. |
102 * @param {Object|null} value Object to interrogate | 107 * @param {Object|null} value Object to interrogate |
103 * @return {boolean} Is the object an array? | 108 * @return {boolean} Is the object an array? |
104 */ | 109 */ |
105 function isArray(value) { | 110 function isArray(value) { |
106 return value != null && | 111 return value != null && |
107 typeof value == TYPE_object && | 112 typeof value == TYPE_object && |
108 typeof value.length == TYPE_number; | 113 typeof value.length == TYPE_number; |
109 } | 114 } |
110 | 115 |
111 | 116 |
112 /** | 117 /** |
113 * Finds a slice of an array. | 118 * Finds a slice of an array. |
114 * | 119 * |
115 * @param {Array} array Array to be sliced. | 120 * @param {Array|Arguments} array Array to be sliced. |
116 * @param {number} start The start of the slice. | 121 * @param {number} start The start of the slice. |
117 * @param {number} opt_end The end of the slice (optional). | 122 * @param {number=} opt_end The end of the slice (optional). |
118 * @return {Array} array The slice of the array from start to end. | 123 * @return {Array} array The slice of the array from start to end. |
119 */ | 124 */ |
120 function arraySlice(array, start, opt_end) { | 125 function arraySlice(array, start, opt_end) { |
121 // Use | 126 // Use |
122 // return Function.prototype.call.apply(Array.prototype.slice, arguments); | 127 // return Function.prototype.call.apply(Array.prototype.slice, arguments); |
123 // instead of the simpler | 128 // instead of the simpler |
124 // return Array.prototype.slice.call(array, start, opt_end); | 129 // return Array.prototype.slice.call(array, start, opt_end); |
125 // here because of a bug in the FF and IE implementations of | 130 // 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 | 131 // Array.prototype.slice which causes this function to return an empty list |
127 // if opt_end is not provided. | 132 // if opt_end is not provided. |
128 return Function.prototype.call.apply(Array.prototype.slice, arguments); | 133 return /** @type {Array} */( |
134 Function.prototype.call.apply(Array.prototype.slice, arguments)); | |
129 } | 135 } |
130 | 136 |
131 | 137 |
132 /** | 138 /** |
133 * Jscompiler wrapper for parseInt() with base 10. | 139 * Jscompiler wrapper for parseInt() with base 10. |
134 * | 140 * |
135 * @param {string} s string repersentation of a number. | 141 * @param {string} s string repersentation of a number. |
136 * | 142 * |
137 * @return {number} The integer contained in s, converted on base 10. | 143 * @return {number} The integer contained in s, converted on base 10. |
138 */ | 144 */ |
(...skipping 15 matching lines...) Expand all Loading... | |
154 | 160 |
155 | 161 |
156 /** | 162 /** |
157 * Prebinds "this" within the given method to an object, but ignores all | 163 * Prebinds "this" within the given method to an object, but ignores all |
158 * arguments passed to the resulting function. | 164 * arguments passed to the resulting function. |
159 * I.e. var_args are all the arguments that method is invoked with when | 165 * I.e. var_args are all the arguments that method is invoked with when |
160 * invoking the bound function. | 166 * invoking the bound function. |
161 * | 167 * |
162 * @param {Object|null} object The object that the method call targets. | 168 * @param {Object|null} object The object that the method call targets. |
163 * @param {Function} method The target method. | 169 * @param {Function} method The target method. |
170 * @param {...*} var_args | |
164 * @return {Function} Method with the target object bound to it and curried by | 171 * @return {Function} Method with the target object bound to it and curried by |
165 * the provided arguments. | 172 * the provided arguments. |
166 */ | 173 */ |
167 function bindFully(object, method, var_args) { | 174 function bindFully(object, method, var_args) { |
168 var args = arraySlice(arguments, 2); | 175 var args = arraySlice(arguments, 2); |
169 return function() { | 176 return function() { |
170 return method.apply(object, args); | 177 return method.apply(object, args); |
171 } | 178 } |
172 } | 179 } |
173 | 180 |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
413 * @param {Node} node The node to remove. | 420 * @param {Node} node The node to remove. |
414 * @return {Node} The removed node. | 421 * @return {Node} The removed node. |
415 */ | 422 */ |
416 function domRemoveNode(node) { | 423 function domRemoveNode(node) { |
417 return domRemoveChild(node.parentNode, node); | 424 return domRemoveChild(node.parentNode, node); |
418 } | 425 } |
419 | 426 |
420 /** | 427 /** |
421 * Remove a child from the specified (parent) node. | 428 * Remove a child from the specified (parent) node. |
422 * | 429 * |
423 * @param {Element} node Parent element. | 430 * @param {Node} node Parent element. |
424 * @param {Node} child Child node to remove. | 431 * @param {Node} child Child node to remove. |
425 * @return {Node} Removed node. | 432 * @return {Node} Removed node. |
426 */ | 433 */ |
427 function domRemoveChild(node, child) { | 434 function domRemoveChild(node, child) { |
428 return node.removeChild(child); | 435 return node.removeChild(child); |
429 } | 436 } |
430 | 437 |
431 | 438 |
432 /** | 439 /** |
433 * Trim whitespace from begin and end of string. | 440 * Trim whitespace from begin and end of string. |
(...skipping 23 matching lines...) Expand all Loading... | |
457 * Trim whitespace from end of string. | 464 * Trim whitespace from end of string. |
458 * | 465 * |
459 * @see testStringTrimRight(); | 466 * @see testStringTrimRight(); |
460 * | 467 * |
461 * @param {string} str Input string. | 468 * @param {string} str Input string. |
462 * @return {string} Trimmed string. | 469 * @return {string} Trimmed string. |
463 */ | 470 */ |
464 function stringTrimRight(str) { | 471 function stringTrimRight(str) { |
465 return str.replace(/\s+$/, ""); | 472 return str.replace(/\s+$/, ""); |
466 } | 473 } |
OLD | NEW |