Home | History | Annotate | Download | only in base
      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.
      4 
      5 'use strict';
      6 
      7 base.exportTo('base', function() {
      8   function asArray(arrayish) {
      9     var values = [];
     10     for (var i = 0; i < arrayish.length; i++)
     11       values.push(arrayish[i]);
     12     return values;
     13   }
     14 
     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]);
     19       if (tmp)
     20         return tmp;
     21     }
     22     if (x.length == y.length)
     23       return 0;
     24 
     25     if (x[i] === undefined)
     26       return -1;
     27 
     28     return 1;
     29   }
     30 
     31   /**
     32    * Compares two values when one or both might be undefined. Undefined
     33    * values are sorted after defined.
     34    */
     35   function comparePossiblyUndefinedValues(x, y, cmp) {
     36     if (x !== undefined && y !== undefined)
     37       return cmp(x, y);
     38     if (x !== undefined)
     39       return -1;
     40     if (y !== undefined)
     41       return 1;
     42     return 0;
     43   }
     44 
     45   function concatenateArrays(/*arguments*/) {
     46     var values = [];
     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]);
     51     }
     52     return values;
     53   }
     54 
     55   function dictionaryKeys(dict) {
     56     var keys = [];
     57     for (var key in dict)
     58       keys.push(key);
     59     return keys;
     60   }
     61 
     62   function dictionaryValues(dict) {
     63     var values = [];
     64     for (var key in dict)
     65       values.push(dict[key]);
     66     return values;
     67   }
     68 
     69   function iterItems(dict, fn, opt_this) {
     70     opt_this = opt_this || this;
     71     for (var key in dict)
     72       fn.call(opt_this, key, dict[key]);
     73   }
     74 
     75   function iterObjectFieldsRecursively(object, func) {
     76     if (!(object instanceof Object))
     77       return;
     78 
     79     if (object instanceof Array) {
     80       for (var i = 0; i < object.length; i++) {
     81         func(object, i, object[i]);
     82         iterObjectFieldsRecursively(object[i], func);
     83       }
     84       return;
     85     }
     86 
     87     for (var key in object) {
     88       var value = object[key];
     89       func(object, key, value);
     90       iterObjectFieldsRecursively(value, func);
     91     }
     92   }
     93 
     94   return {
     95     asArray: asArray,
     96     concatenateArrays: concatenateArrays,
     97     compareArrays: compareArrays,
     98     comparePossiblyUndefinedValues: comparePossiblyUndefinedValues,
     99     dictionaryKeys: dictionaryKeys,
    100     dictionaryValues: dictionaryValues,
    101     iterItems: iterItems,
    102     iterObjectFieldsRecursively: iterObjectFieldsRecursively
    103   };
    104 });
    105