Skip to content

Instantly share code, notes, and snippets.

Avatar

Gil Tayar giltayar

View GitHub Profile
View JsonableEither.scala
case class JsonableEither[+L, +R](left: Option[L] = None, right: Option[R] = None) {
require(left == None && right != None ||
left != None && right == None)
}
View oops-emoji.js
console.log(stringToArray('Robin H😀😀d'));
View unicode-works.js
console.log(stringToArray('Robin Hoווd'));
@giltayar
giltayar / naive-iteration.js
Last active Jan 24, 2017
Unicode String Iteration Article Sample Code
View naive-iteration.js
function stringToArray(s) {
const retVal = [];
for (let i = 0; i < s.length; ++i) {
retVal.push(s[i]);
}
return retVal;
}
View es6-character-iteration.js
const str = 'H😀😀d';
const iterator = str[Symbol.iterator](); // get the iterator from string
iterator.next(); // skip over the 'H'
console.log(iterator.next().value); // get the first emoji
View es6-character-iteration-for-of.js
function stringToArray(s) {
const retVal = [];
for (const ch of s) {
retVal.push(ch);
}
return retVal;
}
View es6-character-iteration-array-from.js
console.log(Array.from('Robin H😀😀d').map(ch => ch == '😀' ? '🙃' : ch));
View es-character-iteration-with-join.js
console.log(Array.from('Robin H😀😀d').map(ch => ch == '😀' ? '🙃' : ch).join(''));
View unit-that-needs-mocking.js
exports.writeSumToFile = (a, b, fileSumWriter) => {
const sum = a + b
fileSumWriter(sum)
}
@giltayar
giltayar / test-calculator.js
Created Feb 9, 2017
Parts of the calculator module unit test
View test-calculator.js
const {describe, it} = require('mocha')
const {expect} = require('chai')
const calculator = require('../../lib/calculator')
describe('calculator', function () {
const stream = (characters, calculatorState = calculator.initialState) =>
!characters
? calculatorState
: stream(characters.slice(1),
calculator.nextState(calculatorState, characters[0]))