Skip to content

Instantly share code, notes, and snippets.

View islam3zzat's full-sized avatar

Islam farg islam3zzat

  • Commercetools
  • United Kingdom
View GitHub Profile
// ...
function updateUserName(user, { firstName, lastName }) {
const { name } = user;
const newName = {
firstName: firstName || name.firstName,
lastName: lastName || name.lastName,
};
return { ...user, name: newName };
}
@islam3zzat
islam3zzat / still-error-prone-code.js
Last active October 26, 2018 19:29
This is an error prone code
// ...
function updateUserName(user, { firstName, lastName }) {
const { name } = user;
if (firstName) name.firstName = firstName;
if (lastName) name.lastName = lastName;
return { ...user, name };
}
function saveUser(users, updatedUser) {
@islam3zzat
islam3zzat / error-prone-code.js
Last active October 26, 2018 19:29
This is an error prone code
function userByMatcher({ key, value }) {
return user => user[key] === value;
}
function updateUserName(user, { firstName, lastName }) {
// <~ #1 mutating argument, bad idea
if (firstName) user.name.firstName = firstName;
if (lastName) user.name.lastName = lastName;
return user;
}
@islam3zzat
islam3zzat / toa.js
Last active August 28, 2017 19:13
Tail calls elimination (ES6)
// Tail call optimization
// this function call itself n times
function f(x) {
// with every new call, new stack frame is created
// there is a limit, this limit varies from engine to another, but there is a limit
// this simple function breaks on chrome at f(100000)
if (x < 1) {
return 0
}