Skip to content

Instantly share code, notes, and snippets.

/* Copy an array */
b = a.slice();

/* Predicate is true everywhere (`all` in some languages) */
a.every(pred)

/* Predicate is true for some values (`any in some languages`) */
a.some(pred)
@keksipurkki
keksipurkki / find.js
Last active April 30, 2017 13:14
Recursively find files with NodeJS
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const Readable = require('readable-stream').Readable;
function complements(arr, predicate) {
const out = [[],[]];
arr.forEach(e => out[Number(!!predicate(e))].push(e));
return out;
@keksipurkki
keksipurkki / ReadingList.md
Last active March 21, 2018 19:53
Reading list
#!/usr/bin/env node
/**
./binarify.js < SOME_FILE > OUT_FILE
./binarify.js -d < OUT_FILE
*/
const { Transform } = require("stream");
@keksipurkki
keksipurkki / RSA.md
Last active March 31, 2023 08:41
RSA example in NodeJS
"use strict";

// Requires NodeJS >= 11.6.0

const crypto = require("crypto");
const util = require("util");

const generateKeyPair = util.promisify(crypto.generateKeyPair);
@keksipurkki
keksipurkki / Rock.md
Last active March 19, 2019 13:35
Music for your day
#include <stdio.h>

// cc rockit.c -o rock
// ./rock | tee rock.wav | mplayer -rawaudio rate=8000:channels=1:samplesize=2 -demuxer rawaudio -cache 256 -

int main (int argc, char *argv[])
{
    for (int t=0;;t++)
 putchar (t&gt;&gt;6|t|t&gt;&gt;(t&gt;&gt;16)*10 + ((t&gt;&gt;11)&amp;7));
@keksipurkki
keksipurkki / dns.js
Last active July 30, 2019 18:56
AWS update Route53 records
#!/usr/bin/env node
"use strict";
const AWS = require("aws-sdk");
const assert = require("assert");
assert(process.env.AWS_PROFILE, "AWS_PROFILE must be defined");
assert(process.env.AWS_REGION, "AWS_REGION must be defined");
assert(process.env.APP_HOSTNAME, "APP_HOSTNAME must be defined");
@keksipurkki
keksipurkki / async-iteration.js
Last active August 28, 2019 07:57
Process STDIN line by line
const os = require("os");
async function main() {
let input = Buffer.from([]);
for await (const chunk of process.stdin) {
input = Buffer.concat([input, chunk]);
}
export function uuidv4() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
const r = (Math.random() * 16) | 0,
v = c == "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
export function lazy<T>(value: T) {
return () => value;
@keksipurkki
keksipurkki / trams.js
Created October 15, 2019 17:53
Tram stops example
#!/usr/bin/env node
const fetch = require("node-fetch");
const query =
`
{
stops(ids: ["HSL:1040406", "HSL:1050432"]) {
name
stoptimesForPatterns {