From 88040f04452c53ee992513661f382bfc2457dd35 Mon Sep 17 00:00:00 2001 From: erick-pacheco Date: Tue, 14 Jul 2020 01:55:28 -0400 Subject: [PATCH 1/2] Dependencies are installed and test file has been moved to a test folder --- orderBook.js | 16 ++++++++++++++++ package.json | 2 +- tests.js => test/tests.js | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 orderBook.js rename tests.js => test/tests.js (98%) diff --git a/orderBook.js b/orderBook.js new file mode 100644 index 0000000..5586a6b --- /dev/null +++ b/orderBook.js @@ -0,0 +1,16 @@ + function reconcileOrder(existingBook, incomingOrder) { + existingBook = [{}]; + + if (incomingOrder.type.value() === 'sell' ) { + existingBook i + } + +}; + +let order = reconcileOrder(1,2) +console.assert(order === 'hellow', 'error') + + +// using console.assert for testing. +//module.exports = reconcileOrder + 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', () => { From 2735a60c5de832658942be5cd3de89ff1f3e3632 Mon Sep 17 00:00:00 2001 From: Erick Pacheco Date: Sun, 27 Sep 2020 08:26:33 -0700 Subject: [PATCH 2/2] Handles incomining orders --- orderBook.js | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/orderBook.js b/orderBook.js index 5586a6b..c7cf009 100644 --- a/orderBook.js +++ b/orderBook.js @@ -1,16 +1,37 @@ - function reconcileOrder(existingBook, incomingOrder) { - existingBook = [{}]; +function reconcileOrder(existingBook, incomingOrder) { + let updatedBook = [] + let changedOrders = [] - if (incomingOrder.type.value() === 'sell' ) { - existingBook i + 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) } -}; - -let order = reconcileOrder(1,2) -console.assert(order === 'hellow', 'error') - - -// using console.assert for testing. -//module.exports = reconcileOrder + return updatedBook.concat(changedOrders) +} +module.exports = { + reconcileOrder +} \ No newline at end of file