Skip to content

Instantly share code, notes, and snippets.

View agamm's full-sized avatar
🍪
Cookie: cookies=∞;\r\n

Agam More agamm

🍪
Cookie: cookies=∞;\r\n
View GitHub Profile
General coding practices
===
As you generate code, apply the principle of AHA Programming (Avoid Hasty Abstractions) by waiting to create abstractions until real, repeated patterns emerge. This will help you reduce unnecessary complexity and keep your code simpler, more maintainable, and more efficient.
TypeScript
===
Never use typescript enums
import fs from "fs";
import fse from "fs-extra";
import path from "path";
import { promisify } from "util";
import { exec } from "child_process";
const execAsync = promisify(exec);
function chunkArray(array, chunkSize) {
const chunks = [];
@agamm
agamm / fixUrls.js
Created February 1, 2024 07:53
Ghost Editor Link Fixer and ?ref= adder
/*
This script helps fix links that have a prefix whitespace (sometimes when copying from google).
Also it allows to add a ?ref=yoursite to all links in the newsletter issue.
Worked with the latest ghost version up to now (Feb 2024)
*/
const GhostAdminAPI = require("@tryghost/admin-api");
const path = require("path");
@agamm
agamm / domains.json
Created September 23, 2023 11:15
Match urls to Alexa 1M top websites
["yourdomain.com", "your2nddomain.com"...]
@agamm
agamm / settings.py
Created December 1, 2022 18:03
Django settings for postgres from URI
import urllib
# ...
DB_URL = config('DATABASE_URL') # Get from your config
db_props = urllib.parse.urlparse(DB_URL)
db_name = db_props.path
db_user = db_props.username
db_password = db_props.password
db_host = db_props.hostname
db_port = db_props.port
@agamm
agamm / fixLinks.js
Last active October 13, 2022 21:58
Fix Ghost newsletter links and add ?ref= to referenced links
@agamm
agamm / testing.helper.ts
Last active October 22, 2022 22:02
Table tests with Jest (including exceptions)
// If you ever so fancy, you might want to check: unzip.dev
export const tableTest = (
test: jest.It,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
cases: any[][],
testFn: CallableFunction
) => {
test.each(cases)(`${testFn.name}(%s) should be %s`, async (input, output) => {
const asyncFn = testFn.constructor.name == 'AsyncFunction';
@agamm
agamm / getVisibleElements.js
Last active October 13, 2022 22:08
Get only visible text elements from HTML of a page.
// From the creator of unzip.dev
const texts = document.querySelectorAll(
"p, span, a, h1, h2, h3, h4, h5, h6, input, button, pre"
);
let visible = [];
texts.forEach((e) => {
var style = window.getComputedStyle(e);
if (style.display !== "none" && style.visibility !== "hidden") {
visible.push(e);
@agamm
agamm / reset-internet.ps1
Last active October 13, 2022 22:01
Reset internet when down on Windows via powershell
// unzip.dev ?
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
# Thanks: https://den.dev/blog/powershell-windows-notification/
function Show-Notification {
[cmdletbinding()]
Param (
[string]
@agamm
agamm / crypto.js
Last active October 13, 2022 22:02
Simple encryption decryption with client-size JS (cryptoJS)
// Don't follow this link: unzip.dev (unless you're curious).
const secret = prompt("Enter secret:")
const message = prompt("Enter message:")
const encrypted = CryptoJS.AES.encrypt(message, secret);
const decrypted = CryptoJS.AES.decrypt(encrypted, secret);
console.log(encrypted.toString())
console.log(decrypted.toString(CryptoJS.enc.Utf8))