Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate table of features as an SVG image from a json file #153

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions .caps-table/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
To generate a table of features as an SVG file, change to this folder and run
```
npm install
npm run generate
```
57 changes: 57 additions & 0 deletions .caps-table/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const fs = require('fs');
const svgTable = require('pure-svg-table');

function convertTable(clientFeatures, has, hasNot) {
let capSet = new Set()
clientFeatures.forEach(c => {
c.features.forEach(f => {
capSet.add(f)
})
})
let sortedCapList = Array.from(capSet).sort()
const header = [""].concat(sortedCapList)
let table = [header]
clientFeatures.forEach(c => {
let row = [c.name]
sortedCapList.forEach(f => {
let cell
if (c.features.indexOf(f) == -1) {
cell = hasNot
}
else {
cell = has
}
row.push(cell)
})
table.push(row)
})
return table
}

function main(newOpts) {
let opts = {
featuresFile: '../caps/clients.json',
has: "✔️",
hasNot: "❌",
svgResultsFile: '../caps/generated-features.svg',
stylesFile: 'styles.css',
}
Object.assign(opts, newOpts)
const clientFeatures = JSON.parse(fs.readFileSync(opts.featuresFile).toString())
let style
if (opts.stylesFile !== undefined) {
style = fs.readFileSync(opts.stylesFile).toString()
}
const rawTable = convertTable(clientFeatures, opts.has, opts.hasNot)
const svg = svgTable.generateTable(rawTable, style)
fs.writeFileSync(opts.svgResultsFile, svg)
}

module.exports = {
convertTable: convertTable,
main: main,
}

if (require.main === module) {
main()
}
20 changes: 20 additions & 0 deletions .caps-table/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const assert = require('assert')
const { convertTable, main } = require('./index')

it('Convert table', () => {
const features = [
{name: "Batman", features: ["rich", "fly"]},
{name: "Superman", features: ["fly", "strong"]},
]
const expected = [
["", "fly", "rich", "strong"],
["Batman", "yes", "yes", "no"],
["Superman", "yes", "no", "yes"],
]
const got = convertTable(features, "yes", "no")
assert.deepStrictEqual(got, expected)
})

it('Generate SVG', () => {
main({featuresFile: 'test/clients.json', svgResultsFile: 'tmp.svg', stylesFile: 'test/style.css'})
})
Loading