Skip to content

Commit

Permalink
[BUGFIX release-1-13] Fix string classify normalizer
Browse files Browse the repository at this point in the history
Resolves #11326. If a piece of a name starts with dash (-), it will be
changed into an underscore (_).
  • Loading branch information
skeate committed Sep 29, 2015
1 parent 47464b6 commit 85d725c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 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
56 changes: 56 additions & 0 deletions packages/ember-runtime/tests/system/string/classify_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,59 @@ QUnit.test('classify namespaced dasherized string', function() {
deepEqual('private-docs/owner-invoice'.classify(), 'PrivateDocs/OwnerInvoice');
}
});

QUnit.test('classify prefixed dasherized string', function() {
deepEqual(classify('-view-registry'), '_ViewRegistry');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('-view-registry'.classify(), '_ViewRegistry');
}
});

QUnit.test('classify namespaced prefixed dasherized string', function() {
deepEqual(classify('components/-text-field'), 'Components/_TextField');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('components/-text-field'.classify(), 'Components/_TextField');
}
});

QUnit.test('does nothing with classified prefixed string', function() {
deepEqual(classify('_FooBar'), '_FooBar');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('_FooBar'.classify(), '_FooBar');
}
});

QUnit.test('classify underscore-prefixed underscored string', function() {
deepEqual(classify('_Foo_Bar'), '_FooBar');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('_Foo_Bar'.classify(), '_FooBar');
}
});

QUnit.test('classify underscore-prefixed dasherized string', function() {
deepEqual(classify('_Foo-Bar'), '_FooBar');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('_Foo-Bar'.classify(), '_FooBar');
}
});

QUnit.test('classify underscore-prefixed-namespaced underscore-prefixed string', function() {
deepEqual(classify('_foo/_bar'), '_Foo/_Bar');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('_foo/_bar'.classify(), '_Foo/_Bar');
}
});

QUnit.test('classify dash-prefixed-namespaced underscore-prefixed string', function() {
deepEqual(classify('-foo/_bar'), '_Foo/_Bar');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('-foo/_bar'.classify(), '_Foo/_Bar');
}
});

QUnit.test('classify dash-prefixed-namespaced dash-prefixed string', function() {
deepEqual(classify('-foo/-bar'), '_Foo/_Bar');
if (Ember.EXTEND_PROTOTYPES) {
deepEqual('-foo/-bar'.classify(), '_Foo/_Bar');
}
});

0 comments on commit 85d725c

Please sign in to comment.