This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/////// upload.js: LOAD data FROM files (aka 'import/upload')... | |
// const buildFileSelector=(a,m)=>{const c=null==a?"":a+"";return Object.assign(document.createElement("input"),{type:"file"},m&&{multiple:m},c.trim().length&&{accept:c})}; | |
// const resetFileSelector=e=>e&&(e.value=null,e); | |
// const getFileSelector=(e,c,a,m,r)=>resetFileSelector(e)&&e.readAs===r?e:Object.assign(e||buildFileSelector(a,m),{onchange:c,readAs:r}); | |
// SEE BELOW: handleUploadOrDropExample = event => { | |
// Warning: "File chooser dialog can only be shown with a user activation." (just like clipboard functionality) | |
// const buildFileSelector=(a,m)=>{const c=null==a?"":a+"";return Object.assign(document.createElement("input"),{type:"file"},m&&{multiple:m},c.trim().length&&{accept:c})}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// dtString.js (dateArg, includeTime, defaultDate, defaultReturnValue) '1-2-2022 1:00' returns '02Jan2022 100am' or '02Jan2022' | |
const assert=(...a)=>!a.reduce((b,c,d,e,f)=>(f=("function"==typeof c?!c():!c)&&[2>a.length?c:`<arg #${d+1}> ${a.map((a,b)=>`#${b+1} = [ ${a} ]`).join(" , ")}`],f&&console.assert(0,...b[b.push(f)-1]),b),a.length?[]:[a]).length; | |
const assertMessage = (test, ...messageAndSubstitutions) => assert(test) || console.error(...messageAndSubstitutions) || false; | |
console.clear(); | |
// 183 chars: const dtString=(a,b,c,e,f=new Date(a),[,,,,g,h,,i]=(f=+f?f:new Date(c)).toLocaleString().toLowerCase().split(/\W/),[,j,k,d]=f.toString().split(" "))=>isNaN(f)?e:`${k}${j}${d}${b?` ${g}${h}${i}`:""}` | |
const dtString = (dateArg, includeTime, defaultDate, defaultReturnValue, z = new Date(dateArg), [, , , , h, n, , ampm] = (z = +z ? z : new Date(defaultDate)).toLocaleString().toLowerCase().split(/\W/), [, m, d, y] = z.toString().split(" ")) => isNaN(z) ? defaultReturnValue : `${d}${m}${y}${includeTime ? ` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sorted.js (array, strFlags) returns CLONE (not in-place sort!) -- flags 'uid': u=Unique original values, i=case Insensitive compare function, d=Descending (reverse) order | |
// minified = 179 chars s=(a,f="",c=a=>f.includes(a),d=[...(c("u")?[...new Set(a)]:a)])=>(d=c("i")?d.sort((a,b,c=(a+"").toUpperCase(),d=(b+"").toUpperCase())=>c<d?-1:c===d):d.sort(),c("d")?d.reverse():d) | |
let sorted = (array, strFlags = "", | |
// friendlier syntax like RegExp( _, strFlags ) rather than fetch( _, {options} ) | |
_tmp_HasFlag = flag => strFlags.includes(flag), | |
// work with CLONE of original array (or Set made from it) -- we do NOT want to do IN-PLACE sort |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// contentsInNewWindow.js - F5-REFRESHABLE! - with TITLE! - (contents = '' or [], title, type = 'octet-stream', array-joining-delim = '\n\n') | |
// const contentsInNewWindow=(c=[],T,t="octet-stream",d="\n\n")=>{const w=open(URL.createObjectURL(new Blob([[c].flat(1/0).join(d)],{type:"text"===t?t+"/plain;charset=utf-8":t})),"_blank");return w&&!w.closed&&(w.onload=()=>w.document.title=T),w}; | |
const contentsInNewWindow = (contents = [], title, type = ["text", "octet-stream"][1], delim = "\n\n") => { | |
const win = open( | |
URL.createObjectURL( | |
new Blob([ | |
[contents].flat(1/0).join(delim) | |
], { | |
// auto-UTF8 if type === 'text', to easily ensure visible emojis etc. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// innerText.js bookmarklet for mobile (to show webpage text contents OR html) (including PARTIAL source of SELECTION) | |
// https://gist.github.com/DarrenSem/7e1216bda80e6269ad53b43d90603158 | |
// 26Jan2025 BUGFIX: now missing ONLY the "System instructions" section (one extra div layer was added SIGH) | |
// 4784 char javascript:void function(){"use strict";const e="main div[data-collapsed",n="engine-select",t=`div.model-select>button>span`,d=[]._,i=((e,t,i,a,n,l,d,r)=>(t=new Date(e||Date()),[i,a,,n]=t.toLocaleTimeString().replace(/(a|p)\.(m)\./i,"$1$2").split(/\W/),[,,l,d]=t.toString().split(" "),r=t.toLocaleString().replace(/(\d+)-(\d+)-(\d+)/,"$2/$1/$3").split("/")[0].padStart(2,0),`${i}${a}${(n||"").toLowerCase()} ${d}-${r}-${l}`))(),a=`${""} ${i}`,l=top.document,r=location,u=(e,n)=>(n||l).querySelectorAll(e||null),o=(e,n)=>{const t=e?.[n?"value":x],i=t==d?d:n?t:t.replace(/\n{3,}/g,"\n\n\n").replace(/\n\n/g,"\n");return i},f=e=>e.trim(),s=e=>(e??"").replace(/\nSYNC$/i,"").replace(/ INSTRUCTIONS$/i,""),c=e=>(e?? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Math.sum.js and Math.avg and Math.max and Math.sum -- ALL will automatically flatten (all, args, passed, [[even, [, Arrays]]]) | |
console.log(Math.sum); | |
// undefined | |
console.log(Math.avg); | |
// undefined | |
console.log(Math.max); | |
// ƒ max() { [native code] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// store.js ALL-IN-ONE function: () = dump Storage, (keyOrNS, value OTHER THAN undefined) = return value after setting it, (keyOrNS) = return value - UNDEFINED if missing, (null, keyOrNS) = removeItem(keyOrNS) then return keyOrNS | |
// let store=(k,d,s=localStorage,z=s.getItem(k))=>null==k?null==d?s:(s.removeItem(d),d):void 0===d?null==z?void 0:JSON.parse(z):(s.setItem(k,JSON.stringify(d,0,"\t")),d); | |
let store = ( | |
keyOrNS, | |
data, | |
storage = [localStorage, sessionStorage][0], | |
_z = storage.getItem(keyOrNS) | |
) => { |