From adc134575c94242b3a1bf087cfeb8a09d3bc31d2 Mon Sep 17 00:00:00 2001 From: devcorpio Date: Sat, 12 Jan 2019 20:32:05 +0100 Subject: [PATCH] chapter 05: copy logAnalyzer.js, errors and fakes from the folder of chapter 04 in order to continue with the book --- .../errors/ArgumentError.js | 3 ++ .../fakes/fakeWebService.js | 21 ++++++++ .../logAnalyzer.js | 53 +++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 chapter_05-isolation-frameworks/errors/ArgumentError.js create mode 100644 chapter_05-isolation-frameworks/fakes/fakeWebService.js create mode 100644 chapter_05-isolation-frameworks/logAnalyzer.js diff --git a/chapter_05-isolation-frameworks/errors/ArgumentError.js b/chapter_05-isolation-frameworks/errors/ArgumentError.js new file mode 100644 index 0000000..d2a2476 --- /dev/null +++ b/chapter_05-isolation-frameworks/errors/ArgumentError.js @@ -0,0 +1,3 @@ +class ArgumentError extends Error {} + +module.exports = ArgumentError; diff --git a/chapter_05-isolation-frameworks/fakes/fakeWebService.js b/chapter_05-isolation-frameworks/fakes/fakeWebService.js new file mode 100644 index 0000000..d6d9d64 --- /dev/null +++ b/chapter_05-isolation-frameworks/fakes/fakeWebService.js @@ -0,0 +1,21 @@ +function fakeWebService() { + let lastError; + + /** + * @param {string} message + */ + function logError(message) { + lastError = message; + } + + function getLastError() { + return lastError; + } + + return { + logError, + getLastError, + }; +} + +module.exports = fakeWebService; diff --git a/chapter_05-isolation-frameworks/logAnalyzer.js b/chapter_05-isolation-frameworks/logAnalyzer.js new file mode 100644 index 0000000..2180119 --- /dev/null +++ b/chapter_05-isolation-frameworks/logAnalyzer.js @@ -0,0 +1,53 @@ +const ArgumentError = require('./errors/ArgumentError'); + +function logAnalyzer(extensionManager, webService) { + /** + * @type {boolean} + */ + let wasLastFileNameValid; + + /** + * @return {boolean} + */ + function getWasLastFileNameValid() { + return wasLastFileNameValid; + } + + /** + * @param {string} fileName + * @return {Promise} + */ + async function isValidLogFileName(fileName) { + wasLastFileNameValid = false; + + if (fileName === '') { + throw new ArgumentError('filename has to be provided'); + } + + const result = await extensionManager.isValid(fileName); + + if (!result) { + return false; + } + + wasLastFileNameValid = true; + return true; + } + + /** + * @param {string} fileName + */ + function analyze(fileName) { + if (fileName.length < 8) { + webService.logError(`Filename too short: ${fileName}`); + } + } + + return { + getWasLastFileNameValid, + isValidLogFileName, + analyze, + }; +} + +module.exports = logAnalyzer;