@warp-drive/legacy 5.6.0-alpha.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -0
- package/LICENSE.md +23 -0
- package/README.md +54 -0
- package/addon-main.cjs +5 -0
- package/declarations/adapter/-private/build-url-mixin.d.ts +34 -0
- package/declarations/adapter/-private/build-url-mixin.d.ts.map +1 -0
- package/declarations/adapter/-private/fastboot-interface.d.ts +9 -0
- package/declarations/adapter/-private/fastboot-interface.d.ts.map +1 -0
- package/declarations/adapter/-private/utils/continue-on-reject.d.ts +8 -0
- package/declarations/adapter/-private/utils/continue-on-reject.d.ts.map +1 -0
- package/declarations/adapter/-private/utils/determine-body-promise.d.ts +5 -0
- package/declarations/adapter/-private/utils/determine-body-promise.d.ts.map +1 -0
- package/declarations/adapter/-private/utils/fetch.d.ts +9 -0
- package/declarations/adapter/-private/utils/fetch.d.ts.map +1 -0
- package/declarations/adapter/-private/utils/parse-response-headers.d.ts +2 -0
- package/declarations/adapter/-private/utils/parse-response-headers.d.ts.map +1 -0
- package/declarations/adapter/-private/utils/serialize-into-hash.d.ts +7 -0
- package/declarations/adapter/-private/utils/serialize-into-hash.d.ts.map +1 -0
- package/declarations/adapter/-private/utils/serialize-query-params.d.ts +6 -0
- package/declarations/adapter/-private/utils/serialize-query-params.d.ts.map +1 -0
- package/declarations/adapter/-private.d.ts +6 -0
- package/declarations/adapter/-private.d.ts.map +1 -0
- package/declarations/adapter/error.d.ts +188 -0
- package/declarations/adapter/error.d.ts.map +1 -0
- package/declarations/adapter/json-api.d.ts +234 -0
- package/declarations/adapter/json-api.d.ts.map +1 -0
- package/declarations/adapter/rest.d.ts +823 -0
- package/declarations/adapter/rest.d.ts.map +1 -0
- package/declarations/adapter.d.ts +801 -0
- package/declarations/adapter.d.ts.map +1 -0
- package/declarations/compat/-private.d.ts +14 -0
- package/declarations/compat/-private.d.ts.map +1 -0
- package/declarations/compat/builders/find-all.d.ts +33 -0
- package/declarations/compat/builders/find-all.d.ts.map +1 -0
- package/declarations/compat/builders/find-record.d.ts +54 -0
- package/declarations/compat/builders/find-record.d.ts.map +1 -0
- package/declarations/compat/builders/query.d.ts +61 -0
- package/declarations/compat/builders/query.d.ts.map +1 -0
- package/declarations/compat/builders/save-record.d.ts +32 -0
- package/declarations/compat/builders/save-record.d.ts.map +1 -0
- package/declarations/compat/builders/utils.d.ts +4 -0
- package/declarations/compat/builders/utils.d.ts.map +1 -0
- package/declarations/compat/builders.d.ts +15 -0
- package/declarations/compat/builders.d.ts.map +1 -0
- package/declarations/compat/legacy-network-handler/fetch-manager.d.ts +47 -0
- package/declarations/compat/legacy-network-handler/fetch-manager.d.ts.map +1 -0
- package/declarations/compat/legacy-network-handler/identifier-has-id.d.ts +3 -0
- package/declarations/compat/legacy-network-handler/identifier-has-id.d.ts.map +1 -0
- package/declarations/compat/legacy-network-handler/legacy-data-fetch.d.ts +12 -0
- package/declarations/compat/legacy-network-handler/legacy-data-fetch.d.ts.map +1 -0
- package/declarations/compat/legacy-network-handler/legacy-data-utils.d.ts +6 -0
- package/declarations/compat/legacy-network-handler/legacy-data-utils.d.ts.map +1 -0
- package/declarations/compat/legacy-network-handler/legacy-network-handler.d.ts +3 -0
- package/declarations/compat/legacy-network-handler/legacy-network-handler.d.ts.map +1 -0
- package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts +527 -0
- package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts.map +1 -0
- package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +224 -0
- package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts.map +1 -0
- package/declarations/compat/legacy-network-handler/serializer-response.d.ts +7 -0
- package/declarations/compat/legacy-network-handler/serializer-response.d.ts.map +1 -0
- package/declarations/compat/legacy-network-handler/snapshot-record-array.d.ts +89 -0
- package/declarations/compat/legacy-network-handler/snapshot-record-array.d.ts.map +1 -0
- package/declarations/compat/legacy-network-handler/snapshot.d.ts +237 -0
- package/declarations/compat/legacy-network-handler/snapshot.d.ts.map +1 -0
- package/declarations/compat/utils.d.ts +138 -0
- package/declarations/compat/utils.d.ts.map +1 -0
- package/declarations/compat.d.ts +134 -0
- package/declarations/compat.d.ts.map +1 -0
- package/declarations/model/-private/attr.d.ts +165 -0
- package/declarations/model/-private/attr.d.ts.map +1 -0
- package/declarations/model/-private/attr.type-test.d.ts +2 -0
- package/declarations/model/-private/attr.type-test.d.ts.map +1 -0
- package/declarations/model/-private/belongs-to.d.ts +173 -0
- package/declarations/model/-private/belongs-to.d.ts.map +1 -0
- package/declarations/model/-private/belongs-to.type-test.d.ts +2 -0
- package/declarations/model/-private/belongs-to.type-test.d.ts.map +1 -0
- package/declarations/model/-private/debug/assert-polymorphic-type.d.ts +6 -0
- package/declarations/model/-private/debug/assert-polymorphic-type.d.ts.map +1 -0
- package/declarations/model/-private/errors.d.ts +290 -0
- package/declarations/model/-private/errors.d.ts.map +1 -0
- package/declarations/model/-private/has-many.d.ts +164 -0
- package/declarations/model/-private/has-many.d.ts.map +1 -0
- package/declarations/model/-private/has-many.type-test.d.ts +2 -0
- package/declarations/model/-private/has-many.type-test.d.ts.map +1 -0
- package/declarations/model/-private/hooks.d.ts +11 -0
- package/declarations/model/-private/hooks.d.ts.map +1 -0
- package/declarations/model/-private/legacy-relationships-support.d.ts +57 -0
- package/declarations/model/-private/legacy-relationships-support.d.ts.map +1 -0
- package/declarations/model/-private/model-for-mixin.d.ts +4 -0
- package/declarations/model/-private/model-for-mixin.d.ts.map +1 -0
- package/declarations/model/-private/model-methods.d.ts +35 -0
- package/declarations/model/-private/model-methods.d.ts.map +1 -0
- package/declarations/model/-private/model.d.ts +1270 -0
- package/declarations/model/-private/model.d.ts.map +1 -0
- package/declarations/model/-private/model.type-test.d.ts +2 -0
- package/declarations/model/-private/model.type-test.d.ts.map +1 -0
- package/declarations/model/-private/notify-changes.d.ts +5 -0
- package/declarations/model/-private/notify-changes.d.ts.map +1 -0
- package/declarations/model/-private/promise-belongs-to.d.ts +40 -0
- package/declarations/model/-private/promise-belongs-to.d.ts.map +1 -0
- package/declarations/model/-private/promise-many-array.d.ts +126 -0
- package/declarations/model/-private/promise-many-array.d.ts.map +1 -0
- package/declarations/model/-private/promise-proxy-base.d.ts +3 -0
- package/declarations/model/-private/promise-proxy-base.d.ts.map +1 -0
- package/declarations/model/-private/record-state.d.ts +78 -0
- package/declarations/model/-private/record-state.d.ts.map +1 -0
- package/declarations/model/-private/references/belongs-to.d.ts +495 -0
- package/declarations/model/-private/references/belongs-to.d.ts.map +1 -0
- package/declarations/model/-private/references/has-many.d.ts +504 -0
- package/declarations/model/-private/references/has-many.d.ts.map +1 -0
- package/declarations/model/-private/schema-provider.d.ts +63 -0
- package/declarations/model/-private/schema-provider.d.ts.map +1 -0
- package/declarations/model/-private/type-utils.d.ts +57 -0
- package/declarations/model/-private/type-utils.d.ts.map +1 -0
- package/declarations/model/-private/util.d.ts +6 -0
- package/declarations/model/-private/util.d.ts.map +1 -0
- package/declarations/model/-private.d.ts +8 -0
- package/declarations/model/-private.d.ts.map +1 -0
- package/declarations/model/migration-support.d.ts +287 -0
- package/declarations/model/migration-support.d.ts.map +1 -0
- package/declarations/model/migration-support.type-test.d.ts +2 -0
- package/declarations/model/migration-support.type-test.d.ts.map +1 -0
- package/declarations/model.d.ts +50 -0
- package/declarations/model.d.ts.map +1 -0
- package/declarations/serializer/-private/embedded-records-mixin.d.ts +97 -0
- package/declarations/serializer/-private/embedded-records-mixin.d.ts.map +1 -0
- package/declarations/serializer/-private/transforms/boolean.d.ts +47 -0
- package/declarations/serializer/-private/transforms/boolean.d.ts.map +1 -0
- package/declarations/serializer/-private/transforms/boolean.type-test.d.ts +2 -0
- package/declarations/serializer/-private/transforms/boolean.type-test.d.ts.map +1 -0
- package/declarations/serializer/-private/transforms/date.d.ts +28 -0
- package/declarations/serializer/-private/transforms/date.d.ts.map +1 -0
- package/declarations/serializer/-private/transforms/number.d.ts +29 -0
- package/declarations/serializer/-private/transforms/number.d.ts.map +1 -0
- package/declarations/serializer/-private/transforms/string.d.ts +29 -0
- package/declarations/serializer/-private/transforms/string.d.ts.map +1 -0
- package/declarations/serializer/-private/transforms/transform.d.ts +119 -0
- package/declarations/serializer/-private/transforms/transform.d.ts.map +1 -0
- package/declarations/serializer/-private/utils.d.ts +4 -0
- package/declarations/serializer/-private/utils.d.ts.map +1 -0
- package/declarations/serializer/json-api.d.ts +496 -0
- package/declarations/serializer/json-api.d.ts.map +1 -0
- package/declarations/serializer/json.d.ts +1047 -0
- package/declarations/serializer/json.d.ts.map +1 -0
- package/declarations/serializer/rest.d.ts +554 -0
- package/declarations/serializer/rest.d.ts.map +1 -0
- package/declarations/serializer/transform.d.ts +6 -0
- package/declarations/serializer/transform.d.ts.map +1 -0
- package/declarations/serializer.d.ts +258 -0
- package/declarations/serializer.d.ts.map +1 -0
- package/dist/-private-DFfBszo5.js +1182 -0
- package/dist/-private-DFfBszo5.js.map +1 -0
- package/dist/adapter/-private.js +1 -0
- package/dist/adapter/-private.js.map +1 -0
- package/dist/adapter/error.js +254 -0
- package/dist/adapter/error.js.map +1 -0
- package/dist/adapter/json-api.js +129 -0
- package/dist/adapter/json-api.js.map +1 -0
- package/dist/adapter/rest.js +1262 -0
- package/dist/adapter/rest.js.map +1 -0
- package/dist/adapter.js +1284 -0
- package/dist/adapter.js.map +1 -0
- package/dist/compat/-private.js +1 -0
- package/dist/compat/-private.js.map +1 -0
- package/dist/compat/builders.js +292 -0
- package/dist/compat/builders.js.map +1 -0
- package/dist/compat/utils.js +225 -0
- package/dist/compat/utils.js.map +1 -0
- package/dist/compat.js +999 -0
- package/dist/compat.js.map +1 -0
- package/dist/errors-D74uk36r.js +2541 -0
- package/dist/errors-D74uk36r.js.map +1 -0
- package/dist/json-BCH3fil7.js +1349 -0
- package/dist/json-BCH3fil7.js.map +1 -0
- package/dist/model/-private.js +2 -0
- package/dist/model/-private.js.map +1 -0
- package/dist/model/migration-support.js +453 -0
- package/dist/model/migration-support.js.map +1 -0
- package/dist/model.js +736 -0
- package/dist/model.js.map +1 -0
- package/dist/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/runtime-BPCpkOf1-BKOwiRJp.js.map +1 -0
- package/dist/schema-provider-CXFLTMjg.js +2228 -0
- package/dist/schema-provider-CXFLTMjg.js.map +1 -0
- package/dist/serialize-into-hash-BxfqWC8u.js +260 -0
- package/dist/serialize-into-hash-BxfqWC8u.js.map +1 -0
- package/dist/serializer/json-api.js +514 -0
- package/dist/serializer/json-api.js.map +1 -0
- package/dist/serializer/json.js +6 -0
- package/dist/serializer/json.js.map +1 -0
- package/dist/serializer/rest.js +1245 -0
- package/dist/serializer/rest.js.map +1 -0
- package/dist/serializer/transform.js +313 -0
- package/dist/serializer/transform.js.map +1 -0
- package/dist/serializer.js +252 -0
- package/dist/serializer.js.map +1 -0
- package/logos/NCC-1701-a-blue.svg +4 -0
- package/logos/NCC-1701-a-gold.svg +4 -0
- package/logos/NCC-1701-a-gold_100.svg +1 -0
- package/logos/NCC-1701-a-gold_base-64.txt +1 -0
- package/logos/NCC-1701-a.svg +4 -0
- package/logos/README.md +4 -0
- package/logos/docs-badge.svg +2 -0
- package/logos/ember-data-logo-dark.svg +12 -0
- package/logos/ember-data-logo-light.svg +12 -0
- package/logos/github-header.svg +444 -0
- package/logos/social1.png +0 -0
- package/logos/social2.png +0 -0
- package/logos/warp-drive-logo-dark.svg +4 -0
- package/logos/warp-drive-logo-gold.svg +4 -0
- package/package.json +72 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { singularize, dasherize } from '@warp-drive/utilities/string';
|
|
2
|
+
import { macroCondition, getGlobalConfig } from '@embroider/macros';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
Utilities for helping to migrate to stricter
|
|
6
|
+
and more consistent use of IDs and types.
|
|
7
|
+
|
|
8
|
+
@module
|
|
9
|
+
@deprecated
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
let MismatchReporter = function () {};
|
|
13
|
+
|
|
14
|
+
// TODO: @runspired This pattern prevents AssertFn from being removed in production builds
|
|
15
|
+
// but we should enable that if we can.
|
|
16
|
+
let _AssertFn = function () {};
|
|
17
|
+
const AssertFn = (message, condition) => {
|
|
18
|
+
if (!condition) {
|
|
19
|
+
_AssertFn(message, condition);
|
|
20
|
+
}
|
|
21
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
22
|
+
if (!test) {
|
|
23
|
+
throw new Error(message);
|
|
24
|
+
}
|
|
25
|
+
})(condition) : {};
|
|
26
|
+
};
|
|
27
|
+
let NormalizedType = str => {
|
|
28
|
+
return singularize(dasherize(str));
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Configure a function to be called when an id or type
|
|
33
|
+
* changes during normalization. This is useful for instrumenting
|
|
34
|
+
* to discover places where usage in the app is not consistent.
|
|
35
|
+
*
|
|
36
|
+
* @param method a function which takes a mismatch-type ('formatted-id' | 'formatted-type'), actual, and expected value
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
39
|
+
function configureMismatchReporter(fn) {
|
|
40
|
+
MismatchReporter = fn;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Configure a function to be called when an id or type
|
|
45
|
+
* fails validation. This is useful for instrumenting
|
|
46
|
+
* to discover places where usage in the app is not consistent.
|
|
47
|
+
*
|
|
48
|
+
* @param method a function which takes a message and a condition
|
|
49
|
+
* @public
|
|
50
|
+
*/
|
|
51
|
+
function configureAssertFn(fn) {
|
|
52
|
+
_AssertFn = fn;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Configure a function to be called to normalize
|
|
57
|
+
* a resource type string. Used by both formattedType
|
|
58
|
+
* and isEquivType to ensure consistent normalization
|
|
59
|
+
* during comparison.
|
|
60
|
+
*
|
|
61
|
+
* If validation fails or the type turns out be unnormalized
|
|
62
|
+
* the configured mismatch reporter and assert functions will
|
|
63
|
+
* be called.
|
|
64
|
+
*
|
|
65
|
+
* @param method a function which takes a string and returns a string
|
|
66
|
+
* @public
|
|
67
|
+
*/
|
|
68
|
+
function configureTypeNormalization(fn) {
|
|
69
|
+
NormalizedType = fn;
|
|
70
|
+
}
|
|
71
|
+
const NORMALIZED_TYPES = new Map();
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Converts a potentially unnormalized type into the format expected
|
|
75
|
+
* by our EmberData Cache. Currently this is singular-dasherized.
|
|
76
|
+
*
|
|
77
|
+
* you should not rely on this function to give you an exact format
|
|
78
|
+
* for display purposes. Formatting for display should be handled
|
|
79
|
+
* differently if the exact format matters.
|
|
80
|
+
*
|
|
81
|
+
* Asserts invalid types (undefined, null, '') in dev.
|
|
82
|
+
*
|
|
83
|
+
* **Usage**
|
|
84
|
+
*
|
|
85
|
+
* ```js
|
|
86
|
+
* import formattedType from 'soxhub-client/helpers/formatted-type';
|
|
87
|
+
*
|
|
88
|
+
* formattedType('post'); // => 'post'
|
|
89
|
+
* formattedType('posts'); // => 'post'
|
|
90
|
+
* formattedType('Posts'); // => 'post'
|
|
91
|
+
* formattedType('post-comment'); // => 'post-comment'
|
|
92
|
+
* formattedType('post-comments'); // => 'post-comment'
|
|
93
|
+
* formattedType('post_comment'); // => 'post-comment'
|
|
94
|
+
* formattedType('postComment'); // => 'post-comment'
|
|
95
|
+
* formattedType('PostComment'); // => 'post-comment'
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* @param {String} type the potentially un-normalized type
|
|
99
|
+
* @return {String} the normalized type
|
|
100
|
+
* @public
|
|
101
|
+
*/
|
|
102
|
+
function formattedType(type) {
|
|
103
|
+
AssertFn('formattedType: type must not be null', type !== null);
|
|
104
|
+
AssertFn('formattedType: type must not be undefined', type !== undefined);
|
|
105
|
+
AssertFn('formattedType: type must be a string', typeof type === 'string');
|
|
106
|
+
AssertFn('formattedType: type must not be empty', type.length > 0);
|
|
107
|
+
let normalized = NORMALIZED_TYPES.get(type);
|
|
108
|
+
if (normalized === undefined) {
|
|
109
|
+
normalized = NormalizedType(type);
|
|
110
|
+
NORMALIZED_TYPES.set(type, normalized);
|
|
111
|
+
}
|
|
112
|
+
if (normalized !== type) {
|
|
113
|
+
MismatchReporter('formatted-type', type, normalized);
|
|
114
|
+
}
|
|
115
|
+
return normalized;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Format an id to the format expected by the EmberData Cache.
|
|
120
|
+
* Currently this means that id should be `string | null`.
|
|
121
|
+
*
|
|
122
|
+
* Asserts invalid IDs (undefined, '', 0, '0') in dev.
|
|
123
|
+
*
|
|
124
|
+
* **Usage**
|
|
125
|
+
*
|
|
126
|
+
* ```js
|
|
127
|
+
* import formattedId from 'client/utils/formatted-id';
|
|
128
|
+
*
|
|
129
|
+
* formattedId('1'); // => '1'
|
|
130
|
+
* formattedId(1); // => '1'
|
|
131
|
+
* formattedId(null); // => null
|
|
132
|
+
* ```
|
|
133
|
+
*
|
|
134
|
+
* @param {String | Number | null} id the potentially un-normalized id
|
|
135
|
+
* @return {String | null} the normalized id
|
|
136
|
+
* @public
|
|
137
|
+
*/
|
|
138
|
+
|
|
139
|
+
function formattedId(id) {
|
|
140
|
+
AssertFn('formattedId: id must not be undefined', id !== undefined);
|
|
141
|
+
AssertFn('formattedId: id must be a number, string or null', typeof id === 'number' || typeof id === 'string' || id === null);
|
|
142
|
+
AssertFn('formattedId: id must not be empty', typeof id === 'number' || id === null || typeof id === 'string' && id.length > 0);
|
|
143
|
+
AssertFn('formattedId: id must not be 0', id !== '0' && id !== 0);
|
|
144
|
+
const formatted = id === null ? null : String(id);
|
|
145
|
+
if (formatted !== id) {
|
|
146
|
+
MismatchReporter('formatted-id', id, formatted);
|
|
147
|
+
}
|
|
148
|
+
return id === null ? null : String(id);
|
|
149
|
+
}
|
|
150
|
+
function expectId(id) {
|
|
151
|
+
AssertFn('expectId: id must not be null', id !== null);
|
|
152
|
+
return formattedId(id);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Compares two types for strict equality, converting them to
|
|
157
|
+
* the format expected by the EmberData Cache to ensure
|
|
158
|
+
* differences in format are accounted for in the comparison.
|
|
159
|
+
*
|
|
160
|
+
* Asserts when expected or actual are invalid types in dev.
|
|
161
|
+
* Expected may never be null.
|
|
162
|
+
*
|
|
163
|
+
* ```js
|
|
164
|
+
* isEquivType('posts', 'post'); // true
|
|
165
|
+
* isEquivType('post', 'post'); // true
|
|
166
|
+
* isEquivType('posts', 'posts'); // true
|
|
167
|
+
* isEquivType('post-comment', 'postComment'); // true
|
|
168
|
+
* isEquivType('post-comment', 'PostComment'); // true
|
|
169
|
+
* isEquivType('post-comment', 'post_comment'); // true
|
|
170
|
+
* isEquivType('post-comment', 'post-comment'); // true
|
|
171
|
+
* isEquivType('post-comment', 'post'); // false
|
|
172
|
+
* isEquivType('posts', null); // false
|
|
173
|
+
* ```
|
|
174
|
+
*
|
|
175
|
+
* @param {String} expected a potentially unnormalized type to match against
|
|
176
|
+
* @param {String} actual a potentially unnormalized type to match against
|
|
177
|
+
* @return {Boolean} true if the types are equivalent
|
|
178
|
+
* @public
|
|
179
|
+
*/
|
|
180
|
+
function isEquivType(expected, actual) {
|
|
181
|
+
AssertFn('isEquivType: Expected type must not be null', expected !== null);
|
|
182
|
+
AssertFn('isEquivType: Expected type must not be undefined', expected !== undefined);
|
|
183
|
+
AssertFn('isEquivType: Expected type must be a string', typeof expected === 'string');
|
|
184
|
+
AssertFn('isEquivType: Expected type must not be empty', expected.length > 0);
|
|
185
|
+
AssertFn('isEquivType: Actual type must not be null', actual !== null);
|
|
186
|
+
AssertFn('isEquivType: Actual type must not be undefined', actual !== undefined);
|
|
187
|
+
AssertFn('isEquivType: Actual type must be a string', typeof actual === 'string');
|
|
188
|
+
AssertFn('isEquivType: Actual type must not be empty', actual.length > 0);
|
|
189
|
+
return expected === actual || formattedType(expected) === formattedType(actual);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Compares two IDs for strict equality, converting them to
|
|
194
|
+
* the format expected by the EmberData Cache to ensure
|
|
195
|
+
* differences in format are accounted for in the comparison.
|
|
196
|
+
*
|
|
197
|
+
* Asserts when expected or actual are invalid IDs in dev.
|
|
198
|
+
* Expected may never be null.
|
|
199
|
+
*
|
|
200
|
+
* ```js
|
|
201
|
+
* isEquivId('1', 1); // true
|
|
202
|
+
* isEquivId('2', '2'); // true
|
|
203
|
+
* isEquivId(3, '3'); // true
|
|
204
|
+
* isEquivId(4, '3'); // false
|
|
205
|
+
* isEquivId(1, null); // false
|
|
206
|
+
* ```
|
|
207
|
+
*
|
|
208
|
+
* @param {string | number} expected a potentially un-normalized id to match against
|
|
209
|
+
* @param {string | number} actual a potentially un-normalized id to match against
|
|
210
|
+
* @return {Boolean} true if the ids are equivalent
|
|
211
|
+
* @public
|
|
212
|
+
*/
|
|
213
|
+
function isEquivId(expected, actual) {
|
|
214
|
+
AssertFn('isEquivId: Expected id must not be null', expected !== null);
|
|
215
|
+
AssertFn('isEquivId: Expected id must not be undefined', expected !== undefined);
|
|
216
|
+
AssertFn('isEquivId: Expected id must be a number or string', typeof expected === 'number' || typeof expected === 'string');
|
|
217
|
+
AssertFn('isEquivId: Expected id must not be empty', typeof expected === 'number' || typeof expected === 'string' && expected.length > 0);
|
|
218
|
+
AssertFn('isEquivId: Expected id must not be 0', expected !== '0' && expected !== 0);
|
|
219
|
+
AssertFn('isEquivId: Actual id must not be undefined', actual !== undefined);
|
|
220
|
+
AssertFn('isEquivId: Actual id must be a number, string or null', typeof actual === 'number' || typeof actual === 'string' || actual === null);
|
|
221
|
+
AssertFn('isEquivId: Actual id must not be empty', actual === null || typeof actual === 'number' || typeof actual === 'string' && actual.length > 0);
|
|
222
|
+
AssertFn('isEquivId: Actual id must not be 0', actual !== '0' && actual !== 0);
|
|
223
|
+
return expected === actual || formattedId(expected) === formattedId(actual);
|
|
224
|
+
}
|
|
225
|
+
export { configureAssertFn, configureMismatchReporter, configureTypeNormalization, expectId, formattedId, formattedType, isEquivId, isEquivType };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sources":["../../src/compat/utils.ts"],"sourcesContent":["/**\n Utilities for helping to migrate to stricter\n and more consistent use of IDs and types.\n\n @module\n @deprecated\n*/\nimport { assert } from '@warp-drive/core/build-config/macros';\nimport { dasherize, singularize } from '@warp-drive/utilities/string';\n\ninterface AssertFunc {\n (desc: string, condition: unknown): asserts condition;\n (desc: string): never;\n}\n\ntype Reporter = (type: 'formatted-id' | 'formatted-type', actual: unknown, expected: unknown) => void;\ntype Normalizer = (type: string) => string;\n\nlet MismatchReporter: Reporter = function () {};\n\n// TODO: @runspired This pattern prevents AssertFn from being removed in production builds\n// but we should enable that if we can.\nlet _AssertFn: (message: string, condition: unknown) => void = function () {};\nconst AssertFn: AssertFunc = ((message: string, condition: unknown) => {\n if (!condition) {\n _AssertFn(message, condition);\n }\n assert(message, condition);\n}) as unknown as AssertFunc;\nlet NormalizedType: Normalizer = (str: string) => {\n return singularize(dasherize(str));\n};\n\n/**\n * Configure a function to be called when an id or type\n * changes during normalization. This is useful for instrumenting\n * to discover places where usage in the app is not consistent.\n *\n * @param method a function which takes a mismatch-type ('formatted-id' | 'formatted-type'), actual, and expected value\n * @public\n */\nexport function configureMismatchReporter(fn: Reporter): void {\n MismatchReporter = fn;\n}\n\n/**\n * Configure a function to be called when an id or type\n * fails validation. This is useful for instrumenting\n * to discover places where usage in the app is not consistent.\n *\n * @param method a function which takes a message and a condition\n * @public\n */\nexport function configureAssertFn(fn: (message: string, condition: unknown) => void): void {\n _AssertFn = fn;\n}\n\n/**\n * Configure a function to be called to normalize\n * a resource type string. Used by both formattedType\n * and isEquivType to ensure consistent normalization\n * during comparison.\n *\n * If validation fails or the type turns out be unnormalized\n * the configured mismatch reporter and assert functions will\n * be called.\n *\n * @param method a function which takes a string and returns a string\n * @public\n */\nexport function configureTypeNormalization(fn: (type: string) => string): void {\n NormalizedType = fn;\n}\n\nconst NORMALIZED_TYPES = new Map<string, string>();\n\n/**\n * Converts a potentially unnormalized type into the format expected\n * by our EmberData Cache. Currently this is singular-dasherized.\n *\n * you should not rely on this function to give you an exact format\n * for display purposes. Formatting for display should be handled\n * differently if the exact format matters.\n *\n * Asserts invalid types (undefined, null, '') in dev.\n *\n * **Usage**\n *\n * ```js\n * import formattedType from 'soxhub-client/helpers/formatted-type';\n *\n * formattedType('post'); // => 'post'\n * formattedType('posts'); // => 'post'\n * formattedType('Posts'); // => 'post'\n * formattedType('post-comment'); // => 'post-comment'\n * formattedType('post-comments'); // => 'post-comment'\n * formattedType('post_comment'); // => 'post-comment'\n * formattedType('postComment'); // => 'post-comment'\n * formattedType('PostComment'); // => 'post-comment'\n * ```\n *\n * @param {String} type the potentially un-normalized type\n * @return {String} the normalized type\n * @public\n */\nexport function formattedType<T extends string>(type: T | string): T {\n AssertFn('formattedType: type must not be null', type !== null);\n AssertFn('formattedType: type must not be undefined', type !== undefined);\n AssertFn('formattedType: type must be a string', typeof type === 'string');\n AssertFn('formattedType: type must not be empty', type.length > 0);\n let normalized = NORMALIZED_TYPES.get(type);\n\n if (normalized === undefined) {\n normalized = NormalizedType(type);\n NORMALIZED_TYPES.set(type, normalized);\n }\n\n if (normalized !== type) {\n MismatchReporter('formatted-type', type, normalized);\n }\n\n return normalized as T;\n}\n\n/**\n * Format an id to the format expected by the EmberData Cache.\n * Currently this means that id should be `string | null`.\n *\n * Asserts invalid IDs (undefined, '', 0, '0') in dev.\n *\n * **Usage**\n *\n * ```js\n * import formattedId from 'client/utils/formatted-id';\n *\n * formattedId('1'); // => '1'\n * formattedId(1); // => '1'\n * formattedId(null); // => null\n *\t```\n *\n * @param {String | Number | null} id the potentially un-normalized id\n * @return {String | null} the normalized id\n * @public\n */\nexport function formattedId(id: string | number): string;\nexport function formattedId(id: null): null;\nexport function formattedId(id: string | number | null): string | null;\nexport function formattedId(id: string | number | null): string | null {\n AssertFn('formattedId: id must not be undefined', id !== undefined);\n AssertFn(\n 'formattedId: id must be a number, string or null',\n typeof id === 'number' || typeof id === 'string' || id === null\n );\n AssertFn(\n 'formattedId: id must not be empty',\n typeof id === 'number' || id === null || (typeof id === 'string' && id.length > 0)\n );\n AssertFn('formattedId: id must not be 0', id !== '0' && id !== 0);\n\n const formatted = id === null ? null : String(id);\n if (formatted !== id) {\n MismatchReporter('formatted-id', id, formatted);\n }\n return id === null ? null : String(id);\n}\n\nexport function expectId(id: string | number): string;\nexport function expectId(id: null): never;\nexport function expectId(id: string | number | null): string {\n AssertFn('expectId: id must not be null', id !== null);\n\n return formattedId(id);\n}\n\n/**\n * Compares two types for strict equality, converting them to\n * the format expected by the EmberData Cache to ensure\n * differences in format are accounted for in the comparison.\n *\n * Asserts when expected or actual are invalid types in dev.\n * Expected may never be null.\n *\n * ```js\n * isEquivType('posts', 'post'); // true\n * isEquivType('post', 'post'); // true\n * isEquivType('posts', 'posts'); // true\n * isEquivType('post-comment', 'postComment'); // true\n * isEquivType('post-comment', 'PostComment'); // true\n * isEquivType('post-comment', 'post_comment'); // true\n * isEquivType('post-comment', 'post-comment'); // true\n * isEquivType('post-comment', 'post'); // false\n * isEquivType('posts', null); // false\n * ```\n *\n * @param {String} expected a potentially unnormalized type to match against\n * @param {String} actual a potentially unnormalized type to match against\n * @return {Boolean} true if the types are equivalent\n * @public\n */\nexport function isEquivType(expected: string, actual: string): boolean {\n AssertFn('isEquivType: Expected type must not be null', expected !== null);\n AssertFn('isEquivType: Expected type must not be undefined', expected !== undefined);\n AssertFn('isEquivType: Expected type must be a string', typeof expected === 'string');\n AssertFn('isEquivType: Expected type must not be empty', expected.length > 0);\n\n AssertFn('isEquivType: Actual type must not be null', actual !== null);\n AssertFn('isEquivType: Actual type must not be undefined', actual !== undefined);\n AssertFn('isEquivType: Actual type must be a string', typeof actual === 'string');\n AssertFn('isEquivType: Actual type must not be empty', actual.length > 0);\n\n return expected === actual || formattedType(expected) === formattedType(actual);\n}\n\n/**\n * Compares two IDs for strict equality, converting them to\n * the format expected by the EmberData Cache to ensure\n * differences in format are accounted for in the comparison.\n *\n * Asserts when expected or actual are invalid IDs in dev.\n * Expected may never be null.\n *\n * ```js\n * isEquivId('1', 1); // true\n * isEquivId('2', '2'); // true\n * isEquivId(3, '3'); // true\n * isEquivId(4, '3'); // false\n * isEquivId(1, null); // false\n * ```\n *\n * @param {string | number} expected a potentially un-normalized id to match against\n * @param {string | number} actual a potentially un-normalized id to match against\n * @return {Boolean} true if the ids are equivalent\n * @public\n */\nexport function isEquivId(expected: string | number, actual: string | number | null): boolean {\n AssertFn('isEquivId: Expected id must not be null', expected !== null);\n AssertFn('isEquivId: Expected id must not be undefined', expected !== undefined);\n AssertFn(\n 'isEquivId: Expected id must be a number or string',\n typeof expected === 'number' || typeof expected === 'string'\n );\n AssertFn(\n 'isEquivId: Expected id must not be empty',\n typeof expected === 'number' || (typeof expected === 'string' && expected.length > 0)\n );\n AssertFn('isEquivId: Expected id must not be 0', expected !== '0' && expected !== 0);\n\n AssertFn('isEquivId: Actual id must not be undefined', actual !== undefined);\n AssertFn(\n 'isEquivId: Actual id must be a number, string or null',\n typeof actual === 'number' || typeof actual === 'string' || actual === null\n );\n AssertFn(\n 'isEquivId: Actual id must not be empty',\n actual === null || typeof actual === 'number' || (typeof actual === 'string' && actual.length > 0)\n );\n AssertFn('isEquivId: Actual id must not be 0', actual !== '0' && actual !== 0);\n\n return expected === actual || formattedId(expected) === formattedId(actual);\n}\n"],"names":["MismatchReporter","_AssertFn","AssertFn","message","condition","macroCondition","getGlobalConfig","WarpDrive","env","DEBUG","test","Error","NormalizedType","str","singularize","dasherize","configureMismatchReporter","fn","configureAssertFn","configureTypeNormalization","NORMALIZED_TYPES","Map","formattedType","type","undefined","length","normalized","get","set","formattedId","id","formatted","String","expectId","isEquivType","expected","actual","isEquivId"],"mappings":";;;AAAA;AACA;AACA;;AAEA;AACA;AACA;;AAYA,IAAIA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