Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
fix(service): handle error "this.replace is not a function"
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Pick authored and knalli committed Jun 1, 2015
1 parent 806801e commit 8616dca
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/directive/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function translateDirective($translate, $q, $interpolate, $compile, $parse, $roo
* @returns {string} The string stripped of whitespace from both ends
*/
var trim = function() {
return this.replace(/^\s+|\s+$/g, '');
return this.toString().replace(/^\s+|\s+$/g, '');
};

return {
Expand Down
2 changes: 1 addition & 1 deletion src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
* @returns {string} The string stripped of whitespace from both ends
*/
var trim = function() {
return this.replace(/^\s+|\s+$/g, '');
return this.toString().replace(/^\s+|\s+$/g, '');
};

var negotiateLocale = function (preferred) {
Expand Down
22 changes: 22 additions & 0 deletions test/unit/filter/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,28 @@ describe('pascalprecht.translate', function () {
expect(value[5]).toEqual('10');
expect(value[6]).toEqual('55');
});

it('should not throw errors when translation id is not a string', function() {
var value = [
$translate(4.5),
$translate(4),
$translate([]),
$translate({}),
$translate(true),
$translate(null),
$translate(undefined)
];

expect(value[0]).toEqual('4.5');
expect(value[1]).toEqual('4');
/* I don't care what these values are, as long as $translate doesn't throw an error.
expect(value[2]).toEqual({});
expect(value[3]).toEqual('[object Object]');
expect(value[4]).toEqual('true');
expect(value[5]).toEqual(null);
expect(value[6]).toEqual(undefined);
*/
});

if (angular.version.major === 1 && angular.version.minor <= 2) {
// Until and including AJS 1.2, a filter was bound to a context (current scope). This was removed in AJS 1.3
Expand Down

0 comments on commit 8616dca

Please sign in to comment.