Skip to content

Instantly share code, notes, and snippets.

@FQ400
Last active August 11, 2018 15:07
Show Gist options
  • Save FQ400/8c5623140db04f8245c71f1a9e7c9bc1 to your computer and use it in GitHub Desktop.
Save FQ400/8c5623140db04f8245c71f1a9e7c9bc1 to your computer and use it in GitHub Desktop.
Kata
// https://codurance.com/2017/11/16/katas-for-functional-calisthenics/
// Rules: https://codurance.com/2017/10/12/functional-calisthenics/
// Thoughs
// - build a field ???
// initial position
// new position function
// - parse moving string
// Pairing Session: @FQ400 @oleks-fedotov
const getPosition = ({x, y} = {x: 0, y: 0}) => ({
x: Number(x) || 0,
y: Number(y) || 0
});
const getDirection = (d ='N') => ['E', 'W', 'S'].includes(d)
? d
: 'N';
const getStep = dir => {
switch(dir) {
case 'F':
return -1;
case 'B':
return 1;
default:
return 0;
}
}
const move = ({x, y}) => (d) => (moves) => (
Array.from(moves)
.reduce(({x, y, d}, curMove) =>
({
x,
y: y + getStep(curMove)
}),
{
x,
y,
d
}
)
);
// const move = ({x, y}) => (d) => (movements) => {x,y,d};
// move(getPosition({x: 1}))(getDirection())('rrrrbrl');
// const newDefPosition = move(getPosition({x: 5, y:5}));
describe('getPosition()',function() {
it('return an object', function() {
assert.equal(typeof getPosition(), "object")
});
const propsToCheck = ['x', 'y'];
propsToCheck.forEach((prop)=>{
it(`return object has ${prop} prop`, function(){
const result = getPosition();
assert(result.hasOwnProperty(prop))
});
it(`return object has ${prop} defaulted to 0`, function(){
const result = getPosition();
assert.equal(result[prop], 0)
});
});
it('return object has with x=0, y=0', function(){
const result = getPosition({ x:"asd", y: "duck" });
assert.deepEqual(result, {x: 0, y: 0});
});
it('return object has x = 5, y = 0', function(){
const result = getPosition({ x:5 });
assert.deepEqual(result, {x: 5, y: 0});
});
it('return object has x = 5, y = 9', function(){
const result = getPosition({ x:5, y: 9 });
assert.deepEqual(result, {x: 5, y: 9});
});
});
describe('getDirection()', function() {
it('return a string', function() {
assert.equal(typeof getDirection(), "string");
});
it('return default direction "N"', function() {
assert.equal(getDirection(), "N");
});
it('return the given direction "S"', function() {
assert.equal(getDirection("S"), "S");
});
it('return the another given direction "E"', function() {
assert.equal(getDirection("E"), "E");
});
it('return "N" for invalid argument', function() {
assert.equal(getDirection("1"), "N");
});
});
describe('move()', function() {
it('returns the new position', function() {
const result = move(getPosition())(getDirection())('FF');
assert.deepEqual(result, { x: 0, y: -2 });
});
it('returns the new position', function() {
const result = move(getPosition())(getDirection())('BB');
assert.deepEqual(result, { x: 0, y: 2 });
});
it('returns the new position', function() {
const result = move(getPosition())(getDirection())('FB');
assert.deepEqual(result, { x: 0, y: 0 });
});
it('returns the new position', function() {
const result = move(getPosition())(getDirection())('FL');
assert.deepEqual(result, { x: 0, y: -1 });
});
it('returns the new position', function() {
const result = move(getPosition())(getDirection())('FLRF');
assert.deepEqual(result, { x: 0, y: -2 });
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment