Skip to content

Instantly share code, notes, and snippets.

View johandalabacka's full-sized avatar
🤡
Fiddling with unknown things...

Johan Dahl johandalabacka

🤡
Fiddling with unknown things...
View GitHub Profile
@johandalabacka
johandalabacka / readStdin.js
Created June 16, 2023 07:03
Read and return all of stdin in node
function readStdin () {
return new Promise((resolve, reject) => {
process.stdin.resume()
process.stdin.setEncoding('utf8')
let buffer = ''
process.stdin.on('data', (chunk) => {
buffer += chunk
})
process.stdin.on('error', (err) => {
reject(err)
/**
*
* @param {Response} response
* @returns {any}
*/
async function _fetchBody (response) {
const contentType = response.headers.get('content-type')
if (contentType === 'application/json') {
return await response.json()
function fetchWithParams(resource, params, init) {
const u = resource instanceof URL ? resource : new URL(resource)
for([k,v] of Object.entries(params)) {
u.searchParams.set(k, v)
}
return fetch(u, init)
}
@johandalabacka
johandalabacka / clear-cache.ts
Created October 30, 2021 07:59
Deno clear cache
#!/usr/bin/env deno run --allow-run --allow-write
const p = Deno.run({
cmd: ['deno', 'info', '--json'],
stdout: 'piped',
env: { NO_COLOR: "1" } // No color in output
})
const status = await p.status()
if (!status.success) {
Deno.exit(status.code)
@johandalabacka
johandalabacka / lines.js
Last active October 18, 2021 01:14
Iterator file lines (naive version)
import fs from 'fs/promises';
async function* lines(path) {
const content = await fs.readFile(path, { encoding: 'utf-8' })
const lines = content.split(/\n/)
for (const line of lines) {
yield line
}
}
@johandalabacka
johandalabacka / range.js
Created October 17, 2021 23:09
Range iterator
function* range(start, end, step = 1) {
if (start < end) {
for (let i = start; i <= end; i += step) {
yield i
}
} else {
for (let i = start; i >= end; i -= step) {
yield i
}
}
@johandalabacka
johandalabacka / range.dart
Created October 16, 2021 15:27
Range of ints iterator
/// Generate numbers from start to end.
/// If step is 0 will it return start indefinitly
Iterable<int> range(int start, int end, [int step = 1]) sync* {
if (start > end && step > 0 || start > end && step < 0) {
step = -step;
}
var i = start;
while (i <= end) {
yield i += step;
}
@johandalabacka
johandalabacka / build-deno-centos7.sh
Created June 11, 2021 22:15
Build deno on centos7
#!/bin/sh
# The offical build for linux doesn't work on Centos/RHEL 7
# The error is deno: /lib64/libc.so.6: version `GLIBC_2.18' not found (required by deno)
# The version on Centos/RHEL7 is GLIBC_2.17
#
# Inspiration from https://gist.github.com/nodakai/bc0c80381cd0b787d8a5c65a1771ef5f
# On a newly installed centos 7 machine (Machine needs at least 4Gb memory)
sudo yum install -y git gcc
function objectDiff(o1, o2) {
o = {}
for(let [key, value] of Object.entries(o2)) {
if (value !== o1[key] && o1[key] !== undefined) {
o[key] = value
}
}
return o
@johandalabacka
johandalabacka / forInterval.js
Created October 29, 2019 10:21
Run a function func, count times with a delay of delay milliseconds between each
function forInterval(func, count, delay) {
let interval = setInterval(() => {
if (count-- === 0) {
clearInterval(interval);
} else {
func();
}
}, delay);
}