Skip to content

Instantly share code, notes, and snippets.

// http://jsperf.com/events-object-compare-vs-string-compare/2
@thurt
thurt / generateUUID.js
Created February 17, 2016 22:55
Client-side UUID Generator
function generateUUID() {
var d = new Date().getTime()
if (window.performance && typeof window.performance.now === "function") {
d += window.performance.now() //use high-precision timer if available
}
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (d + Math.random() * 16) % 16 | 0
d = Math.floor(d / 16)
return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16)
})
@thurt
thurt / bind-vs-call-or-apply.js
Last active February 27, 2016 16:47
bind vs call or apply
<script>
Benchmark.prototype.setup = function() {
var emptyFn = function(str, num) {}
var str = 'test string'
var num = 1
var preBoundFn = emptyFn.bind(null, str, num)
};
Benchmark.prototype.teardown = function() {
// approximate implementation of Set using Symbol (ES6)
(() => {
function aSet() {
this.hash = {}
}
aSet.prototype = {
add(a) { this.hash[a] = true },
has(a) { return a in this.hash },
values() { return Object.keys(this.hash) },
@thurt
thurt / immutable_counter.js
Last active April 3, 2016 23:27
an example of how to implement an immutable counter
(() => {
class Counter {
constructor(start_value) {
this.plus = this.plus(start_value)
}
plusFn(num, _num) {
var res = num + _num
this.plus = (_num) => {
return this.plusFn(res, _num)
@thurt
thurt / filter_stream_example.js
Created April 7, 2016 20:17
filter a stream of objects by their key property
var Stream = require('stream.js') // https://github.com/dionyziz/stream.js
var myStream = Stream.make({
key:'please'
}, {
key: 'filter'
}, {
key: 'this'
}, {
key: 'data'
(() => {
class BinaryTree {
constructor(value, left = null, right = null) {
this.value = value
this.left = left
this.right = right
}
[Symbol.iterator]() {
var tree = [this.value, this.left, this.right]
@thurt
thurt / objectEntries.js
Last active May 8, 2016 05:11
Implementation for Object.entries
// Alt 1
Object.prototype.entries = function() {
let keys = Reflect.ownKeys(this).values()
return {
[Symbol.iterator]() {
return this
},
next: () => {
let { done, value: key } = keys.next()
@thurt
thurt / generic_pipe_compose.js
Last active May 8, 2016 23:50
Example usage of a generic pipe and compose
var trace = x => {
console.log(x)
return x
}
var plus1 = x => x + 1
var add2 = (x, y) => x + y
var addList = list => list.reduce(add2, 0)
var concat2 = (x, y) => x.concat(y)
var concatLists = (...lists) => lists.reduce(concat2, [])
@thurt
thurt / DataBind.js
Created May 7, 2016 17:13
A quick one-way data binder which will change the textContent of the nodes which have a data-key attribute when calling DataBind function
// gets all html elements with data-key attribute and stores them in a dictonary.
// later, using DataBind function triggers a setter function for that key
// which applies the assigned value to the associated node.textContent
var DataBind = (() => {
// private member
var _binder = Array.from(document.querySelectorAll('[data-key]'))
.reduce((dict, node) => {
Object.defineProperty(dict, node.dataset.key, {
set(new_value) {
// node is in the closure