cluedin-widget
Version:
This is the project for creating and managing widgets in CluedIn.
109 lines (88 loc) • 2.5 kB
JavaScript
var moment = require( 'moment' );
var addPixel = function( value ) {
if ( !value ) {
return;
}
return value + ' px';
};
var formatMinutesFromSeconds = function( seconds ) {
if ( !seconds ) {
return;
}
return '~' + Math.floor( seconds / 60 ) + ' min.';
};
var formatReadingTime = function( wordCound ) {
if ( !wordCound ) {
return;
}
return '~' + Math.floor( wordCound / 250 ) + ' min.';
};
var getPresentationTime = function( wordCound ) {
if ( !wordCound ) {
return;
}
var readingTime = Math.floor( wordCound / 250 );
return Math.floor( readingTime * 3 );
};
var formatPresentationTime = function( wordCound ) {
if ( !wordCound ) {
return;
}
return '~' + getPresentationTime( wordCound ) + ' min.';
};
var bytesToSize = function( bytes ) {
if ( !bytes ) {
return;
}
if ( typeof bytes === 'string' ) {
bytes = parseInt( bytes, 10 );
}
var sizes = [ 'Bytes', 'KB', 'MB', 'GB', 'TB' ];
if ( bytes === 0 ) {
return '0 Byte';
}
var i = parseInt( Math.floor( Math.log( bytes ) / Math.log( 1024 ) ) );
return Math.round( bytes / Math.pow( 1024, i ), 2 ) + ' ' + sizes[ i ];
};
module.exports = {
formatNumberAbbreviation: function( value ) {
if ( !value ) {
return;
}
var value = parseInt( value, 10 );
if ( value && value >= 1000 && value < 1000000 ) {
value = value / 1000;
return (value.toFixed( 2 ) + 'K');
}
if ( value && value >= 1000000 && value < 1000000000 ) {
value = value / 1000000;
return (value.toFixed( 2 ) + 'M');
}
if ( value && value >= 1000000000 ) {
value = value / 1000000000;
return (value.toFixed( 2 ) + 'G')
}
return value;
},
formatDate: function( value ) {
if ( !value ) {
return;
}
return moment( value ).format( 'MMMM Do YYYY, h:mm:ss a' );
},
/**
* @return {string}
*/
YesOrNo: function( value ) {
if ( !value ) {
return '';
}
return "True" !== value ? 'No' : 'Yes';
},
addPixel: addPixel,
formatMinutesFromSeconds: formatMinutesFromSeconds,
formatReadingTime: formatReadingTime,
formatPresentationTime: formatPresentationTime,
getPresentationTime: getPresentationTime,
bytesToSize: bytesToSize
};