Skip to content

Instantly share code, notes, and snippets.

View alperg's full-sized avatar

Alper Gokcehan alperg

  • RTP, NC, United States
View GitHub Profile
@alperg
alperg / sortObjectIntoArray.js
Created December 19, 2022 15:14 — forked from armenic/sortObjectIntoArray.js
JavaScript Sort Object by Values Into Array of Objects
import assert from "assert";
function main() {
let obj = {
big: 4,
biggest: 6,
bigger: 5,
small: 1,
};
@alperg
alperg / keybase.md
Created February 26, 2020 17:52
Keybase proof

Keybase proof

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:

@alperg
alperg / debounce.js
Last active November 14, 2019 02:32
Debouncing examples
const debounce1 = (fn, delay) => {
let timer = null;
return function (...args) {
const context = this;
timer && clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(context, args);
}, delay);
};
}
@alperg
alperg / pubsub2.js
Created November 13, 2019 14:17
PubSub with observer pattern
class PubSub {
constructor () {
this.subIds = 0;
this.subscriptions = {}
}
subscribe (topic, fn) {
if(!this.subscriptions[topic]) this.subscriptions[topic] = {}
const token = ++this.subIds;
@alperg
alperg / pubsub.js
Created November 13, 2019 14:13
Simple PubSub implementation with JavaScript - by Addy Osmani
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,
@alperg
alperg / binary-search-tree-dfs.js
Last active October 17, 2019 19:29
Binary Search Tree and Depth First Traversal in Javascript
class BinarySearchTree {
constructor(){
this.root = null;
}
// add a node to the tree
add(value){
let newNode = { value, left: null, right: null};
@alperg
alperg / js-exclude-object-props.js
Created October 17, 2019 15:49
Exclude object props
// 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!'
};
@alperg
alperg / js-conditional-props.js
Last active October 17, 2019 15:50
Add conditional props
// 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 })
@alperg
alperg / js-rename-props.js
Last active October 17, 2019 15:51
Renaming Properties
// 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"
};
@alperg
alperg / js-default-props.js
Last active October 17, 2019 15:52
Default props
// 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'
};