Skip to content

Instantly share code, notes, and snippets.

@bouwe77
Created March 23, 2021 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bouwe77/8af27b85572e965f39a760bea5342814 to your computer and use it in GitHub Desktop.
Save bouwe77/8af27b85572e965f39a760bea5342814 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const pipe = (...fns) => (x) => fns.reduce((y, f) => f(y), x);
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) return fn(...args);
return function (a) {
return curried(...[...args, a]);
};
};
}
const handleError = (err) => {
if (err) throw err;
console.log('Saved!');
};
const datetime = {
toString: () => new Date().toISOString(),
};
// The three functions that are the building blocks of the logging solution.
// Building block 1: formatLog
function formatLog(datetime, severity, message) {
return `${datetime} [${severity}] - ${message}`;
}
// Building block 2: writeToConsole
function writeToConsole(message) {
console.log(message);
return message;
}
// Building block 3: writeToFile
function writeToFile(filePath, message) {
fs.appendFile(filePath, message + '\n', handleError);
return message;
}
// Calling the building blocks using nesting: no composition:
// Write a log message to the console:
writeToConsole(formatLog(new Date().toISOString(), 'INFO', 'Hello World'));
// Write a log message to a file:
writeToFile(
'/path/to/file',
formatLog(new Date().toISOString(), 'INFO', 'Hello World')
);
// Write a log message to both the console and a file:
writeToFile(
'/path/to/file',
writeToConsole(formatLog(new Date().toISOString(), 'INFO', 'Hello World'))
);
// Let's compose using currying and piping:
const curriedFormatLog = curry(formatLog);
const formatLogInfo = curriedFormatLog(datetime)("INFO");
const curriedWriteToFile = curry(writeToFile);
const writeToLogFile = curriedWriteToFile('/path/to/file');
// Pipe (or chain) some functions into a new function that does logging for us:
const logInfoToFile = pipe(formatLogInfo, writeToConsole, writeToLogFile);
// Let's do some logging:
logInfoToFile("Hello World");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment