Skip to content

Instantly share code, notes, and snippets.

View kevinchisholm's full-sized avatar

Kevin Chisholm kevinchisholm

View GitHub Profile
// Example 1 - Using a for loop
var i = 0,
records = ['foo', 'bar', 'baz'],
arrayLength = records.length;
for (; i < arrayLength; i++) {
console.log(`(${i}) The record is: ${records[i]}`);
}
@kevinchisholm
kevinchisholm / validate-array-example-1.js
Last active December 18, 2019 12:25
Check if array has length
// example # 1: check if the length property of the passed-in array is 0
function processRecords (records) {
if (records.length === 0) {
console.log(`No records to process`);
return;
}
records.forEach(record => {
console.log(`The record is: ${record}`);
@kevinchisholm
kevinchisholm / copy-object.js
Last active October 6, 2019 19:23
Quick and Dirty Way to Copy a JavaScript Object
//--------- PART 1 -------------------
const objectA = {
foo: ['a', 'b', 'c']
};
const objectB = Object.assign({}, objectA);
console.dir(objectB); // {"foo":["a","b","c"]}
function validatePhoneForE164(phoneNumber) {
const regEx = /^\+[1-9]\d{10,14}$/;
return regEx.test(phoneNumber);
};
validatePhoneForE164('+12125551212'); // true
validatePhoneForE164('12125551212'); // false
validatePhoneForE164('2125551212'); // false
validatePhoneForE164('+1-212-555-1212'); // false
function validateEmailAddress(emailAddress) {
const emailRegex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/;
return emailRegex.test(emailAddress);
};
validateEmailAddress('[email protected]'); // true
validateEmailAddress('[email protected]'); // true
_.remove(this.rates, {
id: resp.rate.id
});
_.remove(this.rates, rate => {
return rate === resp.rate.id;
});
@kevinchisholm
kevinchisholm / javascript-async-await-intro-1.js
Last active December 5, 2019 14:16
Introduction to Async/Await in JavaScript
function sleep (milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds));
};
function goToSleep (milliseconds) {
console.log('Going to bed now...');
await sleep (milliseconds);
console.log('Finished sleeping!');
// example # 2: check if the length property of the passed-in array is "truthy"
function processRecords (records) {
if (!records.length) {
console.log(`No records to process`);
return;
}
records.forEach(record => {
console.log(`The record is: ${record}`);
// example # 3: If we pass-in an object with a lenght property, the code throws an error
function processRecords (records) {
if (!records.length) {
console.log(`No records to process`);
return;
}
records.forEach(record => {
console.log(`The record is: ${record}`);
// example # 4: Make sure that the passed-in object is an instance of the Array constructor
function processRecords (records) {
if (!(records instanceof Array)) {
console.log(`Array provided array not valid`);
return;
}
if (!records.length) {
console.log(`No records to process`);