Skip to content

Instantly share code, notes, and snippets.

View mohsentaleb's full-sized avatar
😎

Mohsen Taleb mohsentaleb

😎
View GitHub Profile
@mohsentaleb
mohsentaleb / classEventSpy.js
Last active August 29, 2015 13:57
Spying unobtrusively on Class instance events in MooTools
(function() {
var spyId = 'spyEvent';
Class.extend({
addEventSpy: function (instance) {
/*
accepts one argument - Class instance.
works by adding a local fireEvent method on the instance
instead of going TO proto chain to Class.Event::fireEvent
*/
@mohsentaleb
mohsentaleb / outerclick.js
Last active August 29, 2015 13:57
OuterClick Event in Mootools
//Source: https://github.com/yearofmoo/MooTools-Event.outerClick/blob/master/Source/Event.outerClick.js
(function($,$$){
var events;
var check = function(e){
var target = $(e.target);
var parents = target.getParents();
events.each(function(item){
var element = item.element;
if (element != target && !parents.contains(element))
@mohsentaleb
mohsentaleb / once.js
Created June 30, 2014 20:40
JavaScript Once Function
// The "once" wrapper
function once(fn, context) {
var executed = false;
return function() {
if(executed) return;
executed = true;
fn.apply(context || this, arguments);
};
}
var canOnlyFireOnce = once(function() {
// Get Multiple URLs to file
var OutputUrlsToFile, arrayOfUrls, system, fs, t;
system = require("system");
fs = require('fs');
/*
Get given urls
@param array of URLs to get
@param callbackPerUrl Function called after finishing each URL, including the last URL
@param callbackFinal Function called after finishing everything
*/
/*
* extend Leaflet's LatLng class
* giving it the ability to calculate the bearing to another LatLng
* Usage example:
* here = map.getCenter(); / some latlng
* there = L.latlng([37.7833,-122.4167]);
* var whichway = here.bearingWordTo(there);
* var howfar = (here.distanceTo(there) / 1609.34).toFixed(2);
* alert("San Francisco is " + howfar + " miles, to the " + whichway );
*
componentDidUpdate(prevProps, prevState) {
Object.entries(this.props).forEach(
([key, val]) =>
prevProps[key] !== val && console.log(`Prop '${key}' changed`)
)
if (this.state) {
Object.entries(this.state).forEach(
([key, val]) =>
prevState[key] !== val && console.log(`State '${key}' changed`)
import _ from "lodash";
const iterations = 10000;
const sortByIndex = (a, b, index, ascending) =>
ascending ? a[index] - b[index] : b[index] - a[index];
const sortListingsWithMemoize = _.memoize((listings, index, ascending) => {
//console.log('sortListings with memoize...')
return listings.sort((a, b) => sortByIndex(a, b, index, ascending));
});
import _ from 'lodash'
const id = 'unique_id'
const arr = [
{ id: "cd3405db863314ea9c0a923c9e21ee28", beds: 2, bath: 1 },
{ id: "90472efaad627e518957da32f31b1558", beds: 4, bath: 2 },
{ id: "14ed01de968e15c7dcfe39a726e9b514", beds: 5, bath: 3 },
{ id: "14ed01de968e15c7dcfe39a726e9b514", beds: 1, bath: 4 }
];
const sort = (listings, index, ascending) => {
[
{
"id": "cd3405db863314ea9c0a923c9e21ee28",
"beds": 2,
"bath": 1
}
]
import React, { Component } from "react";
class Listings extends Component {
sortByIndex = (a, b, index, ascending) => (ascending ? a[index] - b[index] : b[index] - a[index])
sortListings = (listings, index, ascending) =>
listings.sort((a,b) => this.sortByIndex(a, b, index, ascending))
render() {
const sortedListings = this.sortListings(this.props.listings, 'beds', false)