-
Notifications
You must be signed in to change notification settings - Fork 18
/
util.js
161 lines (136 loc) · 4.12 KB
/
util.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
Rye.define('Util', function(){
var _slice = Array.prototype.slice
, _forEach = Array.prototype.forEach
, _toString = Object.prototype.toString
var uid = {
current: 0
, next: function(){ return ++this.current }
}
function each (obj, fn, context) {
if (!obj) {
return
}
if (obj.forEach === _forEach) {
return obj.forEach(fn, context)
}
if (obj.length === +obj.length) {
for (var i = 0; i < obj.length; i++) {
fn.call(context || obj, obj[i], i, obj)
}
} else {
var keys = Object.keys(obj)
for (var i = 0; i < keys.length; i++) {
var key = keys[i]
fn.call(context || obj, obj[key], key, obj)
}
}
}
function extend (obj) {
each(_slice.call(arguments, 1), function(source){
each(source, function(value, key){
obj[key] = value
})
})
return obj
}
function inherits (child, parent) {
extend(child, parent)
function Ctor () {
this.constructor = child
}
Ctor.prototype = parent.prototype
child.prototype = new Ctor()
child.__super__ = parent.prototype
return child
}
function isElement (element) {
return element && (element.nodeType === 1 || element.nodeType === 9)
}
function isNodeList (obj) {
return obj && is(['nodelist', 'htmlcollection', 'htmlformcontrolscollection'], obj)
}
function unique (array) {
return array.filter(function(item, idx){
return array.indexOf(item) == idx
})
}
function pluck (array, property) {
return array.map(function(item){
return item[property]
})
}
function put (array, property, value) {
return array.forEach(function(item, i){
array[i][property] = value
})
}
function prefix (key, obj) {
var result
, upcased = key[0].toUpperCase() + key.substring(1)
, prefixes = ['moz', 'webkit', 'ms', 'o']
, p
obj = obj || window
if (result = obj[key]){
return result
}
// No pretty array methods here :(
// http://jsperf.com/everywhile
while(p = prefixes.shift()){
if (result = obj[p + upcased]){
break;
}
}
return result
}
function _apply (context, fn, applyArgs, cutoff, fromLeft) {
if (typeof fn === 'string') {
fn = context[fn]
}
return function () {
var args = _slice.call(arguments, 0, cutoff || Infinity)
if (applyArgs) {
args = fromLeft ? applyArgs.concat(args) : args.concat(applyArgs)
}
if (typeof context === 'number') {
context = args[context]
}
return fn.apply(context || this, args)
}
}
function applyRight (context, fn, applyArgs, cutoff) {
return _apply(context, fn, applyArgs, cutoff)
}
function applyLeft (context, fn, applyArgs, cutoff) {
return _apply(context, fn, applyArgs, cutoff, true)
}
function curry (fn) {
return applyLeft(this, fn, _slice.call(arguments, 1))
}
function getUid (element) {
return element.rye_id || (element.rye_id = uid.next())
}
function type (obj) {
var ref = _toString.call(obj).match(/\s(\w+)\]$/)
return ref && ref[1].toLowerCase()
}
function is (kind, obj) {
return kind.indexOf(type(obj)) >= 0
}
return {
each : each
, extend : extend
, inherits : inherits
, isElement : isElement
, isNodeList : isNodeList
, unique : unique
, pluck : pluck
, put : put
, prefix : prefix
, applyRight : applyRight
, applyLeft : applyLeft
, curry : curry
, getUid : getUid
, type : type
, is : is
}
})