blob: 31b2876af39c7b4d62f24a7a99755fb8dd7685cc [file] [log] [blame]
<!DOCTYPE html>
<!--
Copyright (c) 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<link rel="import" href="/base/base.html">
<script>
'use strict';
tr.exportTo('tr.b', function() {
function asArray(arrayish) {
var values = [];
for (var i = 0; i < arrayish.length; i++)
values.push(arrayish[i]);
return values;
}
function compareArrays(x, y, elementCmp) {
var minLength = Math.min(x.length, y.length);
for (var i = 0; i < minLength; i++) {
var tmp = elementCmp(x[i], y[i]);
if (tmp)
return tmp;
}
if (x.length == y.length)
return 0;
if (x[i] === undefined)
return -1;
return 1;
}
/**
* Compares two values when one or both might be undefined. Undefined
* values are sorted after defined.
*/
function comparePossiblyUndefinedValues(x, y, cmp, opt_this) {
if (x !== undefined && y !== undefined)
return cmp.call(opt_this, x, y);
if (x !== undefined)
return -1;
if (y !== undefined)
return 1;
return 0;
}
function concatenateArrays(/*arguments*/) {
var values = [];
for (var i = 0; i < arguments.length; i++) {
if (!(arguments[i] instanceof Array))
throw new Error('Arguments ' + i + 'is not an array');
values.push.apply(values, arguments[i]);
}
return values;
}
function concatenateObjects(/*arguments*/) {
var result = {};
for (var i = 0; i < arguments.length; i++) {
var object = arguments[i];
for (var j in object) {
result[j] = object[j];
}
}
return result;
}
function dictionaryKeys(dict) {
var keys = [];
for (var key in dict)
keys.push(key);
return keys;
}
function dictionaryValues(dict) {
var values = [];
for (var key in dict)
values.push(dict[key]);
return values;
}
function dictionaryLength(dict) {
var n = 0;
for (var key in dict)
n++;
return n;
}
/**
* Returns a new dictionary with items grouped by the return value of the
* specified function being called on each item.
* @param {!Array.<Object>} ary The array being iterated through
* @param {!Function} fn The mapping function between the array value and the
* map key.
*/
function group(ary, fn) {
return ary.reduce(function(accumulator, curr) {
var key = fn(curr);
if (key in accumulator)
accumulator[key].push(curr);
else
accumulator[key] = [curr];
return accumulator;
}, {});
}
function iterItems(dict, fn, opt_this) {
opt_this = opt_this || this;
var keys = Object.keys(dict);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
fn.call(opt_this, key, dict[key]);
}
}
/**
* Create a new dictionary with the same keys as the original dictionary
* mapped to the results of the provided function called on the corresponding
* entries in the original dictionary, i.e. result[key] = fn(key, dict[key])
* for all keys in dict (own enumerable properties only).
*
* Example:
* var srcDict = {a: 10, b: 15};
* var dstDict = mapItems(srcDict, function(k, v) { return 2 * v; });
* // srcDict is unmodified and dstDict is now equal to {a: 20, b: 30}.
*/
function mapItems(dict, fn, opt_this) {
opt_this = opt_this || this;
var result = {};
var keys = Object.keys(dict);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
result[key] = fn.call(opt_this, key, dict[key]);
}
return result;
}
function iterObjectFieldsRecursively(object, func) {
if (!(object instanceof Object))
return;
if (object instanceof Array) {
for (var i = 0; i < object.length; i++) {
func(object, i, object[i]);
iterObjectFieldsRecursively(object[i], func);
}
return;
}
for (var key in object) {
var value = object[key];
func(object, key, value);
iterObjectFieldsRecursively(value, func);
}
}
function identity(d) {
return d;
}
function findFirstIndexInArray(ary, opt_func, opt_this) {
var func = opt_func || identity;
for (var i = 0; i < ary.length; i++) {
if (func.call(opt_this, ary[i], i))
return i;
}
return -1;
}
function findFirstInArray(ary, opt_func, opt_this) {
var i = findFirstIndexInArray(ary, opt_func, opt_func);
if (i === -1)
return undefined;
return ary[i];
}
return {
asArray: asArray,
concatenateArrays: concatenateArrays,
concatenateObjects: concatenateObjects,
compareArrays: compareArrays,
comparePossiblyUndefinedValues: comparePossiblyUndefinedValues,
dictionaryLength: dictionaryLength,
dictionaryKeys: dictionaryKeys,
dictionaryValues: dictionaryValues,
group: group,
iterItems: iterItems,
mapItems: mapItems,
iterObjectFieldsRecursively: iterObjectFieldsRecursively,
identity: identity,
findFirstIndexInArray: findFirstIndexInArray,
findFirstInArray: findFirstInArray
};
});
</script>