Skip to content

Instantly share code, notes, and snippets.

View getify's full-sized avatar
💭
Just coding

Kyle Simpson getify

💭
Just coding
View GitHub Profile
@getify
getify / 1.js
Last active July 11, 2019 23:51
what could a `replaceAll(..)` for JS strings, which handles non-global regexes as if they're global, look like?
function replaceAll(str,re,replacement) {
if (re.global) {
return str.replace(re,replacement);
}
else {
// do we need to "patch" the replacement function to pass correct arguments?
if (typeof replacement == "function") {
let fn = replacement;
replacement = function replacer(...args) {
// patch `offset` to correct position from whole string
@getify
getify / 1.js
Last active June 8, 2019 08:17
race-promises-pool
async function racePromisesPool([ ...prs ] = []) {
var raceWon = false;
var prListeners = prs.map(function listen(pr,idx){
return pr.then(function t(v){
if (!raceWon) {
raceWon = true;
prs.splice(idx,1); // remove the promise from the pool since it won the race
return v;
}
});
@getify
getify / 1.js
Created June 7, 2019 21:04
merging defaults/settings objects with destructuring
ajax(
ajaxOptions({
url: "https://my.other.tld/api",
headers: {
"Cache-Control": "no-cache"
},
cb: resp => console.log(resp)
})
);
@getify
getify / 1.js
Last active May 31, 2019 11:07
Force Monad #MayThe4thBeWithYou
var Force = { Skywalker, RegularFolk, of: Skywalker };
function Skywalker(v) {
return { map, chain, ap };
function map(fn) {
return Skywalker(fn(v));
}
function chain(fn) {
return fn(v);
}
@getify
getify / 1.js
Last active March 3, 2023 09:23
is Maybe a "monad?
// is Just(..) a monad? Well, it's a monad constructor.
// Its instances are certainly monads.
function Just(v) {
return { map, chain, ap };
function map(fn) {
return Just(fn(v));
}
function chain(fn) {
return fn(v);
}
@getify
getify / 1.js
Created April 9, 2019 15:24
if..else if.. else clauses
// note: mathematically, `x` can only ever be 0, 1, 2, or 3
var x = someNumber % 4;
// let's consider some options for an if..else if..else clause series...
@getify
getify / 1.js
Last active September 17, 2020 22:45
Nested ternary: good vs bad
var data = x ? y ? z ? z : y : x : 0;
// I call this the "if..if" pattern, and it's BAD. It means:
var data;
if (x) {
if (y) {
if (z) {
data = z;
}
@getify
getify / 1.js
Last active March 14, 2019 13:28
ESLint: test cases for determining if a function parameter is unused
// these `xx` parameters are all used, so no "unused parameter" errors should be reported
var f;
f = (xx) => xx;
f = (xx) => () => xx;
f = (xx) => { xx; };
f = (xx, yy = xx) => { yy; };
f = (xx, yy = xx) => { var xx; xx = yy; };
f = (xx, yy = xx) => { var xx = yy; };
@getify
getify / better-web.md
Created March 12, 2019 13:19
Building That Better Web

Building That Better Web

In honor of the 30th anniversary of the web, I wanted to share a few thoughts about what a better web could aspire to be, and challenge us to move toward it.

The Worser Web

It's tempting to frame "better" simply in terms of improvement and progress, as in how far the web has come over the last 20+ years. As a developer, I like many others get all too excited about fancy new features like Service Workers, WebRTC, and yes, even CSS Grids. The pace of change is dizzying, but it feels like a great problem to have too many awesome features to learn and use!

So in one practical respect, a better web is one that empowers developers and users alike to express themselves and connect with others more fluently.

@getify
getify / 1.js
Created March 6, 2019 02:02
Promise.lazyAll(..)
Promise.lazyAll = async function lazyAll(iter) {
var results = [];
for await (let val of iter) {
results.push(val);
}
return results;
};
(async function(){
console.log( `results(1): ${ await Promise.lazyAll( [1,2,3] )}` );