1
hao
2025-03-27 e610e1c17f62b423a717fadaaa7b139d02857793
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
44
45
46
47
48
49
50
exports.L = { bit: 1 }
exports.M = { bit: 0 }
exports.Q = { bit: 3 }
exports.H = { bit: 2 }
 
function fromString (string) {
  if (typeof string !== 'string') {
    throw new Error('Param is not a string')
  }
 
  const lcStr = string.toLowerCase()
 
  switch (lcStr) {
    case 'l':
    case 'low':
      return exports.L
 
    case 'm':
    case 'medium':
      return exports.M
 
    case 'q':
    case 'quartile':
      return exports.Q
 
    case 'h':
    case 'high':
      return exports.H
 
    default:
      throw new Error('Unknown EC Level: ' + string)
  }
}
 
exports.isValid = function isValid (level) {
  return level && typeof level.bit !== 'undefined' &&
    level.bit >= 0 && level.bit < 4
}
 
exports.from = function from (value, defaultValue) {
  if (exports.isValid(value)) {
    return value
  }
 
  try {
    return fromString(value)
  } catch (e) {
    return defaultValue
  }
}