source: trip-planner-front/node_modules/karma/lib/helper.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.6 KB
Line 
1'use strict'
2
3const fs = require('graceful-fs')
4const path = require('path')
5const _ = require('lodash')
6const useragent = require('ua-parser-js')
7const mm = require('minimatch')
8
9exports.browserFullNameToShort = (fullName) => {
10 const ua = useragent(fullName)
11 if (!ua.browser.name && !ua.browser.version && !ua.os.name && !ua.os.version) {
12 return fullName
13 }
14 return `${ua.browser.name} ${ua.browser.version || '0.0.0'} (${ua.os.name} ${ua.os.version || '0.0.0'})`
15}
16
17exports.isDefined = (value) => {
18 return !_.isUndefined(value)
19}
20
21const parser = (pattern, out) => {
22 if (pattern.length === 0) return out
23 const p = /^(\[[^\]]*\]|[*+@?]\((.+?)\))/g
24 const matches = p.exec(pattern)
25 if (!matches) {
26 const c = pattern[0]
27 let t = 'word'
28 if (c === '*') {
29 t = 'star'
30 } else if (c === '?') {
31 t = 'optional'
32 }
33 out[t]++
34 return parser(pattern.substring(1), out)
35 }
36 if (matches[2] !== undefined) {
37 out.ext_glob++
38 parser(matches[2], out)
39 return parser(pattern.substring(matches[0].length), out)
40 }
41 out.range++
42 return parser(pattern.substring(matches[0].length), out)
43}
44
45const gsParser = (pattern, out) => {
46 if (pattern === '**') {
47 out.glob_star++
48 return out
49 }
50 return parser(pattern, out)
51}
52
53const compareWeightObject = (w1, w2) => {
54 return exports.mmComparePatternWeights(
55 [w1.glob_star, w1.star, w1.ext_glob, w1.range, w1.optional],
56 [w2.glob_star, w2.star, w2.ext_glob, w2.range, w2.optional]
57 )
58}
59
60exports.mmPatternWeight = (pattern) => {
61 const m = new mm.Minimatch(pattern)
62 if (!m.globParts) return [0, 0, 0, 0, 0, 0]
63 const result = m.globParts.reduce((prev, p) => {
64 const r = p.reduce((prev, p) => {
65 return gsParser(p, prev)
66 }, { glob_star: 0, ext_glob: 0, word: 0, star: 0, optional: 0, range: 0 })
67 if (prev === undefined) return r
68 return compareWeightObject(r, prev) > 0 ? r : prev
69 }, undefined)
70 result.glob_sets = m.set.length
71 return [result.glob_sets, result.glob_star, result.star, result.ext_glob, result.range, result.optional]
72}
73
74exports.mmComparePatternWeights = (weight1, weight2) => {
75 const n1 = weight1[0]
76 const n2 = weight2[0]
77 const diff = n1 - n2
78 if (diff !== 0) return diff / Math.abs(diff)
79 return weight1.length > 1 ? exports.mmComparePatternWeights(weight1.slice(1), weight2.slice(1)) : 0
80}
81
82exports.isFunction = _.isFunction
83exports.isString = _.isString
84exports.isObject = _.isObject
85exports.isArray = _.isArray
86exports.isNumber = _.isNumber
87
88const ABS_URL = /^https?:\/\//
89exports.isUrlAbsolute = (url) => {
90 return ABS_URL.test(url)
91}
92
93exports.camelToSnake = (camelCase) => {
94 return camelCase.replace(/[A-Z]/g, (match, pos) => {
95 return (pos > 0 ? '_' : '') + match.toLowerCase()
96 })
97}
98
99exports.ucFirst = (word) => {
100 return word.charAt(0).toUpperCase() + word.substr(1)
101}
102
103exports.dashToCamel = (dash) => {
104 const words = dash.split('-')
105 return words.shift() + words.map(exports.ucFirst).join('')
106}
107
108exports.arrayRemove = (collection, item) => {
109 const idx = collection.indexOf(item)
110
111 if (idx !== -1) {
112 collection.splice(idx, 1)
113 return true
114 }
115
116 return false
117}
118
119exports.merge = function () {
120 const args = Array.prototype.slice.call(arguments, 0)
121 args.unshift({})
122 return _.merge.apply({}, args)
123}
124
125exports.formatTimeInterval = (time) => {
126 const mins = Math.floor(time / 60000)
127 const secs = (time - mins * 60000) / 1000
128 let str = secs + (secs === 1 ? ' sec' : ' secs')
129
130 if (mins) {
131 str = mins + (mins === 1 ? ' min ' : ' mins ') + str
132 }
133
134 return str
135}
136
137const replaceWinPath = (path) => {
138 return _.isString(path) ? path.replace(/\\/g, '/') : path
139}
140
141exports.normalizeWinPath = process.platform === 'win32' ? replaceWinPath : _.identity
142
143exports.mkdirIfNotExists = (directory, done) => {
144 // TODO(vojta): handle if it's a file
145 /* eslint-disable handle-callback-err */
146 fs.stat(directory, (err, stat) => {
147 if (stat && stat.isDirectory()) {
148 done()
149 } else {
150 exports.mkdirIfNotExists(path.dirname(directory), () => {
151 fs.mkdir(directory, done)
152 })
153 }
154 })
155 /* eslint-enable handle-callback-err */
156}
157
158exports.defer = () => {
159 let res
160 let rej
161 const promise = new Promise((resolve, reject) => {
162 res = resolve
163 rej = reject
164 })
165
166 return {
167 resolve: res,
168 reject: rej,
169 promise: promise
170 }
171}
172
173exports.saveOriginalArgs = (config) => {
174 if (config && config.client && config.client.args) {
175 config.client.originalArgs = _.cloneDeep(config.client.args)
176 }
177}
178
179exports.restoreOriginalArgs = (config) => {
180 if (config && config.client && config.client.originalArgs) {
181 config.client.args = _.cloneDeep(config.client.originalArgs)
182 }
183}
Note: See TracBrowser for help on using the repository browser.