Skip to content

Commit 507c60e

Browse files
committed
WIP
1 parent 2fe9ba3 commit 507c60e

File tree

14 files changed

+216
-62
lines changed

14 files changed

+216
-62
lines changed

packages/@ember/-internals/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ export {
3333
teardownMandatorySetter,
3434
setWithMandatorySetter,
3535
} from './lib/mandatory-setter';
36+
export type { TypeName } from './lib/type-of';
37+
export { default as typeOf } from './lib/type-of';
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// import CoreObject from '@ember/object/core';
2+
3+
export type TypeName =
4+
| 'undefined'
5+
| 'null'
6+
| 'string'
7+
| 'number'
8+
| 'boolean'
9+
| 'function'
10+
| 'array'
11+
| 'regexp'
12+
| 'date'
13+
| 'filelist'
14+
| 'class'
15+
| 'instance'
16+
| 'error'
17+
| 'object';
18+
19+
// ........................................
20+
// TYPING & ARRAY MESSAGING
21+
//
22+
const TYPE_MAP: Record<string, TypeName> = {
23+
'[object Boolean]': 'boolean',
24+
'[object Number]': 'number',
25+
'[object String]': 'string',
26+
'[object Function]': 'function',
27+
'[object AsyncFunction]': 'function',
28+
'[object Array]': 'array',
29+
'[object Date]': 'date',
30+
'[object RegExp]': 'regexp',
31+
'[object Object]': 'object',
32+
'[object FileList]': 'filelist',
33+
} as const;
34+
35+
const { toString } = Object.prototype;
36+
37+
/**
38+
@module @ember/utils
39+
*/
40+
/**
41+
Returns a consistent type for the passed object.
42+
43+
Use this instead of the built-in `typeof` to get the type of an item.
44+
It will return the same result across all browsers and includes a bit
45+
more detail. Here is what will be returned:
46+
47+
| Return Value | Meaning |
48+
|---------------|------------------------------------------------------|
49+
| 'string' | String primitive or String object. |
50+
| 'number' | Number primitive or Number object. |
51+
| 'boolean' | Boolean primitive or Boolean object. |
52+
| 'null' | Null value |
53+
| 'undefined' | Undefined value |
54+
| 'function' | A function |
55+
| 'array' | An instance of Array |
56+
| 'regexp' | An instance of RegExp |
57+
| 'date' | An instance of Date |
58+
| 'filelist' | An instance of FileList |
59+
| 'class' | An Ember class (created using EmberObject.extend()) |
60+
| 'instance' | An Ember object instance |
61+
| 'error' | An instance of the Error object |
62+
| 'object' | A JavaScript object not inheriting from EmberObject |
63+
64+
Examples:
65+
66+
```javascript
67+
import { A } from '@ember/array';
68+
import { typeOf } from '@ember/utils';
69+
import EmberObject from '@ember/object';
70+
71+
typeOf(); // 'undefined'
72+
typeOf(null); // 'null'
73+
typeOf(undefined); // 'undefined'
74+
typeOf('michael'); // 'string'
75+
typeOf(new String('michael')); // 'string'
76+
typeOf(101); // 'number'
77+
typeOf(new Number(101)); // 'number'
78+
typeOf(true); // 'boolean'
79+
typeOf(new Boolean(true)); // 'boolean'
80+
typeOf(A); // 'function'
81+
typeOf(A()); // 'array'
82+
typeOf([1, 2, 90]); // 'array'
83+
typeOf(/abc/); // 'regexp'
84+
typeOf(new Date()); // 'date'
85+
typeOf(event.target.files); // 'filelist'
86+
typeOf(EmberObject.extend()); // 'class'
87+
typeOf(EmberObject.create()); // 'instance'
88+
typeOf(new Error('teamocil')); // 'error'
89+
90+
// 'normal' JavaScript object
91+
typeOf({ a: 'b' }); // 'object'
92+
```
93+
94+
@method typeOf
95+
@for @ember/-internals/utils
96+
@param item the item to check
97+
@return {String} the type
98+
@public
99+
@static
100+
*/
101+
export default function typeOf(item: unknown): TypeName {
102+
if (item === null) {
103+
return 'null';
104+
}
105+
if (item === undefined) {
106+
return 'undefined';
107+
}
108+
let ret = TYPE_MAP[toString.call(item)] || 'object';
109+
110+
if (ret === 'function') {
111+
// if (CoreObject.detect(item)) {
112+
// ret = 'class';
113+
// }
114+
} else if (ret === 'object') {
115+
if (item instanceof Error) {
116+
ret = 'error';
117+
// } else if (item instanceof CoreObject) {
118+
// ret = 'instance';
119+
} else if (item instanceof Date) {
120+
ret = 'date';
121+
}
122+
}
123+
124+
return ret;
125+
}

packages/@ember/array/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import Mixin from '@ember/object/mixin';
1616
import { assert } from '@ember/debug';
1717
import Enumerable from '@ember/enumerable';
1818
import MutableEnumerable from '@ember/enumerable/mutable';
19-
import { compare, typeOf } from '@ember/utils';
19+
import { compare } from '@ember/utils';
20+
import { typeOf } from '@ember/-internals/utils';
2021
import Observable from '@ember/object/observable';
2122
import type { MethodNamesOf, MethodParams, MethodReturns } from '@ember/-internals/utility-types';
2223
import type { ComputedPropertyCallback } from '@ember/-internals/metal';

packages/@ember/debug/container-debug-adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { classify, dasherize } from '@ember/-internals/string';
22
import EmberObject from '@ember/object';
3-
import { typeOf } from '@ember/utils';
3+
import { typeOf } from '@ember/-internals/utils';
44
import type Owner from '@ember/owner';
55
import { getOwner } from '@ember/-internals/owner';
66
import type { Resolver } from '@ember/owner';

