- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / resources / media / util.js
1 // Copyright 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 /**
6  * @fileoverview Some utility functions that don't belong anywhere else in the
7  * code.
8  */
9
10 var util = (function() {
11   var util = {};
12   util.object = {};
13   /**
14    * Calls a function for each element in an object/map/hash.
15    *
16    * @param obj The object to iterate over.
17    * @param f The function to call on every value in the object.  F should have
18    * the following arguments: f(value, key, object) where value is the value
19    * of the property, key is the corresponding key, and obj is the object that
20    * was passed in originally.
21    * @param optObj The object use as 'this' within f.
22    */
23   util.object.forEach = function(obj, f, optObj) {
24     'use strict';
25     var key;
26     for (key in obj) {
27       if (obj.hasOwnProperty(key)) {
28         f.call(optObj, obj[key], key, obj);
29       }
30     }
31   };
32   util.millisecondsToString = function(timeMillis) {
33     function pad(num) {
34       num = num.toString();
35       if (num.length < 2) {
36         return '0' + num;
37       }
38       return num;
39     }
40
41     var date = new Date(timeMillis);
42     return pad(date.getUTCHours()) + ':' + pad(date.getUTCMinutes()) + ':' +
43         pad(date.getUTCSeconds()) + ' ' + pad((date.getMilliseconds()) % 1000);
44   };
45
46   return util;
47 }());