Skip to content

Instantly share code, notes, and snippets.

@alanwei43
Created August 5, 2016 05:35
Show Gist options
  • Save alanwei43/34eb9f1a8adb790a1ae379cfcc4269da to your computer and use it in GitHub Desktop.
Save alanwei43/34eb9f1a8adb790a1ae379cfcc4269da to your computer and use it in GitHub Desktop.
//TODO
/**
* filter
* map
* skip
* take
* aggregate
* sum
* order
*/
(function () {
"use strict"
var linqToJs = function (array, prevIterator) {
var currentIterator = undefined;
if (Array.isArray(array)) {
var iteral = function* () {
for (let item of array) {
yield item;
}
};
currentIterator = iteral;
} else if (array instanceof Function) {
currentIterator = array;
} else {
throw "error para type";
}
this.getCurrentIterator = function () {
return currentIterator;
};
this.getPrevIterator = function () {
return prevIterator;
};
};
linqToJs.prototype.reduce = function (callback, initialValue) {
var lastIteral = this.iterals();
var iteral = (function* () {
var firstPreItem = initialValue;
if (arguments.length === 1) {
}
for (let item of lastIteral) {
}
})();
};
linqToJs.prototype.filter = function (filter) {
var prevIterator = this.getPrevIterator() || this.getCurrentIterator();
var iteral = function* () {
console.log("filter");
for (let item of prevIterator()) {
if (filter(item)) yield item;
}
};
return new _(iteral, prevIterator);
};
linqToJs.prototype.map = function (map) {
var prevIterator = this.getPrevIterator() || this.getCurrentIterator();
var iteral = function* () {
console.log("map");
for (let item of prevIterator()) {
yield map(item)
}
};
return new _(iteral, prevIterator);
};
linqToJs.prototype.each = function (each) {
var iterator = this.getCurrentIterator();
for (let item of iterator()) {
each(item);
}
return this;
};
linqToJs.prototype.toArray = function () {
var items = [];
this.each(function (item) {
items.push(item);
});
return items;
}
if (window.module && window.exports) module.exports = linqToJs;
if (window && window.document && window.document.querySelector) {
window._ = linqToJs;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment