-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat($interpolate): use custom toString member if present #8350
Conversation
Thanks for the PR! Please check the items below to help us merge this faster. See the contributing docs for more information.
If you need to make changes to your pull request, you can update the commit with Thanks again for your help! |
@@ -272,7 +272,11 @@ function $InterpolateProvider() { | |||
break; | |||
} | |||
default: { | |||
value = toJson(value); | |||
if(typeof value.toString !== 'undefined' && value != null && value.toString !== Object.prototype.toString && !isArray(value)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have an isFunction()
method:
if (isFunction(value.toString)) {
value = value.toString();
} else {
value = toJson(value);
}
@danielkrainas - I am not sure if this will be accepted but you would help your case by including unit tests and also
Adding to the IceBox for the moment as that is where the original issue is to be found. |
BREAKING CHANGE: use custom toString implementation when present on object value types. Behavior is consistent with implementations found in other languages such as Ruby, Python, and CoffeeScript. http://rubymonk.com/learning/books/1-ruby-primer/chapters/5-strings/lessons/31-string-basics https://docs.python.org/2/library/stdtypes.html#string-formatting-operations http://coffeescriptcookbook.com/chapters/strings/interpolation Closes angular#7317
@petebacondarwin I've included a couple tests and added links to relevant sources. |
02dc2aa
to
fd2d6c0
Compare
cad9560
to
f294244
Compare
e8dc429
to
e83fab9
Compare
4dd5a20
to
998c61c
Compare
This behavior is consistent with implementations found in other languages such as Ruby, Python, and CoffeeScript. http://rubymonk.com/learning/books/1-ruby-primer/chapters/5-strings/lessons/31-string-basics https://docs.python.org/2/library/stdtypes.html#string-formatting-operations http://coffeescriptcookbook.com/chapters/strings/interpolation Fixes angular#7317 Closes angular#8350 BREAKING CHANGE: When converting values to strings, interpolation now uses a custom toString() function on objects that are not numbers, dates or arrays (custom means that the `toString()` function is not the same as `Object.prototype.toString()`). Otherwise, interpolation uses JSON.stringify() as usual. Should you have a custom toString() function but still want the output of JSON.stringify(), migrate as shown in the following examples: Before: ```html <span>{{myObject}}</span> ``` After - use the `json` filter to stringify the object: ```html <span>{{myObject | json}}</span> ```
This behavior is consistent with implementations found in other languages such as Ruby, Python, and CoffeeScript. http://rubymonk.com/learning/books/1-ruby-primer/chapters/5-strings/lessons/31-string-basics https://docs.python.org/2/library/stdtypes.html#string-formatting-operations http://coffeescriptcookbook.com/chapters/strings/interpolation Fixes angular#7317 Closes angular#8350 BREAKING CHANGE: When converting values to strings, interpolation now uses a custom toString() function on objects that are not numbers or arrays (custom means that the `toString()` function is not the same as `Object.prototype.toString()`). Otherwise, interpolation uses JSON.stringify() as usual. Should you have a custom toString() function but still want the output of JSON.stringify(), migrate as shown in the following examples: Before: ```html <span>{{myObject}}</span> ``` After - use the `json` filter to stringify the object: ```html <span>{{myObject | json}}</span> ``` fixup
Except on Numbers, Dates and Arrays. Thanks to @danielkrainas for the initial implementation of this feature. This behavior is consistent with implementations found in other languages such as Ruby, Python, and CoffeeScript. http://rubymonk.com/learning/books/1-ruby-primer/chapters/5-strings/lessons/31-string-basics https://docs.python.org/2/library/stdtypes.html#string-formatting-operations http://coffeescriptcookbook.com/chapters/strings/interpolation The commit also exposes a private $$stringify method on the angular global, so that ngMessageFormat can use the same logic without duplicating it. Fixes angular#7317 Closes angular#8350 BREAKING CHANGE: When converting values to strings, interpolation now uses a custom toString() function on objects that are not Number, Array or Date (custom means that the `toString()` function is not the same as `Object.prototype.toString()`). Otherwise, interpolation uses JSON.stringify() as usual. Should you have a custom toString() function but still want the output of JSON.stringify(), migrate as shown in the following examples: Before: ```html <span>{{myObject}}</span> ``` After - use the `json` filter to stringify the object: ```html <span>{{myObject | json}}</span> ```
Except on Numbers, Dates and Arrays. Thanks to @danielkrainas for the initial implementation of this feature. This behavior is consistent with implementations found in other languages such as Ruby, Python, and CoffeeScript. http://rubymonk.com/learning/books/1-ruby-primer/chapters/5-strings/lessons/31-string-basics https://docs.python.org/2/library/stdtypes.html#string-formatting-operations http://coffeescriptcookbook.com/chapters/strings/interpolation The commit also exposes a private $$stringify method on the angular global, so that ngMessageFormat can use the same logic without duplicating it. Fixes angular#7317 Closes angular#8350 BREAKING CHANGE: When converting values to strings, interpolation now uses a custom toString() function on objects that are not Number, Array or Date (custom means that the `toString()` function is not the same as `Object.prototype.toString()`). Otherwise, interpolation uses JSON.stringify() as usual. Should you have a custom toString() function but still want the output of JSON.stringify(), migrate as shown in the following examples: Before: ```html <span>{{myObject}}</span> ``` After - use the `json` filter to stringify the object: ```html <span>{{myObject | json}}</span> ```
Except on Numbers, Dates and Arrays. Thanks to @danielkrainas for the initial implementation of this feature. This behavior is consistent with implementations found in other languages such as Ruby, Python, and CoffeeScript. http://rubymonk.com/learning/books/1-ruby-primer/chapters/5-strings/lessons/31-string-basics https://docs.python.org/2/library/stdtypes.html#string-formatting-operations http://coffeescriptcookbook.com/chapters/strings/interpolation The commit also exposes a private $$stringify method on the angular global, so that ngMessageFormat can use the same logic without duplicating it. Fixes angular#7317 Closes angular#8350 BREAKING CHANGE: When converting values to strings, interpolation now uses a custom toString() function on objects that are not Number, Array or Date (custom means that the `toString()` function is not the same as `Object.prototype.toString()`). Otherwise, interpolation uses JSON.stringify() as usual. Should you have a custom toString() function but still want the output of JSON.stringify(), migrate as shown in the following examples: Before: ```html <span>{{myObject}}</span> ``` After - use the `json` filter to stringify the object: ```html <span>{{myObject | json}}</span> ```
Except on Numbers, Dates and Arrays. Thanks to @danielkrainas for the initial implementation of this feature. This behavior is consistent with implementations found in other languages such as Ruby, Python, and CoffeeScript. http://rubymonk.com/learning/books/1-ruby-primer/chapters/5-strings/lessons/31-string-basics https://docs.python.org/2/library/stdtypes.html#string-formatting-operations http://coffeescriptcookbook.com/chapters/strings/interpolation The commit also exposes a private $$stringify method on the angular global, so that ngMessageFormat can use the same logic without duplicating it. Fixes angular#7317 Closes angular#8350 Fixes angular#11406 BREAKING CHANGE: When converting values to strings, interpolation now uses a custom toString() function on objects that are not Number, Array or Date (custom means that the `toString` function is not the same as `Object.prototype.toString`). Otherwise, interpolation uses JSON.stringify() as usual. Should you have a custom toString() function but still want the output of JSON.stringify(), migrate as shown in the following examples: Before: ```html <span>{{myObject}}</span> ``` After - use the `json` filter to stringify the object: ```html <span>{{myObject | json}}</span> ```
BREAKING CHANGE: use custom toString implementation when present on object value types.
Behavior is consistent with other implementations.
Closes #7317