source: node_modules/property-information/find.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.3 KB
RevLine 
[d24f17c]1'use strict'
2
3var normalize = require('./normalize')
4var DefinedInfo = require('./lib/util/defined-info')
5var Info = require('./lib/util/info')
6
7var data = 'data'
8
9module.exports = find
10
11var valid = /^data[-\w.:]+$/i
12var dash = /-[a-z]/g
13var cap = /[A-Z]/g
14
15function find(schema, value) {
16 var normal = normalize(value)
17 var prop = value
18 var Type = Info
19
20 if (normal in schema.normal) {
21 return schema.property[schema.normal[normal]]
22 }
23
24 if (normal.length > 4 && normal.slice(0, 4) === data && valid.test(value)) {
25 // Attribute or property.
26 if (value.charAt(4) === '-') {
27 prop = datasetToProperty(value)
28 } else {
29 value = datasetToAttribute(value)
30 }
31
32 Type = DefinedInfo
33 }
34
35 return new Type(prop, value)
36}
37
38function datasetToProperty(attribute) {
39 var value = attribute.slice(5).replace(dash, camelcase)
40 return data + value.charAt(0).toUpperCase() + value.slice(1)
41}
42
43function datasetToAttribute(property) {
44 var value = property.slice(4)
45
46 if (dash.test(value)) {
47 return property
48 }
49
50 value = value.replace(cap, kebab)
51
52 if (value.charAt(0) !== '-') {
53 value = '-' + value
54 }
55
56 return data + value
57}
58
59function kebab($0) {
60 return '-' + $0.toLowerCase()
61}
62
63function camelcase($0) {
64 return $0.charAt(1).toUpperCase()
65}
Note: See TracBrowser for help on using the repository browser.