Skip to content

Instantly share code, notes, and snippets.

View nash403's full-sized avatar
🦄
want a cookie ?

Honoré Nintunze nash403

🦄
want a cookie ?
View GitHub Profile
@nash403
nash403 / rx_multiclick.js
Created May 4, 2016 13:48
Distinguish clicks and multi clicks with RxJS
var tapStream = Rx.Observable.fromEvent(element, 'click');
// multi-tap logic
var multiTapStream = tapStream
.buffer(function() { return tapStream.debounce(250); })
.map(function(list) { return list.length; })
.filter(function(x) { return x >= 2; });
// Same as above, but detects single taps
var singleTapStream = tapStream
@nash403
nash403 / insertBetweenElements.js
Created June 29, 2016 12:48
JS function that inserts a value between each elements of an array
function insertBetweenElements(arr, value) {
// Get first element in array
var first = arr.shift();
// if array is empty return array
if (!first) return arr;
// insert the value
return arr.reduce(function(valeurPrecedente, valeurCourante, index, array){
return valeurPrecedente.concat(value,valeurCourante);
},(Array.isArray(first) ? first : [first]));
};
@nash403
nash403 / AccessibleMasking.css
Created July 5, 2016 08:22
Accessible Masking for screen readers
.sr_only {
position: absolute !important;
height: 1px;
width: 1px;
overflow: hidden;
clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
clip: rect(1px, 1px, 1px, 1px);
}
@nash403
nash403 / safe-get.js
Last active September 14, 2016 08:28
Safe access to any nested value of a javascript object without getting a TypeError but undefined instead.
// Utils
function isFnCall(key){
if (typeof key !== 'string') return false;
return key.slice(-2) === "()";
}
/*
* @param {key} string concatenation of nested keys in this form: 'foo.bar.toto'.
* You can even call a function if the last key ends with '()'.
* @param {obj} the object we are accessing
* @return a nested value OR the result of a nested function OR undefined
@nash403
nash403 / browser.html
Last active September 18, 2016 18:01
Writing JS for both Node & Browser
<script src="mymodule.js"></script>
<script>
alert(mymodule.test());
</script>
@nash403
nash403 / excerpt_package.json.txt
Last active November 22, 2018 15:45
Node/Npm script to force update project dependencies (command: npm run update:packages)
“scripts”: {
“update:packages”: “node wipe-dependencies.js &&
rm -rf node_modules && npm update --save-dev
&& npm update --save”
},
@nash403
nash403 / myExport.js
Created September 19, 2016 17:43
Simple export function for nodeJS modules meant to be used in the browser
function myExport(exported, isDefault, name) {
if (!name) throw "myExport function: <name> mustn't be undefined";
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
if (isDefault) {
module.exports = exported;
} else {
module.exports[name] = exported;
}
}
else {
@nash403
nash403 / insertBtwn.js
Last active October 3, 2016 12:26
Insert an element between array elements
/*
* Insert insertedEl or the result of insertedEl (if it is a function) between the elements of arr.
* Attention: nothing is inserted before the first element or after the last element of arr.
* If the predicate function is defined, the function will instert the value only if the predicate is verified (returns a truthy value).
*
* insertedEl and predicate function are both called with these arguments: prev, curr, tIdx and tArr.
* predicate has one more argument as last which is called value.
* where prev = the accumulator array wich contains the current partial value of the final array or []
* curr = the current element that will be pushed in the prev array
* tIdx = index of curr in the original array
@nash403
nash403 / minimal.html
Created October 5, 2016 11:44
minimal HTML code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<!-- page content -->
@nash403
nash403 / groupBy.js
Created October 10, 2016 14:23
Group array elements upon a criteria.
/*
* @param property string, number or function. This is the crieteria of the grouping function
*/
function groupBy(arr, property) {
return arr.reduce( (memo, el, i) => {
let grpProperty = (() => {
if (typeof property === 'string' || typeof property === 'number') {
return el[property];
} else if (typeof property === 'function') {
return property(el, i, arr);