I hereby claim:
- I am alperg on github.
- I am alperg (https://keybase.io/alperg) on keybase.
- I have a public key ASBDdw98bmdl-2Kk7yPFDgA0q77kq5c4e9CIZy_oof0z6Qo
To claim this, I am signing this object:
import assert from "assert"; | |
function main() { | |
let obj = { | |
big: 4, | |
biggest: 6, | |
bigger: 5, | |
small: 1, | |
}; |
I hereby claim:
To claim this, I am signing this object:
const debounce1 = (fn, delay) => { | |
let timer = null; | |
return function (...args) { | |
const context = this; | |
timer && clearTimeout(timer); | |
timer = setTimeout(() => { | |
fn.apply(context, args); | |
}, delay); | |
}; | |
} |
class PubSub { | |
constructor () { | |
this.subIds = 0; | |
this.subscriptions = {} | |
} | |
subscribe (topic, fn) { | |
if(!this.subscriptions[topic]) this.subscriptions[topic] = {} | |
const token = ++this.subIds; | |
var pubsub = {}; | |
(function(q) { | |
var topics = {}, subUid = -1; | |
q.subscribe = function(topic, func) { | |
if (!topics[topic]) { | |
topics[topic] = []; | |
} | |
var token = (++subUid).toString(); | |
topics[topic].push({ | |
token: token, |
class BinarySearchTree { | |
constructor(){ | |
this.root = null; | |
} | |
// add a node to the tree | |
add(value){ | |
let newNode = { value, left: null, right: null}; | |
// Properties can be removed using destructuring in combination with the rest operator. | |
// Here password is destructured out (ignored) and the rest of the properties are returned as rest. | |
const noPassword = ({ password, ...rest }) => rest; | |
const user = { | |
id: 100, | |
name: 'Howard Moon', | |
password: 'Password!' | |
}; |
// In this example, password will be added only when password is truthy! | |
const user = { id: 100, name: 'Howard Moon' }; | |
const password = 'Password!'; | |
const userWithPassword = { | |
...user, | |
id: 100, | |
...(password && { password }) |
// By combining the techniques above, a function can be created to rename properties. | |
// Imagine there are some objects with an uppercase ID that should be lowercase. | |
// Start by destructuring ID out of the object. Then add it back as id while object is being spread. | |
const renamed = ({ ID, ...object }) => ({ id: ID, ...object }); | |
const user = { | |
ID: 500, | |
name: "Bob Fossil" | |
}; |
// Default properties are values that will be set only when they are not included in the original object. | |
// In this example, user2 does not contain quotes. The setDefaults function ensures all objects have quotes set otherwise it will be set to []. | |
// When calling setDefaults(user2), the return value will include quotes: []. | |
// When calling setDefaults(user4), because user4 already has quotes, that property will not be modified. | |
const user2 = { | |
id: 200, | |
name: 'Vince Noir' | |
}; |