Skip to content

Commit

Permalink
Added support for iterable objects in lib/array.js
Browse files Browse the repository at this point in the history
There is still one trouble with lib/chain.js. I mean `fetch after fetch method`. I can't implement chain of async functions that operates on previous fn's returning data without using state, becuse if there are two fetchs, the will operate on initial data parallel, not consistently.
  • Loading branch information
Yehor Khilchenko committed Mar 29, 2019
1 parent 4596e10 commit 2fe1576
Show file tree
Hide file tree
Showing 11 changed files with 359 additions and 331 deletions.
390 changes: 164 additions & 226 deletions lib/array.js

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions lib/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,37 @@ ArrayChain.prototype.fetch = function(fn) {
return this;
};

// ArrayChain.prototype.execute = function(chain) {
// this.chain = [];
// return new Promise((resolve, reject) => {
// reduce(
// chain,
// (prev, cur, cb) => {
// async(cur.op)(prev, cur.fn, (err, res) => {
// if (err) cb(err);
// else cb(null, res);
// });
// },
// (err, res) => {
// if (err) reject(err);
// else resolve(res);
// },
// this.array.slice()
// );
// });
// };

// ArrayChain.prototype.fetch = function(fn) {
// const next = (err, data) => {
// this.array = data;
// if (err) throw err;
// };
// this.execute(this.chain)
// .then(r => fn(null, r, next))
// .catch(e => fn(e));
// return this;
// };

ArrayChain.prototype.map = function(fn) {
this.chain.push({ op: 'map', fn });
return this;
Expand Down
9 changes: 1 addition & 8 deletions lib/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@

const common = require('@metarhia/common');

const { each } = require('./array');

// Executes all asynchronous functions and pass first result to callback
// fns - <Function[]>, callback-last / err-first
// callback - <Function>, on done, err-first
const firstOf = (fns, callback) => {
const done = common.once(callback);
each(fns, (f, iterCb) =>
f((...args) => {
done(...args);
iterCb(...args);
})
);
fns.forEach(fn => fn((...args) => done(...args)));
};

// Parallel execution
Expand Down
30 changes: 22 additions & 8 deletions test/array.asyncMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ metatests.test('succesfull map / set', test => {
metasync.asyncMap(
set,
item => item * 2,
(err, newArr) => {
(err, newSet) => {
test.error(err);
test.strictSame(newArr, expectedSet);
test.strictSame([...newSet], [...expectedSet]);
}
);
});
Expand All @@ -39,14 +39,14 @@ metatests.test('succesfull map / map', test => {
test.plan(2);

const map = new Map([[1, 'a'], [2, 'b'], [3, 'c']]);
const expectedMap = new Map([[1, 'a'], [4, 'b'], [9, 'c']]);
const expectedMap = new Map([['a', 1], ['b', 2], ['c', 3]]);

metasync.asyncMap(
map,
item => item[0] * 2,
(err, newArr) => {
item => item.reverse(),
(err, newMap) => {
test.error(err);
test.strictSame(newArr, expectedMap);
test.strictSame(newMap, expectedMap);
}
);
});
Expand All @@ -60,9 +60,9 @@ metatests.test('succesfull map / string', test => {
metasync.asyncMap(
string,
item => item.toUpperCase(),
(err, newArr) => {
(err, newStr) => {
test.error(err);
test.strictSame(newArr, expectedStr);
test.strictSame(newStr, expectedStr);
}
);
});
Expand Down Expand Up @@ -100,3 +100,17 @@ metatests.test('Non-blocking', test => {
}
);
});

metatests.test('map with another object', test => {
const obj = { a: '1', b: '2', c: '3' };

metasync.map(
obj,
item => item * 2,
(err, res) => {
test.error(err);
test.strictSame(res, null);
test.end();
}
);
});
60 changes: 24 additions & 36 deletions test/array.each.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,144 +5,132 @@ const metatests = require('metatests');

metatests.test('successful each / array', test => {
const arr = [1, 2, 3, 4];

const elementsSet = new Set();
const expectedElementsSet = new Set(arr);
const elementsArr = [];

metasync.each(
arr,
(el, callback) =>
process.nextTick(() => {
elementsSet.add(el);
elementsArr.push(el);
callback(null);
}),
err => {
test.error(err);
test.strictSame(elementsSet, expectedElementsSet);
test.strictSame(elementsArr, arr);
test.end();
}
);
});

metatests.test('successful each / set', test => {
const set = new Set([1, 2, 3, 4, 5]);
const elementsSet = new Set();
const elementsArr = [];

metasync.each(
set,
(el, callback) =>
process.nextTick(() => {
elementsSet.add(el);
elementsArr.push(el);
callback(null);
}),
err => {
test.error(err);
test.strictSame(elementsSet, set);
test.strictSame(elementsArr, [...set]);
test.end();
}
);
});

metatests.test('successful each / map', test => {
const map = new Map([[1, 'a'], [2, 'b'], [3, 'c']]);

const elementsSet = new Set();
const expectedElementsSet = new Set(map);
const elementsArr = [];

metasync.each(
map,
(el, callback) =>
process.nextTick(() => {
elementsSet.add(el);
elementsArr.push(el);
callback(null);
}),
err => {
test.error(err);
test.strictSame(elementsSet, expectedElementsSet);
test.strictSame(elementsArr, [...map]);
test.end();
}
);
});

metatests.test('successful each / string', test => {
const string = 'aaabcdeefff';

const elementsSet = new Set();
const expectedElementsSet = new Set();
const elementsArr = [];

metasync.each(
string,
(el, callback) =>
process.nextTick(() => {
elementsSet.add(el);
elementsArr.push(el);
callback(null);
}),
err => {
test.error(err);
test.strictSame(elementsSet, expectedElementsSet);
test.strictSame(elementsArr, [...string]);
test.end();
}
);
});

metatests.test('each with empty / array', test => {
const arr = [];

const elementsSet = new Set();
const expectedElementsSet = new Set(arr);
const elementsArr = [];

metasync.each(
arr,
(el, callback) =>
process.nextTick(() => {
elementsSet.add(el);
elementsArr.push(el);
callback(null);
}),
err => {
test.error(err);
test.strictSame(elementsSet, expectedElementsSet);
test.strictSame(elementsArr, arr);
test.end();
}
);
});

metatests.test('each with empty / map', test => {
const map = new Map();

const elementsSet = new Set();
const expectedElementsSet = new Set(map);
const elementsArr = [];

metasync.each(
map,
(el, callback) =>
process.nextTick(() => {
elementsSet.add(el);
elementsArr.add(el);
callback(null);
}),
err => {
test.error(err);
test.strictSame(elementsSet, expectedElementsSet);
test.strictSame(elementsArr, [...map]);
test.end();
}
);
});

metatests.test('each with empty / string', test => {
const string = '';

const elementsSet = new Set();
const expectedElementsSet = new Set(string);
const elementsArr = [];

metasync.each(
string,
(el, callback) =>
process.nextTick(() => {
elementsSet.add(el);
elementsArr.push(el);
callback(null);
}),
err => {
test.error(err);
test.strictSame(elementsSet, expectedElementsSet);
test.strictSame(elementsArr, [...string]);
test.end();
}
);
Expand All @@ -151,18 +139,18 @@ metatests.test('each with empty / string', test => {
metatests.test('each with empty / set', test => {
const set = new Set();

const elementsSet = new Set();
const elementsArr = [];

metasync.each(
set,
(el, callback) =>
process.nextTick(() => {
elementsSet.add(el);
elementsArr.add(el);
callback(null);
}),
err => {
test.error(err);
test.strictSame(elementsSet, set);
test.strictSame(elementsArr, [...set]);
test.end();
}
);
Expand Down
Loading

0 comments on commit 2fe1576

Please sign in to comment.