diff --git a/orderBook.js b/orderBook.js new file mode 100644 index 0000000..c7cf009 --- /dev/null +++ b/orderBook.js @@ -0,0 +1,37 @@ +function reconcileOrder(existingBook, incomingOrder) { + let updatedBook = [] + let changedOrders = [] + + for (let i = 0; i < existingBook.length; i++) { + if (!existingBook.length) { + updatedBook.push(incomingOrder) + return updatedBook + } + if (existingBook[i].type !== incomingOrder.type && + existingBook[i].price === incomingOrder.price && + existingBook[i].quantity === incomingOrder.quantity) { + incomingOrder.price = -1 + } else if (existingBook[i].type !== incomingOrder.type && + existingBook[i].price === incomingOrder.price && + existingBook[i].quantity !== incomingOrder.quantity) { + if (existingBook[i].quantity > incomingOrder.quantity) { + existingBook[i].quantity = existingBook[i].quantity - incomingOrder.quantity + incomingOrder.quantity = 0 + changedOrders.push(existingBook[i]) + } else { + incomingOrder.quantity = incomingOrder.quantity - existingBook[i].quantity + } + } else { + updatedBook.push(existingBook[i]) + } + } + if (incomingOrder.price !== -1 && incomingOrder.quantity !== 0) { + updatedBook.push(incomingOrder) + } + + return updatedBook.concat(changedOrders) +} + +module.exports = { + reconcileOrder +} \ No newline at end of file diff --git a/package.json b/package.json index a3ac999..643a55e 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": ".eslintrc.js", "scripts": { "lint": "./node_modules/.bin/eslint --format codeframe .", - "test": "./node_modules/.bin/mocha ./tests.js" + "test": "./node_modules/.bin/mocha ./test/tests.js" }, "repository": { "type": "git", diff --git a/tests.js b/test/tests.js similarity index 98% rename from tests.js rename to test/tests.js index 4926c8a..8aaa113 100644 --- a/tests.js +++ b/test/tests.js @@ -1,6 +1,6 @@ /* eslint-disable max-len */ const { expect } = require('chai') -const reconcileOrder = require('./orderBook') +const reconcileOrder = require('../orderBook').reconcileOrder describe('Order Book', () => { describe('reconcileOrder', () => {