Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:
FOSS

fzn0x

:octocat:
FOSS
View GitHub Profile
@fzn0x
fzn0x / signature.html
Last active Jun 29, 2022
Receive metamask signature by sign in to wallet and sign message.
View signature.html
<html>
<head>
<title>Web3 Metamask Login</title>
<link
href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css"
rel="stylesheet"
/>
</head>
<body class="flex w-screen h-screen justify-center items-center">
@fzn0x
fzn0x / copyable.js
Last active Jun 17, 2022
Merge your schemas like a boss in GraphQL - How to merge schema in GraphQL with Nodejs
View copyable.js
// schema.js
const path = require("path");
const fs = require("fs");
const { makeExecutableSchema } = require("@graphql-tools/schema");
const { stitchSchemas } = require("@graphql-tools/stitch");
const subschemas = [];
const schemaDir = path.resolve(__dirname, "./schema");
@fzn0x
fzn0x / getTimestamp.js
Created Jun 15, 2022
Format dates with pattern matching in Javascript
View getTimestamp.js
const monthNames = [
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
];
const timeZone = n => ({
true: "Asia/Jakarta",
[ n === "WIB" ]: "Asia/Jakarta",
@fzn0x
fzn0x / example.js
Created Jun 13, 2022
Axie composition and valuation sample script
View example.js
const { AbortController } = require("node-abort-controller");
const HttpsProxyAgent = require("https-proxy-agent");
const { ua, randomElement } = require("../config/userAgent.js");
async function runRequestAxieBriefList(options, type = "") {
const timeout = 60000;
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
@fzn0x
fzn0x / mongodb_group.js
Created May 20, 2022
Group data by dates in mongodb without extract all the values.
View mongodb_group.js
import mongoose from "mongoose";
import dotenv from "dotenv";
dotenv.config();
mongoose
.connect(process.env.MONGO_SOURCE)
.then(async () => {
console.log("connected");
View dummy.json
{
"dummy": true
}
View async-await-forEach-alternatives.md

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 Apr 17, 2022
Convert camelCase to snakeCase without including capital-only word, first capital word like replace(/([A-Z])/g, "_$1").toLowerCase(); to lower case.
View toSuperSnakeCase.js
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 Apr 15, 2022
Scrape using puppeteer through iframe
View iframe_scrape.js
const frame = await page.waitForSelector("iframe");
const framePage = await frame.contentFrame();
const framePageData = await framePage.$$eval(".table tbody tr", (columns) => {});
View child_process.js
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