2 var path = require('path')
3 var _ = require('lodash')
4 var useragent = require('useragent')
6 exports.browserFullNameToShort = function (fullName) {
7 var agent = useragent.parse(fullName)
8 return agent.toAgent() + ' (' + agent.os + ')'
11 exports.isDefined = function (value) {
12 return !_.isUndefined(value)
15 exports.isFunction = _.isFunction
16 exports.isString = _.isString
17 exports.isObject = _.isObject
18 exports.isArray = _.isArray
20 var ABS_URL = /^https?:\/\//
21 exports.isUrlAbsolute = function (url) {
22 return ABS_URL.test(url)
25 exports.camelToSnake = function (camelCase) {
26 return camelCase.replace(/[A-Z]/g, function (match, pos) {
27 return (pos > 0 ? '_' : '') + match.toLowerCase()
31 exports.ucFirst = function (word) {
32 return word.charAt(0).toUpperCase() + word.substr(1)
35 exports.dashToCamel = function (dash) {
36 var words = dash.split('-')
37 return words.shift() + words.map(exports.ucFirst).join('')
40 exports.arrayRemove = function (collection, item) {
41 var idx = collection.indexOf(item)
44 collection.splice(idx, 1)
51 exports.merge = function () {
52 var args = Array.prototype.slice.call(arguments, 0)
54 return _.merge.apply({}, args)
57 exports.formatTimeInterval = function (time) {
58 var mins = Math.floor(time / 60000)
59 var secs = (time - mins * 60000) / 1000
60 var str = secs + (secs === 1 ? ' sec' : ' secs')
63 str = mins + (mins === 1 ? ' min ' : ' mins ') + str
69 var replaceWinPath = function (path) {
70 return exports.isDefined(path) ? path.replace(/\\/g, '/') : path
73 exports.normalizeWinPath = process.platform === 'win32' ? replaceWinPath : _.identity
75 exports.mkdirIfNotExists = function mkdir (directory, done) {
76 // TODO(vojta): handle if it's a file
77 /* eslint-disable handle-callback-err */
78 fs.stat(directory, function (err, stat) {
79 if (stat && stat.isDirectory()) {
82 mkdir(path.dirname(directory), function () {
83 fs.mkdir(directory, done)
87 /* eslint-enable handle-callback-err */