Skip to content

Instantly share code, notes, and snippets.

View nicolo-ribaudo's full-sized avatar

Nicolò Ribaudo nicolo-ribaudo

View GitHub Profile

Optional chaining assignment proposal

Basically, allow this:

foo?.bar = value;

which is equivalent to

foo == null ? undefined : (foo.bar = value);
const CH_BRACE_L = 0x7b as const;
const CH_BRACE_R = 0x7d as const;
const CH_SQUARE_L = 0x5b as const;
const CH_SQUARE_R = 0x5d as const;
const CH_QUOTE_D = 0x22 as const;
const CH_ESCAPE = 0x5c as const;
const CH_COMMA = 0x2c as const;
const CH_COLON = 0x3a as const;
const CH_DOT = 0x2e as const;
const CH_MINUS = 0x2d as const;

Meta

Key ID: C6CB6CC54E363735

Subkeys: AAFDA9101C58F338, A96EDD9C10BC77A5, 5BB1D2E5A4C8404B

Setup GPG

git config --global user.signingkey AAFDA9101C58F338

This for loop:

for (let i = 0, getI = () => i; i < 3; i++)
  console.log(getI());

unrolls to:

@nicolo-ribaudo
nicolo-ribaudo / function.cjs
Created October 6, 2020 20:20
Synchronize async functions
"use strict";
const synchronize = require("./synchronize.cjs");
synchronize.export({
async getMyObj(value) {
await new Promise((resolve) => setTimeout(resolve, 2000));
return { foo: value * 3 };
}
});
@nicolo-ribaudo
nicolo-ribaudo / _true-false.js
Last active August 26, 2023 11:52
true -> false
var unshift = Function.call.bind([].unshift);
var test = Function.call.bind(/./.test);
function update(ar) {
unshift(ar, ar[ar.length - 1]);
}
function fn(check, id) {
if (check != true) throw new Error("check must be true");
if (id != 1 && id != 2) throw new Error("id must be 1 or 2");
@nicolo-ribaudo
nicolo-ribaudo / index.js
Last active December 8, 2015 18:16
Pascal's triangle
import { pascal, binomePower } from "./math.js";
console.log(pascal(10)); // [1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1]
console.log(binomePower(3)); // "x^3 + 3x^2y + 3xy^2 + y^3"
@nicolo-ribaudo
nicolo-ribaudo / y.js
Last active September 30, 2015 14:36
JS Y-combinator
const Y = a=>(a=>a(a))(b=>a((...a)=>b(b)(...a)));
// Usage
/*
* var result = Y((the function itself) => (...parameters) => {
* return result;
* })(...parameters);
*
*/