Skip to content

Instantly share code, notes, and snippets.

View droid001's full-sized avatar

droid001

View GitHub Profile
@droid001
droid001 / pubsub.js
Created October 20, 2022 14:18
PubSub class with UUID
export const getUUID = ()=>{
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace( /[xy]/g ,
function(c) {
var rnd = Math.random()*16 |0, v = c === 'x' ? rnd : (rnd&0x3|0x8) ;
return v.toString(16);
}
);
}
@droid001
droid001 / isElement.js
Created June 10, 2021 07:26
Checks if the argument is an instance of Element
/**
* isElement - Checks if the argument is a DOM element. from https://stackoverflow.com/a/36894871/7197027
*
* @param {object} element Argument to be tested
*
* @returns {Boolean} Result if the argument is a DOM element or not.
*/
export const isElement = element=> element instanceof Element || element instanceof HTMLDocument;
@droid001
droid001 / git delete gone branches
Created March 31, 2021 11:24
Script for git which deletes local branches which are deleted remotely
git config --global alias.gone "! git fetch -p && git for-each-ref --format '%(refname:short) %(upstream:track)' | awk '\$2 == \"[gone]\" {print \$1}' | xargs -r git branch -D"
@droid001
droid001 / hasPointer.js
Created September 2, 2020 08:28
Tests the user agent for the existance of a pointer. Used to tect mobile deveices.
// Derived from https://stackoverflow.com/a/52854585/7197027
export const hasPointer = ()=>window.matchMedia("(any-hover: none)").matches === false
Number(num) != NaN && Number.isInteger(Number(num))
@droid001
droid001 / string-helpers.js
Created May 27, 2020 08:19
replace diacritics from a string
// From https://gist.github.com/yeah/1283961
const diacriticsMap = [
{'base':'A', 'letters':/[\u0041\u24B6\uFF21\u00C0\u00C1\u00C2\u1EA6\u1EA4\u1EAA\u1EA8\u00C3\u0100\u0102\u1EB0\u1EAE\u1EB4\u1EB2\u0226\u01E0\u01DE\u1EA2\u00C5\u01FA\u01CD\u0200\u0202\u1EA0\u1EAC\u1EB6\u1E00\u0104\u023A\u2C6F]/g},
{'base':'AA','letters':/[\uA732]/g},
{'base':'AE','letters':/[\u00C4\u00C6\u01FC\u01E2]/g},
{'base':'AO','letters':/[\uA734]/g},
{'base':'AU','letters':/[\uA736]/g},
{'base':'AV','letters':/[\uA738\uA73A]/g},
{'base':'AY','letters':/[\uA73C]/g},
{'base':'B', 'letters':/[\u0042\u24B7\uFF22\u1E02\u1E04\u1E06\u0243\u0182\u0181]/g},
@droid001
droid001 / formatNumber.js
Last active April 5, 2020 20:28
Format the number to a string space padded every 3 decimals
/**
* Format the number to a string space padded every 3 decimals
*/
export const formatNumber = (n, suffix="")=>{
let num = String(n).split(".")[0];
let decimal = String(n).split(".")[1];
decimal = decimal ? "," + decimal : "";
let numStr = "";
let l = num.length-1;
let i = num.length;
@droid001
droid001 / math.js
Created March 21, 2020 13:35
rounds off number by given amount of decimal points
export const roundOff = (num, decimalPoints)=>Math.round( num * Math.pow(10, decimalPoints) ) / Math.pow(10, decimalPoints)
@droid001
droid001 / number.js
Created January 20, 2020 08:51
Checks if the value is a number
/**
* Checks if the value is a Number.
* For a longer discussion on the solution see: https://medium.com/javascript-in-plain-english/how-to-check-for-a-number-in-javascript-8d9024708153
*/
export const isNumber = val=>Number.isFinite(val);
@droid001
droid001 / randomColor.js
Created October 4, 2019 07:32
generates a random color
const randomColour = () => '#'+(Math.random()*0xFFFFFF<<0).toString(16);