This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @param {Response} response | |
* @returns {any} | |
*/ | |
async function _fetchBody (response) { | |
const contentType = response.headers.get('content-type') | |
if (contentType === 'application/json') { | |
return await response.json() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function forInterval(func, count, delay) { | |
let interval = setInterval(() => { | |
if (count-- === 0) { | |
clearInterval(interval); | |
} else { | |
func(); | |
} | |
}, delay); | |
} |
NewerOlder