Skip to content

Instantly share code, notes, and snippets.

View cshanejennings's full-sized avatar

Shane Jennings cshanejennings

View GitHub Profile
@cshanejennings
cshanejennings / math-std-dev.js
Created December 23, 2022 05:34
Standard Deviation snippets (terse & verbose)
// explained
function calculateStandardDeviation(numbers) {
// Calculate the mean of the array of numbers
const mean = numbers.reduce((a, b) => a + b, 0) / numbers.length;
// Calculate the differences between the numbers and the mean
const differences = numbers.map(num => num - mean);
// Square each of the differences
const squaredDifferences = differences.map(diff => diff ** 2);
@cshanejennings
cshanejennings / chat_gpt_to_md.js
Last active February 17, 2023 18:02
Save ChatGPT script as Markdown file
(function (CONFIG) {
const make_headlines = (toc, q) => [...toc, q.replace(/----\n###(.*)\n----/g,'* [[#$1]]')];
const post_process = (str) => {
const with_headers = str.replace(/\*\*You\*\*:(.*)(\n\n\*\*ChatGPT)/g, "----\n###$1\n----$2")
.replace(/\*\*ChatGPT\*\*\:[\s]*/g, "#unconfirmed-chatgpt\n");
const body_text = with_headers.split(/----\n### .*\n----/g).reduce((body, ans) => (ans)
? body.replace(ans, ans + '\n[[#Questions asked|Back to Questions]]\n\n')
: body, with_headers);
const toc = with_headers.match(/----\n\#\#\#.*\n----/g).reduce(make_headlines, ['## Questions asked to GPT', '----']).join('\n');
return [toc, body_text].join('\n\n').trim();
@cshanejennings
cshanejennings / array_chunk_examples.js
Last active December 20, 2022 18:37
Javascript array chunking examples
const sample_array = [...Array(100)].map((v,i)=>i);
const chunk_size = 10;
// reuseable reducer: non-destructive
const reduce_array_chunks = (size) => ((chunks, val, i)=> {
if (i % size) { chunks[Math.floor(i / size)].push(val); } else chunks.push([val]);
return chunks;
});
const chunk_reducer = reduce_array_chunks(chunk_size); // reusable function option
const chunked_by_reduce = sample_array.reduce(chunk_reducer, []);
@cshanejennings
cshanejennings / string-cleaner-function.js
Last active January 6, 2023 17:42
Cut and paste string cleaning function with diacritics and allowed characters
const get_string_sanitizer = (user_maps = {}, user_allowed ) => {
const build_map = (map_str) => map_str.split(',').map(c => c.split('|')).reduce((dm, [c, r])=> c.split('').reduce((dm, c) => ({...dm, [c]: r.replace('\\u002C', ',')}), {...dm}), {})
const map_strings = {
diacritics: '⁰₀⓪⓿0|0,¹₁①⓵❶➀➊1|1,²₂②⓶❷➁➋2|2,³₃③⓷❸➂➌3|3,⁴₄④⓸❹➃➍4|4,⁵₅⑤⓹❺➄➎5|5,⁶₆⑥⓺❻➅➏6|6,⁷₇⑦⓻❼➆➐7|7,⁸₈⑧⓼❽➇➑8|8,⁹₉⑨⓽❾➈➒9|9,⑩⓾❿➉➓|10,⑪⓫|11,⑫⓬|12,⑬⓭|13,⑭⓮|14,⑮⓯|15,⑯⓰|16,⑰⓱|17,⑱⓲|18,⑲⓳|19,⑳⓴|20,ΑÀÁÂÃÄÅĀĂĄƏǍǞǠǺȀȂȦȺᴀḀẠẢẤẦẨẪẬẮẰẲẴẶⒶA|A,ÆǢǼᴁ|AE,ΒƁƂɃʙᴃḂḄḆⒷB|B,ÇĆĈĊČƇȻʗᴄḈⒸC|C,ÈΕÉÊËĒĔĖĘĚƎƐȄȆȨɆᴇḔḖḘḚḜẸẺẼẾỀỂỄỆⒺⱻE|E,ÌÍÎÏĨĪĬĮİƖƗǏȈȊɪᵻḬḮỈỊⒾꟾIΙ|I,ÐĎĐƉƊƋᴅᴆḊḌḎḐḒⒹꝹD|D,ÑŃŅŇŊƝǸȠɴᴎṄṆṈṊⓃNΝ|N,ÒÓÔÕÖØŌŎŐƆƟƠǑǪǬǾȌȎȪȬȮȰᴏᴐṌṎṐṒỌỎỐỒỔỖỘỚỜỞỠỢⓄꝊꝌOΟ|O,ÙÚÛÜŨŪŬŮŰŲƯǓǕǗǙǛȔȖɄᴜᵾṲṴṶṸṺỤỦỨỪỬỮỰⓊU|U,ÝŶŸƳȲɎʏẎỲỴỶỸỾⓎYΥ|Y,ÞꝦ|TH,ß|ss,àáâãäåāăąǎǟǡǻȁȃȧɐəɚᶏᶕḁẚạảấầẩẫậắằẳẵặₐₔⓐⱥⱯa|a,æǣǽᴂ|ae,çćĉċčƈȼɕḉↄⓒꜾꜿc|c,èéêëēĕėęěǝȅȇȩɇɘɛɜɝɞʚᴈᶒᶓᶔḕḗḙḛḝẹẻẽếềểễệₑⓔⱸe|e,ìíîïĩīĭįıǐȉȋɨᴉᵢᵼᶖḭḯỉịⁱⓘi|i,ðďđƌȡɖɗᵭᶁᶑḋḍḏḑḓⓓꝺd|d,ñńņňʼnŋƞǹȵɲɳᵰᶇṅṇṉṋⁿⓝn|n,òóôõöøōŏőơǒǫǭǿȍȏȫȭȯȱɔɵᴖᴗᶗṍṏṑṓọỏốồổỗộớờởỡợₒⓞⱺꝋꝍo|o,ùúûüũūŭůűųưǔǖǘǚǜȕȗʉᵤᶙṳ