// at top of file (usually):const link = require('link');// Link querytactics = link(allTactics) .where(function(it) { return it.target.isAlive(); }) .where(function(it) { return it.moveIndex < it.moves.length; }) .toArray();
var items1 = ["A", "B"];var items2 = ["C", "D"];Link(items1).cross(items2).each(function(item, index) { Print("Item at: "+ index); Print(item); // e.g: ["A", "C"] or ["B", "D"]});
link(Object.keys(tests)) .where(function(key) { return key.substring(0, 4) === 'test'; }) .where(function(key) { return key !== 'test' }) .map(function(key) { return tests[key]; }) .each(function(test) { if (typeof test === 'function') test(); else if (typeof test === 'object' && test !== null) run(test); });
var obj = { a: 1, b: true, c: "hi" };Link([obj]).keys().forEach(function (key) { console.log(key); }); // "a" "b" "c"Link([obj]).keys().forEach(function (key) { console.log(obj[key]); }); // 1 true "hi"
Alright, I added a .forEach alias so it can relate to other libraries. I didn't want to make each become execute or an alias of it because it may break a lot of existing code (I use this library at work professionally, and so do my coworkers, lol).
// note: ES6 notation because it's more conciseLink(obj).forEach((v, k) => console.log(`${k} = ${v}`));
const from = require('from');// ES6 syntaxvar food = { pig: "people", cow: "kitties", ape: "bananas" };var result = from(food) .where((v, k) => k === 'pig') .besides((v, k) => console.log(`${k} = ${v}`)) // debug .select((v, k) => "812 " + v);console.log("Which animals survived the eating? " + Object.keys(result));console.log("What did the pig eat? " + result.pig);
var obj = Reflect.construct(LinkObj, arguments);
It turns out that almost everything can be implemented in terms of map so there was no need to have a bunch of different link types.
Link([1, 2, 3]).reduce((a, b) => a + b).each(console.log); // 6