|
1 | 1 | ;(function SwivelJS(undefined) { |
2 | 2 | 'use strict'; |
3 | 3 | /** |
4 | | - * SwivelJS v2.1.0 - 2018-05-29 |
| 4 | + * SwivelJS v2.1.1 - 2022-07-24 |
5 | 5 | * Strategy driven, segmented feature toggles |
6 | 6 | * |
7 | | - * Copyright (c) 2018 Zumba® |
| 7 | + * Copyright (c) 2022 Zumba® |
8 | 8 | * Licensed MIT |
9 | 9 | */ |
10 | | - /* jshint freeze: false */ |
11 | | - /* jshint maxcomplexity: 9 */ |
12 | | - |
13 | 10 | // Production steps of ECMA-262, Edition 5, 15.4.4.21 |
14 | 11 | // Reference: http://es5.github.io/#x15.4.4.21 |
15 | 12 | if (!Array.prototype.reduce) { |
16 | | - Array.prototype.reduce = function(callback /*, initialValue*/) { |
17 | | - if (this === null || this === undefined) { |
18 | | - throw new TypeError('Array.prototype.reduce called on null or undefined'); |
19 | | - } |
20 | | - if (typeof callback !== 'function') { |
21 | | - throw new TypeError(callback + ' is not a function'); |
22 | | - } |
23 | | - var t = Object(this), len = t.length >>> 0, k = 0, value; |
24 | | - if (arguments.length === 2) { |
25 | | - value = arguments[1]; |
26 | | - } else { |
27 | | - while (k < len && !(k in t)) { |
28 | | - k++; |
| 13 | + Object.defineProperty(Array.prototype, 'reduce', { |
| 14 | + value: function(callback /*, initialValue*/) { |
| 15 | + /* jshint maxcomplexity: 10 */ |
| 16 | + if (this === null || this === undefined) { |
| 17 | + throw new TypeError('Array.prototype.reduce called on null or undefined'); |
29 | 18 | } |
30 | | - if (k >= len) { |
31 | | - throw new TypeError('Reduce of empty array with no initial value'); |
| 19 | + if (typeof callback !== 'function') { |
| 20 | + throw new TypeError(callback + ' is not a function'); |
32 | 21 | } |
33 | | - value = t[k++]; |
34 | | - } |
35 | | - for (; k < len; k++) { |
36 | | - if (k in t) { |
37 | | - value = callback(value, t[k], k, t); |
| 22 | + var t = Object(this), len = t.length >>> 0, k = 0, value; |
| 23 | + if (arguments.length === 2) { |
| 24 | + value = arguments[1]; |
| 25 | + } else { |
| 26 | + while (k < len && !(k in t)) { |
| 27 | + k++; |
| 28 | + } |
| 29 | + if (k >= len) { |
| 30 | + throw new TypeError('Reduce of empty array with no initial value'); |
| 31 | + } |
| 32 | + value = t[k++]; |
38 | 33 | } |
| 34 | + for (; k < len; k++) { |
| 35 | + if (k in t) { |
| 36 | + value = callback(value, t[k], k, t); |
| 37 | + } |
| 38 | + } |
| 39 | + return value; |
39 | 40 | } |
40 | | - return value; |
41 | | - }; |
| 41 | + }); |
42 | 42 | } |
43 | 43 |
|
44 | | - /* jshint freeze: true */ |
45 | | - /* jshint maxcomplexity: 6 */ |
46 | | - |
47 | 44 | /** |
48 | 45 | * Delimiter |
49 | 46 | * |
|
274 | 271 | }; |
275 | 272 |
|
276 | 273 | /** |
277 | | - * Used by reduceToBitmask |
| 274 | + * Used by parse reducer |
278 | 275 | * |
279 | 276 | * @param Number mask |
280 | 277 | * @param Number index |
281 | 278 | * @return Number |
282 | 279 | */ |
283 | 280 | var bitmaskIterator = function bitmaskIterator(mask, index) { |
| 281 | + if (!index || parseInt(index, 10) === 0) { |
| 282 | + return mask; |
| 283 | + } |
284 | 284 | return mask | 1 << --index; |
285 | 285 | }; |
286 | 286 |
|
|
335 | 335 | return data; |
336 | 336 | }; |
337 | 337 |
|
| 338 | + /** |
| 339 | + * Return the existent fields in base that are missing in compared |
| 340 | + * |
| 341 | + * @param Object base |
| 342 | + * @param Object compared |
| 343 | + * @returns Object |
| 344 | + */ |
| 345 | + var diffMissing = function(base, compared) { |
| 346 | + var data = {}; |
| 347 | + var key; |
| 348 | + |
| 349 | + for (key in base) { |
| 350 | + if (base.hasOwnProperty(key) && compared[key] === undefined) { |
| 351 | + data[key] = base[key]; |
| 352 | + } |
| 353 | + } |
| 354 | + return data; |
| 355 | + }; |
| 356 | + |
338 | 357 | /** |
339 | 358 | * Merge this map with another map and return a new one. |
340 | 359 | * |
|
361 | 380 | FeatureMapPrototype.diff = function diff(featureMap) { |
362 | 381 | var base = this.map; |
363 | 382 | var compared = featureMap.map; |
364 | | - var data = {}; |
| 383 | + var data = Object.assign(diffMissing(compared, base), diffMissing(base, compared)); |
365 | 384 | var key; |
366 | 385 |
|
367 | 386 | for (key in compared) { |
|
370 | 389 | } |
371 | 390 | } |
372 | 391 |
|
373 | | - for (key in base) { |
374 | | - if (base.hasOwnProperty(key) && compared[key] === undefined) { |
375 | | - data[key] = base[key]; |
376 | | - } |
377 | | - } |
378 | | - |
379 | 392 | return new FeatureMap(data); |
380 | 393 | }; |
381 | 394 |
|
|
401 | 414 | key += key ? DELIMITER + child : child; |
402 | 415 |
|
403 | 416 | var isMissing = !this.slugExists(key); |
404 | | - var isDisabled = isMissing || !(map[key] & index); |
| 417 | + var isDisabled = isMissing || !(parseInt(map[key], 10) & index); |
405 | 418 |
|
406 | 419 | if (isMissing || isDisabled) { |
407 | 420 | return false; |
|
575 | 588 | }; |
576 | 589 |
|
577 | 590 | (function exportSwivel(root) { |
| 591 | + /* jshint maxcomplexity: false */ |
578 | 592 |
|
579 | 593 | /** |
580 | 594 | * Free variable exports |
|
0 commit comments