Skip to content

Instantly share code, notes, and snippets.

View DarrenSem's full-sized avatar

Darren Semotiuk DarrenSem

View GitHub Profile
@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 / 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 / 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 / mongodb-getDocumentArray.js
Last active November 2, 2023 22:12
MongoDB - function getDocumentArray( fnEachDocument, ...documents ) - returns cloned Array of passed documents (after running function on each)
// MongoDB - function getDocumentArray( fnEachDocument, ...documents ) - returns cloned Array of passed documents (after running function on each)
// Purpose: returns cloned Array of passed documents (after running function on each)
// Usage: const docArray = getDocumentArray( fnEachDocument, ...documents ); await coll.insertMany(docArray);
// see the docs for https://www.mongodb.com/docs/manual/reference/method/db.collection.insertMany
// esp. "_id Field" https://www.mongodb.com/docs/manual/reference/method/db.collection.insertMany/#_id-field
// ^ because DURING THE .insert[Many|One]() call, "_id" field is ADDED to the original document! (if missing)
const getDocumentArray = ( fnEachDocument, ...documents ) => (
fnEachDocument ||= ( document => document ),
@DarrenSem
DarrenSem / contentsInNewWindow.js
Last active October 15, 2023 17:28
OpenInNewWindow(data, type) - F5-REFRESHABLE! - auto-UTF8 if type==="text"! - also ContentsInNewWindow(contents = '' or [], Title, type, delimToJoinArray = "\n\n")
// 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.
@DarrenSem
DarrenSem / hostnameRoot.js
Created September 25, 2023 11:22
hostnameRoot.js -- returns URL/location.hostname with certain prefixes removed: m. mob. mobi. mobile. www. www[0-9]. ww[0-9].
// hostnameRoot.js -- returns URL/location.hostname with certain prefixes removed: m. mob. mobi. mobile. www. www[0-9]. ww[0-9].
const hostnameRoot = hostname => String(hostname ?? "")
.toLowerCase()
.replace(
/^(m(ob(i(le)?)?)?|ww(w?\d|w))\.(.+?\..+)/,
"$6"
);
console.log([
@DarrenSem
DarrenSem / process.js-for-HackerRank.js.md
Last active June 15, 2023 19:49
process.js-for-HackerRank

process.js-for-HackerRank

#nodejs #polyfill #browser #hackerrank #node-js #client-side #hackerrank-solutions #hackerrank-javascript #hackerrank-30dayschallange #hackerrank-challenges

https://github.com/DarrenSem/process.js-for-HackerRank (might have more recent updates/improvements)

Minimal polyfill (324 characters minified) for client-side JS to add missing NodeJS functions for completing HackerRank.com challenges -- process.stdin/stdout/env.OUTPUT_PATH and require("fs").createWriteStream

(So I can complete HackerRank.com challenges on my cell using my custom REPL bookmarklet.)

@DarrenSem
DarrenSem / negativeZero.js
Last active March 12, 2023 21:45
negativeZero.js - tiny JavaScript function to differentiate -0 from +0 (can process any type other than Symbol or BigInt)
// negativeZero.js - tiny JavaScript function to differentiate -0 from +0 (can process any type other than Symbol or BigInt)
// 15 chars: N=a=>1/0==1/-a;
var negativeZero = v => Infinity === 1 / -v; // => v === 0 && Number.NEGATIVE_INFINITY === 1 / v;
var isWeb = Date.now; // JScript is missing this method
var values = [
@DarrenSem
DarrenSem / truncate.js
Created March 10, 2023 01:43
truncate.js (string, max) => "Ellipses after 1st half of ...then the second half of it." [2000 = default for max if omitted]
// truncate.js (string, max) => "Ellipses after 1st half of ...then the second half of it." [2000 = default for max if omitted]
// 101 chars:
// T=(a,b=2e3)=>null==a?"":(a=a+"").length>(b=Math.max(b,5))?a.slice(0,b/2-1)+"..."+a.slice(-b/2+1.5):a;
var truncate = (string, max = 2000) => (
string == null ? ""
: ( string = String(string) ).length > ( max = Math.max(max, 5) )
? (
string.slice(0, max / 2 - 1) + "..." + string.slice(-max / 2 + 1.5)
)
@DarrenSem
DarrenSem / JSONstringify.js
Created February 15, 2023 19:02
JSONstringify.js - from-scratch implementation of JSON.stringify as a coding exercise ( and for JScript WSH using CSCRIPT.EXE /nologo MyJScript.js )
// JSONstringify.js - from-scratch implementation of JSON.stringify as a coding exercise ( and for JScript WSH using CSCRIPT.EXE /nologo MyJScript.js )
// 525 chars _=function(a){var b,c=null,d=typeof a,e=/^(undefined|function|bigint|symbol)$/i,f=0,g=[],h=function(a,b){return a==b?a+"":{}.toString.call(a).slice(8,-1)};if(!e.test(d)){if("string"===d)return"\""+a+"\"";if(!a||"object"!==d||"Number"===h(a))return("Number"===h(a)?isFinite(a)?+a:c:a)+"";if("Date"===h(a)&&!isNaN(a))return _(a.toISOString());if("Array"===h(a)){for(b=a.length;f<b;f++)g.push(_(e.test(h(a[f]))?c:a[f]));return"["+g+"]"}for(f in a)a.hasOwnProperty(f)&&!e.test(h(a[f]))&&g.push(_(f)+":"+_(a[f]));return"{"+g+"}"}}
var JSONstringify = function(obj) {
var NULL = null;
var type = typeof obj;
var skip = /^(undefined|function|bigint|symbol)$/i
var L;
var key = 0;
var elements = [];