Skip to content

Commit

Permalink
Merge pull request #12383 from skeate/master
Browse files Browse the repository at this point in the history
[BUGFIX release-1-13] Fix string classify normalizer
  • Loading branch information
rwjblue committed Sep 29, 2015
2 parents 145bc36 + 9f0f28d commit aefcf14
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 52 deletions.
22 changes: 17 additions & 5 deletions packages/ember-runtime/lib/system/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,25 @@ var CAMELIZE_CACHE = new Cache(1000, function(key) {
});
});

var STRING_CLASSIFY_REGEXP_1 = (/(\-|\_|\.|\s)+(.)?/g);
var STRING_CLASSIFY_REGEXP_2 = (/(^|\/|\.)([a-z])/g);
var STRING_CLASSIFY_REGEXP_1 = (/^(\-|_)+(.)?/);
var STRING_CLASSIFY_REGEXP_2 = (/(.)(\-|\_|\.|\s)+(.)?/g);
var STRING_CLASSIFY_REGEXP_3 = (/(^|\/|\.)([a-z])/g);

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) {
var replace1 = function(match, separator, chr) {
return chr ? ('_' + chr.toUpperCase()) : '';
};
var replace2 = function(match, initialChar, separator, chr) {
return initialChar + (chr ? chr.toUpperCase() : '');
};
var parts = str.split('/');
for (var i = 0, len = parts.length; i < len; i++) {
parts[i] = parts[i]
.replace(STRING_CLASSIFY_REGEXP_1, replace1)
.replace(STRING_CLASSIFY_REGEXP_2, replace2);
}
return parts.join('/')
.replace(STRING_CLASSIFY_REGEXP_3, function(match, separator, chr) {
return match.toUpperCase();
});
});
Expand Down
70 changes: 23 additions & 47 deletions packages/ember-runtime/tests/system/string/classify_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,27 @@ if (!Ember.EXTEND_PROTOTYPES && !Ember.EXTEND_PROTOTYPES.String) {
});
}

QUnit.test('classify normal string', function() {
deepEqual(classify('my favorite items'), 'MyFavoriteItems');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('my favorite items'.classify(), 'MyFavoriteItems');
}
});

QUnit.test('classify dasherized string', function() {
deepEqual(classify('css-class-name'), 'CssClassName');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('css-class-name'.classify(), 'CssClassName');
}
});

QUnit.test('classify underscored string', function() {
deepEqual(classify('action_name'), 'ActionName');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('action_name'.classify(), 'ActionName');
}
});

QUnit.test('does nothing with classified string', function() {
deepEqual(classify('InnerHTML'), 'InnerHTML');
if (Ember.EXTEND_PROTOTYPES) {
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');
}
});
function test(given, expected, description) {
QUnit.test(description, function() {
deepEqual(classify(given), expected);
if (Ember.EXTEND_PROTOTYPES) {
deepEqual(given.classify(), expected);
}
});
}

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');
}
});
test('my favorite items', 'MyFavoriteItems', 'classify normal string');
test('css-class-name', 'CssClassName', 'classify dasherized string');
test('action_name', 'ActionName', 'classify underscored string');
test('privateDocs/ownerInvoice', 'PrivateDocs/OwnerInvoice', 'classify namespaced camelized string');
test('private_docs/owner_invoice', 'PrivateDocs/OwnerInvoice', 'classify namespaced underscored string');
test('private-docs/owner-invoice', 'PrivateDocs/OwnerInvoice', 'classify namespaced dasherized string');
test('-view-registry', '_ViewRegistry', 'classify prefixed dasherized string');
test('components/-text-field', 'Components/_TextField', 'classify namespaced prefixed dasherized string');
test('_Foo_Bar', '_FooBar', 'classify underscore-prefixed underscored string');
test('_Foo-Bar', '_FooBar', 'classify underscore-prefixed dasherized string');
test('_foo/_bar', '_Foo/_Bar', 'classify underscore-prefixed-namespaced underscore-prefixed string');
test('-foo/_bar', '_Foo/_Bar', 'classify dash-prefixed-namespaced underscore-prefixed string');
test('-foo/-bar', '_Foo/_Bar', 'classify dash-prefixed-namespaced dash-prefixed string');
test('InnerHTML', 'InnerHTML', 'does nothing with classified string');
test('_FooBar', '_FooBar', 'does nothing with classified prefixed string');

0 comments on commit aefcf14

Please sign in to comment.