The library for web and native user interfaces.
Use `invariant` in `OrderedMap`
Just some therapeutic cleanup.
Tim Yung committed
Jul 24, 2013 at 17:19 UTC
759425fc90642722d5d9f111d373eb55ca84e790
1 file changed
+62
-69
src/utils/OrderedMap.js
+62
-69
@@ -20,40 +20,9 @@
20
21
var invariant = require('invariant');
22
var mixInto = require('mixInto');
23
-var throwIf = require('throwIf');
23
24
var PREFIX = 'key:';
25
27
-var ARRAY_MUST_CB = 'ARRAY_MUST_CB';
28
-var INVALID_KEY = 'INVALID_KEY';
29
-var DUPLICATE_KEY = 'DUPLICATE_KEY';
30
-var OPERATION_ARGS = 'OPERATION_ARGS';
31
-var RANGE_ARGS = 'RANGE_ARGS';
32
-var NO_EXIST = 'NO_EXIST';
33
-
34
-if (__DEV__) {
35
- ARRAY_MUST_CB =
36
- 'If providing an array to an OrderedMap constructor, you must provide ' +
37
- 'a callback that may determine the unique key for each entry. ' +
38
- 'The key that you return from the callback should uniquely define that ' +
39
- 'entity. What is returned from that function must answer the question: ' +
40
- '"If you were to shuffle the array of entities, how would you be able to ' +
41
- 'determine the identity of the Array entry?"';
42
- INVALID_KEY =
43
- 'Key must be non-empty, non-null, string or number';
44
- DUPLICATE_KEY =
45
- 'At creation time, you must extract out IDs that are unique.';
46
- OPERATION_ARGS =
47
- 'Invalid argument type for construction or operation on OrderedMap. ' +
48
- 'from/merge accepts another OrderedMap. fromArray accepts an Array ' +
49
- 'and a callback to extract a unique ID.';
50
- RANGE_ARGS =
51
- 'OrderedMap.mapRange requires end of range to be >= start, start to be ' +
52
- '>= 0 and end < length.';
53
- NO_EXIST =
54
- 'The requested key does not exist in the OrderedMap';
55
-}
56
-
26
/**
27
* Utility to extract a backing object from an initialization `Array`, allowing
28
* the caller to assist in resolving the unique ID for each entry via the
@@ -70,9 +39,12 @@ function extractObjectFromArray(arr, keyExtractor) {
39
for (var i=0; i < arr.length; i++) {
40
var item = arr[i];
41
var key = keyExtractor(item);
73
- validatePublicKey(key);
42
+ assertValidPublicKey(key);
43
var normalizedKey = PREFIX + key;
75
- throwIf(normalizedKey in normalizedObj, DUPLICATE_KEY);
44
+ invariant(
45
+ !(normalizedKey in normalizedObj),
46
+ 'OrderedMap: IDs returned by the key extraction function must be unique.'
47
+ );
48
normalizedObj[normalizedKey] = item;
49
}
50
return normalizedObj;
@@ -120,12 +92,11 @@ function OrderedMapImpl(normalizedObj, computedLength) {
92
* `OrderedMap`.
93
* @throws Error if key is not appropriate for use in `OrderedMap`.
94
*/
123
-function validatePublicKey(key) {
124
- var isEmpty = key === '';
125
- var correctKeyType = typeof key === 'string' || typeof key === 'number';
126
- if (isEmpty || !correctKeyType) {
127
- throw new Error(INVALID_KEY);
128
- }
95
+function assertValidPublicKey(key) {
96
+ invariant(
97
+ key !== '' && (typeof key === 'string' || typeof key === 'number'),
98
+ 'OrderedMap: Key must be non-empty, non-null string or number.'
99
+ );
100
}
101
102
/**
@@ -137,15 +108,16 @@ function validatePublicKey(key) {
108
* exceeded.
109
* @return {void} description
110
*/
140
-function validateRangeIndices(start, length, actualLen) {
141
- var startType = typeof start;
142
- var lengthType = typeof length;
143
- var invalid =
144
- startType !== 'number' || lengthType !== 'number' ||
145
- length < 0 || start < 0 || start + length > actualLen;
146
- if (invalid) {
147
- throw new Error(RANGE_ARGS);
148
- }
111
+function assertValidRangeIndices(start, length, actualLen) {
112
+ invariant(
113
+ typeof start === 'number' &&
114
+ typeof length === 'number' &&
115
+ length >= 0 &&
116
+ start >= 0 &&
117
+ start + length <= actualLen,
118
+ 'OrderedMap: `mapRange` and `forEachRange` expect non-negative start and ' +
119
+ 'length arguments within the bounds of the instance.'
120
+ );
121
}
122
123
/**
@@ -157,10 +129,12 @@ function validateRangeIndices(start, length, actualLen) {
129
* @return {OrderedMap} new `OrderedMap` that results in merging `a` and `b`.
130
*/
131
function _fromNormalizedObjects(a, b) {
160
- // Second optional, both must be a Plain Old JavaScript Object.
161
- var invalidArgs =
162
- !a || a.constructor !== Object || (b && b.constructor !== Object);
163
- throwIf(invalidArgs, OPERATION_ARGS);
132
+ // Second optional, both must be plain JavaScript objects.
133
+ invariant(
134
+ a && a.constructor === Object && (!b || b.constructor === Object),
135
+ 'OrderedMap: Corrupted instance of OrderedMap detected.'
136
+ );
137
+
138
var newSet = {};
139
var length = 0;
140
var key;
@@ -203,7 +177,7 @@ var OrderedMapMethods = {
177
* @throws Error if provided known invalid key.
178
*/
179
has: function(key) {
206
- validatePublicKey(key);
180
+ assertValidPublicKey(key);
181
var normalizedKey = PREFIX + key;
182
return normalizedKey in this._normalizedObj;
183
},
@@ -217,7 +191,7 @@ var OrderedMapMethods = {
191
* @throws Error if provided known invalid key.
192
*/
193
get: function(key) {
220
- validatePublicKey(key);
194
+ assertValidPublicKey(key);
195
var normalizedKey = PREFIX + key;
196
return this.has(key) ? this._normalizedObj[normalizedKey] : undefined;
197
},
@@ -235,7 +209,10 @@ var OrderedMapMethods = {
209
* merge.
210
*/
211
merge: function(orderedMap) {
238
- throwIf(!(orderedMap instanceof OrderedMapImpl), OPERATION_ARGS);
212
+ invariant(
213
+ orderedMap instanceof OrderedMapImpl,
214
+ 'OrderedMap.merge(...): Expected an OrderedMap instance.'
215
+ );
216
return _fromNormalizedObjects(
217
this._normalizedObj,
218
orderedMap._normalizedObj
@@ -267,7 +244,7 @@ var OrderedMapMethods = {
244
var thisSet = this._normalizedObj;
245
var newSet = {};
246
var i = 0;
270
- validateRangeIndices(start, length, this.length);
247
+ assertValidRangeIndices(start, length, this.length);
248
var end = start + length - 1;
249
for (var key in thisSet) {
250
if (thisSet.hasOwnProperty(key)) {
@@ -323,7 +300,7 @@ var OrderedMapMethods = {
300
},
301
302
forEachRange: function(cb, start, length, context) {
326
- validateRangeIndices(start, length, this.length);
303
+ assertValidRangeIndices(start, length, this.length);
304
var thisSet = this._normalizedObj;
305
var i = 0;
306
var end = start + length - 1;
@@ -354,9 +331,10 @@ var OrderedMapMethods = {
331
startIndex !== undefined && endIndex !== undefined,
332
'mapKeyRange must be given keys that are present.'
333
);
357
- if (endIndex < startIndex) {
358
- throw new Error(RANGE_ARGS);
359
- }
334
+ invariant(
335
+ endIndex >= startIndex,
336
+ 'OrderedMap.mapKeyRange(...): `endKey` must not come before `startIndex`.'
337
+ );
338
return this.mapRange(cb, startIndex, (endIndex - startIndex) + 1, context);
339
},
340
@@ -367,9 +345,11 @@ var OrderedMapMethods = {
345
startIndex !== undefined && endIndex !== undefined,
346
'forEachKeyRange must be given keys that are present.'
347
);
370
- if (endIndex < startIndex) {
371
- throw new Error(RANGE_ARGS);
372
- }
348
+ invariant(
349
+ endIndex >= startIndex,
350
+ 'OrderedMap.forEachKeyRange(...): `endKey` must not come before ' +
351
+ '`startIndex`.'
352
+ );
353
this.forEachRange(cb, startIndex, (endIndex - startIndex) + 1, context);
354
},
355
@@ -413,7 +393,11 @@ var OrderedMapMethods = {
393
*/
394
nthKeyAfter: function(key, n) {
395
var curIndex = this.indexOfKey(key);
416
- throwIf(curIndex === undefined, NO_EXIST);
396
+ invariant(
397
+ curIndex !== undefined,
398
+ 'OrderedMap.nthKeyAfter: The key `%s` does not exist in this instance.',
399
+ key
400
+ );
401
return this.keyAtIndex(curIndex + n);
402
},
403
@@ -434,7 +418,7 @@ var OrderedMapMethods = {
418
* key is not found.
419
*/
420
indexOfKey: function(key) {
437
- validatePublicKey(key);
421
+ assertValidPublicKey(key);
422
var normalizedKey = PREFIX + key;
423
var computedPositions = this._getOrComputePositions();
424
var computedPosition = computedPositions.indexByKey[normalizedKey];
@@ -505,14 +489,23 @@ mixInto(OrderedMapImpl, OrderedMapMethods);
489
490
var OrderedMap = {
491
from: function(orderedMap) {
508
- var invalidArg = !(orderedMap instanceof OrderedMapImpl);
509
- throwIf(invalidArg, OPERATION_ARGS);
492
+ invariant(
493
+ orderedMap instanceof OrderedMapImpl,
494
+ 'OrderedMap.from(...): Expected an OrderedMap instance.'
495
+ );
496
return _fromNormalizedObjects(orderedMap._normalizedObj, null);
497
},
498
499
fromArray: function(arr, keyExtractor) {
514
- throwIf(!Array.isArray(arr), OPERATION_ARGS);
515
- throwIf(!keyExtractor, ARRAY_MUST_CB);
500
+ invariant(
501
+ Array.isArray(arr),
502
+ 'OrderedMap.fromArray(...): First argument must be an array.'
503
+ );
504
+ invariant(
505
+ typeof keyExtractor === 'function',
506
+ 'OrderedMap.fromArray(...): Second argument must be a function used ' +
507
+ 'to determine the unique key for each entry.'
508
+ );
509
return new OrderedMapImpl(
510
extractObjectFromArray(arr, keyExtractor),
511
arr.length