Skip to content

Instantly share code, notes, and snippets.

View pi0's full-sized avatar

Pooya Parsa pi0

View GitHub Profile
# The Lazy Coder's Guide to Programming
## Chapter 1: The Art of Copy-Pasting
### Section 1.1: Ctrl+C, Ctrl+V, Repeat
Programming can be hard. But fear not! With the power of copy-paste, you can conquer any coding challenge without breaking a sweat. Just remember: if it works once, it'll work a thousand times. Who needs original code anyway?
## Chapter 2: Debugging 101: Blame the Compiler
@pi0
pi0 / to-pnpm.mjs
Last active March 15, 2022 20:15
Script to migrate to pnpm
#!/bin/env node
import { existsSync } from 'fs'
import fs from 'fs/promises'
import path from 'path'
import { execSync } from 'child_process'
const LATEST_PNPM = '6.32.3'
const dir = path.resolve(process.argv[2] || '.')
const resolve = (...p) => path.resolve(dir, ...p)
@pi0
pi0 / next.config.js
Created March 12, 2021 17:29
Webpackbar with Next.js
const Webpackbar = require('webpackbar')
module.exports = {
webpack: (config, { isServer }) => {
config.plugins.push(new Webpackbar({ name: isServer ? 'server' : 'client' }))
return config
}
}
@pi0
pi0 / hrtime.js
Last active November 15, 2020 02:32
process.hrtime polyfill
(function() {
const nowOffset = Date.now();
const now = () => Date.now() - nowOffset;
global.process.hrtime = global.process.hrtime || ((previousTimestamp) => {
const baseNow = Math.floor((Date.now() - now()) * 1e-3)
const clocktime = now() * 1e-3
let seconds = Math.floor(clocktime) + baseNow
let nanoseconds = Math.floor((clocktime % 1) * 1e9)
if (previousTimestamp) {
@pi0
pi0 / README.md
Last active November 11, 2020 14:32
Nuxt Unattended Config Injection

Nuxt loads configuration from several places and merges. Here is the order: (first has more periority)

  • Config overrides by CLI
  • nuxt.config (format: CJS/MJS/TS can be also wrapped into an async function)
  • .nuxtrc
  • ~/.nuxtrc

In order to automatically inject some configuration without direct modification of nuxt.config, it is possible to use .nuxtrc file.

Thanks to super simpler syntax of rc9 this configuration can be injected via a shell script or package itself.

const fs = require('fs')
const axios = require('axios')
callback
fs.readFile('test.txt', 'utf-8', (err, str) => {
if (err) {
console.error('Something bad!' + err)
}
console.log(str)
})

Nuxt functions implementation specs

Initial RFC: nuxt/rfcs#35

This is a Draft

Introducing a functions/ directory could have a powerful effect for users that want to add server functions but without having to go through serverMiddleware Functions can be also provided by Nuxt modules, for example, CMS module or auth provider can add it's functionalities to the user context.

Usage

@pi0
pi0 / cms.vue
Last active August 20, 2020 00:57
Vue Universal Runtime Compile
<template>
<div>
<component :is="CMSComponent" :x="x" />
<button @click="x++">
Click on me
</button>
</div>
</template>
<script>
@pi0
pi0 / WikipediaDump.md
Last active February 27, 2020 14:16
A Dummy Way to Extract Clean Wikipedia Contents

Dump Wikipedia

  • Go to https://dumps.wikimedia.org/backup-index.html
  • Go to language and download -pages-meta-current.xml.bz2 version
  • Extract
  • Flatten with pv file.xml | xml2 > file.flat
  • Exrtract text with pv file.flat| grep -oP "(?<=^/mediawiki/page/revision/text=).*" > file.txt
  • Clean with pv fawiki-20200220-pages-articles.txt | node clean.js
  • Enjoy using out.txt
@pi0
pi0 / README.md
Last active November 10, 2020 12:45
doNotTrack polyFill

Tiny (230 char) Polyfill for window.doNotTrack. Checks for:

  • window.doNotTrack == 1
  • navigator.doNotTrack == 'yes'
  • navigator.doNotTrack == 1
  • navigator.msDoNotTrack == 1'
  • window.external.msTrackingProtectionEnabled()

And sets value to either 1 (do not track) or 0 (track)