Skip to content
This repository was archived by the owner on Sep 13, 2021. It is now read-only.

Commit a58e376

Browse files
committed
Fixed lastIndexOf. Added test of implementation
1 parent 604f9d7 commit a58e376

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

array.generics.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
begin = toInteger(begin);
154154

155155
// handle -begin, begin > length - 1
156-
index = 0 > begin ? Math.max(length + begin, 0) : Math.min(begin, length - 1);
156+
index = 0 > begin ? length - Math.abs(begin) : Math.min(begin, length - 1);
157157
} else {
158158
// default value
159159
index = length - 1;
@@ -456,17 +456,53 @@
456456
}
457457
}
458458

459+
/**
460+
* Helper function.
461+
* Tests implementation of standard Array method.
462+
*/
463+
function supportsStandard(key) {
464+
var support = true;
465+
if (Array.prototype[key]) {
466+
try {
467+
Array.prototype[key].call(undefined, /test/, null);
468+
support = false;
469+
} catch (e) {}
470+
} else {
471+
support = false;
472+
}
473+
return support;
474+
}
475+
476+
/**
477+
* Helper function.
478+
* Tests implementation of generic Array method.
479+
*/
480+
function supportsGeneric(key) {
481+
var support = true;
482+
if (Array[key]) {
483+
try {
484+
Array[key](undefined, /test/, null);
485+
support = false;
486+
} catch (e) {}
487+
} else {
488+
support = false;
489+
}
490+
return support;
491+
}
492+
459493
/**
460494
* Helper function.
461495
* Assigns method to Array constructor.
462496
*/
463497
function extendArray(key) {
464-
Array[key] = Array[key] || makeStatic(key);
498+
if (!supportsGeneric(key)) {
499+
Array[key] = makeStatic(key);
500+
}
465501
}
466502

467503
/**
468504
* Helper function.
469-
* Creates static method from an instance method.
505+
* Creates generic method from an instance method.
470506
*/
471507
function makeStatic(key) {
472508
return function (value) {
@@ -495,10 +531,14 @@
495531
"reduce": reduce,
496532
"reduceRight": reduceRight
497533
};
534+
498535
for (var key in ES5) {
499536
if (ES5.hasOwnProperty(key)) {
537+
538+
if (!supportsStandard(key)) {
539+
Array.prototype[key] = ES5[key];
540+
}
500541
extendArray(key);
501-
Array.prototype[key] = Array.prototype[key] || ES5[key];
502542
}
503543
}
504544
Array.isArray = Array.isArray || isArray;

array.generics.min.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)