Skip to content

Instantly share code, notes, and snippets.

View johnbeech's full-sized avatar
🔬
Experimenting

John Beech johnbeech

🔬
Experimenting
View GitHub Profile
@johnbeech
johnbeech / promise.js
Created July 2, 2015 18:12
Simple promises
module.exports = require('./default-config');
module.exports.environment = 'test';
module.exports.configService = function(query) {
var deferred = {};
deferred.then = function(callback) {
deferred.resolve = callback;
return this;
@johnbeech
johnbeech / configService-callbacks.js
Created July 3, 2015 13:11
Harness for comparing and contrasting Class Based API or Callback Function API
// Config Service using callback
var Service = function(query, readyCallback, errorCallback) {
var arc = "arc data";
if(query.indexOf("bad") > -1) {
if(typeof errorCallback === 'function') {
errorCallback("bad contents");
}
}
else {
if(typeof readyCallback === 'function') {
@johnbeech
johnbeech / poll-status-endpoint.js
Last active August 29, 2015 14:27
Test if a status endpoint is working by polling it every 10 seconds for 2 minutes.
var request = require('request');
var assert = require('assert');
var testUrl = process.argv[2] || 'No url set';
var expectedStatusBody = 'Status 200 OK';
// Try for 2 minutes, every 10 seconds, to see if server has booted up
runTest(12);
function runTest(attempts) {
@johnbeech
johnbeech / server.js
Created July 22, 2016 14:05
Experiment in testing /'s on routes
/*
* Express server - Testing /'s on routes
* npm i express
* node server.js
*/
const express = require('express');
const app = express();
@johnbeech
johnbeech / cookie.js
Last active March 13, 2017 15:24
Interface to read, write, and clear browser cookies
@johnbeech
johnbeech / moveTo.js
Created April 8, 2017 13:07
Function to move items between arrays
function move(a) {
function to(b) {
while (a.length > 0) {
let item = a.shift()
b.push(item)
}
}
return {
to
}
@johnbeech
johnbeech / split.js
Last active August 12, 2017 20:15
Splits a large list of corporation fittings into a separate smaller XML files
/* Use node 4.5, npm i cheerio, then run: node split.js */
var cheerio = require('cheerio')
var fs = require('fs')
var contents = fs.readFileSync(__dirname + '/CORP-FITTINGS.xml')
var $ = cheerio.load(contents, {
normalizeWhitespace: true,
xmlMode: true
})
var $fittings = $('fitting')
@johnbeech
johnbeech / parse-url-params.js
Created November 20, 2018 13:10
Parse parameters from URL
// collapsed onto one line
const getParams(href) => (href.split('?')[1] || '').split('&').map(kvp => kvp.split('=')).reduce((acc, kvp) => { acc[kvp[0]] = kvp[1]; return acc }, {})
// spread out into a traditional function
function getParams(href) {
return (href.split('?')[1] || '')
.split('&')
.map(kvp => kvp.split('='))
.reduce((acc, kvp) => {
acc[kvp[0]] = kvp[1];
@johnbeech
johnbeech / chart-options.config.js
Last active November 23, 2018 09:57
Example CFD config X and Y axis labels
const chartOptions = {
maintainAspectRatio: false,
spanGaps: false,
elements: {
line: {
tension: 0.4,
backgroundColor: 'rgba(0,0,0,0.1)'
},
point: {
radius: 0,
@johnbeech
johnbeech / Console Output
Created January 14, 2019 22:00
ftp-deploy 501 Error: Invalid number of arguments - remoteRoot: '/' not working (works ok if I changed roots to /site
>node deploy stage-all
[Deploy] stage-all : { include: [ 'site/**/*' ] }
[Deploy] Connected to: ftp.mkv25.net
[Deploy] Connected: Server message: 192.252.146.30 FTP server ready
[Deploy] Error: { Error: Invalid number of arguments
at makeError (C:\Users\User\Work\Local\mkv25-responsive-website\deploy\node_modules\@icetee\ftp\lib\connection.js:1128:13)
at Parser.<anonymous> (C:\Users\User\Work\Local\mkv25-responsive-website\deploy\node_modules\@icetee\ftp\lib\connection.js:122:25)
at Parser.emit (events.js:182:13)
at Parser._write (C:\Users\User\Work\Local\mkv25-responsive-website\deploy\node_modules\@icetee\ftp\lib\parser.js:61:10)
at doWrite (_stream_writable.js:410:12)