Skip to content

Instantly share code, notes, and snippets.

View codfish's full-sized avatar

Chris O'Donnell codfish

View GitHub Profile
@codfish
codfish / rsaa-cache-middleware.js
Last active April 20, 2020 20:40
Dynamic RSAA (redux-api-middleware) cache middleware. Must come before rssa in middleware order.
import { RSAA } from 'redux-api-middleware';
import get from 'lodash/get';
/**
* Intercept redux api middleware actions and bailout of api resource requests
* if the resource is already in the store.
*
* NOTE: State always gets updated on non-GET requests, essentially updating our "cache"
*
* Examples of resource routes that we want to cache:
@codfish
codfish / roman-to-decimal.js
Created April 13, 2020 14:45
Convert decimal number to Roman numeral
const NUMERALS = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000,
IV: 4,
IX: 9,
@codfish
codfish / meta-service.js
Created September 16, 2019 12:39
AngularJS v1 Meta Service
// ***********************************************************************************
// meta-service.js
'use strict';
/**
* Meta Service
*
* The Meta Service allows you to abstract a lot of business logic
* that goes into setting meta & link tags for your templates.
@codfish
codfish / Dockerfile
Last active February 20, 2019 18:03
Example json-server module for marketing campaigns. `json-server api.js`
FROM mhart/alpine-node:11.10
RUN mkdir /app
WORKDIR /app
COPY ./package.json ./package-lock.json ./
RUN npm install
EXPOSE 4000
CMD npm run dev
@codfish
codfish / Ethereum event fixtures for js.md
Last active January 15, 2019 13:44
Ethereum event fixtures for js.

To install a relatively recent version of Faker you'll need to install from a commit. The maintainer is still active and merging new features, but does not cut new releases.

npm install --save-dev faker@Marak/faker.js#<commit-sha>

I.e. npm install --save-dev faker@Marak/faker.js#d3ce6f1 (latest commit at the time of writing).

@codfish
codfish / error-handling.js
Last active January 4, 2019 18:03
Default express api error handling middleware.
/**
* Handle application errors.
*
* NOTE: 500 status code is returned by default.
*
* To properly leverage this middleware, rather than return responses in your routes,
* simply set a response status in the route and then pass the error object to `next`.
* This middleware will catch that error and handle it in a consistent way across
* the entire application. For example:
*
@codfish
codfish / remove-duplicates.js
Last active November 20, 2018 03:55
Remove duplicate values from an array by converting it to a Set, then back to an array.
[...new Set([].concat(...arrayName))]
@codfish
codfish / rick-roll-terminal.sh
Last active September 28, 2018 22:13
Rick roll terminal prank
#!/bin/bash
#
# some shortcomings:
# - prankee needs to be running rvm, rbenv, or some other
# ruby version manager that doesn't require sudo permissions to
# install gems.
# - can be killed by simply closing current tab/session
#
# Any bug reports or suggestions on improvements are welcome!
#
@codfish
codfish / swagger.yml
Created April 3, 2018 13:32
Example swagger 2.0 doc with extensive use of reusable definitions, responses & parameters.
swagger: '2.0'
info:
version: '0.0.1'
title: Marketing Campaign API
host: api.example.com
basePath: /v1
@codfish
codfish / htmlentity.js
Created February 28, 2018 14:24 — forked from CatTail/htmlentity.js
Javascript: encode(decode) html text into html entity
// encode(decode) html text into html entity
var decodeHtmlEntity = function(str) {
return str.replace(/&#(\d+);/g, function(match, dec) {
return String.fromCharCode(dec);
});
};
var encodeHtmlEntity = function(str) {
var buf = [];
for (var i=str.length-1;i>=0;i--) {