Skip to content

Instantly share code, notes, and snippets.

View vaidd4's full-sized avatar

David vaidd4

  • France
View GitHub Profile
@vaidd4
vaidd4 / DocumentLog.js
Last active July 18, 2017 13:33
A document.log() implementation
/**
* Add a 'pre' tag, with a specified id, to the page and log the arguments passed to it
*
* WIP:
* - support array & object deep log
* - option to log in console (last parameter)
*/
function documentLog (...arg) {
if (!arg || !arg.length) return
if (!((typeof arg[0]) === 'string')) return
@vaidd4
vaidd4 / ConsoleWatch.js
Last active March 22, 2018 13:45
A console.watch() implementation
/**
* Watch for object properties
* you can log on each property change and even add a breakpoint
*/
function consoleWatch (oObj, sProp) {
let sPrivateProp = `_${sProp}_$`
oObj[sPrivateProp] = oObj[sProp]
Object.defineProperty(oObj, sProp, {
get: () => { return oObj[sPrivateProp] },
set: value => {
@vaidd4
vaidd4 / xblihapi.js
Last active April 25, 2019 08:15
eXtended Blih API project
/**
* This is a proxy for making w3c compliant requests to the EPITECH's BLIH API.
* It allows to make such calls fron any w3c compliant source (e.g. XHR requests).
* The proxy uses https for security reasons.
* To check BLIH API status simply make a GET request on '/'.
*
* TODO:
* - Add endpoint showing every endpoint available (API documentation).
*/
@vaidd4
vaidd4 / checkDeepObject.js
Last active August 31, 2017 09:46
A function to make deep checking on objects
/**
* Run a simple check on every attribute of an object
* It returns true if any attribute (non object) passes the test function
*/
function checkDeep(obj, fun) {
for (let k in obj) {
if (!obj.hasOwnProperty(k)) continue
if (Array.isArray(obj[k])) {
if (obj[k].some(fun))
@vaidd4
vaidd4 / StringifyDuration.js
Last active November 29, 2017 11:49
Simple durations stringifier for Moment.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) {
'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 / 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) {
@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 / 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 / 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