Skip to content

Commit

Permalink
Fixing all lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrohan committed Jan 31, 2019
1 parent 44fc8fd commit 875bc33
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 83 deletions.
62 changes: 32 additions & 30 deletions lib/octicons_node/index.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,65 @@
var data = require('./build/data.json')
var objectAssign = require('object-assign')

Object.keys(data).forEach(function(key) {
const data = require('./build/data.json')
const objectAssign = require('object-assign')

for (const key of Object.keys(data)) {
// Returns a string representation of html attributes
var htmlAttributes = function(icon, options) {
var attributes = []
var attrObj = objectAssign({}, data[key].options, options)
const htmlAttributes = (icon, options) => {
const attributes = []
const attrObj = objectAssign({}, data[key].options, options)

// If the user passed in options
if (options) {

// If any of the width or height is passed in
if(options["width"] || options["height"]) {
attrObj["width"] = options["width"] ? options["width"] : (parseInt(options["height"]) * data[key].options["width"] / data[key].options["height"])
attrObj["height"] = options["height"] ? options["height"] : (parseInt(options["width"]) * data[key].options["height"] / data[key].options["width"])
if (options['width'] || options['height']) {
attrObj['width'] = options['width']
? options['width']
: (parseInt(options['height']) * data[key].options['width']) / data[key].options['height']
attrObj['height'] = options['height']
? options['height']
: (parseInt(options['width']) * data[key].options['height']) / data[key].options['width']
}

// If the user passed in class
if (options["class"]) {
attrObj["class"] = "octicon octicon-" + key + " " + options["class"]
attrObj["class"].trim()
if (options['class']) {
attrObj['class'] = `octicon octicon-${key} ${options['class']}`
attrObj['class'].trim()
}

// If the user passed in aria-label
if (options["aria-label"]) {
attrObj["aria-label"] = options["aria-label"]
attrObj["role"] = "img"
if (options['aria-label']) {
attrObj['aria-label'] = options['aria-label']
attrObj['role'] = 'img'

// Un-hide the icon
delete attrObj["aria-hidden"]
delete attrObj['aria-hidden']
}
}

Object.keys(attrObj).forEach(function(option) {
attributes.push(option + "=\"" + attrObj[option] + "\"")
})
for (const option of Object.keys(attrObj)) {
attributes.push(`${option}="${attrObj[option]}"`)
}

return attributes.join(" ").trim()
return attributes.join(' ').trim()
}

// Set the symbol for easy access
data[key].symbol = key

// Set all the default options
data[key].options = {
"version": "1.1",
"width": data[key].width,
"height": data[key].height,
"viewBox": "0 0 " + data[key].width + " " + data[key].height,
"class": "octicon octicon-" + key,
"aria-hidden": "true"
version: '1.1',
width: data[key].width,
height: data[key].height,
viewBox: `0 0 ${data[key].width} ${data[key].height}`,
class: `octicon octicon-${key}`,
'aria-hidden': 'true'
}

// Function to return an SVG object
data[key].toSVG = function(options) {
return "<svg " + htmlAttributes(data[key], options) + ">" + data[key].path + "</svg>"
return `<svg ${htmlAttributes(data[key], options)}>${data[key].path}</svg>`
}
})
}

// Import data into exports
module.exports = data
133 changes: 80 additions & 53 deletions lib/octicons_node/tests/index.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,108 @@
import test from 'ava';
import octicons from '../';
import fs from 'fs';
import test from 'ava'
import octicons from '../'
import fs from 'fs'

const octiconsLib = fs.readdirSync("./build/svg/");
const octiconsLib = fs.readdirSync('./build/svg/')

test('css got moved', t => {
t.truthy(fs.existsSync("./build/build.css"), "build.css does not exist!");
});
t.truthy(fs.existsSync('./build/build.css'), 'build.css does not exist!')
})

test('Octicons are loaded', t => {
t.truthy(octicons, "Didn't find any octicons.");
t.truthy(octicons, "Didn't find any octicons.")
t.not(Object.keys(octicons).length, 0, "Didn't find any octicons.")
});
})

test('Octicons have keywords', t => {
t.truthy(octicons, "Didn't find any octicons.");
Object.keys(octicons).forEach( point => {
t.truthy(octicons[point].keywords, 'The octicon "' + point + '" doesn\'t have any keywords')
t.not(octicons[point].keywords.length, 0, 'The octicon "' + point + '" doesn\'t have any keywords')
})
});
t.truthy(octicons, "Didn't find any octicons.")
for (const point of Object.keys(octicons)) {
t.truthy(octicons[point].keywords, `The octicon "${point}" doesn't have any keywords`)
t.not(octicons[point].keywords.length, 0, `The octicon "${point}" doesn't have any keywords`)
}
})

test('Every octicon is in ./build/data.json', t => {
octiconsLib.forEach( point => {
for (let point of octiconsLib) {
point = point.replace('.svg', '')
t.truthy(octicons[point], './build/data.json doesn\'t include the octicon "' + point + '"')
})
t.truthy(octicons[point], `./build/data.json doesn't include the octicon "${point}"`)
}
})

