'use strict';
var _ = require('lodash'),
moment = require('moment-timezone');
/**
* Utility methods for use throughout our code base.
* @module SHUtils
*/
module.exports = {
prettyPrice: function(price, format) {
if (_.isString(price)) {
price = parseInt(price, 10);
}
if (!format && price.toFixed(2).split('.')[1] === '00') {
return price;
} else {
return price.toFixed(2);
}
},
/**
* Outputs SpotHero standard formatted time duration string from milliseconds.
* @param {Object} duration - Difference between two times in milliseconds
* @param {Object} hoursOnly - Returns a string with hour total only
* @example
* SHUtils.prettyDuration(100000);
* SHUtils.prettyDuration(momentEndDate.diff(momentStartDate));
* @returns {String} SpotHero standard formatted time duration string (i.e. "3 days, 12 hours, 30 minutes")
*/
prettyDuration: function(duration, hoursOnly) {
let formattedDuration = moment.duration(duration);
if (hoursOnly) {
formattedDuration = formattedDuration
.format('h m [min]')
.replace(' 30 min', '.5')
.replace('30 min', '.5')
.replace(' 0 min', '');
} else {
formattedDuration = formattedDuration
.format('d [days], h [hours], m [min]')
.replace(', 0 hours', '')
.replace(', 0 min', '')
.replace('1 days', '1 day')
.replace('1 hours', '1 hour');
}
return formattedDuration;
},
stripTime: function(timeStr) {
if (_.isUndefined(timeStr)) {
return '';
}
var ampm = (timeStr.match(/AM$/) ? 'AM' : 'PM'),
timeStripped = timeStr.replace(/[^\d.]/g, '');
return timeStripped + ampm;
},
isHighDensityDisplay: function() {
return ((window.matchMedia && (window.matchMedia('only screen and (min-resolution: 124dpi), only screen and (min-resolution: 1.3dppx), only screen and (min-resolution: 48.8dpcm)').matches || window.matchMedia('only screen and (-webkit-min-device-pixel-ratio: 1.3), only screen and (-o-min-device-pixel-ratio: 2.6/2), only screen and (min--moz-device-pixel-ratio: 1.3), only screen and (min-device-pixel-ratio: 1.3)').matches)) || (window.devicePixelRatio && window.devicePixelRatio > 1.3));
},
getParameterByName: function(name) {
var match = new RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
},
trackModalAsPageView: function(dataurl) {
if (!_.isUndefined(window.ga) && dataurl) {
var url = window.pageViewUrls[dataurl];
if (_.isUndefined(url)) {
return;
} else if (url.charAt(0) === '{' && url.charAt(1) === '}') {
url = window.location.pathname + url.substring(2);
url = url.replace('//', '/');
}
window.ga('send', 'pageview', {page: url});
}
},
generateUUID: function() {
var d = new Date().getTime(),
uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
return uuid;
},
isInUserAgent: function(string) {
return navigator.userAgent.toLowerCase().indexOf(string) > -1;
},
isTouchDevice: function() {
return 'ontouchstart' in window // works on most browsers
|| navigator.maxTouchPoints; // works on IE10/11 and Surface
},
isPlaceholderSupported: function() {
return ('placeholder' in document.createElement('input'));
}
};