Skip to content

Commit 031b2d5

Browse files
committed
Add Ember Utils deprecation
1 parent fb67e7b commit 031b2d5

File tree

1 file changed

+210
-0
lines changed

1 file changed

+210
-0
lines changed
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
---
2+
title: "@ember/utils package"
3+
until: 7.0.0
4+
since: 6.8.0
5+
---
6+
7+
The `@ember/utils` package is now deprecated and will be removed in Ember 7.0.
8+
9+
## What is being deprecated
10+
11+
The entire `@ember/utils` package, which includes utility functions such as:
12+
13+
- `compare`
14+
- `isBlank`
15+
- `isEmpty`
16+
- `isEqual`
17+
- `isNone`
18+
- `isPresent`
19+
- `typeOf`
20+
21+
## Why is this being deprecated
22+
23+
The `@ember/utils` package was created as a way to provide utility functions that were previously available on the `Ember` global. With the move towards modern JavaScript and the deprecation of the `Ember` global, these utility functions are no longer needed as part of the core Ember framework.
24+
25+
## Migration path
26+
27+
### Option 1: Replace with native JavaScript alternatives (Recommended)
28+
29+
Most utility functions can be replaced with native JavaScript alternatives, which is the preferred approach for better performance and reduced bundle size. See below for examples.
30+
31+
### Option 2: Use @ember/legacy-utils (Fallback)
32+
33+
If you need to maintain the exact same API, you can install the `@ember/legacy-utils` addon:
34+
35+
```bash
36+
ember install @ember/legacy-utils
37+
```
38+
39+
Then update your imports:
40+
41+
```js
42+
// Before
43+
import { compare, isBlank, isEmpty, isEqual, isNone, isPresent, typeOf } from '@ember/utils';
44+
45+
// After
46+
import { compare, isBlank, isEmpty, isEqual, isNone, isPresent, typeOf } from '@ember/legacy-utils';
47+
```
48+
49+
Many of these utility functions can be replaced with native JavaScript alternatives:
50+
51+
#### `isBlank` and `isEmpty`
52+
53+
```js
54+
// Before
55+
import { isBlank, isEmpty } from '@ember/utils';
56+
57+
if (isBlank(value)) { /* ... */ }
58+
if (isEmpty(value)) { /* ... */ }
59+
60+
// After
61+
if (value == null || value === '') { /* ... */ }
62+
if (value == null || value.length === 0) { /* ... */ }
63+
```
64+
65+
#### `isPresent`
66+
67+
```js
68+
// Before
69+
import { isPresent } from '@ember/utils';
70+
71+
if (isPresent(value)) { /* ... */ }
72+
73+
// After
74+
if (value != null && value !== '') { /* ... */ }
75+
```
76+
77+
#### `isEqual`
78+
79+
```js
80+
// Before
81+
import { isEqual } from '@ember/utils';
82+
83+
if (isEqual(a, b)) { /* ... */ }
84+
85+
// After
86+
if (a === b) { /* ... */ }
87+
// or use a deep equality library like lodash.isEqual
88+
```
89+
90+
#### `typeOf`
91+
92+
The `typeOf` function provides more detailed type checking than native `typeof`. Here are examples for different types:
93+
94+
```js
95+
// Before
96+
import { typeOf } from '@ember/utils';
97+
98+
if (typeOf(value) === 'string') { /* ... */ }
99+
if (typeOf(value) === 'array') { /* ... */ }
100+
if (typeOf(value) === 'date') { /* ... */ }
101+
if (typeOf(value) === 'error') { /* ... */ }
102+
if (typeOf(value) === 'null') { /* ... */ }
103+
104+
// After - Basic types
105+
if (typeof value === 'string') { /* ... */ }
106+
if (Array.isArray(value)) { /* ... */ }
107+
if (value instanceof Date) { /* ... */ }
108+
if (value instanceof Error) { /* ... */ }
109+
if (value === null) { /* ... */ }
110+
111+
// After - More complex type checking
112+
function getType(value) {
113+
if (value === null) return 'null';
114+
if (value === undefined) return 'undefined';
115+
if (Array.isArray(value)) return 'array';
116+
if (value instanceof Date) return 'date';
117+
if (value instanceof RegExp) return 'regexp';
118+
if (value instanceof Error) return 'error';
119+
if (typeof value === 'string') return 'string';
120+
if (typeof value === 'number') return 'number';
121+
if (typeof value === 'boolean') return 'boolean';
122+
if (typeof value === 'function') return 'function';
123+
return 'object';
124+
}
125+
126+
if (getType(value) === 'string') { /* ... */ }
127+
```
128+
129+
#### `compare`
130+
131+
```js
132+
// Before
133+
import { compare } from '@ember/utils';
134+
135+
array.sort((a, b) => compare(a, b));
136+
137+
// After
138+
array.sort((a, b) => {
139+
if (a < b) return -1;
140+
if (a > b) return 1;
141+
return 0;
142+
});
143+
```
144+
145+
## Computed Properties
146+
147+
If you're using `@ember/utils` functions in computed properties, you have two migration options:
148+
149+
### Option 1: Use tracked with getters (Recommended)
150+
151+
Replace computed properties that use `@ember/utils` functions with tracked properties and getters:
152+
153+
```js
154+
// Before
155+
import { computed } from '@ember/object';
156+
import { isBlank, isEmpty } from '@ember/utils';
157+
158+
export default class MyComponent extends Component {
159+
@computed('name', 'description')
160+
get isValid() {
161+
return !isBlank(this.name) && !isEmpty(this.description);
162+
}
163+
}
164+
165+
// After
166+
import { tracked } from '@glimmer/tracking';
167+
168+
export default class MyComponent extends Component {
169+
@tracked name;
170+
@tracked description;
171+
172+
get isValid() {
173+
return this.name != null && this.name !== '' &&
174+
this.description != null && this.description.length > 0;
175+
}
176+
}
177+
```
178+
179+
### Option 2: Use computed properties (Fallback)
180+
181+
If you need to maintain computed properties, you can still use them with native JavaScript:
182+
183+
```js
184+
// Before
185+
import { computed } from '@ember/object';
186+
import { isBlank, isEmpty } from '@ember/utils';
187+
188+
export default class MyComponent extends Component {
189+
@computed('name', 'description')
190+
get isValid() {
191+
return !isBlank(this.name) && !isEmpty(this.description);
192+
}
193+
}
194+
195+
// After
196+
import { computed } from '@ember/object';
197+
198+
export default class MyComponent extends Component {
199+
@computed('name', 'description')
200+
get isValid() {
201+
return this.name != null && this.name !== '' &&
202+
this.description != null && this.description.length > 0;
203+
}
204+
}
205+
```
206+
207+
## References
208+
209+
- [@ember/legacy-utils addon](https://github.com/bertdeblock/ember-legacy-utils)
210+
- [RFC 0334: Deprecate @ember/utils](https://github.com/emberjs/rfcs/blob/main/text/0334-deprecate-ember-utils.md)

0 commit comments

Comments
 (0)