feature.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 = {
  25. status: statuses[packed.B],
  26. title: packed.C,
  27. shown: packed.D
  28. }
  29. unpacked.stats = Object.keys(packed.A).reduce((browserStats, key) => {
  30. let browser = packed.A[key]
  31. browserStats[browsers[key]] = Object.keys(browser).reduce(
  32. (stats, support) => {
  33. let packedVersions = browser[support].split(' ')
  34. let unpacked2 = unpackSupport(support)
  35. packedVersions.forEach(v => (stats[versions[v]] = unpacked2))
  36. return stats
  37. },
  38. {}
  39. )
  40. return browserStats
  41. }, {})
  42. return unpacked
  43. }
  44. module.exports = unpackFeature
  45. module.exports.default = unpackFeature