/* * UnitFmt.js - Unit formatter class * * Copyright © 2014-2015, JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * limitations under the License. */ /* !depends ilib.js Locale.js ResBundle.js LocaleInfo.js IString.js NumFmt.js Utils.js */ // !data unitfmt var ilib = require("./ilib.js"); var Utils = require("./Utils.js"); var Locale = require("./Locale.js"); var LocaleInfo = require("./LocaleInfo.js"); var ResBundle = require("./ResBundle.js"); var IString = require("./IString.js"); var NumFmt = require("./NumFmt.js"); /** * @class * Create a new unit formatter instance. The unit formatter is immutable once * it is created, but can format as many different strings with different values * as needed with the same options. Create different unit formatter instances * for different purposes and then keep them cached for use later if you have * more than one unit string to format.
* * The options may contain any of the following properties: * *
* * * @constructor * @param {Object} options options governing the way this date formatter instance works */ var UnitFmt = function(options) { var sync = true, loadParams = undefined; this.length = "long"; this.scale = true; this.measurementType = 'undefined'; this.convert = true; this.locale = new Locale(); if (options) { if (options.locale) { this.locale = (typeof(options.locale) === 'string') ? new Locale(options.locale) : options.locale; } if (typeof(options.sync) === 'boolean') { sync = options.sync; } if (typeof(options.loadParams) !== 'undefined') { loadParams = options.loadParams; } if (options.length) { this.length = options.length; } if (typeof(options.autoScale) === 'boolean') { this.scale = options.autoScale; } if (typeof(options.autoConvert) === 'boolean') { this.convert = options.autoConvert; } if (typeof(options.useNative) === 'boolean') { this.useNative = options.useNative; } if (options.measurementSystem) { this.measurementSystem = options.measurementSystem; } if (typeof (options.maxFractionDigits) === 'number') { /** * @private * @type {number|undefined} */ this.maxFractionDigits = options.maxFractionDigits; } if (typeof (options.minFractionDigits) === 'number') { /** * @private * @type {number|undefined} */ this.minFractionDigits = options.minFractionDigits; } /** * @private * @type {string} */ this.roundingMode = options.roundingMode; } if (!UnitFmt.cache) { UnitFmt.cache = {}; } Utils.loadData({ object: UnitFmt, locale: this.locale, name: "unitfmt.json", sync: sync, loadParams: loadParams, callback: ilib.bind(this, function (format) { var formatted = format; this.template = formatted["unitfmt"][this.length]; new NumFmt({ locale: this.locale, useNative: this.useNative, maxFractionDigits: this.maxFractionDigits, minFractionDigits: this.minFractionDigits, roundingMode: this.roundingMode, sync: sync, loadParams: loadParams, onLoad: ilib.bind(this, function (numfmt) { this.numFmt = numfmt; if (options && typeof(options.onLoad) === 'function') { options.onLoad(this); } }) }); }) }); }; UnitFmt.prototype = { /** * Return the locale used with this formatter instance. * @return {Locale} the Locale instance for this formatter */ getLocale: function() { return this.locale; }, /** * Return the template string that is used to format date/times for this * formatter instance. This will work, even when the template property is not explicitly * given in the options to the constructor. Without the template option, the constructor * will build the appropriate template according to the options and use that template * in the format method. * * @return {string} the format template for this formatter */ getTemplate: function() { return this.template; }, /** * Convert this formatter to a string representation by returning the * format template. This method delegates to getTemplate. * * @return {string} the format template */ toString: function() { return this.getTemplate(); }, /** * Return whether or not this formatter will auto-scale the units while formatting. * @returns {boolean} true if auto-scaling is turned on */ getScale: function() { return this.scale; }, /** * Return the measurement system that is used for this formatter. * @returns {string} the measurement system used in this formatter */ getMeasurementSystem: function() { return this.measurementSystem; }, /** * Format a particular unit instance according to the settings of this * formatter object. * * @param {Measurement} measurement measurement to format * @return {string} the formatted version of the given date instance */ format: function (measurement) { var u = this.convert ? measurement.localize(this.locale.getSpec()) : measurement; u = this.scale ? u.scale(this.measurementSystem) : u; var formatted = new IString(this.template[u.getUnit()]); // make sure to use the right plural rules formatted.setLocale(this.locale, true, undefined, undefined); formatted = formatted.formatChoice(u.amount,{n:this.numFmt.format(u.amount)}); return formatted.length > 0 ? formatted : u.amount +" " + u.unit; } }; module.exports = UnitFmt;