Skip to content

Instantly share code, notes, and snippets.

@humphd
Created June 17, 2019 19:18
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 humphd/5be4918d6445e7cea89a79bba73db5fb to your computer and use it in GitHub Desktop.
Save humphd/5be4918d6445e7cea89a79bba73db5fb to your computer and use it in GitHub Desktop.
WEB422 Summer 2019 Week 7 Code Example: TypeScript
/**
* TypeScript rewrite of https://gist.github.com/humphd/c37e233f57c4b942ac3e8c30fae855f2
*
* Example module to get info about a multi-line string,
* written as an ES 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
* }
*/
/**
* Information about a line of text.
*/
interface LineMetrics {
line: string,
count: number,
number: number
}
/**
* Information about a multi-line text.
*/
interface TextMetrics {
lines: LineMetrics[],
total: number
}
/**
* Split the given string into lines, taking into
* account Windows (\r\n) vs. Unix (\n) line endings.
*/
const splitIntoLines = (text: string): Array<string> =>
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: string): number =>
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: Array<string>): Array<LineMetrics> =>
list.map((element, index) => {
return {
line: element,
count: countNonWhitespace(element),
number: index + 1
};
});
const getTotalCharacters = (lines: Array<LineMetrics>): number =>
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: string): TextMetrics {
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.
*/
export {
processText,
TextMetrics
};
<script src="main.js"></script>
import { processText } from './count';
const text: string = `This is the first line.
This is the second line.
This is the final line of the text.`;
console.log(processText(text));
{
"name": "words-ts",
"version": "1.0.0",
"scripts": {
"compile": "tsc main",
"serve": "parcel index.html",
"start": "npm run compile && npm run serve"
},
"devDependencies": {
"parcel": "^1.12.3",
"typescript": "^3.5.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment