Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.

Commit a0ec018

Browse files
author
Steffan
committed
add array function
1 parent 27574e4 commit a0ec018

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

src/EventManager.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Event manager class.
33
*/
44

5-
import {isArray, isUndefined} from './util';
5+
import {array, isArray, isUndefined} from './util';
66

77
export default class EventManager {
88

@@ -12,7 +12,7 @@ export default class EventManager {
1212

1313
on(event, callback, priority = 0) {
1414

15-
const listeners = this.listeners[event] || [];
15+
const listeners = array(this.listeners[event]);
1616
const index = listeners.findIndex(listener => listener.priority < priority);
1717

1818
if (~index) {

src/util.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,44 @@ export function forEach(collection, callback) {
1818
});
1919
}
2020

21-
/**
22-
* Array.findIndex() polyfill.
23-
*/
24-
if (!Array.prototype.findIndex) {
21+
export function array(array = []) {
2522

26-
// eslint-disable-next-line
27-
Object.defineProperty(Array.prototype, 'findIndex', {
23+
if (!array.findIndex) {
24+
array.findIndex = findIndex;
25+
}
2826

29-
value(predicate) {
30-
31-
if (this == null) {
32-
throw new TypeError('"this" is null or not defined');
33-
}
27+
return array;
28+
}
3429

35-
if (typeof predicate !== 'function') {
36-
throw new TypeError('predicate must be a function');
37-
}
30+
/**
31+
* Array.findIndex() polyfill.
32+
*/
33+
function findIndex(predicate) {
3834

39-
const o = Object(this);
40-
const len = o.length >>> 0;
41-
const thisArg = arguments[1];
35+
if (this == null) {
36+
throw new TypeError('"this" is null or not defined');
37+
}
4238

43-
let k = 0;
39+
if (typeof predicate !== 'function') {
40+
throw new TypeError('predicate must be a function');
41+
}
4442

45-
while (k < len) {
43+
const o = Object(this);
44+
const len = o.length >>> 0;
45+
const thisArg = arguments[1];
4646

47-
const kValue = o[k];
47+
let k = 0;
4848

49-
if (predicate.call(thisArg, kValue, k, o)) {
50-
return k;
51-
}
49+
while (k < len) {
5250

53-
k++;
54-
}
51+
const kValue = o[k];
5552

56-
return -1;
53+
if (predicate.call(thisArg, kValue, k, o)) {
54+
return k;
5755
}
5856

59-
});
57+
k++;
58+
}
59+
60+
return -1;
6061
}

0 commit comments

Comments
 (0)