/server/node_modules/karma/lib/helper.js

https://gitlab.com/flangelier/tictacme · JavaScript · 108 lines · 84 code · 20 blank · 4 comment · 6 complexity · e2aeefdf9d283b4695af91513cbc0ff1 MD5 · raw file

  1. var fs = require('fs')
  2. var path = require('path')
  3. var _ = require('lodash')
  4. var useragent = require('useragent')
  5. var Promise = require('bluebird')
  6. exports.browserFullNameToShort = function (fullName) {
  7. var agent = useragent.parse(fullName)
  8. var isKnown = agent.family !== 'Other' && agent.os.family !== 'Other'
  9. return isKnown ? agent.toAgent() + ' (' + agent.os + ')' : fullName
  10. }
  11. exports.isDefined = function (value) {
  12. return !_.isUndefined(value)
  13. }
  14. exports.isFunction = _.isFunction
  15. exports.isString = _.isString
  16. exports.isObject = _.isObject
  17. exports.isArray = _.isArray
  18. var ABS_URL = /^https?:\/\//
  19. exports.isUrlAbsolute = function (url) {
  20. return ABS_URL.test(url)
  21. }
  22. exports.camelToSnake = function (camelCase) {
  23. return camelCase.replace(/[A-Z]/g, function (match, pos) {
  24. return (pos > 0 ? '_' : '') + match.toLowerCase()
  25. })
  26. }
  27. exports.ucFirst = function (word) {
  28. return word.charAt(0).toUpperCase() + word.substr(1)
  29. }
  30. exports.dashToCamel = function (dash) {
  31. var words = dash.split('-')
  32. return words.shift() + words.map(exports.ucFirst).join('')
  33. }
  34. exports.arrayRemove = function (collection, item) {
  35. var idx = collection.indexOf(item)
  36. if (idx !== -1) {
  37. collection.splice(idx, 1)
  38. return true
  39. }
  40. return false
  41. }
  42. exports.merge = function () {
  43. var args = Array.prototype.slice.call(arguments, 0)
  44. args.unshift({})
  45. return _.merge.apply({}, args)
  46. }
  47. exports.formatTimeInterval = function (time) {
  48. var mins = Math.floor(time / 60000)
  49. var secs = (time - mins * 60000) / 1000
  50. var str = secs + (secs === 1 ? ' sec' : ' secs')
  51. if (mins) {
  52. str = mins + (mins === 1 ? ' min ' : ' mins ') + str
  53. }
  54. return str
  55. }
  56. var replaceWinPath = function (path) {
  57. return exports.isDefined(path) ? path.replace(/\\/g, '/') : path
  58. }
  59. exports.normalizeWinPath = process.platform === 'win32' ? replaceWinPath : _.identity
  60. exports.mkdirIfNotExists = function mkdir (directory, done) {
  61. // TODO(vojta): handle if it's a file
  62. /* eslint-disable handle-callback-err */
  63. fs.stat(directory, function (err, stat) {
  64. if (stat && stat.isDirectory()) {
  65. done()
  66. } else {
  67. mkdir(path.dirname(directory), function () {
  68. fs.mkdir(directory, done)
  69. })
  70. }
  71. })
  72. /* eslint-enable handle-callback-err */
  73. }
  74. exports.defer = function () {
  75. var resolve
  76. var reject
  77. var promise = new Promise(function () {
  78. resolve = arguments[0]
  79. reject = arguments[1]
  80. })
  81. return {
  82. resolve: resolve,
  83. reject: reject,
  84. promise: promise
  85. }
  86. }
  87. // export lodash
  88. exports._ = _