Skip to content

Instantly share code, notes, and snippets.

@MatthewCallis
Created September 7, 2025 00:06
Show Gist options
  • Select an option

  • Save MatthewCallis/31f226baae6801594b83a22d7f800709 to your computer and use it in GitHub Desktop.

Select an option

Save MatthewCallis/31f226baae6801594b83a22d7f800709 to your computer and use it in GitHub Desktop.
Random Markdown & String Utilities
/**
* Strip indentation from a string.
* @param {string} text The text to strip the indentation from.
* @param {boolean} [stripAll] When true, strip all indentation, when false, only the initial indentation. Defaults to false.
* @returns {string} The text with the indentation stripped.
*/
export function stripIndent(text, stripAll = false) {
if (stripAll) {
// remove all indentation from each line
return text.replace(/^[^\S\n]+/gm, '');
}
// Remove the shortest leading indentation from each line
const match = text.match(/^[^\S\n]*(?=\S)/gm);
const indent = match && Math.min(...match.map((el) => el.length));
if (indent) {
const regexp = new RegExp(`^.{${indent}}`, 'gm');
return text.replace(regexp, '');
}
return text;
}
/**
* Calculate the cosine similarity between two vectors A and B.
* Cosine similarity is a measure of the angle between the two vectors, and is commonly used in machine learning and data analysis to determine how similar two sets of data are.
* The formula for cosine similarity is:
* cosine_similarity = (A ⋅ B) / (||A|| * ||B||)
* Where:
* A ⋅ B is the dot product of the two vectors
* ||A|| and ||B|| are the magnitudes (norms) of the vectors
* @param {number[]} vectorA Vector A
* @param {number[]} vectorB Vector B
* @returns {number} The simularity.
*/
export function cosineSimilarity(vectorA, vectorB) {
let dotProduct = 0;
let normA = 0;
let normB = 0;
for (let i = 0; i < vectorA.length; i++) {
dotProduct += vectorA[i] * vectorB[i];
normA += vectorA[i] * vectorA[i];
normB += vectorB[i] * vectorB[i];
}
const magnitudeProduct = Math.sqrt(normA * normB);
return dotProduct / magnitudeProduct;
}
/**
* Convert newlines to spaces.
* @param {string} text The text to convert newlines to spaces.
* @param {string} [replace] The string to replace newlines with, defaults to a single space.
* @returns {string} The text with newlines converted to spaces.
*/
export const oneLine = (text, replace = ' ') => text.replace(/(?:\n\s*)+/g, replace).trim();
/**
* Takes an array of arrays and returns a `,` sparated csv file.
* @param {string[][]} table The array of arrays of strings to join.
* @param {string} [seperator] The seperator to use when joining the items, defaults to `,`.
* @param {string} [newLine] The seperator to use when joining the rows, defaults to `\n`.
* @param {boolean} [alwaysDoubleQuote] Always double quote the cell, defaults to true.
* @returns {string} The joined CSV row.
*/
export const toCSV = (table, seperator = ',', newLine = '\n', alwaysDoubleQuote = true) => table
.map((row) => row
.map((cell) => {
// We remove blanks and check if the column contains other whitespace, `,` or `"`.
// In that case, we need to quote the column.
if (alwaysDoubleQuote || cell.replace(/ /g, '').match(/[\s",]/)) {
return `"${cell.replace(/"/g, '""')}"`;
}
return cell;
})
.join(seperator))
.join(newLine);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment