Issues (117)

lib/handlebars/javascript.js (1 issue)

1 1
var format     = require('string-template');
2 1
var handlebars = require('handlebars');
3 1
var hljs       = require('highlight.js');
4 1
var kebab      = require('kebab-case');
5
6
/**
7
 * Formats a JavaScript plugin constructor name to look like this:
8
 * ```js
9
 * var elem = new Foundation.Plugin(element, options);
10
 * ```
11
 *
12
 * @param {string} name - Name of the plugin.
13
 * @returns {string} A formatted code sample.
14
 */
15 1
handlebars.registerHelper('writeJsConstructor', function(name) {
16 1
  var str = 'var elem = new YourApp.'+name+'(element, options);';
17 1
  return new handlebars.SafeString(hljs.highlight('js', str).value);
18
})
19
20
/**
21
 * Formats a JavaScript plugin method to look like this:
22
 * ```js
23
 * $('#element').foundation('method', param1, param2);
24
 * ```
25
 *
26
 * @param {object} method - Method data, taken from a JSDoc object.
27
 * @returns {string} A formatted code sample.
28
 */
29 1
handlebars.registerHelper('writeJsFunction', function(method) {
30 2
  var str = '$(\'#element\').your_app(\'' + method.name;
31 4
  if (method.params && method.params.length) {
32 1
    str += '\', ';
33 2
    str += (method.params || []).map(function(val) {
34 2
      return val.name;
35
    }).join(', ');
36 1
    str += ');';
37
  }
38
  else {
39 1
    str += '\');'
40
  }
41
42 2
  return new handlebars.SafeString(hljs.highlight('js', str).value);
43
});
44
45
/**
46
 * Convert JSDoc's module definition to a filename.
47
 * @param {string} value - Module name to convert.
48
 * @returns {string} A JavaScript filename.
49
 */
50 1
handlebars.registerHelper('formatJsModule', function(value) {
51 1
  return value.replace('module:', '') + '.js';
52
});
53
54
/**
55
 * Format a JavaScript plugin option by converting it to an HTML data attribute.
56
 * @param {string} name - Plugin option name.
57
 * @returns {string} Plugin option as an HTML data attribute.
58
 */
59 1
handlebars.registerHelper('formatJsOptionName', function(name) {
60 1
  return 'data-' + kebab(name);
61
});
62
63
/**
64
 * Formats JsDoc "type" definitions to read "x or y or z"
65
 * @param {string[]} types - Array of types.
66
 * @returns {string} A formatted string.
67
 */
68 1
handlebars.registerHelper('formatJsOptionTypes', function(types) {
69 4
  if (typeof types === 'undefined' || typeof types.names == 'undefined') return '';
70 1
  var types = types.names;
71 1
  var output = '';
72
73 1
  for (var i in types) {
0 ignored issues
show
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
74 3
    output += types[i] + ' or ';
75
  }
76
77 1
  return output.slice(0, -4);
78
});
79
80
/**
81
 * Format a JavaScript plugin option's default value:
82
 *   - For string values, remove the quotes on either side.
83
 *   - For all other values, return as-is.
84
 * @param {string} value - Value to process.
85
 * @returns {string} Processed value.
86
 */
87 1
handlebars.registerHelper('formatJsOptionValue', function(name) {
88 4
  if (typeof name === 'undefined') return '';
89
90
  var output;
91
92 3
  if (name.match(/^('|").+('|")$/)) {
93 2
    output = name.slice(1, -1);
94
  }
95
  else {
96 1
    output = name;
97
  }
98
99 3
  return new handlebars.SafeString(output);
100
});
101
102
/**
103
 * Format the name of a JavaScript event. This converts JSDoc's slightly convoluted syntax to match our namespaced event names: `event.zf.pluginname`.
104
 * @todo Make this unnecessary by using the actual event names in JSDoc annotations.
105
 * @param {string} name - Event name.
106
 * @param {string} title - Plugin name, used for the event namespace.
107
 * @returns {string} Formatted event name.
108
 */
109 1
handlebars.registerHelper('formatJsEventName', function(name, title) {
110 2
  title = title.charAt(0).toLowerCase() + title.slice(1);
111 2
  return format('{0}.zf.{1}', [name, title.replace(/(-| )/, '')]);
112
});
113