Skip to content

Instantly share code, notes, and snippets.

View vaidd4's full-sized avatar

David vaidd4

  • France
View GitHub Profile
@vaidd4
vaidd4 / goldmark-cli.go
Created February 10, 2022 23:08
Markdown to Html Converter - Basic command line tool using https://github.com/yuin/goldmark
package main
import (
"bufio"
"github.com/yuin/goldmark"
"os"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
@vaidd4
vaidd4 / cbtoasync.js
Created January 12, 2021 22:50
Callback-based function to promise-based function.
function cbtoasync(fncb, ...params) {
return new Promise((res, rej) => {
fncb(...params, (...data) => {res(...data)})
})
}
function tr_cbtoasync(fncb) {
return (...data) => cbtoasync(fncb, ...data)
}
@vaidd4
vaidd4 / git-worktree-cheatsheet.md
Last active January 24, 2024 15:58
A cheat sheet for Git Worktrees

Git Worktree Cheatsheet

Documentation

Setup a folder inside a repo with a specific commit of that repo.

Create a new worktree

git worktree add [-f] [--detach] [--checkout] [--lock] [-b ]  []

Git orphan branches & working trees

Creating an ophan branch

This command will create a branch with no parent (no commit history) on a repo.

git checkout --orphan <new-branch>

Managing working trees

Manage folders linked to others branches of your repo. Do not confuse them with git submodules which are links to different repositories.

@vaidd4
vaidd4 / object-mapper.js
Last active May 24, 2019 13:57
Map to object with a mapping description
const f1 = a => a + 1
const f2 = (...args) => args.join(', ')
const src = { a: 1, b: 2, c: 3, d: 4, e: 5 }
const mapDesc = [
'a',
['b', 'f'],
[[f1, 'c'], 'g'],
[[f2, 'd', 'e'], 'h']
@vaidd4
vaidd4 / null-undefined-properties.js
Created May 21, 2019 13:17
JS handling of undefined & null as object properties
let obj = { a: 'a', b: null, undefined: 'undef', null: 'nil' }
obj['a'] // 'a'
obj[obj.b] // 'nil'
obj[obj.c] // 'undef' (obj.c is undefined)
obj[obj] // undefined
// Can be used for making conditions with objects
@vaidd4
vaidd4 / static_server.js
Last active March 14, 2019 15:56 — forked from ryanflorence/static_server.js
Node.JS static file web server. Put it in your path to fire up servers in any directory, takes an optional port argument.
const http = require('http')
const url = require('url')
const path = require('path')
const fs = require('fs')
const PORT = parseInt(process.argv[2]) || 8888
http.createServer(function(req, res) {
const uri = url.parse(req.url).pathname
const filename = path.join(process.cwd(), uri)
@vaidd4
vaidd4 / ObjectPick.js
Last active June 27, 2018 12:39
Pick the specified properties of an object
/**
* Pick the specified properties of an object
*
* @param obj
* @param {Array<Array<string> | string>} keys
* @returns {{}}
*/
function pick (obj, keys) {
return keys.reduce((sum, k) => (Array.isArray(k) ? sum[k[1]] = obj[k[0]] : sum[k] = obj[k], sum), {})
}
@vaidd4
vaidd4 / LogObservable.js
Last active March 22, 2018 13:47
RxJS/Observable logging before `subscribe()`
/**
* Simple logging on an observable, without altering the subscription.
*
* @param {Observable<any>} obs - Observable (RxJS)
* @param success - success message
* @param failure - error message
* @returns {Observable<any>}
*/
function logObservable (obs, success, failure) {
if (success) {
'use strict';
const parseMs = require('parse-ms');
const plur = require('plur');
const units = [{ s: 'y', l: 'year' },
{ s: 'd', l: 'day' },
{ s: 'h', l: 'hour' },
{ s: 'm', l: 'minute' },
{ s: 's', l: 'second' },
{ s: 'ms', l: 'millisecond' }];