@webgap/format-utils
Version:
Common formatter utilities to handle numbers, dates, times, etc.
144 lines (132 loc) • 4.19 kB
JavaScript
/**
* (C) Copyright 2014 WebGAP (http://www.webgap.eu/).
*
* 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.
*
* Created by: ManuelMartins
* Created on: 08-09-2014
*
*/
var configuration = require('@webgap/configuration');
var S = require('string');
var numeral = require('numeral');
var moment = require('moment');
/**
* Format utility class
*
* @returns {Format}
* @constructor
*/
module.exports = function(options) {
var self = this;
options = options || {};
// default values
self.dateFormat = options.dateFormat || configuration.get('GENERAL.DATE.DATE_FORMAT');
self.dateTimeFormat = options.dateTimeFormat || configuration.get('GENERAL.DATE.DATETIME_FORMAT');
self.timeFormat = options.timeFormat || configuration.get('GENERAL.DATE.TIME_FORMAT');
self.decimalFormat = options.decimalFormat || configuration.get('GENERAL.NUMBER.DECIMAL_FORMAT');
self.percentageFormat = options.percentageFormat || configuration.get('GENERAL.NUMBER.PERCENTAGE_FORMAT');
self.byteFormat = options.byteFormat || configuration.get('GENERAL.NUMBER.BYTE_FORMAT');
return {
/**
* Formats numeric values to *.00
* If no value is provided 0 (zero) is used
*
* @param val the value to format
* @returns {*|exports}
*/
number: function number(val) {
return numeral(val).format(self.decimalFormat);
},
/**
* Format numeric values to bytes
* If no value is provided 0 (zero) is used
*
* @param val the value to format
* @returns {*}
*/
bytes: function bytes(val) {
return numeral(val).format(self.byteFormat);
},
/**
* Format numeric values to percentage
* If no value is provided 0 (zero) is used
*
* @param val the value to format
* @returns {*}
*/
percentage: function percentage(val) {
return numeral(val).format(self.percentageFormat);
},
/**
* Format values to date format
* If no value is provided the current date is used
*
* @param val the value to format
* @returns {*}
*/
date: function date(val) {
return moment(val).utc().format(self.dateFormat).toString();
},
/**
* Format values to datetime format
* If no value is provided the current date is used
*
* @param val the value to format
* @returns {*}
*/
dateTime: function dateTime(val) {
return moment(val).utc().format(self.dateTimeFormat).toString();
},
/**
* Format values to from now representation
* If no value is provided the current date is used
*
* @param val the value to format
* @returns {*}
*/
dateFromNow: function dateFromNow(val) {
return moment(val).utc().fromNow();
},
/**
* Format values by truncating them
* If no value is provided the default is 15
*
* @param val the value to format
* @returns {*}
*/
truncate: function truncate(value, length) {
value = value || '';
length = length || 15;
return S(value).truncate(length).s;
},
/**
* Format values by returning Not Defined ND if value is none
*
* @param val the value to format
* @returns {*}
*/
ND: function ND(val) {
return S(val).isEmpty() ? 'ND' : val;
},
/**
* Format values by returning Not Available ND if value is none
*
* @param val the value to format
* @returns {*}
*/
NA: function NA(val) {
return S(val).isEmpty() ? 'NA' : val;
}
};
};