Abstracted capitalize to the utility script

This commit is contained in:
Angelos Chalaris
2018-02-04 20:09:28 +02:00
parent b710847d58
commit c37df3ee82
3 changed files with 14 additions and 14 deletions

View File

@ -93,8 +93,6 @@ let startPart = '',
// Load helper functions (these are from existing snippets in 30 seconds of code!) // Load helper functions (these are from existing snippets in 30 seconds of code!)
const objectFromPairs = arr => arr.reduce((a, v) => ((a[v[0]] = v[1]), a), {}); const objectFromPairs = arr => arr.reduce((a, v) => ((a[v[0]] = v[1]), a), {});
const capitalize = (str, lowerRest = false) =>
str.slice(0, 1).toUpperCase() + (lowerRest ? str.slice(1).toLowerCase() : str.slice(1));
console.time('Builder'); console.time('Builder');
@ -120,7 +118,7 @@ try {
Object.entries(tagDbData) Object.entries(tagDbData)
.map(t => t[1][0]) .map(t => t[1][0])
.filter(v => v) .filter(v => v)
.sort((a, b) => capitalize(a, true) === 'Uncategorized' ? 1 : capitalize(b, true) === 'Uncategorized' ? -1 : a.localeCompare(b))) .sort((a, b) => util.capitalize(a, true) === 'Uncategorized' ? 1 : util.capitalize(b, true) === 'Uncategorized' ? -1 : a.localeCompare(b)))
]; ];
// Add the start static part // Add the start static part
@ -128,10 +126,10 @@ try {
// Loop over tags and snippets to create the table of contents // Loop over tags and snippets to create the table of contents
for (const tag of tags) { for (const tag of tags) {
const capitalizedTag = capitalize(tag, true); const capitalizedTag = util.capitalize(tag, true);
output += `### ${ output += `### ${
EMOJIS[tag] || '' EMOJIS[tag] || ''
} ${capitalizedTag}\n\n<details>\n<summary>View contents</summary>\n\n`; } ${util.capitalizedTag}\n\n<details>\n<summary>View contents</summary>\n\n`;
for (const taggedSnippet of Object.entries(tagDbData).filter(v => v[1][0] === tag)) { for (const taggedSnippet of Object.entries(tagDbData).filter(v => v[1][0] === tag)) {
output += `* [\`${taggedSnippet[0]}\`](#${taggedSnippet[0].toLowerCase()}${taggedSnippet[1].includes('advanced')?'-':''})\n`; output += `* [\`${taggedSnippet[0]}\`](#${taggedSnippet[0].toLowerCase()}${taggedSnippet[1].includes('advanced')?'-':''})\n`;
} }
@ -140,8 +138,8 @@ try {
// Loop over tags and snippets to create the list of snippets // Loop over tags and snippets to create the list of snippets
for (const tag of tags) { for (const tag of tags) {
const capitalizedTag = capitalize(tag, true); const capitalizedTag = util.capitalize(tag, true);
output += `---\n ## ${EMOJIS[tag] || ''} ${capitalizedTag}\n`; output += `---\n ## ${EMOJIS[tag] || ''} ${util.capitalizedTag}\n`;
for (const taggedSnippet of Object.entries(tagDbData).filter(v => v[1][0] === tag)) { for (const taggedSnippet of Object.entries(tagDbData).filter(v => v[1][0] === tag)) {
let data = snippets[taggedSnippet[0] + '.md']; let data = snippets[taggedSnippet[0] + '.md'];
// Add advanced tag // Add advanced tag

View File

@ -48,6 +48,7 @@ const readTags = () => {
} }
return tagDbData; return tagDbData;
} }
// Optimizes nodes in an HTML document
const optimizeNodes = (data, regexp, replacer) => { const optimizeNodes = (data, regexp, replacer) => {
let count = 0; let count = 0;
let output = data; let output = data;
@ -60,4 +61,7 @@ const optimizeNodes = (data, regexp, replacer) => {
} while (count > 0); } while (count > 0);
return output; return output;
} }
module.exports = {readSnippets, readTags, optimizeNodes}; // Capitalizes the first letter of a string
const capitalize = (str, lowerRest = false) =>
str.slice(0, 1).toUpperCase() + (lowerRest ? str.slice(1).toLowerCase() : str.slice(1));
module.exports = {readSnippets, readTags, optimizeNodes, capitalize};

View File

@ -59,8 +59,6 @@ let snippets = {},
tagDbData = {}; tagDbData = {};
// Load helper functions (these are from existing snippets in 30 seconds of code!) // Load helper functions (these are from existing snippets in 30 seconds of code!)
const objectFromPairs = arr => arr.reduce((a, v) => ((a[v[0]] = v[1]), a), {}); const objectFromPairs = arr => arr.reduce((a, v) => ((a[v[0]] = v[1]), a), {});
const capitalize = (str, lowerRest = false) =>
str.slice(0, 1).toUpperCase() + (lowerRest ? str.slice(1).toLowerCase() : str.slice(1));
// Start the timer of the script // Start the timer of the script
console.time('Webber'); console.time('Webber');
// Synchronously read all snippets and sort them as necessary (case-insensitive) // Synchronously read all snippets and sort them as necessary (case-insensitive)
@ -83,11 +81,11 @@ try {
// Loop over tags and snippets to create the table of contents // Loop over tags and snippets to create the table of contents
for (let tag of [...new Set(Object.entries(tagDbData).map(t => t[1][0]))] for (let tag of [...new Set(Object.entries(tagDbData).map(t => t[1][0]))]
.filter(v => v) .filter(v => v)
.sort((a, b) => capitalize(a, true) === 'Uncategorized' ? 1 : capitalize(b, true) === 'Uncategorized' ? -1 : a.localeCompare(b))) { .sort((a, b) => util.capitalize(a, true) === 'Uncategorized' ? 1 : util.capitalize(b, true) === 'Uncategorized' ? -1 : a.localeCompare(b))) {
output += output +=
`<h3>` + `<h3>` +
md md
.render(`${capitalize(tag, true)}\n`) .render(`${util.capitalize(tag, true)}\n`)
.replace(/<p>/g, '') .replace(/<p>/g, '')
.replace(/<\/p>/g, '') + .replace(/<\/p>/g, '') +
`</h3>`; `</h3>`;
@ -104,9 +102,9 @@ try {
// Loop over tags and snippets to create the list of snippets // Loop over tags and snippets to create the list of snippets
for (let tag of [...new Set(Object.entries(tagDbData).map(t => t[1][0]))] for (let tag of [...new Set(Object.entries(tagDbData).map(t => t[1][0]))]
.filter(v => v) .filter(v => v)
.sort((a, b) => capitalize(a, true) === 'Uncategorized' ? 1 : capitalize(b, true) === 'Uncategorized' ? -1 : a.localeCompare(b))) { .sort((a, b) => util.capitalize(a, true) === 'Uncategorized' ? 1 : util.capitalize(b, true) === 'Uncategorized' ? -1 : a.localeCompare(b))) {
output += md output += md
.render(`## ${capitalize(tag, true)}\n`) .render(`## ${util.capitalize(tag, true)}\n`)
.replace(/<h2>/g, '<h2 style="text-align:center;">'); .replace(/<h2>/g, '<h2 style="text-align:center;">');
for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1][0] === tag)) for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1][0] === tag))
output += output +=