Skip to content

Instantly share code, notes, and snippets.

Avatar
🎄

pooya parsa pi0

🎄
View GitHub Profile
@pi0
pi0 / hrtime.js
Last active Nov 15, 2020
process.hrtime polyfill
View hrtime.js
(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 Nov 11, 2020
Nuxt Unattended Config Injection
View README.md

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.

View gist:0d55194d6c8415f5d8b9b7050fe76bf3
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)
})
View nuxt-functins.md

Nuxt functions implementation specs

Initial RFC: https://github.com/nuxt/rfcs/issues/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 Aug 20, 2020
Vue Universal Runtime Compile
View cms.vue
<template>
<div>
<component :is="CMSComponent" :x="x" />
<button @click="x++">
Click on me
</button>
</div>
</template>
<script>
@pi0
pi0 / WikipediaDump.md
Last active Feb 27, 2020
A Dummy Way to Extract Clean Wikipedia Contents
View WikipediaDump.md

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 Nov 10, 2020
doNotTrack polyFill
View README.md

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)

@pi0
pi0 / git_commit.js
Last active Nov 18, 2019
Get git commit in one line!
View git_commit.js
const gitCommit = process.env.GIT_COMMIT || require('child_process')
.spawnSync('git', ['rev-parse', '--short', 'HEAD'], { encoding: 'utf8' })
.stdout.slice(0, -1)
console.log(gitCommit)
View vscode.json5
{
// Editor
"diffEditor.renderSideBySide": true,
"editor.fontFamily": "Anonymous, InputMono, Menlo, Monaco, 'Courier New', monospace",
"editor.find.autoFindInSelection": true,
"editor.fontLigatures": true,
"editor.formatOnSave": false,
"editor.formatOnType": true,
"editor.formatOnPaste": true,
View hotel.js
function substractIdArrays(a, b) {
return a.filter(x => !b.find(y => y + '' === x + ''))
}
async function updateArea(areaCode) {
const area = await Area.findOne({ areaCode })
const associatedHotels = await Hotel.find({ areaCode }).then(hotels => hotels.map(h => h._id))
const hotelsInArea = await Hotel.find({