Skip to content

Instantly share code, notes, and snippets.

View areichman's full-sized avatar

Aaron Reichman areichman

View GitHub Profile
@areichman
areichman / parse_as_module.js
Last active March 22, 2018 19:34
Parse a raw JS file as a Node.js module
request('/foo.js')
.then((body) => {
const bodyExported = body + 'module.exports = foo;';
const Module = module.constructor;
const m = new Module();
m._compile(bodyExported, '');
console.log(m.exports);
});
@areichman
areichman / fetch.js
Created January 17, 2017 20:15
ES6 fetch
fetch(url, {credentials: 'include'})
.then((response) => {
if (response.ok) {
response.json().then(fetchSuccess);
} else {
response.json().then(fetchError);
}
}).catch(fetchError);
@areichman
areichman / grunt-githash.js
Created August 30, 2016 16:23
Grunt task to get a repo's short Git hash
'use strict';
module.exports = function(grunt) {
grunt.task.registerTask('githash', function() {
var done = this.async();
grunt.util.spawn({
cmd: 'git',
args: ['rev-parse', '--short', 'HEAD']
}, function(err, result, code) {
@areichman
areichman / reload_rsyslog.sh
Created August 24, 2016 15:44
Reload rsyslog configuration without restarting
# Restart rsyslog so it re-opens its file pointer (e.g. during logrotate postrotate script)
# via https://access.redhat.com/solutions/232793
kill -HUP $(cat /var/run/syslogd.pid)
@areichman
areichman / map_tile_iterator.js
Created June 29, 2015 21:36
Iterate through all image tiles for a given bounds and zoom range
// Iterate through all map tiles for a given bounds and zoom range
// lat-lon to tile conversions from http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Derivation_of_tile_names
function lat2tile(lat, zoom) {
return (Math.floor((1-Math.log(Math.tan(lat*Math.PI/180) + 1/Math.cos(lat*Math.PI/180))/Math.PI)/2 *Math.pow(2,zoom)))
}
function lon2tile(lon, zoom) {
return (Math.floor((lon+180)/360*Math.pow(2,zoom)))
}
@areichman
areichman / check_box.js
Created March 25, 2015 20:23
Check box helper for Handlebars, based on the Rails check_box_helper
// Checkbox helper, based on http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-check_box
//
// Usage:
// {{check_box object name="test" type="checkbox" value=true hidden="false"}}
//
// Note that the value in the hash can be specified with/without quotes so a proper type comparison
// can be made.
//
Handlebars.registerHelper('check_box', function(context, options) {
var checked = context !== undefined && context == options.hash.value ? 'checked' : '',
@areichman
areichman / request_mock.js
Created February 12, 2015 21:36
Create a mock response for the request module using a Jasmine spy
var request = require('request')
spyOn(request, 'get').andCallFake(function(params, callback) {
var res = {
statusCode: 200
}
var body = {
// JSON attributes
}
callback(null, res, body)
@areichman
areichman / reformat_tags.js
Created August 20, 2014 17:24
Format an input tag list (either comma-delimited or an array) as an output array with blanks and whitespace removed
function reformatTags(tags) {
var _tags = (tags || '').toString().trim().replace(/\s*,\s*/g, ',').replace(/^,|,$/g, '').split(',');
if (_tags[0] === '' && _tags.length === 1) _tags = []; // hack for empty form submissions
return _tags;
};
@areichman
areichman / multiparty_reformat_fields.js
Last active July 5, 2023 22:05
Make the nested fields parsed by multiparty look like req.body from body-parser
// Make the nested fields parsed by multiparty look like req.body from body-parser
// e.g. 'metadata[foo]': ['1'] => {metadata: {foo: 1}}
// 'metadata[foo]': ['bar'] => {metadata: {foo: 'bar'}}
// 'metadata[foo][]': ['bar', 'bat'] => {metadata: {foo: ['bar', 'bat']}}
var qs = require('qs');
function reformatFields(fields) {
// convert numbers to real numbers instead of strings
function toNumber(i) {
@areichman
areichman / dl-metadata.less
Created May 21, 2014 19:29
Custom styles for Bootstrap's .dl-horizontal class to display key-value pairs
.dl-metadata {
dl {
border-top: 1px solid #e5e5e5;
width: 100%;
overflow: hidden;
dt {
width: 100%;
padding: 12px 20px 0 0;
color: #888;