-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclunderscore.js
169 lines (152 loc) · 4.46 KB
/
clunderscore.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* global HTMLCollection, NodeList */
;(function () {
var arrayProperties = {}
var liveCollectionProperties = {}
var functionProperties = {}
var objectProperties = {}
var slice = Array.prototype.slice
arrayProperties.cl_each = function (cb) {
var i = 0
var length = this.length
for (; i < length; i++) {
cb(this[i], i, this)
}
}
arrayProperties.cl_map = function (cb) {
var i = 0
var length = this.length
var result = Array(length)
for (; i < length; i++) {
result[i] = cb(this[i], i, this)
}
return result
}
arrayProperties.cl_reduce = function (cb, memo) {
var i = 0
var length = this.length
for (; i < length; i++) {
memo = cb(memo, this[i], i, this)
}
return memo
}
arrayProperties.cl_some = function (cb) {
var i = 0
var length = this.length
for (; i < length; i++) {
if (cb(this[i], i, this)) {
return true
}
}
}
arrayProperties.cl_filter = function (cb) {
var i = 0
var length = this.length
var result = []
for (; i < length; i++) {
cb(this[i], i, this) && result.push(this[i])
}
return result
}
liveCollectionProperties.cl_each = function (cb) {
slice.call(this).cl_each(cb)
}
liveCollectionProperties.cl_map = function (cb) {
return slice.call(this).cl_map(cb)
}
liveCollectionProperties.cl_reduce = function (cb, memo) {
return slice.call(this).cl_reduce(cb, memo)
}
functionProperties.cl_bind = function (context) {
var self = this
var args = slice.call(arguments, 1)
context = context || null
return args.length
? function () {
return arguments.length
? self.apply(context, args.concat(slice.call(arguments)))
: self.apply(context, args)
}
: function () {
return arguments.length
? self.apply(context, arguments)
: self.call(context)
}
}
objectProperties.cl_each = function (cb) {
var i = 0
var keys = Object.keys(this)
var length = keys.length
for (; i < length; i++) {
cb(this[keys[i]], keys[i], this)
}
}
objectProperties.cl_map = function (cb) {
var i = 0
var keys = Object.keys(this)
var length = keys.length
var result = Array(length)
for (; i < length; i++) {
result[i] = cb(this[keys[i]], keys[i], this)
}
return result
}
objectProperties.cl_reduce = function (cb, memo) {
var i = 0
var keys = Object.keys(this)
var length = keys.length
for (; i < length; i++) {
memo = cb(memo, this[keys[i]], keys[i], this)
}
return memo
}
objectProperties.cl_some = function (cb) {
var i = 0
var keys = Object.keys(this)
var length = keys.length
for (; i < length; i++) {
if (cb(this[keys[i]], keys[i], this)) {
return true
}
}
}
objectProperties.cl_extend = function (obj) {
if (obj) {
var i = 0
var keys = Object.keys(obj)
var length = keys.length
for (; i < length; i++) {
this[keys[i]] = obj[keys[i]]
}
}
return this
}
function build (properties) {
return objectProperties.cl_reduce.call(properties, function (memo, value, key) {
memo[key] = {
value: value
}
return memo
}, {})
}
arrayProperties = build(arrayProperties)
liveCollectionProperties = build(liveCollectionProperties)
functionProperties = build(functionProperties)
objectProperties = build(objectProperties)
/* eslint-disable no-extend-native */
Object.defineProperties(Array.prototype, arrayProperties)
Object.defineProperties(Int8Array.prototype, arrayProperties)
Object.defineProperties(Uint8Array.prototype, arrayProperties)
Object.defineProperties(Uint8ClampedArray.prototype, arrayProperties)
Object.defineProperties(Int16Array.prototype, arrayProperties)
Object.defineProperties(Uint16Array.prototype, arrayProperties)
Object.defineProperties(Int32Array.prototype, arrayProperties)
Object.defineProperties(Uint32Array.prototype, arrayProperties)
Object.defineProperties(Float32Array.prototype, arrayProperties)
Object.defineProperties(Float64Array.prototype, arrayProperties)
Object.defineProperties(Function.prototype, functionProperties)
Object.defineProperties(Object.prototype, objectProperties)
if (typeof window !== 'undefined') {
Object.defineProperties(HTMLCollection.prototype, liveCollectionProperties)
Object.defineProperties(NodeList.prototype, liveCollectionProperties)
}
})()