[go: up one dir, main page]

Today I Learned

tags


2021/07/24

How to stream large responses using the Javascript Fetch API:

const response = await fetch(url);
const reader = response.body.getReader();

while (true) {
  const { value, done } = await reader.read();
  if (done) break;
  console.log('Received', value);
}

console.log('Response fully received');

Also: the circumstances under which ~/.*profile files are run: only if you log in.


2022/03/08

random: turns out HTML comments are valid line-comments in JS: https://smitop.com/post/js-html-comments/


2022/04/06

That python f-strings can do

Of the window.prompt() method: https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt


2022/05/09


2022/06/02

How to create a JS BigInt value:

A BigInt value, also sometimes just called a BigInt, is a bigint primitive, created by appending n to the end of an integer literal, or by calling the BigInt() constructor (but without the new operator) and giving it an integer value or string value. – https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt


2022/06/14

That Array.sort separates floats and ints:

x = [8, 7.4, 14, 13, 12, 11]
// [ 8, 7.4, 14, 13, 12, 11 ]
x.sort()
// [ 11, 12, 13, 14, 7.4, 8 ]
x.sort().reverse()
// [ 8, 7.4, 14, 13, 12, 11 ]

2022/09/20

A hack to detect if an element is visible/not:

const isVisible = (el) => (el.offsetWidth || el.offsetHeight) > 0;

see https://stackoverflow.com/questions/1343237/how-to-check-elements-visibility-via-javascript


2023/08/09

That you can cancel/reset timeouts in JS using clearTimeout


2023/10/18

RFCs are labeled with different statuses:

  1. Internet Standard (e.g. HTTP/1.1)
  2. Proposed Standard (e.g. HTTP/3, HTTP/2)
  3. Best Current Practice
  4. Experimental
  5. Informational
  6. Historic

[…]

RFCs usually begin as Internet-Drafts (I-Ds) written by an individual or a small group. In the IETF, these are then usually adopted by a working group, and improved and revised.

https://www.ietf.org/standards/rfcs/


Class fields are public by default, but private class members can be created by using a hash # prefix. The privacy encapsulation of these class features is enforced by JavaScript itself.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields


2023/10/31

That POSIX shell has configuration option, set -a or set -o allexport that exports all assigned variables. This simplifies using .env files in .envrc files:

# evaluate a .env file, if it exists
if [ -f .env ]; then
  set -a # assigning a variable exports the variable
  # shellcheck disable=SC1091
  source .env
  set +a
fi

That JS has a String.raw template literal tag function that unescapes escape sequences in a backtick-string:

String.raw`\t\r\n` // => "\\t\\r\\n"

2025/01/26

That String.split doesn’t need parentheses:

"a b".split` ` // ["a", "b"]

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates


2025/08/08

That node.js bundles a jest-like test runner since 1.20: https://nodejs.org/api/test.html#test-runner.

import {test, describe} from "node:test"

describe("behavior", () => {
  test("case", t => {})
})
node --test

H/T https://kashw1n.com/blog/nodejs-2025/#3-built-in-testing-professional-testing-without-external-dependencies for pointing this out.


2025/12/13

Since May 2024, this feature works across the latest devices and browser versions. The light-dark() CSS function enables setting two colors for a property - returning one of the two colors options by detecting if the developer has set a light or dark color scheme or the user has requested light or dark color theme - without needing to encase the theme colors within a prefers-color-scheme media feature query.

https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/color_value/light-dark

Also, you can detecte system-level dark mode (and listen for changes) using

const query = window.matchMedia("(prefers-color-scheme: dark)")
const isDark: boolean = query.matches # boolean
query.addEventListener('change', (e): boolean => e.matches)

(h/t https://robkendal.co.uk/blog/2024-11-21-detecting-os-level-dark-mode/)