feature.js 1.3 KB

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