|
| 1 | +--- |
| 2 | +id: ember-string.prototype-extensions |
| 3 | +title: Deprecate String prototype extensions |
| 4 | +until: '4.0.0' |
| 5 | +since: 'Upcoming Features' |
| 6 | +--- |
| 7 | + |
| 8 | +Ember applications add methods to the `String` prototype by default, making you able to call `"Tomster".dasherize()`, for example. |
| 9 | +Calling [these methods](https://api.emberjs.com/ember/3.22/classes/String) on a string itself should be replaced with importing the function from `@ember/string`. |
| 10 | + |
| 11 | +Before: |
| 12 | + |
| 13 | +```js |
| 14 | +let mascot = "Empress Zoey"; |
| 15 | + |
| 16 | +mascot.camelize(); //=> empressZoey |
| 17 | +mascot.capitalize(); //=> "Empress Zoey" |
| 18 | +mascot.classify(); //=> "EmpressZoey" |
| 19 | +mascot.decamelize(); //=> "empress zoey" |
| 20 | +mascot.underscore(); //=> "empress_zoey" |
| 21 | +mascot.w(); //=> [ "Empress", "Zoey" ] |
| 22 | +``` |
| 23 | + |
| 24 | +After: |
| 25 | + |
| 26 | +```js |
| 27 | +import { |
| 28 | + camelize, |
| 29 | + capitalize, |
| 30 | + classify, |
| 31 | + decamelize, |
| 32 | + underscore, |
| 33 | + w, |
| 34 | +} from "@ember/string"; |
| 35 | + |
| 36 | +let mascot = "Empress Zoey"; |
| 37 | + |
| 38 | +camelize(mascot); //=> empressZoey |
| 39 | +capitalize(mascot); //=> "Empress Zoey" |
| 40 | +classify(mascot); //=> "EmpressZoey" |
| 41 | +decamelize(mascot); //=> "empress zoey" |
| 42 | +underscore(mascot); //=> "empress_zoey" |
| 43 | +w(mascot); //=> [ "Empress", "Zoey" ] |
| 44 | +``` |
| 45 | + |
| 46 | +You might want to replace these methods with another library, like [lodash](https://lodash.com/). |
| 47 | +Keep in mind that different libraries will behave in slightly different ways, so make sure any critical `String` transformations are thoroughly tested. |
| 48 | + |
| 49 | +You can also [disable String prototype extensions](https://guides.emberjs.com/release/configuring-ember/disabling-prototype-extensions/) by editing your environment file: |
| 50 | + |
| 51 | +```js |
| 52 | +// config/environment.js |
| 53 | +ENV = { |
| 54 | + EmberENV: { |
| 55 | + EXTEND_PROTOTYPES: { |
| 56 | + Date: false, |
| 57 | + String: false, |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
0 commit comments