forked from kriszyp/promised-io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazy-array.js
158 lines (154 loc) · 3.92 KB
/
lazy-array.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
({define:typeof define!="undefined"?define:function(deps, factory){module.exports = factory.apply(this, deps.map(function(id){return require(id)}))}}).
define(["./promise"], function(promise){
try{
var when = promise.when;
}catch(e){
console.log("couldn't load promise library", e.stack);
when = function(value, callback){
return callback(value);
}
}
function LazyArray(hasSomeAndLength){
return new SomeWrapper(hasSomeAndLength);
};
var exports = LazyArray;
exports.LazyArray = LazyArray;
exports.first = function(array){
return exports.get(array, 0);
};
exports.last = function(array){
return exports.get(array, array.length-1);
};
exports.get = function(array, index){
var result, i = 0;
return when(array.some(function(item){
if(i == index){
result = item;
return true;
}
i++;
}),
function(){
return result;
});
};
var testProto = {};
var testProto2 = {a:"b"};
testProto.__proto__ = testProto2;
var mutableProto = testProto.a == "b";
function SomeWrapper(hasSomeAndLength){
if(mutableProto){
hasSomeAndLength.source = hasSomeAndLength;
hasSomeAndLength.__proto__ = SomeWrapper.prototype;
return hasSomeAndLength;
}
this.source = hasSomeAndLength;
if(hasSomeAndLength.length){
this.length = hasSomeAndLength.length;
}
this.totalCount = hasSomeAndLength.totalCount;
}
exports.LazyArray.prototype = SomeWrapper.prototype = [];
SomeWrapper.prototype.some = function(callback){
this.source.some(callback);
}
SomeWrapper.prototype.filter = function(fn, thisObj){
var results = [];
return when(this.source.some(function(item){
if(fn.call(thisObj, item)){
results.push(item);
}
}), function(){
return results;
});
};
SomeWrapper.prototype.every = function(fn, thisObj){
return when(this.source.some(function(item){
if(!fn.call(thisObj, item)){
return true;
}
}), function(result){return !result;});
};
SomeWrapper.prototype.forEach= function(fn, thisObj){
return this.source.some(function(item){
fn.call(thisObj, item);
});
};
SomeWrapper.prototype.concat = function(someOther){
var source = this.source;
return new SomeWrapper({
length : source.length + someOther.length,
some : function(fn,thisObj){
return when(source.some(fn,thisObj), function(result){
return result || someOther.some(fn,thisObj);
});
}
});
};
SomeWrapper.prototype.map = function(mapFn, mapThisObj){
var source = this.source;
return new SomeWrapper({
length : source.length,
some : function(fn,thisObj){
return source.some(function(item){
return fn.call(thisObj, mapFn.call(mapThisObj, item));
});
}
});
};
SomeWrapper.prototype.toRealArray= function(mapFn, mapThisObj){
var array = [];
return when(this.source.some(function(item){
array.push(item);
}), function(){
return array;
});
};
SomeWrapper.prototype.join = function(){
var args = arguments;
return when(this.toRealArray(), function(realArray){
return Array.prototype.join.apply(realArray, args);
});
};
SomeWrapper.prototype.sort = function(){
var args = arguments;
return when(this.toRealArray(), function(realArray){
return Array.prototype.sort.apply(realArray, args);
});
};
SomeWrapper.prototype.reverse = function(){
var args = arguments;
return when(this.toRealArray(), function(realArray){
return Array.prototype.reverse.apply(realArray, args);
});
};
SomeWrapper.prototype.get = SomeWrapper.prototype.item = function(index){
var result, i = 0;
return when(this.source.some(function(item){
if(i == index){
result = item;
return true;
}
i++;
}), function(){
return result;
});
};
SomeWrapper.prototype.toSource = function(){
var serializedParts = [];
return when(this.source.some(function(item){
serializedParts.push(item && item.toSource());
}), function(){
return '[' + serializedParts.join(",") + ']';
});
};
SomeWrapper.prototype.toJSON = function(){
var loadedParts = [];
return when(this.source.some(function(item){
loadedParts.push(item);
}), function(){
return loadedParts;
});
};
return exports;
});