test('No deprecated octicons are in ./build/data.json', t => {
Object.keys(octicons).forEach( point => {
t.truthy(octiconsLib.indexOf(point+'.svg') >= 0, './build/data.json contains the deleted octicon `' + point + '`, please remove it.' );
})
for (const point of Object.keys(octicons)) {
t.truthy(
octiconsLib.indexOf(`${point}.svg`) >= 0,
`./build/data.json contains the deleted octicon \`${point}\`, please remove it.`
)
}
})

test('Octicons have svg', t => {
t.truthy(octicons, "Didn't find any octicons.");
Object.keys(octicons).forEach( point => {
t.truthy(octicons[point].toSVG(), 'The octicon "' + point + '" doesn\'t have svg')
})
});
t.truthy(octicons, "Didn't find any octicons.")
for (const point of Object.keys(octicons)) {
t.truthy(octicons[point].toSVG(), `The octicon "${point}" doesn't have svg`)
}
})

test('Octicons have default html attributes', t => {
t.truthy(octicons, "Didn't find any octicons.");
Object.keys(octicons).forEach( point => {
var svg = octicons[point].toSVG()
t.regex(svg, /version="1\.1"/, 'The octicon "' + point + '" doesn\'t have the version attribute')
t.regex(svg, /aria\-hidden="true"/, 'The octicon "' + point + '" doesn\'t have the aria-hidden attribute')
t.regex(svg, new RegExp("width=\"" + octicons[point].width + "\""), 'The octicon "' + point + '" doesn\'t have the width attribute')
t.regex(svg, new RegExp("height=\"" + octicons[point].height + "\""), 'The octicon "' + point + '" doesn\'t have the height attribute')
t.regex(svg, new RegExp("viewBox=\"0 0 " + octicons[point].width + " " + octicons[point].height + "\""), 'The octicon "' + point + '" doesn\'t have the viewBox attribute')
t.regex(svg, new RegExp("class=\"octicon octicon-" + octicons[point].symbol + "\""), 'The octicon "' + point + '" doesn\'t have the class attribute')
})
});
t.truthy(octicons, "Didn't find any octicons.")
for (const point of Object.keys(octicons)) {
const svg = octicons[point].toSVG()
t.regex(svg, /version="1\.1"/, `The octicon "${point}" doesn't have the version attribute`)
t.regex(svg, /aria\-hidden="true"/, `The octicon "${point}" doesn't have the aria-hidden attribute`)
t.regex(
svg,
new RegExp(`width="${octicons[point].width}"`),
`The octicon "${point}" doesn't have the width attribute`
)
t.regex(
svg,
new RegExp(`height="${octicons[point].height}"`),
`The octicon "${point}" doesn't have the height attribute`
)
t.regex(
svg,
new RegExp(`viewBox="0 0 ${octicons[point].width} ${octicons[point].height}"`),
`The octicon "${point}" doesn't have the viewBox attribute`
)
t.regex(
svg,
new RegExp(`class="octicon octicon-${octicons[point].symbol}"`),
`The octicon "${point}" doesn't have the class attribute`
)
}
})

test('Passing in classnames will be included in output', t => {
t.truthy(octicons, "Didn't find any octicons.");
Object.keys(octicons).forEach( point => {
var svg = octicons[point].toSVG({ class: "new-class another-class" })
t.regex(svg, new RegExp("class=\"octicon octicon-" + octicons[point].symbol + " new-class another-class\""), 'The octicon "' + point + '" doesn\'t have the class attribute')
})
});
t.truthy(octicons, "Didn't find any octicons.")
for (const point of Object.keys(octicons)) {
const svg = octicons[point].toSVG({class: 'new-class another-class'})
t.regex(
svg,
new RegExp(`class="octicon octicon-${octicons[point].symbol} new-class another-class"`),
`The octicon "${point}" doesn't have the class attribute`
)
}
})

test('Passing in aria-label will update the a11y options', t => {
t.truthy(octicons, "Didn't find any octicons.");
Object.keys(octicons).forEach( point => {
var svg = octicons[point].toSVG({ "aria-label": "This is an icon" })
t.regex(svg, new RegExp("aria\-label=\"This is an icon\""), 'The octicon "' + point + '" doesn\'t have the aria-label attribute')
t.truthy(octicons, "Didn't find any octicons.")
Object.keys(octicons).forEach(point => {
const svg = octicons[point].toSVG({'aria-label': 'This is an icon'})
t.regex(
svg,
new RegExp('aria-label="This is an icon"'),
`The octicon "${point}" doesn't have the aria-label attribute`
)
})
});
})

test('Passing in width will size properly', t => {
var svg = octicons["x"].toSVG({ "height": 60 })
t.regex(svg, new RegExp("width=\"45\""), 'The octicon "x" doesn\'t have the width attribute scaled properly')
});
const svg = octicons['x'].toSVG({height: 60})
t.regex(svg, new RegExp('width="45"'), 'The octicon "x" doesn\'t have the width attribute scaled properly')
})

test('Passing in height will size properly', t => {
var svg = octicons["x"].toSVG({ "width": 45 })
t.regex(svg, new RegExp("height=\"60\""), 'The octicon "x" doesn\'t have the height attribute scaled properly')
});
const svg = octicons['x'].toSVG({width: 45})
t.regex(svg, new RegExp('height="60"'), 'The octicon "x" doesn\'t have the height attribute scaled properly')
})

0 comments on commit 875bc33

Please sign in to comment.