find.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict'
  2. var normalize = require('./normalize')
  3. var DefinedInfo = require('./lib/util/defined-info')
  4. var Info = require('./lib/util/info')
  5. var data = 'data'
  6. module.exports = find
  7. var valid = /^data[-a-z0-9.:_]+$/i
  8. var dash = /-[a-z]/g
  9. var cap = /[A-Z]/g
  10. function find(schema, value) {
  11. var normal = normalize(value)
  12. var prop = value
  13. var Type = Info
  14. if (normal in schema.normal) {
  15. return schema.property[schema.normal[normal]]
  16. }
  17. if (normal.length > 4 && normal.slice(0, 4) === data && valid.test(value)) {
  18. // Attribute or property.
  19. if (value.charAt(4) === '-') {
  20. prop = datasetToProperty(value)
  21. } else {
  22. value = datasetToAttribute(value)
  23. }
  24. Type = DefinedInfo
  25. }
  26. return new Type(prop, value)
  27. }
  28. function datasetToProperty(attribute) {
  29. var value = attribute.slice(5).replace(dash, camelcase)
  30. return data + value.charAt(0).toUpperCase() + value.slice(1)
  31. }
  32. function datasetToAttribute(property) {
  33. var value = property.slice(4)
  34. if (dash.test(value)) {
  35. return property
  36. }
  37. value = value.replace(cap, kebab)
  38. if (value.charAt(0) !== '-') {
  39. value = '-' + value
  40. }
  41. return data + value
  42. }
  43. function kebab($0) {
  44. return '-' + $0.toLowerCase()
  45. }
  46. function camelcase($0) {
  47. return $0.charAt(1).toUpperCase()
  48. }