File: utils.js

package info (click to toggle)
node-caniuse-api 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 116 kB
  • sloc: makefile: 6; sh: 2
file content (41 lines) | stat: -rw-r--r-- 1,272 bytes parent folder | download
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
import uniq from "lodash.uniq"
import browserslist from "browserslist"

export function contains(str, substr) {
  return !!~str.indexOf(substr)
}

export function parseCaniuseData(feature, browsers) {
  var support = {}
  var letters
  var letter

  browsers.forEach(function(browser) {
    support[browser] = {}
    for (var info in feature.stats[browser]) {
      letters = feature.stats[browser][info].split(" ")
      info = parseFloat(info.split("-")[0]) //if info is a range, take the left
      if (isNaN(info)) continue
      for (var i = 0; i < letters.length ; i++) {
        letter = letters[i]
        if (letter === "y"){ // min support asked, need to find the min value
          if (typeof support[browser][letter] === "undefined" ||
              info < support[browser][letter]) {
            support[browser][letter] = info
          }
        } else { // any other support, need to find the max value
          if (typeof support[browser][letter] === "undefined" ||
              info > support[browser][letter]) {
            support[browser][letter] = info
          }
        }
      }
    }
  })

  return support
}

export function cleanBrowsersList(browserList) {
  return uniq(browserslist(browserList).map((browser) => browser.split(" ")[0]))
}