Skip to content

Instantly share code, notes, and snippets.

🎛️
I hear and I forget. I see and I remember. I do and I understand.

Anubhav Srivastava anubhavsrivastava

🎛️
I hear and I forget. I see and I remember. I do and I understand.
View GitHub Profile
@anubhavsrivastava
anubhavsrivastava / Chrome
Created Mar 12, 2020
Disable web security to make CORS request
View Chrome
open -a Google\ Chrome\ Canary --args --disable-web-security
on its own does not work anymore. You need to use it with --user-data-dir:
open -a Google\ Chrome\ Canary --args --disable-web-security --user-data-dir=$HOME/profile-folder-name
http://stackoverflow.com/a/34680023/368691
@anubhavsrivastava
anubhavsrivastava / generate_releasenotes
Created Dec 27, 2019
bash script to generate release notes from github repo
View generate_releasenotes
#!/usr/bin/env bash
set -e
usage() {
echo "$0 <repo> <tag> [<release name>]" >&2;
}
if [ "$1" = "-h" -o "$1" = "--help" ]; then
usage
@anubhavsrivastava
anubhavsrivastava / update_changelogs
Created Dec 27, 2019
Script to generate changelogs from Git Repo
View update_changelogs
#!/usr/bin/env bash
set -e
usage() {
echo "$0 <tag> <repo>" >&2;
}
if [ "$1" = "-h" -o "$1" = "--help" ]; then
usage
View generic curry for fixed argument function
function fixCurry(fn, totalArgs){
totalArgs = totalArgs ||fn.length
return function recursor(){
return arguments.length<fn.length?recursor.bind(this, ...arguments): fn.call(this, ...arguments);
}
}
var add = fixCurry((a,b,c)=>a+b+c);
console.log('Add')
View add(2)(3)(4) with add(2,3,4)
function add(){
let args = [].slice.apply(arguments);
function resultFn(){
args = args.concat([].slice.apply(arguments));
if(args.length>=3){
return args.slice(0,3).reduce(function(acc,next){ return acc+next},0)
}
return resultFn
View add(2)(3) via explicit argumentless call
function add(x){
let sum = x;
return function resultFn(y){
if(arguments.length){ //not relying on falsy value
sum += y;
return resultFn;
}
return sum;
}
}
View add(2)(3) via explicit property
function add(x){
let sum = x;
return function resultFn(y){
sum +=y;
resultFn.result = sum;
return resultFn;
}
}
View add(2)(3) via valueOf
function add(x){
let sum = x;
function resultFn(y){
sum +=y;
return resultFn;
}
resultFn.valueOf = function(){ return sum };
return resultFn;
}
@anubhavsrivastava
anubhavsrivastava / add(2)(3)
Last active Feb 3, 2019
Currying Add(2)(3) in JS
View add(2)(3)
function add(x){
return function (y){
return x+y;
}
}
//ES6 version using arrow functions
const add = x=> y=>x+y;
@anubhavsrivastava
anubhavsrivastava / getterDescriptor.js
Last active Feb 6, 2020
Can (a===1 && a===2 && a===3) ever evaluate to true?
View getterDescriptor.js
var value = 0; //window.value
Object.defineProperty(window, 'a', {
get: function() {
return this.value += 1;
}
});
console.log(a===1 && a===2 && a===3) //true
You can’t perform that action at this time.