Skip to content

Instantly share code, notes, and snippets.

View hoodwink73's full-sized avatar

Arijit Bhattacharya hoodwink73

View GitHub Profile
@hoodwink73
hoodwink73 / gist:c046350fc689c2fd0b65016842f38086
Created May 17, 2020 13:18
Batch and iterate async tasks
import ASQ from 'asynquence';
import "asynquence-contrib"
import fetch from 'isomorphic-unfetch';
var fetcher = (url) =>
ASQ(url)
.then((done) =>
fetch(url)
.then(res => res.json())
.then((data) => done(data))
@hoodwink73
hoodwink73 / getGradedSumInsured.js
Last active May 19, 2020 14:44
Template to quickly parse CSV using Papaparse and quokka
function getGradedSumInsured (data) {
return data.reduce((result, member) => {
if (member["Relationship"] === "self") {
var si = member["Sum Insured"]
if (result[si]) {
result[si].push(member["Email"])
} else {
result[si] = [member["Email"]]
}
}
@hoodwink73
hoodwink73 / download-file.js
Created December 26, 2019 08:26
Create downloadable files on the client side
// https://stackoverflow.com/a/18197341/1481710
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
@hoodwink73
hoodwink73 / yup.equalTo.js
Created November 25, 2019 11:47
Yup schema method to ensure a field value equals field value of another field. Useful for fields like confirm password field
// thanks 🙏
// https://github.com/jquense/yup/issues/97
function equalTo(ref, msg) {
return this.test({
name: 'equalTo',
exclusive: false,
message: msg || '${path} must be the same as ${reference}',
params: {
reference: ref.path
@hoodwink73
hoodwink73 / pluralize.js
Created November 13, 2019 11:39
Template tag for pluralise
// this is a tagged template function
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates
// Example
// pluralize`You have ${events.length} ${["meeting","meetings"]} today`
export default function pluralize(
strings,
num,
[singularWord, pluralWord] = []
) {
let word, emptyWord;
@hoodwink73
hoodwink73 / 1. the-basic-problem.js
Last active November 1, 2019 04:24
Understanding transducers
function add1(v) { return v + 1};
function isOdd(v) {return v % 2 === 1};
function sum (total, v) {return total + v};
var list = [2, 5, 8, 11, 14, 17, 20];
list
.map(add1)
.filter(isOdd)
.reduce(sum)
@hoodwink73
hoodwink73 / showErrorOnTouchedField.js
Created October 24, 2019 09:15
Utility fns while using Formik
const shouldShowError = () => {
var { touched, errors } = form;
return Object.entries(touched).some(
([key, value]) => value && errors[key]
);
};
@hoodwink73
hoodwink73 / trampolined-fibonnaci.js
Created October 7, 2019 06:46
Trampolined Fibonnaci
var trampoline = (fn) =>
(...args) => {
var result = fn(...args);
while (typeof result === "function") {
result = result()
}
return result
}
@hoodwink73
hoodwink73 / Homemade-Observable.js
Created October 5, 2019 06:58
This a simple demonstration of what is an observable –– just an object with a subscribe(forEach) method
function Observable(forEach) {
this._forEach = forEach;
}
Observable.prototype = {
forEach: function(onNext, onError, onCompleted) {
if (typeof onNext === "function") {
this._forEach({
onNext: onNext,
onError: onError || function() {},
@hoodwink73
hoodwink73 / job_roles_angel_list.js
Last active August 11, 2019 14:30
A list of professional roles. Extracted from Angel List and Mixpanel
export const USER_JOB_ROLES = new Map([
[
"Software Engineer",
[
"Mobile Developer",
"Frontend Developer",
"Backend Developer",
"Full-Stack Developer",
"Engineering Manager",
"QA Engineer",