Skip to content

Instantly share code, notes, and snippets.

@dylants
dylants / nock.js
Created October 10, 2017 23:42
Nock / mock API requests
// config/mock/nock.js
import nock from 'nock';
import config from '../../config';
// disable all connections so that we use only the mocks
nock.disableNetConnect();
nock(config.blah.api.rootUrl)
@dylants
dylants / proton-mail.css
Last active April 28, 2022 20:39
Dark Theme for Proton Mail
/* ProtonMail */
/* CLASSIC THEME */
body { font-size: 14px }
body,
.pm_opensans {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif
}
@dylants
dylants / child.reducer.js
Last active September 11, 2016 13:37
Dynamically add reducers or state to a Redux application, using parent/child reducers
const initialState = {
...
};
export default function childReducer(state = initialState, action) {
switch (action.type) {
// process the actions as you would normally in any reducer
case CHILD_REDUCER_ACTION:
return Object.assign({}, state, {
...
@dylants
dylants / mock-mongoose-model.js
Created March 13, 2015 22:09
When testing code which uses Mongoose models, you often run into problems when the schemas are not defined. This code provides an idea on how to mock these models (in a `beforeEach`) and clear them later (in an `afterEach`)
"use strict";
var mongoose = require("mongoose");
var mockMongooseModel = {};
module.exports = mockMongooseModel;
/**
* This function is useful for tests that require Mongoose models
* loaded prior to loading the test file. This should be called in
@dylants
dylants / logger.js
Created October 28, 2014 20:03
Node application logger using the debug module. This automatically determines the namespace for the calling file/class/library by generating a stack trace on creation. Provides 2 loggers, one for log and one for error.
"use strict";
var debug = require("debug");
// enable the "myApp" namespace by default
debug.enable("myApp*");
// returns the calling filename, but prepended with "myApp:"
function _getDecorator() {
// the code below screws up test output when a test fails,
@dylants
dylants / node-csv.js
Created July 9, 2014 16:11
Read CSV using node-csv (0.4 and above)
var csv = require("csv"),
fs = require("fs");
// the job of the parser is to convert a CSV file
// to a list of rows (array of rows)
var parser = csv.parse({
columns: function(line) {
// By defining this callback, we get handed the
// first line of the spreadsheet. Which we'll
// ignore and effectively skip this line from processing
@dylants
dylants / version.js
Created June 5, 2014 18:32
Display a version (doubles as a health check) for the app, based on the latest Git commit
/*
* package.json
*/
{
"name": "app-name",
"version": "0.1.0",
"scripts": {
"prestart": "git rev-parse HEAD > ./BUILD",
"start": "node app.js"
}
@dylants
dylants / hapi-handle-errors.js
Created May 21, 2014 20:22
One way of handling errors in your main hapi app.js file. These errors would normally cause the process to stop, but we should try and stop the server prior to killing the process. Hapi by default handles errors using domains, so uncaught exceptions won't cause the server to halt.
var domain = require("domain").create();
// ...
// surround the mongo connection in a domain, to handle errors
domain.run(function() {
// connect to the database
mongoose.connect("mongodb://" + mongoConnection, function(error) {
// handle the error case
if (error) {
@dylants
dylants / hapi-basic-config.js
Last active August 29, 2015 14:00
Hapi basic configuration
// --------------------------------------------
// --------------- app.js -------------------
// --------------------------------------------
"use strict";
var Hapi = require("hapi"),
yaml = require("js-yaml"),
fs = require("fs");
var config, serverOptions, serverConfiguration, server;
@dylants
dylants / keybase.md
Created April 17, 2014 01:57
Keybase.io Proof

Keybase proof

I hereby claim:

  • I am dylants on github.
  • I am dylants (https://keybase.io/dylants) on keybase.
  • I have a public key whose fingerprint is 3F79 80AC 2F9D 2A4D 5F5F C550 2FC5 45DB 9CF6 6BBF

To claim this, I am signing this object: