Skip to content

Instantly share code, notes, and snippets.

View Tahseenm's full-sized avatar

Tahseen Malik Tahseenm

View GitHub Profile
@Tahseenm
Tahseenm / isObjectFreed.js
Created May 27, 2020 10:31
Is Object garbage collected
/** Determines if an object is freed
@param obj is the object of interest
@param freeFn is a function that frees the object.
@returns a promise that resolves to {freed: boolean, memoryDiff:number}
@author Steve Hanov <steve.hanov@gmail.com>
*/
function isObjectFreed(obj, freeFn) {
return new Promise(resolve => {
if (!performance.memory) {
/* :: (Function | string, number | bigint, ...any) -> Function */
const setBigTimeout = (fn, delay, ...args) => {
const maxSafeDelay = typeof delay === 'bigint'
? BigInt(2147483647)
: 2147483647
let id
const cancel = () => {
clearTimeout(id)
}
@Tahseenm
Tahseenm / provider-example-class.js
Last active April 13, 2019 17:39
Hooks vs no hooks
import React from 'react'
import * as _ from 'lodash'
/*---------------------------------------------------------------------------*\
USER
\*---------------------------------------------------------------------------*/
const UserContext = React.createContext()
const UserProvider = withRouter(class UserProvider extends React.Component {
@Tahseenm
Tahseenm / typeOf.js
Created November 5, 2017 00:39
A better checking for type of a value in javascript
const capitalise = word =>
word[0].toUpperCase() + word.slice(1).toLowerCase()
/** (val: any) -> string */
const typeOf = val => {
if (val === null) return 'null'
if (val === void val) return 'undefined'
if (Number.isNaN(val)) return 'NaN'
if (Array.isArray(val)) return 'Array'
if (RegExp.prototype.isPrototypeOf(val)) return 'RegExp'
@Tahseenm
Tahseenm / curryFunction.js
Last active November 4, 2017 19:58
Curry a function
/** (fn: Function) -> Function **/
const curry = (fn) => {
/** (fn: Function, totalArgs: number) -> Function **/
const curryFunc = (fn, totalArgs) => {
const curriedFunc = (...args) => {
if (args.length === 0) {
throw new Error('Please provide the required arguments')
}
if (totalArgs === args.length) return fn(...args)
@Tahseenm
Tahseenm / final-solution.js
Last active October 29, 2017 20:43
Little Javascript Problem Solution
/* -------------------------------------------------------------------------- *\
* Solution
\* -------------------------------------------------------------------------- */
/** :: (val: any) -> boolean */
const isNull = val => val === null
/** :: (list: Function, start: number) -> Function */
const slice = (list, start = 0) => idx => list(idx + start)
@Tahseenm
Tahseenm / factorial.js
Created October 25, 2017 09:58
Factorial using recursion
/* -------------------------------------------------------------------------- *\
* Utils
\* -------------------------------------------------------------------------- */
const isFunction = val => typeof val === 'function'
/**
* Trampoline
*/
const T = fn => (...args) => {
@Tahseenm
Tahseenm / widgets.js
Created October 21, 2017 19:39
Redux Ducks module pattern
/**
* PATH: -> ./src/redux/modules/widgets.js
*
* [RULES]
*
* A module...
*
* - MUST export default a function called reducer()
* - MUST export its action creators as functions
* - MUST have action types in the form npm-module-or-app/reducer/ACTION_TYPE
@Tahseenm
Tahseenm / index.js
Last active January 1, 2018 15:25
Minimal redux(y) lib
import { createStore } from './lib'
/**
* Main reducer
*/
const counter = function (state = 0, action) {
switch (action.type) {
case 'INCREMENT':
return state + 1
@Tahseenm
Tahseenm / foo.js
Last active September 26, 2017 14:50
Javascript Undefined
/**
* Undefined cannot be assigned using window.undefined = someVal or
* undefined = someVal but can be changed using function scope.
* window.undefined is the same as window['undefined']
*
* The same example does not work with the null keyword and program throws
* a syntax error (function (null) {...})(123)
*/
;(function (undefined) {
// x is undefined