1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
import { extend } from '../utils/adopter.js'
// import { subClassArray } from './ArrayPolyfill.js'
class List extends Array {
constructor(arr = [], ...args) {
super(arr, ...args)
if (typeof arr === 'number') return this
this.length = 0
this.push(...arr)
}
}
/* = subClassArray('List', Array, function (arr = []) {
// This catches the case, that native map tries to create an array with new Array(1)
if (typeof arr === 'number') return this
this.length = 0
this.push(...arr)
}) */
export default List
extend([List], {
each(fnOrMethodName, ...args) {
if (typeof fnOrMethodName === 'function') {
return this.map((el, i, arr) => {
return fnOrMethodName.call(el, el, i, arr)
})
} else {
return this.map((el) => {
return el[fnOrMethodName](...args)
})
}
},
toArray() {
return Array.prototype.concat.apply([], this)
}
})
const reserved = ['toArray', 'constructor', 'each']
List.extend = function (methods) {
methods = methods.reduce((obj, name) => {
// Don't overwrite own methods
if (reserved.includes(name)) return obj
// Don't add private methods
if (name[0] === '_') return obj
// Allow access to original Array methods through a prefix
if (name in Array.prototype) {
obj['$' + name] = Array.prototype[name]
}
// Relay every call to each()
obj[name] = function (...attrs) {
return this.each(name, ...attrs)
}
return obj
}, {})
extend([List], methods)
}
|