Skip to content

Instantly share code, notes, and snippets.

View SamJakob's full-sized avatar
:octocat:
Programming

SamJakob SamJakob

:octocat:
Programming
View GitHub Profile
@SamJakob
SamJakob / rand.js
Created August 2, 2023 13:57
random number from GPG key
const { promisify } = require('util');
const exec = promisify(require('child_process').exec);
/** Returns a random byte as a floating-point decimal between 0 and 1. */
async function random() {
const payload = await exec(`gpg-connect-agent "SCD RANDOM 1" /bye`);
const randomBytes = payload.stdout.split('\n')[0].substr(2);
const buffer = Buffer.from(randomBytes, 'utf-8');
const number = buffer.readUIntBE(0, 1);
@SamJakob
SamJakob / PreferredSizedBox.dart
Created October 4, 2023 04:42
Ever wished that SizedBox implements PreferredSizeWidget? Use this to bridge the gap between SizedBox and PreferredSizedWidget!
/// This class extends [SizedBox] whilst implementing [PreferredSizeWidget]
/// based on the defined size. This allows the use of [SizedBox] in places
/// where [PreferredSizeWidget] is required, such as in [AppBar.preferredSize].
class PreferredSizedBox extends SizedBox implements PreferredSizeWidget {
const PreferredSizedBox({super.key});
const PreferredSizedBox.shrink({super.key, super.child}) : super.shrink();
const PreferredSizedBox.expand({super.key, super.child}) : super.expand();
PreferredSizedBox.fromSize({super.key, super.child, super.size})
: super.fromSize();
@SamJakob
SamJakob / completer.ts
Created February 19, 2024 21:16
Completer in TypeScript
/**
* This is a TypeScript version of the `Completer` class from Dart.
*
* A `Completer` provides a future that can be imperatively concluded (once and
* only once) with either `#complete` or `#completeError`.
* Once the completer has completed (or errored), the future will be resolved
* with the specified value.
*
* This is trivially implemented by instantiating a Promise and capturing the
* resolve and reject functions.
@SamJakob
SamJakob / chunked_stream.dart
Last active December 5, 2024 23:23
Dart chunk a stream
/// An extension that adds the [chunked] method to a [Stream] of [List]s, to
/// allow dividing that [Stream] of (potentially) arbitrarily sized [List]s into
/// a stream of fixed sized [List]s.
extension ChunkedStream<T> on Stream<List<T>> {
/// Convert the stream to a chunked stream, where each chunk is of the given
/// [size]. The size of each chunk must be at least one, but the size may be
/// any number larger than one (if the stream is finished before a chunk is
/// finished, that chunk is returned as-is).
Stream<List<T>> chunked(final int size) async* {
if (size < 1) {