blob: d18022ed595d6e4ef89057bf30d2a33c622dc0a4 [file] [log] [blame]
<!DOCTYPE html>
<!--
Copyright 2016 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="/tracing/base/iteration_helpers.html">
<script>
'use strict';
var GREEK_SMALL_LETTER_MU = String.fromCharCode(956);
tr.exportTo('tr.b', function() {
var UnitScale = {};
function defineUnitScale(name, prefixes) {
if (UnitScale[name] !== undefined)
throw new Error('Unit scale \'' + name + '\' already exists');
if (prefixes.AUTO !== undefined) {
throw new Error('\'AUTO\' unit prefix will be added automatically ' +
'for unit scale \'' + name + '\'');
}
// If the 'AUTO' unit prefix is used, the prefix that results in
// the absolute formatted value being as close to the [1, 1024) interval as
// possible is used. Example: 1023 and 1024 bytes are displayed as
// "1,023.0 B" and "1.0 KiB", respectively.
prefixes.AUTO = tr.b.dictionaryValues(prefixes);
prefixes.AUTO.sort((a, b) => a.value - b.value);
UnitScale[name] = prefixes;
}
/**
* Converts |value| from |fromPrefix| (e.g. kilo) to |toPrefix| (e.g. mega).
*
* Returns undefined if |value| is undefined.
* |fromPrefix| and |toPrefix| need not come from the same UnitScale.
*
* @param {(undefined|number)} value
* @param {!object} fromPrefix
* @param {!object} toPrefix
* @return {(undefined|number)}
*/
function convertUnit(value, fromPrefix, toPrefix) {
if (value === undefined)
return undefined;
return value * (fromPrefix.value / toPrefix.value);
}
// See https://en.wikipedia.org/wiki/Binary_prefix.
defineUnitScale('Binary', {
NONE: { value: Math.pow(1024, 0), symbol: '' },
KIBI: { value: Math.pow(1024, 1), symbol: 'Ki' },
MEBI: { value: Math.pow(1024, 2), symbol: 'Mi' },
GIBI: { value: Math.pow(1024, 3), symbol: 'Gi' },
TEBI: { value: Math.pow(1024, 4), symbol: 'Ti' }
});
// See https://en.wikipedia.org/wiki/Metric_prefix.
defineUnitScale('Metric', {
NANO: { value: 1e-9, symbol: 'n' },
MICRO: { value: 1e-6, symbol: GREEK_SMALL_LETTER_MU },
MILLI: { value: 1e-3, symbol: 'm' },
NONE: { value: 1, symbol: ''},
KILO: { value: 1e3, symbol: 'k'},
MEGA: { value: 1e6, symbol: 'M'},
GIGA: { value: 1e9, symbol: 'G'}
});
return {
UnitScale: UnitScale,
convertUnit: convertUnit
};
});
</script>