Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Generate additional file formats from the source text file

import {readFile, writeFile} from 'node:fs/promises';

const LF = '\n';
const textEncoding = {encoding: 'utf-8'};

/**
* Ensure uniform file names
*
* @param {string} suffix
* @returns {string}
*/
function getFileName (suffix) {
return `fortune-cookies${suffix}`;
}

/**
* @param {string} str
* @returns {string}
*/
function appendFinalNewline (str) {
return `${str.trimEnd()}${LF}`;
}

/**
* @param {string} fileNameSuffix
* @param {string} str
* @returns {Promise<void>}
*/
function writeTextFile (fileNameSuffix, str) {
return writeFile(
getFileName(fileNameSuffix),
appendFinalNewline(str),
textEncoding,
);
}

/**
* Returns a new, filtered array of deduplicated strings. When determining
* duplicates, the string comparison algorithm ignores:
* - letter case
* - non-word characters (punctuation, etc.)
*
* @param {string[]} array
* @returns {string[]}
*/
function removeDuplicates (array) {
const set = new Set();
const regexpNotAWord = /\W/g;

return array.filter(str => {
const simplified = str
// Ignore letter case
.toLowerCase()
// Ignore non-word characters
.replaceAll(regexpNotAWord, '');

const keep = !set.has(simplified);
set.add(simplified);
return keep;
});
}

/**
* @param {string} str
* @returns {string[]}
*/
function parseFortunes (str) {
return str.split(LF)
// Trim surrounding whitespace
.map(line => line.trim())
// Remove empty lines
.filter(Boolean);
}

const fortunes = parseFortunes(
await readFile(getFileName('.txt'), textEncoding),
);

// Source text file: rewrite to ensure uniform whitespace
await writeTextFile('.txt', fortunes.join(LF));

// Markdown file: Format as list
await writeTextFile(
'.md',
removeDuplicates(fortunes).map(line => `* ${line}`).join(LF),
);

// JSON file: with whitespace for readability
const fortunesJson = JSON.stringify(removeDuplicates(fortunes), null, 2);
await writeTextFile('.json', fortunesJson);

// ESM file: this is for stability until (JSON) import assertsions are stabilized
const esmSource = `export default ${fortunesJson};`;
await writeTextFile('.mjs', esmSource);
6 changes: 0 additions & 6 deletions fortune-cookie.json → fortune-cookies.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"Your power is in your ability to decide.",
"Wherever you go, whenever you can, try to leave a gift.",
"Kind words can be shot and easy to speak, but their echoes are truly endless.",
"Your ingenuity and imagination will get results.",
"Age can never hope to with you while your heart is young.",
"Example is better than perception.",
"Idleness is the holiday of fools.",
Expand Down Expand Up @@ -82,11 +81,8 @@
"You are going to take a vacation.",
"Commitment is the stuff character is made of; the power to change the face of things.",
"A friend asks only for your time not your money.",
"A friend asks only for your time not your money.",
"A handful of patience is worth more than a bushel of brains.",
"A handful of patience is worth more than a bushel of brains.",
"To be eighty years young is more cheerful and hopeful than forty years old.",
"To be eighty years young is more cheerful and hopeful than forty years old.",
"A person is not wise simply because one talks a lot.",
"It takes guts to get out of the ruts.",
"The greatest quality is seeking to serve others.",
Expand All @@ -110,7 +106,6 @@
"Your cheerful outlook is one of your assets.",
"You can't have everything... where would you put it all?",
"You have an unusually magnetic personality.",
"You have an unusually magnetic personality.",
"He who hurries cannot walk with dignity.",
"You never hesitate to tackle the most difficult problems.",
"Your emotional nature is strong and sensitive.",
Expand Down Expand Up @@ -227,7 +222,6 @@
"Say hello to others. You will have a happier day.",
"Don't let your limitations overshadow your talents.",
"Love is the affinity which links and draws together the elements of the world.",
"You should be able to undertake and complete anything.",
"Character development is the true aim of education.",
"Trust him, but sill keep your eyes open.",
"He who is shipwrecked the second time cannot lay the blame on Neptune.",
Expand Down
10 changes: 4 additions & 6 deletions fortune-cookie.md → fortune-cookies.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* Your power is in your ability to decide.
* Wherever you go, whenever you can, try to leave a gift.
* Kind words can be shot and easy to speak, but their echoes are truly endless.
* Your ingenuity and imagination will get results.
* Age can never hope to with you while your heart is young.
* Example is better than perception.
* Idleness is the holiday of fools.
Expand Down Expand Up @@ -81,11 +80,8 @@
* You are going to take a vacation.
* Commitment is the stuff character is made of; the power to change the face of things.
* A friend asks only for your time not your money.
* A friend asks only for your time not your money.
* A handful of patience is worth more than a bushel of brains.
* A handful of patience is worth more than a bushel of brains.
* To be eighty years young is more cheerful and hopeful than forty years old.
* To be eighty years young is more cheerful and hopeful than forty years old.
* A person is not wise simply because one talks a lot.
* It takes guts to get out of the ruts.
* The greatest quality is seeking to serve others.
Expand All @@ -109,7 +105,6 @@
* Your cheerful outlook is one of your assets.
* You can't have everything... where would you put it all?
* You have an unusually magnetic personality.
* You have an unusually magnetic personality.
* He who hurries cannot walk with dignity.
* You never hesitate to tackle the most difficult problems.
* Your emotional nature is strong and sensitive.
Expand Down Expand Up @@ -226,7 +221,6 @@
* Say hello to others. You will have a happier day.
* Don't let your limitations overshadow your talents.
* Love is the affinity which links and draws together the elements of the world.
* You should be able to undertake and complete anything.
* Character development is the true aim of education.
* Trust him, but sill keep your eyes open.
* He who is shipwrecked the second time cannot lay the blame on Neptune.
Expand All @@ -248,3 +242,7 @@
* If at first you do not succeed... try something harder.
* Your good listening skills will open many doors.
* Two people shorten a road.
* Stop waiting! Buy that ticket take that special trip!
* A problem clearly stated is a problem half solved.
* Accept your independence and use it wisely.
* May the warm winds of heaven blow softly upon your sprint.
Loading