Skip to content

Instantly share code, notes, and snippets.

Avatar

David vaidd4

  • France
View GitHub Profile
@vaidd4
vaidd4 / cbtoasync.js
Created Jan 12, 2021
Callback-based function to promise-based function.
View cbtoasync.js
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 Dec 29, 2020
A cheat sheet for Git Worktrees
View git-worktree-cheatsheet.md

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 <new-branch>] <path> [<commit-ish>]
View git-orphan-worktree.md

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
Map to object with a mapping description
View object-mapper.js
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
JS handling of undefined & null as object properties
View null-undefined-properties.js
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 Mar 14, 2019 — 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.
View static_server.js
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 Jun 27, 2018
Pick the specified properties of an object
View ObjectPick.js
/**
* 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 Mar 22, 2018
RxJS/Observable logging before `subscribe()`
View LogObservable.js
/**
* 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) {
View CustomPrettyMs.js
'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' }];
@vaidd4
vaidd4 / StringifyDuration.js
Last active Nov 29, 2017
Simple durations stringifier for Moment.js
View StringifyDuration.js
/**
* Simple durations stringifier for Moment.js
* Return a human readable string representing the given moment duration
* Based on @cogwirrel comment (https://github.com/moment/moment/issues/348#issuecomment-346233713)
*
* TODO:
* - Add simple lang support (fr, en)
*/
function stringifyDuration (duration) {