Skip to content

Instantly share code, notes, and snippets.

@yairEO
yairEO / browserStorage.js
Created August 28, 2023 20:47
browserStorage.js - better reading/writings to localstorage
import {isString} from 'utility/types';
export const VERSION = 1; // current version of persisted data. if code change breaks persisted data, verison number should be bumped.
export const STORAGE_KEY_PREFIX = 'amdocs/catalog';
/**
* checks arguments are as expected
* @param {string} keyName
* @param {string} type
* @returns boolean
@yairEO
yairEO / foo.html
Created August 23, 2023 20:57
Hide everything inside a DOM node:
body {
content: linear-gradient(transparent, transparent);
}
@yairEO
yairEO / README.md
Created January 13, 2023 19:44
chai + sinon: testing code with DOM events

How to test a React hooks which is binding a global window/document event(s):

This is an easy approach for testing DOM events in hooks/components without actually emmitting/triggering any "fake" events, which is much easier:

  1. In the tests file, create a dummy React component and call the hook. if the hook is returning something, then assign it to a varaible which should be defined from outside the component so it will be available for the tests cases.
@wboykinm
wboykinm / histogram.sql
Created October 12, 2015 13:07 — forked from wolever/histogram.sql
Functions to create and draw histograms with PostgreSQL.
-- Functions to create and draw histograms with PostgreSQL.
-- psql> select * from show_histogram((select histogram(length(email), 0, 32, 6) FROM auth_user limit 100));
-- bucket | range | count | bar | cumbar | cumsum | cumpct
-- --------+-------------------------------------+-------+--------------------------------+--------------------------------+--------+------------------------
-- 0 | [0,5.33333333333333) | 1 | | | 1 | 0.00273224043715846995
-- 1 | [5.33333333333333,10.6666666666667) | 5 | = | | 6 | 0.01639344262295081967
-- 2 | [10.6666666666667,16) | 149 | ============================== | ============= | 155 | 0.42349726775956284153
-- 3 | [16,21.3333333333333) | 145 | =========
@yoavniran
yoavniran / addFrameConsciousEvent.js
Created November 1, 2015 09:04
ES6 animation frame conscious event listener
const addFrameConsciousEvent = (obj, event, handler) => { //adapted from: https://developer.mozilla.org/en-US/docs/Web/Events/resize#Example
let isRunning = false;
const handlerWrapper = (e)=> {
if (!isRunning){
isRunning = true;
requestAnimationFrame(()=>{ //throttling so only execute when the browser is ready to re-render
isRunning = false;
handler(e);
})
@yoavniran
yoavniran / domReady.js
Created July 22, 2015 09:36
cross-browser dom ready helper
(function (doc, w) {
"use strict";
var readyCallbacks = [],
docLoadEv = "DOMContentLoaded",
ready = (document.readyState === "complete");
function registerForReadyEvent(fn) {
if (ready) { //already ready, execute immediately
@yoavniran
yoavniran / promiseActions.js
Last active May 6, 2024 12:33
promise actions - run methods in sequence or parallel using the power of JS Promises
/**
* accepts n methods in sequence as arguments and returns a promise that is resolved
* once all methods finish.
* any argument may be an array of methods instead of a method itself. in this case, the array methods
* will be run in parallel
* any of the passed in methods may return a promise or any other type
*
* the results of the previous methods is handed over to the next method
*
* the last parameter may be an object containing one or both: {context, data}
@yoavniran
yoavniran / extender.js
Last active May 6, 2024 12:33
JS Type Extender (inspired by backbone.js)
//IE9+
(function () {
"use strict";
function merge(){ //shallow copy
var root = arguments[0], fromObj;
for (var i = 1; i < arguments.length; i++) {
fromObj = arguments[i];
@yoavniran
yoavniran / mocha-hooks-order.js
Created April 11, 2015 13:35
shows how mocha hooks are ordered and run within contexts
describe("root context", function(){
before(function(){
console.log("before: root");
});
beforeEach(function(){
console.log("beforeEach: root");
});
@yoavniran
yoavniran / Venter.js
Last active May 6, 2024 12:33
a simple pub/sub class for node with support for scopes
/**
*
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*
* SEE MY venter NPM PACKAGE - https://www.npmjs.org/package/venter
* OR REPOSITORY ON GITHUB - https://github.com/yoavniran/node-venter
*
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!