]>
Commit | Line | Data |
---|---|---|
a5409a6b MF |
1 | // Copyright 2011 Google Inc. All Rights Reserved. |
2 | ||
3 | /** | |
4 | * @fileoverview Some basic, useful utility functions. | |
5 | */ | |
6 | ||
7 | /** | |
8 | * Pours the data in template string. | |
9 | * @param {Object} datObject The data object to be filled in template string. | |
10 | * @return {string} The new string created from template string and filled | |
11 | * with the given data. | |
12 | */ | |
13 | String.prototype.supplant = function(datObject) { | |
14 | return this.replace(/{([^{}]*)}/g, | |
15 | function(match, firstSubMatch) { | |
16 | var replace = datObject[firstSubMatch]; | |
17 | return (typeof replace === 'string' || typeof replace === 'number') ? | |
18 | replace : match; | |
19 | }); | |
20 | }; | |
21 | ||
22 | /** | |
23 | * Queries the DOM. | |
24 | * @param {string} selector Selector to execute. | |
25 | * @param {HTMLElement=} context HTMLElement to query (optional). | |
26 | * @return {Array.<HTMLElement>} Array of matched elements (non-live). | |
27 | */ | |
28 | function $(selector, context) { | |
29 | if (!(context && context instanceof HTMLElement)) { | |
30 | context = document; | |
31 | } | |
32 | return Array.prototype.slice.call(context.querySelectorAll(selector)); | |
33 | } | |
34 | ||
35 | /** | |
36 | * Provides units for time, given in hours, as hours or days. | |
37 | * @param {number} time The time, in hours. | |
38 | * @return {Object} The time and units of time (pluralized and localized). | |
39 | */ | |
40 | function getUnitsForTime(time) { | |
41 | var units = 'hour'; | |
42 | if (time >= 24) { | |
43 | units = 'day'; | |
44 | time = time / 24; | |
45 | } | |
46 | units = chrome.i18n.getMessage(units + (time === 1 ? '' : 's') + 'String'); | |
47 | return { | |
48 | time: time, | |
49 | units: units | |
50 | }; | |
51 | } |