Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Namespaced strings conversions #10980

Merged
merged 1 commit into from
May 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions packages/ember-runtime/lib/system/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,50 @@ var STRING_DASHERIZE_CACHE = new Cache(1000, function(key) {
return decamelize(key).replace(STRING_DASHERIZE_REGEXP, '-');
});

var STRING_CAMELIZE_REGEXP_1 = (/(\-|\_|\.|\s)+(.)?/g);
var STRING_CAMELIZE_REGEXP_2 = (/(^|\/)([A-Z])/g);

var CAMELIZE_CACHE = new Cache(1000, function(key) {
return key.replace(STRING_CAMELIZE_REGEXP, function(match, separator, chr) {
return key.replace(STRING_CAMELIZE_REGEXP_1, function(match, separator, chr) {
return chr ? chr.toUpperCase() : '';
}).replace(/^([A-Z])/, function(match, separator, chr) {
}).replace(STRING_CAMELIZE_REGEXP_2, function(match, separator, chr) {
return match.toLowerCase();
});
});

var CLASSIFY_CACHE = new Cache(1000, function(str) {
var parts = str.split(".");
var out = [];
var STRING_CLASSIFY_REGEXP_1 = (/(\-|\_|\.|\s)+(.)?/g);
var STRING_CLASSIFY_REGEXP_2 = (/(^|\/|\.)([a-z])/g);

for (var i=0, l=parts.length; i<l; i++) {
var camelized = camelize(parts[i]);
out.push(camelized.charAt(0).toUpperCase() + camelized.substr(1));
}

return out.join(".");
var CLASSIFY_CACHE = new Cache(1000, function(str) {
return str.replace(STRING_CLASSIFY_REGEXP_1, function(match, separator, chr) {
return chr ? chr.toUpperCase() : '';
}).replace(STRING_CLASSIFY_REGEXP_2, function(match, separator, chr) {
return match.toUpperCase();
});
});

var STRING_UNDERSCORE_REGEXP_1 = (/([a-z\d])([A-Z]+)/g);
var STRING_UNDERSCORE_REGEXP_2 = (/\-|\s+/g);

var UNDERSCORE_CACHE = new Cache(1000, function(str) {
return str.replace(STRING_UNDERSCORE_REGEXP_1, '$1_$2').
replace(STRING_UNDERSCORE_REGEXP_2, '_').toLowerCase();
});

var STRING_CAPITALIZE_REGEXP = (/(^|\/)([a-z])/g);

var CAPITALIZE_CACHE = new Cache(1000, function(str) {
return str.charAt(0).toUpperCase() + str.substr(1);
return str.replace(STRING_CAPITALIZE_REGEXP, function(match, separator, chr) {
return match.toUpperCase();
});
});

var STRING_DECAMELIZE_REGEXP = (/([a-z\d])([A-Z])/g);

var DECAMELIZE_CACHE = new Cache(1000, function(str) {
return str.replace(STRING_DECAMELIZE_REGEXP, '$1_$2').toLowerCase();
});

var STRING_DECAMELIZE_REGEXP = (/([a-z\d])([A-Z])/g);
var STRING_CAMELIZE_REGEXP = (/(\-|_|\.|\s)+(.)?/g);
var STRING_UNDERSCORE_REGEXP_1 = (/([a-z\d])([A-Z]+)/g);
var STRING_UNDERSCORE_REGEXP_2 = (/\-|\s+/g);

function fmt(str, formats) {
var cachedFormats = formats;

Expand Down Expand Up @@ -225,6 +231,7 @@ export default {
'action_name'.dasherize(); // 'action-name'
'css-class-name'.dasherize(); // 'css-class-name'
'my favorite items'.dasherize(); // 'my-favorite-items'
'privateDocs/ownerInvoice'.dasherize(); // 'private-docs/owner-invoice'
```

@method dasherize
Expand All @@ -242,6 +249,7 @@ export default {
'css-class-name'.camelize(); // 'cssClassName'
'my favorite items'.camelize(); // 'myFavoriteItems'
'My Favorite Items'.camelize(); // 'myFavoriteItems'
'private-docs/owner-invoice'.camelize(); // 'privateDocs/ownerInvoice'
```

@method camelize
Expand All @@ -258,6 +266,7 @@ export default {
'action_name'.classify(); // 'ActionName'
'css-class-name'.classify(); // 'CssClassName'
'my favorite items'.classify(); // 'MyFavoriteItems'
'private-docs/owner-invoice'.classify(); // 'PrivateDocs/OwnerInvoice'
```

@method classify
Expand All @@ -275,6 +284,7 @@ export default {
'action_name'.underscore(); // 'action_name'
'css-class-name'.underscore(); // 'css_class_name'
'my favorite items'.underscore(); // 'my_favorite_items'
'privateDocs/ownerInvoice'.underscore(); // 'private_docs/owner_invoice'
```

@method underscore
Expand All @@ -291,6 +301,7 @@ export default {
'action_name'.capitalize() // 'Action_name'
'css-class-name'.capitalize() // 'Css-class-name'
'my favorite items'.capitalize() // 'My favorite items'
'privateDocs/ownerInvoice'.capitalize(); // 'PrivateDocs/OwnerInvoice'
```

@method capitalize
Expand Down
21 changes: 21 additions & 0 deletions packages/ember-runtime/tests/system/string/camelize_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,24 @@ QUnit.test("does nothing with camelcased string", function() {
deepEqual('innerHTML'.camelize(), 'innerHTML');
}
});

QUnit.test("camelize namespaced classified string", function() {
deepEqual(camelize('PrivateDocs/OwnerInvoice'), 'privateDocs/ownerInvoice');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('PrivateDocs/OwnerInvoice'.camelize(), 'privateDocs/ownerInvoice');
}
});

QUnit.test("camelize namespaced underscored string", function() {
deepEqual(camelize('private_docs/owner_invoice'), 'privateDocs/ownerInvoice');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('private_docs/owner_invoice'.camelize(), 'privateDocs/ownerInvoice');
}
});

QUnit.test("camelize namespaced dasherized string", function() {
deepEqual(camelize('private-docs/owner-invoice'), 'privateDocs/ownerInvoice');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('private-docs/owner-invoice'.camelize(), 'privateDocs/ownerInvoice');
}
});
21 changes: 21 additions & 0 deletions packages/ember-runtime/tests/system/string/capitalize_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,24 @@ QUnit.test("does nothing with capitalized string", function() {
deepEqual('Capitalized string'.capitalize(), 'Capitalized string');
}
});

QUnit.test("capitalize namespaced camelized string", function() {
deepEqual(capitalize('privateDocs/ownerInvoice'), 'PrivateDocs/OwnerInvoice');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('privateDocs/ownerInvoice'.capitalize(), 'PrivateDocs/OwnerInvoice');
}
});

QUnit.test("capitalize namespaced underscored string", function() {
deepEqual(capitalize('private_docs/owner_invoice'), 'Private_docs/Owner_invoice');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('private_docs/owner_invoice'.capitalize(), 'Private_docs/Owner_invoice');
}
});

QUnit.test("capitalize namespaced dasherized string", function() {
deepEqual(capitalize('private-docs/owner-invoice'), 'Private-docs/Owner-invoice');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('private-docs/owner-invoice'.capitalize(), 'Private-docs/Owner-invoice');
}
});
21 changes: 21 additions & 0 deletions packages/ember-runtime/tests/system/string/classify_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,24 @@ QUnit.test("does nothing with classified string", function() {
deepEqual('InnerHTML'.classify(), 'InnerHTML');
}
});

QUnit.test("classify namespaced camelized string", function() {
deepEqual(classify('privateDocs/ownerInvoice'), 'PrivateDocs/OwnerInvoice');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('privateDocs/ownerInvoice'.classify(), 'PrivateDocs/OwnerInvoice');
}
});

QUnit.test("classify namespaced underscored string", function() {
deepEqual(classify('private_docs/owner_invoice'), 'PrivateDocs/OwnerInvoice');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('private_docs/owner_invoice'.classify(), 'PrivateDocs/OwnerInvoice');
}
});

QUnit.test("classify namespaced dasherized string", function() {
deepEqual(classify('private-docs/owner-invoice'), 'PrivateDocs/OwnerInvoice');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('private-docs/owner-invoice'.classify(), 'PrivateDocs/OwnerInvoice');
}
});
21 changes: 21 additions & 0 deletions packages/ember-runtime/tests/system/string/dasherize_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,24 @@ QUnit.test("dasherize string that is the property name of Object.prototype", fun
deepEqual('toString'.dasherize(), 'to-string');
}
});

QUnit.test("dasherize namespaced classified string", function() {
deepEqual(dasherize('PrivateDocs/OwnerInvoice'), 'private-docs/owner-invoice');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('PrivateDocs/OwnerInvoice'.dasherize(), 'private-docs/owner-invoice');
}
});

QUnit.test("dasherize namespaced camelized string", function() {
deepEqual(dasherize('privateDocs/ownerInvoice'), 'private-docs/owner-invoice');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('privateDocs/ownerInvoice'.dasherize(), 'private-docs/owner-invoice');
}
});

QUnit.test("dasherize namespaced underscored string", function() {
deepEqual(dasherize('private_docs/owner_invoice'), 'private-docs/owner-invoice');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('private_docs/owner_invoice'.dasherize(), 'private-docs/owner-invoice');
}
});
14 changes: 14 additions & 0 deletions packages/ember-runtime/tests/system/string/decamelize_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,17 @@ QUnit.test("decamelizes strings with numbers", function() {
deepEqual('size160Url'.decamelize(), 'size160_url');
}
});

QUnit.test("decamelize namespaced classified string", function() {
deepEqual(decamelize('PrivateDocs/OwnerInvoice'), 'private_docs/owner_invoice');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('PrivateDocs/OwnerInvoice'.decamelize(), 'private_docs/owner_invoice');
}
});

QUnit.test("decamelize namespaced camelized string", function() {
deepEqual(decamelize('privateDocs/ownerInvoice'), 'private_docs/owner_invoice');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('privateDocs/ownerInvoice'.decamelize(), 'private_docs/owner_invoice');
}
});
21 changes: 21 additions & 0 deletions packages/ember-runtime/tests/system/string/underscore_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,24 @@ QUnit.test("with camelcased string", function() {
deepEqual('innerHTML'.underscore(), 'inner_html');
}
});

QUnit.test("underscore namespaced classified string", function() {
deepEqual(underscore('PrivateDocs/OwnerInvoice'), 'private_docs/owner_invoice');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('PrivateDocs/OwnerInvoice'.underscore(), 'private_docs/owner_invoice');
}
});

QUnit.test("underscore namespaced camelized string", function() {
deepEqual(underscore('privateDocs/ownerInvoice'), 'private_docs/owner_invoice');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('privateDocs/ownerInvoice'.underscore(), 'private_docs/owner_invoice');
}
});

QUnit.test("underscore namespaced dasherized string", function() {
deepEqual(underscore('private-docs/owner-invoice'), 'private_docs/owner_invoice');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('private-docs/owner-invoice'.underscore(), 'private_docs/owner_invoice');
}
});