Last active
May 21, 2019 17:55
-
-
Save humphd/c37e233f57c4b942ac3e8c30fae855f2 to your computer and use it in GitHub Desktop.
WEB422 Summer 2019 Week 2 Code Example: Modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Example module to get info about a multi-line string, | |
| * written as a node.js module. | |
| * | |
| * Takes a multiline string, and returns an Object: | |
| * | |
| * "here is a line of text" | |
| * | |
| * { | |
| * lines: [ | |
| * { | |
| * line: "here is a line of text", | |
| * count: 17 | |
| * }, | |
| * { | |
| * line: "here is the 2nd line of text", | |
| * count: 17 | |
| * }, | |
| * | |
| * count: 17 | |
| * } | |
| /** | |
| * Split the given string into lines, taking into | |
| * account Windows (\r\n) vs. Unix (\n) line endings. | |
| */ | |
| const splitIntoLines = text => text.split(/\r?\n/); | |
| /* | |
| * Count the number of non-whitespace (space, tab, etc) | |
| * characters in the given string, returning the count | |
| * "here is a line of text" -> 17 | |
| */ | |
| const countNonWhitespace = text => text.replace(/\s+/g, '').length; | |
| /** | |
| * Process a list of lines (Array of Strings) into an | |
| * Array of Objects, where each Object has info about | |
| * the given line: | |
| * | |
| * - The `line` itself | |
| * - The `count` of non-whitespace characters in the line | |
| * - The `number` of the line in the string, starting at 1 | |
| */ | |
| const processLines = list => | |
| list.map((element, index) => { | |
| return { | |
| line: element, | |
| count: countNonWhitespace(element), | |
| number: index + 1 | |
| }; | |
| }); | |
| const getTotalCharacters = lines => | |
| lines.reduce((total, element) => total + element.count, 0); | |
| /** | |
| * Main public entry point to the module. Take | |
| * the given multi-line string, and produce an Object | |
| * with info about each line, as well as a total | |
| * character count for non-whitespace characters. | |
| */ | |
| function processText(text) { | |
| const lines = processLines(splitIntoLines(text)); | |
| return { | |
| lines, | |
| total: getTotalCharacters(lines) | |
| }; | |
| } | |
| /** | |
| * Expose the `processText` function on the module's `exports` | |
| * Object, making it accessible to callers. All other functions | |
| * will remain "hidden" within this module. | |
| */ | |
| exports.processText = processText; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- | |
| To build our code, we need to use a Bundler. In this example | |
| we use Parcel. The package.json file defines this, and provides | |
| a `build` script. To use it: | |
| npm install | |
| npm run build | |
| --> | |
| <script src="./index.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Import the count.js module into our code and | |
| * access the `processText` property on the imported | |
| * exports Object. | |
| */ | |
| const { processText } = require('./count'); | |
| // Define a multiline string to analyze | |
| const text = `This is the first line | |
| This is the second line. | |
| And this is the third.`; | |
| // Pass our string to `processText` | |
| const info = processText(text); | |
| // Log the result in the console | |
| console.log(info); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "words", | |
| "version": "1.0.0", | |
| "scripts": { | |
| "build": "parcel index.html" | |
| }, | |
| "devDependencies": { | |
| "parcel": "^1.12.3" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment