-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfaye.js
More file actions
119 lines (102 loc) · 3.19 KB
/
Copy pathfaye.js
File metadata and controls
119 lines (102 loc) · 3.19 KB
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
if (!this.Faye) Faye = {};
Faye.extend = function(dest, source, overwrite) {
if (!source) return dest;
for (var key in source) {
if (!source.hasOwnProperty(key)) continue;
if (dest.hasOwnProperty(key) && overwrite === false) continue;
if (dest[key] !== source[key])
dest[key] = source[key];
}
return dest;
};
Faye.extend(Faye, {
BAYEUX_VERSION: '<%= Faye::BAYEUX_VERSION %>',
VERSION: '<%= Faye::VERSION %>',
JSONP_CALLBACK: '<%= Faye::JSONP_CALLBACK %>',
ID_LENGTH: <%= Faye::ID_LENGTH %>,
CONNECTION_TYPES: <%= Faye::CONNECTION_TYPES.inspect %>,
ENV: this,
Grammar: {
<% %w[ LOWALPHA UPALPHA ALPHA DIGIT
ALPHANUM MARK STRING TOKEN
INTEGER
CHANNEL_SEGMENT CHANNEL_SEGMENTS
CHANNEL_NAME
WILD_CARD CHANNEL_PATTERN
VERSION_ELEMENT VERSION
CLIENT_ID ID
ERROR_MESSAGE ERROR_ARGS
ERROR_CODE ERROR ].each do |bnf| %>
<%= bnf %>: /<%= Faye::Grammar.const_get(bnf).source %>/<%= bnf == 'ERROR' ? '' : ',' %>
<% end %>
},
commonElement: function(lista, listb) {
for (var i = 0, n = lista.length; i < n; i++) {
if (this.indexOf(listb, lista[i]) !== -1)
return lista[i];
}
return null;
},
indexOf: function(list, needle) {
for (var i = 0, n = list.length; i < n; i++) {
if (list[i] === needle) return i;
}
return -1;
},
each: function(object, callback, scope) {
if (object instanceof Array) {
for (var i = 0, n = object.length; i < n; i++) {
if (object[i] !== undefined)
callback.call(scope || null, object[i], i);
}
} else {
for (var key in object) {
if (object.hasOwnProperty(key))
callback.call(scope || null, key, object[key]);
}
}
},
filter: function(array, callback, scope) {
var result = [];
this.each(array, function() {
if (callback.apply(scope, arguments))
result.push(arguments[0]);
});
return result;
},
size: function(object) {
var size = 0;
this.each(object, function() { size += 1 });
return size;
},
random: function(bitlength) {
bitlength = bitlength || this.ID_LENGTH;
if (bitlength > 32) {
var parts = Math.ceil(bitlength / 32),
string = '';
while (parts--) string += this.random(32);
return string;
}
var field = Math.pow(2, bitlength);
return Math.floor(Math.random() * field).toString(16);
},
enumEqual: function(actual, expected) {
if (expected instanceof Array) {
if (!(actual instanceof Array)) return false;
var i = actual.length;
if (i !== expected.length) return false;
while (i--) {
if (actual[i] !== expected[i]) return false;
}
return true;
} else {
if (!(actual instanceof Object)) return false;
if (this.size(expected) !== this.size(actual)) return false;
var result = true;
this.each(actual, function(key, value) {
result = result && (expected[key] === value);
});
return result;
}
}
});