Skip to content

Commit

Permalink
find and replace of chai and solution imports to use ESM and extension
Browse files Browse the repository at this point in the history
  • Loading branch information
unjust committed Apr 9, 2024
1 parent 02243f4 commit 2fe343f
Show file tree
Hide file tree
Showing 258 changed files with 757 additions and 765 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ const applyDiscount = (cart) => {
return [current].concat(applyDiscount(cart));
};

module.exports = applyDiscount;
export default applyDiscount;
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ const serializeUser = user => {
return JSON.stringify(user);
};

module.exports = serializeUser;
export default serializeUser;
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ const serializeUser = user => JSON.stringify(Object.assign({}, user, {
date: user.date.toJSON(),
}));

module.exports = serializeUser;
export default serializeUser;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Assert = require('chai').assert;
const Submission = require('../solution/serializeUser');
import { assert } from 'chai';
import Submission from '../solution/serializeUser.js';


describe('serializeUser()', () => {
Expand All @@ -12,7 +12,7 @@ describe('serializeUser()', () => {
date: new Date()
};
const dateStr = grace.date.toJSON();
Assert.equal(
assert.equal(
Submission(grace),
`{"userId":"xxx","name":"Grace H...","country":"us","date":"${dateStr}"}`
);
Expand All @@ -26,8 +26,8 @@ describe('serializeUser()', () => {
date: new Date()
};
Submission(grace);
Assert.equal(grace.name, 'Grace Hopper');
Assert.ok(grace.date instanceof Date);
assert.equal(grace.name, 'Grace Hopper');
assert.ok(grace.date instanceof Date);
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ const parseItems = (items) => {
return items.sort();
};

module.exports = parseItems;
export default parseItems;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const parseItems = items => items.map(item => parseInt(item)).sort();

module.exports = parseItems;
export default parseItems;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Assert = require('chai').assert;
const Submission = require('../solution/parseItems');
import { assert } from 'chai';
import Submission from '../solution/parseItems.js';


describe('parseItems()', () => {
Expand All @@ -8,22 +8,22 @@ describe('parseItems()', () => {
const original = ['6', '3', '5', '2', '4'];
const parsed = Submission(original);

Assert.notDeepEqual(original, parsed);
Assert.notEqual(original, parsed);
assert.notDeepEqual(original, parsed);
assert.notEqual(original, parsed);
});

it('debería retornar un arreglo de números', () => {
Submission(['6', '3', '5', '2', '4']).forEach(Assert.isNumber);
Submission(['6', '3', '5', '2', '4']).forEach(assert.isNumber);
});

it('debería retornar un arreglo ordenado', () => {
Assert.deepEqual(Submission(['6', '3', '5', '2', '4']), [2, 3, 4, 5, 6]);
assert.deepEqual(Submission(['6', '3', '5', '2', '4']), [2, 3, 4, 5, 6]);
});

it('no debería mutar arreglo de entrada', () => {
const original = ['6', '3', '5', '2', '4'];
const parsed = Submission(original);
Assert.deepEqual(original, ['6', '3', '5', '2', '4']);
assert.deepEqual(original, ['6', '3', '5', '2', '4']);
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ function repeat(operation, num) {
}

// No borres la línea de abajo
module.exports = repeat;
export default repeat;
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ function repeat(operation, num) {
}

// No borres la línea de abajo
module.exports = repeat;
export default repeat;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Assert = require('chai').assert;
const Submission = require('../solution/repeat');
import { assert } from 'chai';
import Submission from '../solution/repeat.js';


describe('repeat()', () => {
Expand All @@ -8,26 +8,26 @@ describe('repeat()', () => {
let calls = 0;
const fn = () => calls++;
Submission(fn, 5);
Assert.equal(calls, 5);
assert.equal(calls, 5);
});

it('debería invocar la función 8 veces cuando n es 8', () => {
let calls = 0;
const fn = () => calls++;
Submission(fn, 8);
Assert.equal(calls, 8);
assert.equal(calls, 8);
});

it('no debería invocar la función cuando n es 0', () => {
let calls = 0;
const fn = () => calls++;
Submission(fn, 0);
Assert.equal(calls, 0);
assert.equal(calls, 0);
});

it('debería retornar undefined', () => {
const fn = () => true;
Assert.equal(Submission(fn, 5), undefined);
assert.equal(Submission(fn, 5), undefined);
});

});
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
exports.log = null;
export const log = null;

exports.logger = null;
export const logger = null;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
exports.log = (...args) => args.join(' ');
export const log = (...args) => args.join(' ');

exports.logger = namespace => exports.log.bind(null, namespace);
export const logger = namespace => log.bind(null, namespace);
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const Assert = require('chai').assert;
const Sinon = require('sinon');
const Submission = require('../solution/boundLogger');
import { assert } from 'chai';
import Sinon from 'sinon';
import * as Submission from '../solution/boundLogger.js';


describe('with bind', () => {

it('debería exportar log() y logger()', () => {
Assert.equal(typeof Submission.log, 'function');
Assert.equal(typeof Submission.logger, 'function');
assert.equal(typeof Submission.log, 'function');
assert.equal(typeof Submission.logger, 'function');
});

describe('log()', () => {
Expand All @@ -19,7 +19,7 @@ describe('with bind', () => {
[['foo', 'bar', 'baz', 1, 2, 3], 'foo bar baz 1 2 3'],
].forEach(pair => {
it(`debería retornar "${pair[1]}" para [${pair[0]}]`, () => {
Assert.equal(Submission.log.apply(null, pair[0]), pair[1]);
assert.equal(Submission.log.apply(null, pair[0]), pair[1]);
});
});

Expand All @@ -29,12 +29,12 @@ describe('with bind', () => {

it('debería combinar namespace + 1 string', () => {
const info = Submission.logger('INFO:');
Assert.equal(info('this is an info message'), 'INFO: this is an info message');
assert.equal(info('this is an info message'), 'INFO: this is an info message');
});

it('debería combinar namespace + 2 strings', () => {
const warn = Submission.logger('WARN:');
Assert.equal(
assert.equal(
warn('this is a warning message', 'with more info'),
'WARN: this is a warning message with more info'
);
Expand All @@ -43,9 +43,9 @@ describe('with bind', () => {
it('debería usar Function.prototype.bind', () => {
const spy = Sinon.spy(Function.prototype, 'bind');
Submission.logger('ERROR:')('foo');
Assert.equal(spy.callCount, 1);
Assert.equal(spy.args[0][0], null);
Assert.equal(spy.args[0][1], 'ERROR:');
assert.equal(spy.callCount, 1);
assert.equal(spy.args[0][0], null);
assert.equal(spy.args[0][1], 'ERROR:');
spy.restore();
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const logger = null;

module.exports = logger;
export default logger;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const logger = namespace => (...args) => `${namespace} ${args.join(' ')}`;

module.exports = logger;
export default logger;
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
const Assert = require('chai').assert;
const Sinon = require('sinon');
const Submission = require('../solution/logger');
import { assert } from 'chai';
import Sinon from 'sinon';
import Submission from '../solution/logger.js';


describe('logger()', () => {

it('debería exportar una función', () => {
Assert.equal(typeof Submission, 'function');
assert.equal(typeof Submission, 'function');
});

it('debería combinar namespace + 1 string', () => {
const info = Submission('INFO:');
Assert.equal(info('this is an info message'), 'INFO: this is an info message');
assert.equal(info('this is an info message'), 'INFO: this is an info message');
});

it('debería combinar namespace + 2 strings', () => {
const warn = Submission('WARN:');
Assert.equal(
assert.equal(
warn('this is a warning message', 'with more info'),
'WARN: this is a warning message with more info'
);
Expand All @@ -25,7 +25,7 @@ describe('logger()', () => {
it('no debería usar Function.prototype.bind', () => {
const spy = Sinon.spy(Function.prototype, 'bind');
Submission('ERROR:')('foo');
Assert.equal(spy.callCount, 0);
assert.equal(spy.callCount, 0);
spy.restore();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ function curryN(fn, n) {
// La solución vá aquí,
}

module.exports = curryN
export default curryN;
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ function curryN(fn, n) {
}
}

module.exports = curryN
export default curryN
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const Assert = require('chai').assert;
const Sinon = require('sinon');
const Submission = require('../solution/curryN');
import { assert } from 'chai';
import Sinon from 'sinon';
import Submission from '../solution/curryN.js';


describe('curryN()', () => {

it('debería exportar una función', () => {
Assert.equal(typeof Submission, 'function');
assert.equal(typeof Submission, 'function');
});

it('debería crear cadena de funciones hasta haber agotado todos los argumentos', () => {
Expand All @@ -18,15 +18,15 @@ describe('curryN()', () => {
const curryB = curryC(1);
const curryA = curryB(2);

Assert.equal(curryA(3), 6);
Assert.equal(curryA(10), 13);
assert.equal(curryA(3), 6);
assert.equal(curryA(10), 13);

Assert.equal(Submission(add3)(1)(2)(3), 6);
assert.equal(Submission(add3)(1)(2)(3), 6);
});

it('no debería usar bucles for/while', () => {
const fnBody = Submission.toString();
Assert.equal(/(for|while)\s*\(/g.test(fnBody), false);
assert.equal(/(for|while)\s*\(/g.test(fnBody), false);
});

it('no debería usar forEach', () => {
Expand All @@ -36,8 +36,8 @@ describe('curryN()', () => {

const spy = Sinon.spy(Array.prototype, 'forEach');

Assert.equal(Submission(add3)(1)(2)(3), 6);
Assert.equal(spy.callCount, 0);
assert.equal(Submission(add3)(1)(2)(3), 6);
assert.equal(spy.callCount, 0);
spy.restore();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ const applyDiscount = (cart, discount) => {
return cart;
};

module.exports = applyDiscount;
export default applyDiscount;
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ const applyDiscount = (cart, discount) => (!cart.length) ?
[{...cart[0], price: cart[0].price * (1 - discount) }]
.concat(applyDiscount(cart.slice(1), discount));

module.exports = applyDiscount;
export default applyDiscount;
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const Assert = require('chai').assert;
const Sinon = require('sinon');
const Submission = require('../solution/applyDiscount');
import { assert } from 'chai';
import Sinon from 'sinon';
import Submission from '../solution/applyDiscount.js';


describe('applyDiscount()', () => {

it('debería de retornar una array nuevo en vez de mutar el array de entrada', () => {
const cart = [{ price: 1 }, { price: 2 }, { price: 3 }];
Assert.notEqual(Submission(cart, .2), cart);
assert.notEqual(Submission(cart, .2), cart);
});

it('debería aplicar descuento esperado', () => {
const cart = [{ price: 1 }, { price: 2 }, { price: 3 }];
Assert.deepEqual(
assert.deepEqual(
Submission(cart, .2),
[{ price: 0.8 }, { price: 1.6 }, { price: 2.4000000000000004 }]
);
Expand All @@ -22,14 +22,14 @@ describe('applyDiscount()', () => {
const fnBody = Function.prototype.toString.call(Submission);
// strip comments from source code
const strippedFnBody = fnBody.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '$1');
Assert.equal(/(applyDiscount|module\.exports)\(/.test(strippedFnBody), true);
assert.equal(/(applyDiscount|module\.exports)\(/.test(strippedFnBody), true);
});

it('no debería usar for o while', () => {
const fnBody = Function.prototype.toString.call(Submission);
// strip comments from source code
const strippedFnBody = fnBody.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '$1');
Assert.equal(/(for|while)\s*\(/g.test(strippedFnBody), false);
assert.equal(/(for|while)\s*\(/g.test(strippedFnBody), false);
});

it('no debería usar Array#map, Array#forEach o Array#reduce', () => {
Expand All @@ -38,13 +38,13 @@ describe('applyDiscount()', () => {
const forEachSpy = Sinon.spy(Array.prototype, 'forEach');
const reduceSpy = Sinon.spy(Array.prototype, 'reduce');
const result = Submission(cart, .2);
Assert.equal(result.length, 3);
Assert.equal(result[0].price, 0.8);
Assert.equal(result[1].price, 1.6);
Assert.equal(result[2].price, 2.4000000000000004);
Assert.equal(mapSpy.callCount, 0);
Assert.equal(forEachSpy.callCount, 0);
Assert.equal(reduceSpy.callCount, 0);
assert.equal(result.length, 3);
assert.equal(result[0].price, 0.8);
assert.equal(result[1].price, 1.6);
assert.equal(result[2].price, 2.4000000000000004);
assert.equal(mapSpy.callCount, 0);
assert.equal(forEachSpy.callCount, 0);
assert.equal(reduceSpy.callCount, 0);
mapSpy.restore();
forEachSpy.restore();
reduceSpy.restore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ function reduce(arr, fn, initial) {
// TU SOLUCIÓN AQUÍ
}

module.exports = reduce;
export default reduce;
Loading

0 comments on commit 2fe343f

Please sign in to comment.