Skip to content

Instantly share code, notes, and snippets.

@remorses
remorses / test.ts
Created June 26, 2024 10:31
Cuid2 is very slow, don't use it
import { createId } from '@paralleldrive/cuid2'
import { v7, v4 } from 'uuid'
function cuid() {
let n = 1_000_000
let list = [] as string[]
let start = Date.now()
for (let i = 0; i < n; i++) {
let id = createId()
list.push(id)
@remorses
remorses / renamer.ts
Last active May 6, 2024 21:32
Babel `BatchRenamer`, 100x faster babel `scope.rename()` when you have many identifier to rename
// original https://github.com/babel/babel/blob/9c77558234c87b9220604fbc1519089e2d6334e2/packages/babel-traverse/src/scope/lib/renamer.ts#L61
import splitExportDeclaration from '@babel/helper-split-export-declaration'
import type { Scope } from '@babel/traverse'
import { visitors } from '@babel/traverse'
import { traverseNode } from '@babel/traverse/lib/traverse-node'
import * as t from '@babel/types'
import { NodePath, Visitor } from '@babel/core'
import type { Identifier } from '@babel/types'
@remorses
remorses / script.py
Created March 4, 2024 11:17
Convert Openai ChatGPT exported data to csv
import csv
import json
def extract_messages(data, max_messages):
messages = []
for message_id, message_data in data["mapping"].items():
if message_data["message"]:
message = message_data["message"]
content = message.get("content", {}).get("parts", [""])[0]
@remorses
remorses / main.py
Created January 24, 2024 10:11
Remove Adobe watermarks from pdf with Python
from pypdf import PdfReader, PdfWriter
def remove_watermark(pdf_path, output_path):
reader = PdfReader(pdf_path)
writer = PdfWriter()
for page in reader.pages:
if '/Resources' in page and '/XObject' in page['/Resources']:
@remorses
remorses / gist:ceb7a3cfc3fac671866b5c67d464a1a5
Created July 11, 2023 18:56
nextjs pattern to redirect to /docs.js
module.exports = {
async redirects() {
return [
{
source: '/',
destination: 'https://example.com/docs',
permanent: false,
},
{
source: '/:slug*',
import path from "path";
import fs from "fs";
import { getPackages } from "@monorepo-utils/package-utils";
const sourceDir = "src";
getPackages(__dirname).map(x => {
let types = getTypesFile({
sourceDir: path.resolve(x.location, sourceDir)
});
@remorses
remorses / main.graphql
Created March 7, 2020 15:39
Get graphql enum values
query enumValuesOfMetaInformationTags {
__type(name: "META_INFORMATION_TAGS") {
name
enumValues {
name
}
}
}
@remorses
remorses / main.ts
Created February 26, 2020 15:21
Download a file with node-fetch
const buffer = await fetch(url).then((r) => r.buffer())
const content = buffer.toString('utf8')
// then pass this where you like
@remorses
remorses / main.ts
Created February 26, 2020 14:44
Cloud storage uplaod file and get url
import admin from 'firebase-admin'
const file = admin
.storage()
.bucket('')
.file('name.txt')
await file.save('dsfgsdf', {
gzip: true,
public: true,
contentType: 'image',
})
@remorses
remorses / main.md
Created January 25, 2020 14:48
react-extra-hooks-article

I have always used Apollo to make graphql requests inside of react, usually doing something like this

import { useQuery } from '@apollo/react-hooks';
import gql from 'graphql-tag';

const GET_GREETING = gql`
  query getGreeting($language: String!) {
    greeting(language: $language) {
      message