-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
coerce.js
43 lines (35 loc) · 1.4 KB
/
coerce.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const _ = require('lodash')
const toBoolean = require('underscore.string/toBoolean')
const isValue = (value) => {
return (comparison) => {
return _.invoke(comparison, 'toString') === value
}
}
// https://github.com/cypress-io/cypress/issues/6810
const toArray = (value) => {
const valueIsNotStringOrArray = typeof (value) !== 'string' || (value[0] !== '[' && value[value.length - 1] !== ']')
if (valueIsNotStringOrArray) {
return
}
// '[foo,bar]' => ['foo', 'bar']
const convertStringToArray = () => value.substring(1, value.length - 1).split(',')
const arr = convertStringToArray()
// The default `toString` array method returns one string containing each array element separated
// by commas, but without '[' or ']'. If an environment variable is intended to be an array, it
// will begin and end with '[' and ']' respectively. To correctly compare the value argument to
// the value in `process.env`, the `toString` method must be updated to include '[' and ']'.
// Default `toString()` on array: ['foo', 'bar'].toString() => 'foo,bar'
// Custom `toString()` on array: ['foo', 'bar'].toString() => '[foo,bar]'
arr.toString = () => `[${arr.join(',')}]`
return arr
}
module.exports = (value) => {
const num = _.toNumber(value)
const bool = toBoolean(value)
const arr = toArray(value)
return _
.chain([num, bool, arr])
.find(isValue(value))
.defaultTo(value)
.value()
}