Skip to content

Instantly share code, notes, and snippets.

View ahmehri's full-sized avatar

Ahmed Mehri ahmehri

  • Berlin, Germany
View GitHub Profile
const _ = require('lodash');
const array1 = [1, 2];
const array2 = [1, 2];
const result = array1 === array2;
result
console.log([] === [])
const msg = 'test';
const test1 = 'test1';
const regex = /test/;
console.log(regex.test(msg));
const regex1 = `/${msg}/`;
// console.log(regex1.test(msg)) // regex1.test is not a function
const regex3 = /`${msg}$`/
@ahmehri
ahmehri / conditional-add-property.js
Last active July 14, 2019 13:38
How to conditionally add a property to an object, using an object literal, only when its value is defined?
let result;
let value;
result = {
value
};
// how to avoid the following result, adding a property to an object
// if it's value is undefined?
result;
const _ = require('lodash');
const obj1 = { entities: { jobs: { 1: 1 }, source: 'obj1' } };
const obj2 = { entities: { jobs: { 1: 3 } }};
const merge = { ...obj1, ...obj2};
merge
const mergeWithLodash = _.merge({}, obj1, obj2);
mergeWithLodash
const person = {
name: 'ahmehri',
age: 29,
}
const key = 'age';
const { [key]: destructuredAge } = person;
destructuredAge
const result = Array.from({ length: 3 }, (elt, index) => index);
result
@ahmehri
ahmehri / function-context.js
Created August 23, 2017 22:10
Default function context when using/not using 'use strict'
function f1WithoutUseStrict() {
return this;
}
function f1WithUseStrict() {
'use strict';
return this;
}
@ahmehri
ahmehri / forEach-array-promises.js
Last active April 20, 2017 11:06
Usage of forEach with an array of promises is not a correct usage, Promise.all and for of should be used instead.
'use strict';
import test from 'ava';
let promise1;
let promise2;
let promise3;
let promises;
test.beforeEach(() => {