forked from primer/octicons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (66 loc) · 2.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const data = require('./build/data.json')
const objectAssign = require('object-assign')
const DEFAULT_HEIGHT = 16
for (const key of Object.keys(data)) {
// Returns a string representation of html attributes
const htmlAttributes = (icon, defaultOptions, options) => {
const attributes = []
const attrObj = objectAssign({}, defaultOptions, 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']) * defaultOptions['width']) / defaultOptions['height']
attrObj['height'] = options['height']
? options['height']
: (parseInt(options['width']) * defaultOptions['height']) / defaultOptions['width']
}
// If the user passed in class
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'
// Un-hide the icon
delete attrObj['aria-hidden']
}
}
for (const option of Object.keys(attrObj)) {
attributes.push(`${option}="${attrObj[option]}"`)
}
return attributes.join(' ').trim()
}
// Set the symbol for easy access
data[key].symbol = key
// Set options for each icon height
for (const height of Object.keys(data[key].heights)) {
data[key].heights[height].options = {
version: '1.1',
width: data[key].heights[height].width,
height: parseInt(height),
viewBox: `0 0 ${data[key].heights[height].width} ${height}`,
class: `octicon octicon-${key}`,
'aria-hidden': 'true'
}
}
// Function to return an SVG object
data[key].toSVG = function (options = {}) {
const {height, width} = options
const naturalHeight = closestNaturalHeight(Object.keys(data[key].heights), height || width || DEFAULT_HEIGHT)
return `<svg ${htmlAttributes(data[key], data[key].heights[naturalHeight].options, options)}>${
data[key].heights[naturalHeight].path
}</svg>`
}
}
// Import data into exports
module.exports = data
function closestNaturalHeight(naturalHeights, height) {
return naturalHeights
.map(naturalHeight => parseInt(naturalHeight, 10))
.reduce((acc, naturalHeight) => (naturalHeight <= height ? naturalHeight : acc), naturalHeights[0])
}