feature.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const statuses = require('../lib/statuses')
  2. const supported = require('../lib/supported')
  3. const { browsers } = require('./browsers')
  4. const versions = require('./browserVersions').browserVersions
  5. const MATH2LOG = Math.log(2)
  6. function unpackSupport(cipher) {
  7. // bit flags
  8. let stats = Object.keys(supported).reduce((list, support) => {
  9. if (cipher & supported[support]) list.push(support)
  10. return list
  11. }, [])
  12. // notes
  13. let notes = cipher >> 7
  14. let notesArray = []
  15. while (notes) {
  16. let note = Math.floor(Math.log(notes) / MATH2LOG) + 1
  17. notesArray.unshift(`#${note}`)
  18. notes -= Math.pow(2, note - 1)
  19. }
  20. return stats.concat(notesArray).join(' ')
  21. }
  22. function unpackFeature(packed) {
  23. let unpacked = { status: statuses[packed.B], title: packed.C }
  24. unpacked.stats = Object.keys(packed.A).reduce((browserStats, key) => {
  25. let browser = packed.A[key]
  26. browserStats[browsers[key]] = Object.keys(browser).reduce(
  27. (stats, support) => {
  28. let packedVersions = browser[support].split(' ')
  29. let unpacked2 = unpackSupport(support)
  30. packedVersions.forEach(v => (stats[versions[v]] = unpacked2))
  31. return stats
  32. },
  33. {}
  34. )
  35. return browserStats
  36. }, {})
  37. return unpacked
  38. }
  39. module.exports = unpackFeature
  40. module.exports.default = unpackFeature