Skip to content

Instantly share code, notes, and snippets.

View kdabir's full-sized avatar
👨‍💻
go git it

Kunal Dabir kdabir

👨‍💻
go git it
View GitHub Profile
input = [
{ name: "key1", value: "value1" },
{ name: "key2", value: "value2" }
]
output = {
key1: "value1",
key2: "value2"
}
@kdabir
kdabir / empty_pouch.coffee
Created June 1, 2015 19:11
empty pouch db
db.allDocs(include_docs:true).then((docs) -> docs.rows.map((doc) -> db.remove(doc.doc)))
@kdabir
kdabir / jq_events_issue.js
Last active August 29, 2015 14:20
Events triggered by jquery are not visible for event listener defined through Dom API
// register event listener with jquery
$(document).on("my:foo", function(){alert("ev thru jq api")} );
// register event with dom api
document.addEventListener("my:foo", function(){alert("ev thru dom api")});
// trigger event with jquery api
$(document).trigger("my:foo", {});
// trigger event with dom api
@kdabir
kdabir / map.coffee
Created April 17, 2015 06:29
Simplifying map usage in JS and CoffeeScript
# Step 1
[1,2,3].map((x) -> x * 2)
# Step 2
double = (x) -> x * 2
[1,2,3].map((x) -> double x)
# Step 3
[1,2,3].map(double)
@kdabir
kdabir / flatten.coffee
Created March 13, 2015 01:13
flatten nested arrays with reduce
y = [1,2,[4,5],3]
y.reduce(((a,b)-> a.concat b),[])
.map((z)->console.log z)
# or
y.reduce((a,b) -> [].concat(a,b))
.map((z)->console.log z)
@kdabir
kdabir / ns.coffee
Created March 12, 2015 14:48
access nested objects by string paths
x = a:b:c:10
ns = "a.b.c"
a = ns.split('.').reduce(((z,i) -> z[i]), x)
console.log a
@kdabir
kdabir / error_assert.coffee
Created February 19, 2015 11:57
mocha chai assert throwing exception
# will not work
expect(new MyObject("with invlaid data")).to.throw(Error)
# give a function to expect
expect( -> new MyObject("with invlaid data")).to.throw(Error)
# this should work
@kdabir
kdabir / file.sh
Created February 18, 2015 03:23
create filename based on date
echo "hello" > logs-$(date +"%Y-%m-%d").txt
@kdabir
kdabir / random.md
Created February 13, 2015 13:01
generate random hex on command line

$ openssl rand -hex 10

f5c50af369ffac1ec902

@kdabir
kdabir / init.groovy
Created February 12, 2015 03:31
init a new project in current directory
import java.util.zip.ZipInputStream
import groovy.io.FileType
import java.util.zip.ZipEntry
def zip = new ZipInputStream(new URL("https://github.com/kdabir/init/archive/master.zip").newInputStream())
def currentDir = new File(System.getProperty("user.dir"))
def thisFile = new File(getClass().protectionDomain.codeSource.location.path).name
zip.withStream {