index.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. var data = require('./build/data.json')
  2. Object.keys(data).forEach(function(key) {
  3. // Returns a string representation of html attributes
  4. var htmlAttributes = function(icon, options) {
  5. var attributes = []
  6. var attrObj = Object.assign({}, data[key].options, options)
  7. // If the user passed in options
  8. if (options) {
  9. // If any of the width or height is passed in
  10. if(options["width"] || options["height"]) {
  11. attrObj["width"] = options["width"] ? options["width"] : (parseInt(options["height"]) * data[key].options["width"] / data[key].options["height"])
  12. attrObj["height"] = options["height"] ? options["height"] : (parseInt(options["width"]) * data[key].options["height"] / data[key].options["width"])
  13. }
  14. // If the user passed in class
  15. if (options["class"]) {
  16. attrObj["class"] = "octicon octicon-" + key + " " + options["class"]
  17. attrObj["class"].trim()
  18. }
  19. // If the user passed in aria-label
  20. if (options["aria-label"]) {
  21. attrObj["aria-label"] = options["aria-label"]
  22. attrObj["role"] = "img"
  23. // Un-hide the icon
  24. delete attrObj["aria-hidden"]
  25. }
  26. }
  27. Object.keys(attrObj).forEach(function(option) {
  28. attributes.push(option + "=\"" + attrObj[option] + "\"")
  29. })
  30. return attributes.join(" ").trim()
  31. }
  32. // Set the symbol for easy access
  33. data[key].symbol = key
  34. // Set all the default options
  35. data[key].options = {
  36. "version": "1.1",
  37. "width": data[key].width,
  38. "height": data[key].height,
  39. "viewBox": "0 0 " + data[key].width + " " + data[key].height,
  40. "class": "octicon octicon-" + key,
  41. "aria-hidden": "true"
  42. }
  43. // Function to return an SVG object
  44. data[key].toSVG = function(options) {
  45. return "<svg " + htmlAttributes(data[key], options) + ">" + data[key].path + "</svg>"
  46. }
  47. // Function to return an SVG object with a use, assuming you use the svg sprite
  48. data[key].toSVGUse = function(options) {
  49. return "<svg " + htmlAttributes(data[key], options) + "><use xlink:href=\"#" + key + "\" /></svg>"
  50. }
  51. })
  52. // Import data into exports
  53. module.exports = data