Skip to content

Instantly share code, notes, and snippets.

View LeoAref's full-sized avatar

Muhammad Aref LeoAref

View GitHub Profile
@LeoAref
LeoAref / dispatcher.js
Last active October 5, 2022 11:04
Simple event dispatcher implementation using JavaScript
export class Dispatcher {
constructor () {
this.events = {};
}
addListener (event, callback) {
// Check if the callback is not a function
if (typeof callback !== 'function') {
console.error(`The listener callback must be a function, the given type is ${typeof callback}`);
return false;
@LeoAref
LeoAref / renameKeyName.js
Created May 19, 2017 05:49
Immutable object key renaming using Lodash
export function renameKeyName(obj, oldName, newName) {
const clone = cloneDeep(obj);
const keyVal = clone[oldName];
delete clone[oldName];
clone[newName] = keyVal;
return clone;
}
h scroll left
j scroll down
k scroll up
l scroll right
gg scroll to top of the page
G scroll to bottom of the page
f activate link hints mode to open in current tab
F activate link hints mode to open in new tab
r reload
@LeoAref
LeoAref / form-utils.js
Last active July 11, 2017 15:06
React form validation
import isObject from 'lodash/fp/isObject';
import isBoolean from 'lodash/fp/isBoolean';
// this is a naive implementation
export function validateData(data, rules) {
let errors = {};
const isValid = Object.keys(rules).reduce((_isValid, key) {
const error = validateField(data[key], rules[key]);