Skip to content

Instantly share code, notes, and snippets.

View nicolo-ribaudo's full-sized avatar

Nicolò Ribaudo nicolo-ribaudo

View GitHub Profile

"numeric value with precision" V3

NOTE: The other file in the gist shows the same but using "significant digits" rather than "fractional digits". After trying to use it, I noticed that almost always we want to start counting fro mthe units and not from the most significant digit.

A "numeric with precision" is a pair consisting of a numeric value and an indication of how many of its digits are significant. The number of digits is counted starting from the units: 1 means the first digit after the dot, 0 means that we have an integer, -1 means that we have a multiple of 10.

It can be obtained from a numeric value (wether it's decimal, float, or bigint) by "decorating it":

new Decimal("123.45").withFractionalDigits(1);
123.45.withFractionalDigits(1);

MessageFormat 2 modules integration

This is a proposal for better integration of MessageFormat 2 (Unicode proposal, TC39 proposal) with the rest of the web platform.

TLDR

import message from "./message.mf2" with { type: "messageformat" };

export function Notifications({ count }) {
  return html`<p>${message.format({ count })}</p>`;

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);
*
*/