packages/@ember/object/lib/computed/computed_macros.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ function generateComputedWithPredicate(name: string, predicate: (value: unknown)
9696
@public
9797
*/
9898
export function empty(dependentKey: string) {
99+
deprecate('empty is deprecated. Define your own property using @ember/legacy-utils.', false, {
100+
id: 'ember-object.deprecate-empty',
101+
until: '7.0.0',
102+
for: 'ember-source',
103+
since: { available: '6.8.0' },
104+
});
105+
99106
assert(
100107
'You attempted to use @empty as a decorator directly, but it requires a `dependentKey` parameter',
101108
!isElementDescriptor(Array.prototype.slice.call(arguments))
@@ -142,6 +149,13 @@ export function empty(dependentKey: string) {
142149
@public
143150
*/
144151
export function notEmpty(dependentKey: string) {
152+
deprecate('notEmpty is deprecated. Define your own property using @ember/legacy-utils.', false, {
153+
id: 'ember-object.deprecate-not-empty',
154+
until: '7.0.0',
155+
for: 'ember-source',
156+
since: { available: '6.8.0' },
157+
});
158+
145159
assert(
146160
'You attempted to use @notEmpty as a decorator directly, but it requires a `dependentKey` parameter',
147161
!isElementDescriptor(Array.prototype.slice.call(arguments))
@@ -185,6 +199,13 @@ export function notEmpty(dependentKey: string) {
185199
@public
186200
*/
187201
export function none(dependentKey: string) {
202+
deprecate('none is deprecated. Define your own property using @ember/legacy-utils.', false, {
203+
id: 'ember-object.deprecate-none',
204+
until: '7.0.0',
205+
for: 'ember-source',
206+
since: { available: '6.8.0' },
207+
});
208+
188209
assert(
189210
'You attempted to use @none as a decorator directly, but it requires a `dependentKey` parameter',
190211
!isElementDescriptor(Array.prototype.slice.call(arguments))

packages/@ember/routing/route.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import EmberObject, { computed, get, set, getProperties, setProperties } from '@
1212
import Evented from '@ember/object/evented';
1313
import { A as emberA } from '@ember/array';
1414
import { ActionHandler } from '@ember/-internals/runtime';
15-
import { typeOf } from '@ember/utils';
16-
import { isProxy, lookupDescriptor } from '@ember/-internals/utils';
15+
import { isProxy, lookupDescriptor, typeOf } from '@ember/-internals/utils';
1716
import type { AnyFn } from '@ember/-internals/utility-types';
1817
import Controller from '@ember/controller';
1918
import type { ControllerQueryParamType } from '@ember/controller';

packages/@ember/routing/router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type {
2121
import type RouterService from '@ember/routing/router-service';
2222
import EmberObject from '@ember/object';
2323
import { A as emberA } from '@ember/array';
24-
import { typeOf } from '@ember/utils';
24+
import { typeOf } from '@ember/-internals/utils';
2525
import Evented from '@ember/object/evented';
2626
import { assert, info } from '@ember/debug';
2727
import { cancel, once, run, scheduleOnce } from '@ember/runloop';

packages/@ember/utils/lib/compare.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { TypeName } from './type-of';
22
import typeOf from './type-of';
33
import { Comparable } from '@ember/-internals/runtime';
4-
import { assert } from '@ember/debug';
4+
import { assert, deprecate } from '@ember/debug';
55

66
const TYPE_ORDER: Record<TypeName, number> = {
77
undefined: 0,
@@ -97,6 +97,13 @@ function spaceship(a: number, b: number): Compare {
9797
@public
9898
*/
9999
export default function compare<T>(v: T, w: T): Compare {
100+
deprecate('compare is deprecated. Use @ember/legacy-utils instead.', false, {
101+
for: 'ember-source',
102+
id: 'ember-utils.deprecate-compare',
103+
since: { available: '6.8.0' },
104+
until: '7.0.0',
105+
});
106+
100107
if (v === w) {
101108
return 0;
102109
}

packages/@ember/utils/lib/is-equal.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/**
22
@module @ember/utils
33
*/
4+
5+
import { deprecate } from '@ember/debug';
6+
47
/**
58
Compares two objects, returning true if they are equal.
69
@@ -48,6 +51,13 @@
4851
@public
4952
*/
5053
export default function isEqual(a: unknown, b: unknown): boolean {
54+
deprecate('isEqual is deprecated. Use @ember/legacy-utils instead.', false, {
55+
for: 'ember-source',
56+
id: 'ember-utils.deprecate-isEqual',
57+
since: { available: '6.8.0' },
58+
until: '7.0.0',
59+
});
60+
5161
if (a && typeof (a as IsEqual).isEqual === 'function') {
5262
return (a as IsEqual).isEqual(b);
5363
}

packages/@ember/utils/lib/is_blank.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { deprecate } from '@ember/debug';
12
import isEmpty from './is_empty';
23
/**
34
@module @ember/utils
@@ -29,5 +30,12 @@ import isEmpty from './is_empty';
2930
@public
3031
*/
3132
export default function isBlank(obj: unknown): boolean {
33+
deprecate('isBlank is deprecated. Use @ember/legacy-utils instead.', false, {
34+
for: 'ember-source',
35+
id: 'ember-utils.deprecate-isBlank',
36+
since: { available: '6.8.0' },
37+
until: '7.0.0',
38+
});
39+
3240
return isEmpty(obj) || (typeof obj === 'string' && /\S/.test(obj) === false);
3341
}

0 commit comments

Comments
 (0)