index.js 1.9 KB

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