Skip to content

Instantly share code, notes, and snippets.

@softwarespot
softwarespot / parsecsv.js
Created September 28, 2015 10:17
Simple CSV parser
// 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
*/
@softwarespot
softwarespot / bigsum.js
Last active September 30, 2015 06:12
Sum big numbers
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);
@softwarespot
softwarespot / infixtopostfix.js
Last active September 28, 2015 10:02
Infix to Postfix
// Based on the Shunting-Yard algorithm
function toPostfix(infix) {
// Convert a token to precedence value
const _precedenceValue = (token) => {
switch (token) {
case '+':
case '-':
return 4;
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;
}
@softwarespot
softwarespot / 7.js
Last active September 30, 2015 06:14
Kata
(() => {
// 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;
}
@softwarespot
softwarespot / stack.js
Last active October 31, 2015 08:24
A stack in JavaScript with ES2015
// 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 = [];
@softwarespot
softwarespot / linkedlist.js
Last active October 31, 2015 08:24
A linked list in JavaScript with ES2015
// 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
@softwarespot
softwarespot / ES2015_tagged_template_strings.js
Last active September 30, 2015 06:15
ES2015 tagged template strings
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
@softwarespot
softwarespot / module+interface.js
Last active September 30, 2015 06:17
Creating a module with interface coupled together using ES2015
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 {
@softwarespot
softwarespot / ES2015_destructuring.js
Last active September 30, 2015 06:19
ES2015 destructuring
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