Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
crazy4groovy / args.ts
Last active Apr 23, 2021
Runs a given command for each file in a folder, recursively; replaces #FILE_PATH, #FILE_NAME, #FILE_SIZE_KB in command (Deno)
View args.ts
import * as fs from "https://deno.land/std@0.93.0/fs/mod.ts";
import * as path from "https://deno.land/std@0.93.0/path/mod.ts";
export default () => {
let [execCmdTemplate, basedir = ".", fileExt = ""] = Deno.args;
if (!execCmdTemplate) throw new Error("No command was given");
if (!basedir || !fs.existsSync(basedir)) throw new Error("DIR doesn't exist");
basedir = path.resolve(".", basedir);
return { execCmdTemplate, basedir, fileExt };
}
@crazy4groovy
crazy4groovy / dl.ts
Last active Apr 19, 2021
dl images from cdn (Deno)
View dl.ts
import { Timeout, TimeoutError } from "https://deno.land/x/timeout/mod.ts"
export default async function download(
source: string,
destination: string
): Promise<boolean> {
try {
const req = fetch(source);
const response = await Timeout.race([req], 5000);
const blob = await response.blob();
@crazy4groovy
crazy4groovy / fetcher.js
Last active Apr 6, 2021
Simple web page html parser into markdown or JSON (NodeJS)
View fetcher.js
const fetch = require('node-fetch')
module.exports = async ({q, url}) =>
fetch(
url
? decodeURIComponent(url)
: `https://en.wikipedia.org/wiki/${decodeURIComponent(q)}`
).then(res => res.text())
@crazy4groovy
crazy4groovy / fetch-file.js
Last active Apr 7, 2021
fetch / download a URL / binary file (NodeJS)
View fetch-file.js
const fs = require("fs");
const fetch = require("node-fetch");
function dl(url, destFilename) {
return new Promise((resolve, reject) => {
fetch(url).then((res) => {
res.body.pipe(fs.createWriteStream(destFilename))
res.body.on('end', resolve)
res.body.on('error', reject)
}).catch(reject)
@crazy4groovy
crazy4groovy / file-logger.js
Last active Mar 26, 2021
simple file logger (NodeJS)
View file-logger.js
const newFileLogger = (filename, init = false) => {
if (init) fs.writeFileSync(filename, '', 'utf8'); // wipe/reset
return (...lines) => {
fs.appendFileSync(
filename,
lines
.map(l => JSON.stringify(l, null, 2))
.join('\n') + '\n',
'utf8'
)
@crazy4groovy
crazy4groovy / by-line-lib.js
Last active Apr 1, 2021
Stream: a large text file line-by-line; dir recursively (NodeJS)
View by-line-lib.js
const fs = require("fs");
const readline = require("readline");
const byLine = async ({file, handleLine, charset="utf8"}) => {
return new Promise((done, err) => {
if (!fs.existsSync(file)) {
return err(new Error('File not found!'));
}
const rl = readline.createInterface({
input: fs.createReadStream(file, charset),
@crazy4groovy
crazy4groovy / lock.js
Last active Apr 1, 2021
A simple locking mechanism (for execution throttling, etc) (JavaScript)
View lock.js
function newThrottler({isBusy, lock, unlock, waitMs}) {
const delay = (ms) => new Promise((r) => setTimeout(r, ms));
return async function throttler(cb, ...args) {
while (isBusy()) {
await delay(waitMs());
}
lock();
// ... DO ALL WORK for result
@crazy4groovy
crazy4groovy / listify.js
Last active Apr 1, 2021
Human readable list concat from an Array (JavaScript)
View listify.js
// see: https://kentcdodds.com/blog/listify-a-java-script-array
function listify(
array,
{
type = 'conjunction',
style = 'long',
stringify = JSON.stringify,
lang = 'en'
} = {}
View scramble.css
@import 'https://fonts.googleapis.com/css?family=Roboto+Mono:100';
html,
body {
font-family: 'Roboto Mono', monospace;
background: #212121;
height: 100%;
}
.container {
height: 100%;
@crazy4groovy
crazy4groovy / escalade-test.js
Last active Apr 1, 2021
escalade (walk) directory structure up (JavaScript)
View escalade-test.js
const e = require("./escalade");
(async () => {
// A - for-await-of loop
let g = e(__dirname);
for await (const { dir, filenames } of g) {
console.log(dir, filenames);
}
// B - .next()