Skip to content

Instantly share code, notes, and snippets.

View DarrenSem's full-sized avatar

Darren Semotiuk DarrenSem

View GitHub Profile
@DarrenSem
DarrenSem / fizzbuzz expanded - wordmod.js
Last active November 9, 2023 20:38
fizzbuzz expanded - wordmod( limit = 100, words = { 3: "Fizz", 5: "Buzz" }, sep = "\n" )
// FizzBuzz expanded - wordmod.js - because why not
// https://gist.github.com/DarrenSem/3a63b31542487bb1fd38e08976d8d215
let wordmod = ( limit = 100, words = { 3: "Fizz", 5: "Buzz" }, sep = "\n" ) => {
let results = [];
for (let i = 1; i <= limit; i ++) {
let result = "";
for ( let [divisor, word] of Object.entries(words) ) {
@DarrenSem
DarrenSem / filledArray(n, funcForEachIndex).js
Last active December 1, 2023 20:53
filledArray.js - create a new array of size 'n' filled with values that result from calls to funcForEachIndex(index) - REPLACED BY: Array.range.js
// REPLACED BY: Array.range.js => new Array( length, optionalEachFunction, optionalMapFunction )
// https://gist.github.com/DarrenSem/bc0403bcdad09912eb298e6d3b4824fe
// previously REPLACED BY: Array.build.js -- sugar for functional equivalent of new Array( length, functionUsingIndex ) aka fillArray(L, F) -- returns Array(length).fill(0).map( (_, i) => callbackFn(i) )
// https://gist.github.com/DarrenSem/bf49d0c87083301a88111b5d444f0a5f
@DarrenSem
DarrenSem / Array.build.js
Last active December 1, 2023 20:53
Array.build.js -- sugar for functional equivalent of new Array( length, functionUsingIndex ) aka fillArray(L, F) -- returns Array(length).fill(0).map( (_, i) => callbackFn(i) ) - REPLACED BY Array.range.js
// REPLACED BY: Array.range.js => new Array( length, optionalEachFunction, optionalMapFunction )
// https://gist.github.com/DarrenSem/bc0403bcdad09912eb298e6d3b4824fe
// Array.build.js -- sugar for functional equivalent of new Array( length, functionUsingIndex ) aka fillArray(L, F) -- returns Array(length).fill(0).map( (_, i) => callbackFn(i) )
Array.build ||= (length, callbackFn) => (
Array(length|0)
.fill(0)
@DarrenSem
DarrenSem / shuffle.js
Created November 12, 2022 04:25
shuffle.js - fastest possible after countless speed comparisons - minified to 76 characters
// shuffle.js
// minified = 76 chars (not including 'R=Math.random') s=a=>{for(let b,c,d=a.length-1;0<d;)b=0|R()*(d+1),c=a[d],a[d--]=a[b],a[b]=c}
// after doing console.time speed comparisons of Every. Possible. Version and logic permutation I could find:
// https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
// https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array
// https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
// pre-cached "R" faster because no OBJECT.KEY lookup each time
let R = Math.random;
@DarrenSem
DarrenSem / randomWithSeed.js
Last active December 21, 2023 18:07
randomWithSeed.js - use rnd() after rnd = createRandomWithSeed( seed [0+] = Date.now() ) - Math.random is less than 2x faster than this 'good enough' SEED-able version
// randomWithSeed.js - use rnd() after rnd = createRandomWithSeed( seed [0+] = Date.now() ) - Math.random is less than 2x faster than this 'good enough' SEED-able version
let createRandomWithSeed = seed => {
seed = Math.abs(isNaN(seed) ? Date.now() : seed);
return () => {
seed = (seed * 9301 + 49297) % 233280;
return seed / 233280;
};
};
// R=s=>(s=Math.abs(isNaN(s)?Date.now():s),_=>(s=(9301*s+49297)%233280,s/233280));
@DarrenSem
DarrenSem / MHT Saver - minimal code for Chrome extension.md
Created February 2, 2024 15:22
MHT Saver - minimal code for Chrome extension to Save As MHT

I want to write a Chrome extension. I need the most minimal code for the V3 manifest and worker.js to trigger a download prompt of a data url generated by btoa() of the capture as mhtml function. Use only async await and arrow functions instead of promises and callbacks wherever possible. Use blob.text() instead of FileReader. Image128 .png and no popups or content scripts.

(02Feb2024 806am)

BOT:

To create a minimal Chrome extension that triggers a download prompt of a captured page in MHTML format using Manifest V3, you need to include the following files in your extension:

  1. manifest.json: The metadata file, which includes information about the extension like its name, version, permissions, and which script to run as the service worker.
@DarrenSem
DarrenSem / range(size, optionalEachFunction, optionalMapFunction).js
Last active February 7, 2024 16:13
range.js (arraySize, optionalMapFunction, optionalEachFunction)
// range(arraySize, optionalMapFunction, optionalEachFunction).js
// https://gist.github.com/DarrenSem/bc0403bcdad09912eb298e6d3b4824fe
const range = ( arraySize, fnMapIndex, fnEachIndexFirst, _indexes ) => (
_indexes = Array.from( Array(arraySize | 0).keys() ), // because [...Array(arraySize | 0).keys()] triggers TypeScript/VSC error: Type 'IterableIterator' is not an array type or a string type.
fnEachIndexFirst && _indexes.forEach( index => fnEachIndexFirst(index) ),
fnMapIndex && ( _indexes = _indexes.map( index => fnMapIndex(index) ) ),
_indexes
);
@DarrenSem
DarrenSem / replaced.js
Last active February 10, 2024 18:01
replaced.js ( v, ...rules [ ArrayOf[ pattern, replaceWith, optionalRegExpFlags] ] ) -- flexible multiple string replacements (Flags default = "g")
// replaced.js ( v: any, ...rules: Array of [pattern: any, replaceWith: string | function, optionalRegExpFlags: string | null | undefined] )
const replaced = (v, ...rules) => {
return rules.reduce( (acc, rule) => (
acc.replace(
RegExp( rule[0], rule[2] == null ? "g" : rule[2] ),
rule[1]
)
), String( v ?? "" ) );
};
@DarrenSem
DarrenSem / OpenAIPlaygroundShowPresets.js
Last active March 17, 2024 16:13
OpenAIPlaygroundShowPresets.js -- Bookmarklet to fix March 2024 CSS regressive bug (~"GM_addStyle" but without TamperMonkey or GreaseMonkey)
// OpenAIPlaygroundShowPresets.js -- Bookmarklet to fix March 2024 CSS regressive bug (~"GM_addStyle" but without TamperMonkey or GreaseMonkey)
// by Darren Semotiuk, see: https://community.openai.com/t/playground-recent-ui-changes-broke-mobile-experience/687130
// BOOKMARKLET* for those of us not running TamperMonkey or GreaseMonkey
// *Simply add the one-liner below as a Favorite/Bookmark in your browser, no extensions required!
javascript:void function(){globalThis.GM_addStyle=globalThis.GM_addStyle||function(a){let b=document,c=b.head||b.body,d=b.createElement("style");d.type="text/css",d.innerText=a,c&&c.appendChild(d)},GM_addStyle("@media(max-width:500px){.pg-header-actions,.pg-header-section-settings,.pg-preset-select-container{display:block !important}}")}();
@DarrenSem
DarrenSem / UMD-javascript-module-template.js
Last active March 21, 2024 21:38
Universal Module Definition (UMD) template for JavaScript modules
// UMD-javascript-module-template.js
// Universal Module Definition (UMD) template for JavaScript modules
(function(root, factory) {
// var req1 = foo, req2 = bar; // or even exports
if(typeof exports === "object" && typeof module !== "undefined") {
module.exports = factory(req1, req2);
} else if(typeof define === "function" && define.amd) {
define([req1, req2], factory)
} else {
root["MOD_NAME"] = factory(req1, req2);