Skip to content

Instantly share code, notes, and snippets.

View alanrsoares's full-sized avatar

Alan Soares alanrsoares

View GitHub Profile
class Observable {
constructor(value) {
this.value = value
this.events = { change: [] }
}
update(updateFn) {
this.value = updateFn(this.value)
this.events.change.forEach(fn => fn(this.value))
}
http://jsbin.com/capeyinite/edit?output
@alanrsoares
alanrsoares / currencyFormatter.js
Last active August 2, 2016 05:41
Agnostic, extensible and composable set of functions for formatting numbers to currency
// :: delimitGroups = (number, string) => string => string
const delimitGroups = (groupSize, delimiter) => s =>
s.split('')
.reduceRight((acc, x, i) =>
`${x}${(s.length - 1 - i) % groupSize ? '' : delimiter}${acc}`)
// :: format = number => object => string
export const format = options => n =>
(([left, right]) =>
`${options.symbol}${
const assert = f => ([a, b]) =>
(x => x === b
? { passed: true }
: {
failed: true,
message: `- expected f(${a}) to be ${b}, but was ${x}`
}
)(f(a))
const runTests = (cases, f) => {
@alanrsoares
alanrsoares / lazyRange.js
Last active April 15, 2016 05:34
Lazy range implementation with Symbol.iterator - live demo => http://jsbin.com/jeguzo/edit?js,console
// Practical Application of a Symbol.iterator
// to build a Lazy Sequence
const asc = xs => xs.sort((a, b) => a - b)
class Range {
constructor(to, from = 0, step = 1) {
this.step = step
if (typeof to === 'undefined' || to === null) {
@alanrsoares
alanrsoares / polish-notation-parser.js
Last active November 15, 2016 00:07
A Tiny Polish Notation (Lisp-like) Arithmetic Parser Built in ES6 - live demo => http://jsbin.com/tuqoqaw/11/edit?js,console
/*
A Tiny Lisp Arithmetic Parser
----------------------------------
Built with <3 by @alanrsoares
*/
// functional helpers
const apply = f => (...xs) => xs.reverse().reduceRight(f)
const compose = (...fs) => (...args) =>
(([g, ...gs]) =>
@alanrsoares
alanrsoares / Vagrantfile
Last active March 7, 2016 21:37
UXE Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Config Github Settings
github_username = "fideloper"
github_repo = "Vaprobash"
github_branch = "1.4.2"
github_url = "https://raw.githubusercontent.com/#{github_username}/#{github_repo}/#{github_branch}"
# Because this:https://developer.github.com/changes/2014-12-08-removing-authorizations-token/
@alanrsoares
alanrsoares / cache.js
Last active February 14, 2016 23:06
An overly-simplified cache library for node v4+
'use strict';
const lowdb = require('lowdb');
const storage = require('lowdb/file-sync');
const db = lowdb(`${ __dirname }/db.json`, { storage });
const COLLECTION_ID = 'cache';
const ONE_HOUR = 3600;
const isValidCacheKey = (key, ttl) =>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
var ofType, fib, assertEqual;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="test-out"></div>
<script id="jsbin-javascript">
/*