Skip to content

Instantly share code, notes, and snippets.

@blrobin2
blrobin2 / bank.ts
Last active December 20, 2017 15:20
Using a proxy for persistence in TypeScript
class Account {}
interface Bank {
getAccounts(): Account[]
setAccounts(accounts: Account[]): void
}
// "Plain Old JavaScript Object" implementing abstraction
class BankImpl implements Bank {
private accounts: Account[];
@blrobin2
blrobin2 / ReadStreamFactory.ts
Last active December 8, 2017 13:49
A wrapping factory object for node.js createReadStream to promisify the read stream. Handles adding and removing event listeners for GC.
import { createReadStream, ReadStream } from 'fs'
class ReadStreamFactory {
private readStream: ReadStream
private promiseResolve: (stream: ReadStream) => void
private promiseReject: (reason: Error) => void
static createReadStream(filename: string): Promise<ReadStream> {
const factory = new ReadStreamFactory(filename)
return factory.promisifyReadStream()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Colors</title>
<style>
body {
background-color: black;
font-size: 10em;
font-family: sans-serif;
@blrobin2
blrobin2 / missingRequired.js
Created September 25, 2017 18:39
A dumb way of checking if required parameter is passed
function missingRequired() {
const thisFunc = arguments.callee;
const caller = thisFunc.caller;
const passedArgs = caller.arguments;
const args = caller.toString()
.replace(/((\/\/.*$)|(\/\*[\s\S]*?\*\/)|(\s))/mg,'') // remove all spacing
.split(/function\s*\w+\(?/)[1] // remove 'function name ('
.split(/\)\s*\{/)[0] // remove everything after ){
.split(/,/); // break apart into comma separated list of arguments
@blrobin2
blrobin2 / singly-linked-list.js
Created July 12, 2017 16:58
A naïve approach at a SinglyLinkedList object in JS
class SinglyLinkedList {
constructor(value = []) {
if (Array.isArray(value)) {
value.forEach(val => this.addAtEnd(val))
} else {
this.value = value
this.next = null
}
}
@blrobin2
blrobin2 / proxies.js
Created July 7, 2017 14:42
Extending Dr. Axel Rauschmayer's RESTful web service example
// Source http://exploringjs.com/es6/ch_proxies.html#_accessing-a-restful-web-service-method-calls
const gitHubService = createWebService('http://api.github.com');
gitHubService.feeds.get().then(data => {
console.log(data.timeline_url);
});
gitHubService.users.blrobin2.get().then(data => {
console.log(data.avatar_url);
@blrobin2
blrobin2 / EventStore.php
Created July 14, 2015 21:34
I like to use Doctrine in Laravel, and I needed an annotated event store class for the table. If you utilize event sourcing, and especially if you use Qandidate Labs Broadway package, this could be useful.
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* Class EventStore
*
* @ORM\Entity
* @ORM\Table(name="event_store")
*/
@blrobin2
blrobin2 / helpers.php
Created July 4, 2015 02:15
This is my helpers file I've been using in a few Laravel projects. Just throwing it up here because, why not?
<?php
/**
* Checks for an image and moves it to the set folder.
*
* @param $image . The image, most likely from the Request object
* @param $destination . The folder in source where it's going.
* @return null|string
*/
function moveImage($image, $destination)