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