This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The following kata is going back to basics with creating a parser. So nothing fancy here, just good 'ol best practices | |
/** | |
* CSV Parser. Takes a string as input and returns an array of arrays (for each row) | |
* | |
* @param {string} string CSV input string | |
* @param {string} separator Single character used to quote simple fields. Default is " | |
* @param {string} quote Single character used to separate fields. Default is , | |
* @return {array} A 2-dimensional array which consists of an array of each row split into columns; otherwise, an empty 2-dimensional array | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add(a, b) { | |
// A queue to enqueue the result (minus the remainder) | |
const queue = []; | |
// Store the remainder | |
let remainder = 0; | |
// Determine the largest array length | |
const maxLength = Math.max(a.length, b.length); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on the Shunting-Yard algorithm | |
function toPostfix(infix) { | |
// Convert a token to precedence value | |
const _precedenceValue = (token) => { | |
switch (token) { | |
case '+': | |
case '-': | |
return 4; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function binaryToString(binary) { | |
// Split for readability, though it could be in-lined too | |
const binaryToInteger = (string) => { | |
let final = 0; | |
for (let i = string.length - 1, digits = string.split(''), exp = 1; i >= 0; i--) { | |
final += +digits[i] * exp; | |
// Bit shift left by 1 i.e. 1, 2, 4, 8, 16, 32, 64.... | |
exp <<= 1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(() => { | |
// Add max to the Array prototype chain | |
Array.prototype.max = function () { | |
// I know they say the array will have at least one element, but that can't be guaranteed IMHO | |
const length = this.length; | |
if (length === 0) { | |
return Number.NaN; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note: Save as stack.js and run 'babel-node stack.js' to display in the command window. This is written in ES2015 | |
((global) => { | |
// Note: I'm aware .pop() and .push() exist natively with JavaScript arrays, but where's the fun in that? =) | |
/** | |
* Stack interface | |
*/ | |
function Stack() { | |
this._stack = []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note: Save as linkedlist.js and run 'babel-node linkedlist.js' to display in the command window. This is written in ES2015 | |
((global) => { | |
// Return strings of toString() found on the Object prototype | |
// Based on the implementation by lodash inc. is* function as well | |
const _objectStrings = { | |
BOOLEAN: '[object Boolean]', | |
}; | |
// Store a reference of the toString function on the Object prototype chain |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function valueToUpper(strings, ...values) { | |
// Set the values i.e. first and last name to upper-case | |
values[0] = values[0].toUpperCase(); | |
values[1] = values[1].toUpperCase(); | |
// Re-create the template | |
return `${strings[0]}${values[0]}${strings[1]}${values[1]}${strings[2]}`; | |
} | |
// Create first and last name variables |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const myModule = ((interface) => { | |
// Implementing the interface | |
// This is like an interface, which doesn't pollute the main module with unrelated code | |
console.log(interface.init()); | |
// Instantiate a new class that is attached to the interface | |
const _printClass = new interface.printClass(); | |
return { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function destructuring( {weight, age, space = null }) { | |
// Instead of using something like opts.weight, now just use the property name in the function signature | |
console.log(weight); | |
console.log(age); | |
// Checking for null, standard ES5, as ?? isn't present in ES2015 | |
console.log(space ? space : 'Space not set'); | |
} | |
// Block scoped variable using let/const |