Skip to content

Instantly share code, notes, and snippets.

@mc-funk
Created December 14, 2020 17:22
Show Gist options
  • Save mc-funk/67718634a6c4df6ead8535be5f01dbc4 to your computer and use it in GitHub Desktop.
Save mc-funk/67718634a6c4df6ead8535be5f01dbc4 to your computer and use it in GitHub Desktop.
String Parsing Kata tests/notes
const Mocha = require('mocha')
const assert = require('assert')
const mocha = new Mocha()
// This allows tests to work on this website
mocha.suite.emit('pre-require', this, 'solution', mocha)
/** TDD Kata 1 -- String calculator (http://osherove.com/kata)
* Create a function that takes in a string and processes the numbers within it
* We will use TDD and start with the simplest requirements, then work our way up
* to more and more absurd requirements :)
*/
// Put tests here
describe('when input is empty', () => {
it('returns 0 when no input given', () => {
const result = add();
assert.equal(result, 0)
});
it('returns 0 when empty string given', () => {
const result = add('');
assert.equal(result, 0)
});
})
describe('when there is one input', () => {
it('returns that number', () => {
const result = add('1');
assert.equal(result, 1);
})
});
describe('when there are two inputs', () => {
it('returns the sum when delimiter is ,', () => {
const result = add('1, 2');
assert.equal(result, 3);
});
it('returns the sum when delimiter is \n', () => {
const result = add('1\n 2');
assert.equal(result, 3);
});
});
describe('for an arbirtary number of inputs', () => {
it('returns the sum when delimiter is ,', () => {
const result = add('1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6');
assert.equal(result, 27);
});
it('returns the sum when delimiter is newline', () => {
const result = add('1\n 1\n 1\n 1\n 1\n 1\n 1\n 2\n 3\n 4\n 5\n 6');
assert.equal(result, 27);
});
it('returns the sum when delimiters are combined', () => {
const result = add('1; 1\n 1, 1\n 1\n 1\n 1\n 11\n 2\n 3\n 4\n, 5, 6');
assert.equal(result, 38);
});
it('throws an error when there is a negative delimiter', () => {
const result = () => add('-1\n -1\n 1\n -1\n 1\n 1\n 1\n 1\n 1\n 1\n 1\n 6');
assert.throws(result, Error, `negatives not allowed -1,-1,-1`);
});
it('ignores numbers over 1000', () => {
const result = add('1, 1, 1, 1000, 1; 1\n 1, 1, 2, 3, 4, 5, 6, 1000');
assert.equal(result, 27);
});
it('handles strings with custom delimiter', () => {
const result = add('//?!?!?!\n1?!?!?! 1?!?!?!1?!?!?! 1000?!?!?! 1?!?!?! 1');
assert.equal(result, 5);
});
});
// Put code to be tested here
function add(inputString) {
let parseableString = inputString;
const regex = /[//](.*)([\n])/
if (!parseableString || parseableString === '') return 0;
// let partsOfArray = parseableString.split(/[^0-9]/);
// console.log("partsOfArray = ", partsOfArray);
let delimiter;
const customDelimiters = inputString.match(regex);
if(customDelimiters) {
delimiter = customDelimiters[0].slice(2, -1);
const delimiterEnd = parseableString.indexOf('\n') + 1;
parseableString = parseableString.slice(delimiterEnd);
} else {
delimiter = /[\n \,\;]/
};
let argsArray;
const rejectionsArray = [];
argsArray = parseableString.split(delimiter);
const processInt = (val) => {
const valVal = parseInt(val, 10);
if (valVal < 0) return rejectionsArray.push(valVal);
return valVal;
};
const sum = argsArray.reduce((acc, val) => {
const accumulator = processInt(acc);
const value = processInt(val);
if (value < 1000) {
return accumulator + value;
} else {
return accumulator;
}
});
if (rejectionsArray.length > 0) {
throw new Error(`negatives not allowed: ${rejectionsArray}`);
} else {
return sum;
}
}
mocha.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment