Skip to content
This repository has been archived by the owner on Feb 24, 2020. It is now read-only.

Segment-2 #23

Open
wants to merge 3 commits into
base: segment-2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"prettier.useTabs": false,
"editor.insertSpaces": true,
"editor.tabSize": 2,
"editor.detectIndentation": false
}
302 changes: 145 additions & 157 deletions exercise-1.js
Original file line number Diff line number Diff line change
@@ -1,180 +1,168 @@
var assert = require("assert");
const assert = require('assert');

function promised(val) {
return new Promise(function(f) {
setTimeout(function() {
f(val);
}, 1);
});
return new Promise((f) => {
setTimeout(() => {
f(val);
}, 1);
});
}

function thenabled(val, arr) {
return {
then: function(f){
setTimeout(function() {
if (arr) arr.push(val);
f(val);
}, 1);
}
};
return {
then: (f) => {
setTimeout(() => {
if (arr) arr.push(val);
f(val);
}, 1);
},
};
}

describe("Promise.each", function() {

it("should return the array's values mapped", function() {
var a = [promised(1), promised(2), promised(3)];
var b = [];
return Promise.resolve(a).mapSeries(function(val) {
b.push(3-val);
return val + 2;
}).then(function(ret) {
assert.deepEqual(ret, [3,4,5]);
assert.deepEqual(b, [2, 1, 0]);
});
describe('Promise.each', () => {
it("should return the array's values mapped", () => {
const a = [promised(1), promised(2), promised(3)];
const b = [];
return Promise.resolve(a).mapSeries((val) => {
b.push(3 - val);
return val + 2;
}).then((ret) => {
assert.deepEqual(ret, [3, 4, 5]);
assert.deepEqual(b, [2, 1, 0]);
});


it("takes value, index and length", function() {
var a = [promised(1), promised(2), promised(3)];
var b = [];
return Promise.resolve(a).each(function(value, index, length) {
b.push(value, index, length);
}).then(function(ret) {
assert.deepEqual(b, [1, 0, 3, 2, 1, 3, 3, 2, 3]);
});
});

it('takes value, index and length', () => {
const a = [promised(1), promised(2), promised(3)];
const b = [];
return Promise.resolve(a).each((value, index, length) => {
b.push(value, index, length);
}).then(() => {
assert.deepEqual(b, [1, 0, 3, 2, 1, 3, 3, 2, 3]);
});

it("waits for returned promise before proceeding next", function() {
var a = [promised(1), promised(2), promised(3)];
var b = [];
return Promise.resolve(a).each(function(value) {
b.push(value);
return Promise.delay(1).then(function(){
b.push(value*2);
});
}).then(function(ret) {
assert.deepEqual(b, [1,2,2,4,3,6]);
});
});

it('waits for returned promise before proceeding next', () => {
const a = [promised(1), promised(2), promised(3)];
const b = [];
return Promise.resolve(a).each((value) => {
b.push(value);
return Promise.delay(1).then(() => {
b.push(value * 2);
});
}).then(() => {
assert.deepEqual(b, [1, 2, 2, 4, 3, 6]);
});

it("waits for returned thenable before proceeding next", function() {
var b = [1, 2, 3];
var a = [thenabled(1), thenabled(2), thenabled(3)];
return Promise.resolve(a).each(function(val) {
b.push(val * 50);
return thenabled(val * 500, b);
}).then(function(ret) {
assert.deepEqual(b, [1, 2, 3, 50, 500, 100, 1000, 150, 1500]);
});
});

it("doesnt iterate with an empty array", function() {
return Promise.each([], function(val) {
throw new Error();
}).then(function(ret) {
assert.deepEqual(ret, []);
});
});

it('waits for returned thenable before proceeding next', () => {
const b = [1, 2, 3];
const a = [thenabled(1), thenabled(2), thenabled(3)];
return Promise.resolve(a).each((val) => {
b.push(val * 50);
return thenabled(val * 500, b);
}).then(() => {
assert.deepEqual(b, [1, 2, 3, 50, 500, 100, 1000, 150, 1500]);
});

it("iterates with an array of single item", function() {
var b = [];
return Promise.each([promised(1)], function(val) {
b.push(val);
return thenabled(val*2, b);
}).then(function(ret) {
assert.deepEqual(b, [1,2]);
});
});

it('doesnt iterate with an empty array', () => Promise.each([], () => {
throw new Error();
}).then((ret) => {
assert.deepEqual(ret, []);
}));

it('iterates with an array of single item', () => {
const b = [];
return Promise.each([promised(1)], (val) => {
b.push(val);
return thenabled(val * 2, b);
}).then(() => {
assert.deepEqual(b, [1, 2]);
});
});
});

describe("Promise.prototype.each", function() {

it("should return the array's values", function() {
var a = [promised(1), promised(2), promised(3)];
var b = [];
return Promise.resolve(a).each(function(val) {
b.push(3-val);
return val;
}).then(function(ret) {
assert.deepEqual(ret, [1,2,3]);
assert.deepEqual(b, [2, 1, 0]);
});
describe('Promise.prototype.each', () => {
it('should return the array\'s values', () => {
const a = [promised(1), promised(2), promised(3)];
const b = [];
return Promise.resolve(a).each((val) => {
b.push(3 - val);
return val;
}).then((ret) => {
assert.deepEqual(ret, [1, 2, 3]);
assert.deepEqual(b, [2, 1, 0]);
});
});


it("takes value, index and length", function() {
var a = [promised(1), promised(2), promised(3)];
var b = [];
return Promise.resolve(a).each(function(value, index, length) {
b.push(value, index, length);
}).then(function(ret) {
assert.deepEqual(b, [1, 0, 3, 2, 1, 3, 3, 2, 3]);
});
it('takes value, index and length', () => {
const a = [promised(1), promised(2), promised(3)];
const b = [];
return Promise.resolve(a).each((value, index, length) => {
b.push(value, index, length);
}).then(() => {
assert.deepEqual(b, [1, 0, 3, 2, 1, 3, 3, 2, 3]);
});

it("waits for returned promise before proceeding next", function() {
var a = [promised(1), promised(2), promised(3)];
var b = [];
return Promise.resolve(a).each(function(value) {
b.push(value);
return Promise.delay(1).then(function(){
b.push(value*2);
});
}).then(function(ret) {
assert.deepEqual(b, [1,2,2,4,3,6]);
});
});

it('waits for returned promise before proceeding next', () => {
const a = [promised(1), promised(2), promised(3)];
const b = [];
return Promise.resolve(a).each((value) => {
b.push(value);
return Promise.delay(1).then(() => {
b.push(value * 2);
});
}).then(() => {
assert.deepEqual(b, [1, 2, 2, 4, 3, 6]);
});

it("waits for returned thenable before proceeding next", function() {
var b = [1, 2, 3];
var a = [thenabled(1), thenabled(2), thenabled(3)];
return Promise.resolve(a).each(function(val) {
b.push(val * 50);
return thenabled(val * 500, b);
}).then(function(ret) {
assert.deepEqual(b, [1, 2, 3, 50, 500, 100, 1000, 150, 1500]);
});
});

it('waits for returned thenable before proceeding next', () => {
const b = [1, 2, 3];
const a = [thenabled(1), thenabled(2), thenabled(3)];
return Promise.resolve(a).each((val) => {
b.push(val * 50);
return thenabled(val * 500, b);
}).then(() => {
assert.deepEqual(b, [1, 2, 3, 50, 500, 100, 1000, 150, 1500]);
});

it("doesnt iterate with an empty array", function() {
return Promise.resolve([]).each(function(val) {
throw new Error();
}).then(function(ret) {
assert.deepEqual(ret, []);
});
});

it("iterates with an array of single item", function() {
var b = [];
return Promise.resolve([promised(1)]).each(function(val) {
b.push(val);
return thenabled(val*2, b);
}).then(function(ret) {
assert.deepEqual(b, [1,2]);
});
});

it('doesnt iterate with an empty array', () => Promise.resolve([]).each(() => {
throw new Error();
}).then((ret) => {
assert.deepEqual(ret, []);
}));

it('iterates with an array of single item', () => {
const b = [];
return Promise.resolve([promised(1)]).each((val) => {
b.push(val);
return thenabled(val * 2, b);
}).then(() => {
assert.deepEqual(b, [1, 2]);
});
});
});

describe("mapSeries and each", function() {
it("is mixed", function() {
return Promise.mapSeries([1, 2, 3], function(value) {
return value * 2;
}).then(function(result) {
assert.deepEqual(result, [2, 4, 6]);
}).then(function() {
return Promise.each([1, 2, 3], function(value) {
return value * 2;
}).then(function(result) {
assert.deepEqual(result, [1, 2, 3]);
});
}).thenReturn([1, 2, 3]).mapSeries(function(value) {
return value * 2;
}).then(function(result) {
assert.deepEqual(result, [2, 4, 6]);
}).thenReturn([1, 2, 3]).each(function(value) {
return value * 2;
}).then(function(result) {
assert.deepEqual(result, [1, 2, 3]);
});
})
});
describe('mapSeries and each', () => {
it('is mixed', () =>
Promise.mapSeries([1, 2, 3], value => value * 2)
.then((result) => {
assert.deepEqual(result, [2, 4, 6]);
}).then(() => Promise.each([1, 2, 3], value => value * 2).then((result) => {
assert.deepEqual(result, [1, 2, 3]);
})).thenReturn([1, 2, 3])
.mapSeries(value => value * 2)
.then((result) => {
assert.deepEqual(result, [2, 4, 6]);
})
.thenReturn([1, 2, 3])
.each(value => value * 2)
.then((result) => {
assert.deepEqual(result, [1, 2, 3]);
}));
});
Loading