Просмотр исходного кода

support slashes in component names

Evan You 11 лет назад
Родитель
Сommit
8a02eb1e31
4 измененных файлов с 39 добавлено и 13 удалено
  1. 1 1
      src/api/child.js
  2. 1 1
      src/api/global.js
  3. 30 7
      src/util/lang.js
  4. 7 4
      test/unit/specs/util/lang_spec.js

+ 1 - 1
src/api/child.js

@@ -25,7 +25,7 @@ exports.$addChild = function (opts, BaseCtor) {
     if (!ChildVue) {
       var optionName = BaseCtor.options.name
       var className = optionName
-        ? _.camelize(optionName, true)
+        ? _.classify(optionName)
         : 'VueComponent'
       ChildVue = new Function(
         'return function ' + className + ' (options) {' +

+ 1 - 1
src/api/global.js

@@ -72,7 +72,7 @@ exports.extend = function (extendOptions) {
 
 function createClass (name) {
   return new Function(
-    'return function ' + _.camelize(name, true) +
+    'return function ' + _.classify(name) +
     ' (options) { this._init(options) }'
   )()
 }

+ 30 - 7
src/util/lang.js

@@ -56,6 +56,17 @@ exports.stripQuotes = function (str) {
     : false
 }
 
+/**
+ * Replace helper
+ *
+ * @param {String} _ - matched delimiter
+ * @param {String} c - matched char
+ * @return {String}
+ */
+function toUpper (_, c) {
+  return c ? c.toUpperCase () : ''
+}
+
 /**
  * Camelize a hyphen-delmited string.
  *
@@ -63,14 +74,26 @@ exports.stripQuotes = function (str) {
  * @return {String}
  */
 
-var camelRE = /[-_](\w)/g
-var capitalCamelRE = /(?:^|[-_])(\w)/g
+var camelRE = /-(\w)/g
+exports.camelize = function (str) {
+  return str.replace(camelRE, toUpper)
+}
 
-exports.camelize = function (str, cap) {
-  var RE = cap ? capitalCamelRE : camelRE
-  return str.replace(RE, function (_, c) {
-    return c ? c.toUpperCase () : ''
-  })
+/**
+ * Converts hyphen/underscore/slash delimitered names into
+ * camelized classNames.
+ *
+ * e.g. my-component => MyComponent
+ *      some_else    => SomeElse
+ *      some/comp    => SomeComp
+ *
+ * @param {String} str
+ * @return {String}
+ */
+
+var classifyRE = /(?:^|[-_\/])(\w)/g
+exports.classify = function (str) {
+  return str.replace(classifyRE, toUpper)
 }
 
 /**

+ 7 - 4
test/unit/specs/util/lang_spec.js

@@ -27,10 +27,13 @@ describe('Util - Language Enhancement', function () {
   it('camelize', function () {
     expect(_.camelize('abc')).toBe('abc')
     expect(_.camelize('some-long-name')).toBe('someLongName')
-    expect(_.camelize('what_about_this')).toBe('whatAboutThis')
-    expect(_.camelize('abc', true)).toBe('Abc')
-    expect(_.camelize('some-long-name', true)).toBe('SomeLongName')
-    expect(_.camelize('what_about_this', true)).toBe('WhatAboutThis')
+  })
+
+  it('classify', function () {
+    expect(_.classify('abc')).toBe('Abc')
+    expect(_.classify('some-long-name')).toBe('SomeLongName')
+    expect(_.classify('what_about_this')).toBe('WhatAboutThis')
+    expect(_.classify('how/about/that')).toBe('HowAboutThat')
   })
 
   it('bind', function () {