Skip to content

Instantly share code, notes, and snippets.

View fzn0x's full-sized avatar
🌎
Makes international software.

fzn0x fzn0x

🌎
Makes international software.
View GitHub Profile
@fzn0x
fzn0x / dummy.json
Created May 3, 2022 05:06
dummy json
{
"dummy": true
}

Do not use forEach with async-await

TLDR: Use for...of instead of forEach in asynchronous code.

The problem

Array.prototype.forEach is not designed for asynchronous code. (It was not suitable for promises, and it is not suitable for async-await.)

For example, the following forEach loop might not do what it appears to do:

@fzn0x
fzn0x / toSuperSnakeCase.js
Created April 17, 2022 22:07
Convert camelCase to snakeCase without including capital-only word, first capital word like replace(/([A-Z])/g, "_$1").toLowerCase(); to lower case.
const toSnakeCase = str =>
str &&
str
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
.map(x => x.toLowerCase())
.join('_');
@fzn0x
fzn0x / iframe_scrape.js
Created April 15, 2022 22:44
Scrape using puppeteer through iframe
const frame = await page.waitForSelector("iframe");
const framePage = await frame.contentFrame();
const framePageData = await framePage.$$eval(".table tbody tr", (columns) => {});
var childProcess = require('child_process');
function runScript(scriptPath, callback) {
// keep track of whether callback has been invoked to prevent multiple invocations
var invoked = false;
var process = childProcess.fork(scriptPath);
// listen for errors as they may prevent the exit event from firing
@fzn0x
fzn0x / functionalParseDotNotation.js
Created April 9, 2022 20:56
Parse dot notation functional approach.
export const parseDotNotation = (obj, notation = "") => {
return (notation || "").split(".").reduce((o, i) => o?.[i], obj);
}
@fzn0x
fzn0x / imperativeParseDotNotation.js
Created April 9, 2022 20:55
Parse dot notation imperative approach.
export const parseDotNotation = (obj, notation = "") => {
if (!(notation || "").includes('.')) return obj[notation];
let value = undefined;
const dots = notation.split('.');
for (let i = 0; i < dots.length; i++) {
value = value === undefined ? obj[dots[i]] : value[dots[i]];
}
return value;
}
@fzn0x
fzn0x / [[{}],[{}]].js
Last active March 24, 2022 10:41
Made array of millions data to tuples of 1000 data.
const tuple = arr.reduce(function (r, a, i) {
if (i % 1000) {
r[r.length - 1].push(a);
} else {
r.push([a]);
}
return r;
}, []);
@fzn0x
fzn0x / watcher.js
Created March 18, 2022 06:34
FIX: MongoServerError: Cursor session id is not the same as the operation context's session id (none)
//watches the collection for any inserted documents
function startWatcher (collection) {
console.log('starting watcher');
let watcher = collection.watch([{ $match: { operationType: "insert" }}])
.on("change", (changeEvent) => {
/*whatever you want to do when event is fired*/
})
//changestream connection died (cursor session id is not the same as the operation contexts session id)
.on('error', e => {
console.error('watcher died');
@fzn0x
fzn0x / gist:fa6f5e171679311513243eee094f9392
Created March 1, 2022 18:11
delete duplicate data and keep last record without getting any warning from your MySQL wife
DELETE FROM historical WHERE id NOT IN (SELECT * FROM (SELECT MAX(id) FROM historical GROUP BY guild_id, bot_id, bot_pair, created_at) as t);