Skip to content

Instantly share code, notes, and snippets.

@iandesj
iandesj / methods.js
Created July 16, 2019 14:43
Simplified Methods
// before method simplification
function parseAndSanitizeAndGenerateDocument(data) {
const parsedData = [];
for (let index = 0; index < data.length; index++) {
const element = data[index];
if (element.name === 'bacon') {
parsedData.push({
name: element.name,
age: element.age,
});
@iandesj
iandesj / parameters.js
Created July 16, 2019 14:45
Configuration Objects vs. Many Parameters
// before - 5 args too many
function initializeNewUserWorflow(
email, password, location,
organization, referred) {
// initialize the new user
}
// after - using configuration object
const initializeUserConfiguration = {
email: 'email',
@iandesj
iandesj / my_git_branches.py
Created October 1, 2019 12:32
Just get all branches pushed to all remote repos with me as the committer.
import datetime
from github import Github
g = Github('token')
me = g.get_user()
repos = me.get_repos()
def is_a_repo_contributor(repo):
contributors = repo.get_contributors()
return me.id in map(lambda con: con.id, contributors)
@iandesj
iandesj / foreach_example.js
Created October 11, 2019 17:47
Examples of foreach() vs for loop
const fruits = [ 'pineapple', 'apple', 'banana', 'pear' ];
// improved looping
fruits.forEach((value, index, array) => {
console.log('fruit at index', index, '=', value);
});
// what you may already be used to
for (let i=0; i<fruits.length; i++) {
console.log('fruit at index', i, '=', fruits[i]);
@iandesj
iandesj / find_example.js
Last active November 14, 2019 13:43
Examples of find() vs for loop
const fruits = ['pineapple', 'apple', 'banana','pear'];
const aFruit = fruits.find((element) => {
return element.includes('apple');
});
let aFruit;
for (let i = 0; i < fruits.length; i++) {
if (fruits[i].includes('apple')) {
@iandesj
iandesj / filter_examples.js
Last active November 14, 2019 13:46
Examples of filter() vs for loop
const fruits = [ 'pineapple', 'apple', 'banana', 'pear' ];
const matchedFruits = fruits.filter((element) => {
return element.includes('apple');
});
let matchedFruitsAgain = [];
for (let i = 0; i < fruits.length; i++) {
if (fruits[i].includes('apple')) {
@iandesj
iandesj / map_examples.js
Last active November 14, 2019 13:51
Examples of map() vs for loop
const fruits = [ 'pineapple', 'apple', 'banana', 'pear' ];
console.log('map() example');
const upperCaseFruits = fruits.map((value) => {
return value.toUpperCase();
});
console.log('upperCaseFruits =', upperCaseFruits, '\n');
@iandesj
iandesj / reduce_examples.js
Last active November 14, 2019 13:54
Examples of reduce() vs for loop
const animals = [
{animal: 'zebra', count: 4}, {animal: 'tiger', count: 2},
{animal: 'lion', count: 3}, {animal: 'turtle', count: 9},
{animal: 'pig', count: 2},
];
console.log('reduce() example');
const count = animals.reduce((total, curr) => {
return total += curr.count;
}, 0);
@iandesj
iandesj / array_from_examples.js
Last active November 14, 2019 13:59
Examples of Array.from() vs for loop
const year = 2019;
const month = 10; // november, zero-indexed
const startDay = 14;
console.log('Array.from() example');
const dateList = Array.from({length:7}, (val, idx) => {
return new Date(year, month, startDay + idx);
});
console.log('dateList = ', dateList, '\n');
@iandesj
iandesj / array_destructuring_examples.js
Last active November 14, 2019 14:13
Examples of array destructuring vs array index accessing
const taxPayer = [ 'Ian', 'DesJardins', '123-45-6789', true ];
console.log('Array destructuring example');
// destructure the taxPayer array into new variables
const [ firstName, lastName, ssn, pastDue ] = taxPayer;
console.log('firstName =', firstName);
console.log('lastName =', lastName);
console.log('ssn =', ssn);
console.log('pastDue =', pastDue, '\n');