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 canary] Update Ember.compare to use operators #13007

Merged
merged 1 commit into from
Feb 29, 2016
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
13 changes: 12 additions & 1 deletion packages/ember-runtime/lib/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,18 @@ export default function compare(v, w) {
return spaceship(v, w);

case 'string':
return spaceship(v.localeCompare(w), 0);
// We are comparing Strings using operators instead of `String#localeCompare`
// because of unexpected behavior for certain edge cases.
// For example `'Z'.localeCompare('a')` returns `1`.
//
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare#Description
if (v < w) {
return -1;
} else if (v === w) {
return 0;
}

return 1;

case 'array':
var vLen = v.length;
Expand Down
4 changes: 4 additions & 0 deletions packages/ember-runtime/tests/core/compare_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ QUnit.test('comparables should return values in the range of -1, 0, 1', function
equal(compare('a', negOne), 1, 'Second item comparable - returns -1 (negated)');
equal(compare('b', zero), 0, 'Second item comparable - returns 0 (negated)');
equal(compare('c', one), -1, 'Second item comparable - returns 1 (negated)');

equal(compare('A', 'Z'), -1, `'A' < 'Z' returns -1`);
equal(compare('Z', 'a'), -1, `'Z' < 'a' returns -1`);
equal(compare('a', 'z'), -1, `'a' < 'z' returns -1`);
});