1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 tvcm.exportTo('tvcm', function() {
8 function asArray(arrayish) {
10 for (var i = 0; i < arrayish.length; i++)
11 values.push(arrayish[i]);
15 function compareArrays(x, y, elementCmp) {
16 var minLength = Math.min(x.length, y.length);
17 for (var i = 0; i < minLength; i++) {
18 var tmp = elementCmp(x[i], y[i]);
22 if (x.length == y.length)
25 if (x[i] === undefined)
32 * Compares two values when one or both might be undefined. Undefined
33 * values are sorted after defined.
35 function comparePossiblyUndefinedValues(x, y, cmp) {
36 if (x !== undefined && y !== undefined)
45 function concatenateArrays(/*arguments*/) {
47 for (var i = 0; i < arguments.length; i++) {
48 if (!(arguments[i] instanceof Array))
49 throw new Error('Arguments ' + i + 'is not an array');
50 values.push.apply(values, arguments[i]);
55 function concatenateObjects(/*arguments*/) {
57 for (var i = 0; i < arguments.length; i++) {
58 var object = arguments[i];
59 for (var j in object) {
60 result[j] = object[j];
66 function dictionaryKeys(dict) {
73 function dictionaryValues(dict) {
76 values.push(dict[key]);
80 function dictionaryLength(dict) {
87 function iterItems(dict, fn, opt_this) {
88 opt_this = opt_this || this;
90 fn.call(opt_this, key, dict[key]);
93 function iterObjectFieldsRecursively(object, func) {
94 if (!(object instanceof Object))
97 if (object instanceof Array) {
98 for (var i = 0; i < object.length; i++) {
99 func(object, i, object[i]);
100 iterObjectFieldsRecursively(object[i], func);
105 for (var key in object) {
106 var value = object[key];
107 func(object, key, value);
108 iterObjectFieldsRecursively(value, func);
112 function identity(d) {
116 function findFirstIndexInArray(ary, opt_func, opt_this) {
117 var func = opt_func || identity;
118 for (var i = 0; i < ary.length; i++) {
119 if (func.call(opt_this, ary[i], i))
125 function findFirstInArray(ary, opt_func, opt_this) {
126 var i = findFirstIndexInArray(ary, opt_func, opt_func);
134 concatenateArrays: concatenateArrays,
135 concatenateObjects: concatenateObjects,
136 compareArrays: compareArrays,
137 comparePossiblyUndefinedValues: comparePossiblyUndefinedValues,
138 dictionaryLength: dictionaryLength,
139 dictionaryKeys: dictionaryKeys,
140 dictionaryValues: dictionaryValues,
141 iterItems: iterItems,
142 iterObjectFieldsRecursively: iterObjectFieldsRecursively,
143 findFirstIndexInArray: findFirstIndexInArray,
144 findFirstInArray: findFirstInArray