Skip to content

Instantly share code, notes, and snippets.

View ryanhamley's full-sized avatar

Ryan Hamley ryanhamley

View GitHub Profile
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ryanhamley
ryanhamley / use-svg-as-icon-image-mapboxgl.js
Last active January 16, 2024 13:23
This gist shows how to use an <svg> element as an icon-image for a Mapbox GL symbol layer
// How to add an SVG as a symbol layer's icon image: https://github.com/mapbox/mapbox-gl-js/issues/5529#issuecomment-340011876
// Also see here: https://stackoverflow.com/a/11765731/2748013 (we need the data url stuff for the image src)
// NOTE: Importing SVGs requires an inline module loader such as https://github.com/webpack-contrib/svg-inline-loader
import template from './templates/marker.svg';
const width = 20;
const height = 40;
const img = new Image(width, height);
// map is your Mapbox GL map object

Keybase proof

I hereby claim:

  • I am ryanhamley on github.
  • I am rhamley (https://keybase.io/rhamley) on keybase.
  • I have a public key ASCp8_FUT1irc025hMA6hOgjzH7e_Ljo8_P3xNbh-CmN0Ao

To claim this, I am signing this object:

@ryanhamley
ryanhamley / nodeFileLogger.js
Created November 5, 2015 18:37
Log to a file with Node
var log_file = fs.createWriteStream(__dirname + '/debug.log', {flags : 'w'});
var log_stdout = process.stdout;
console.log = function(d) { //
log_file.write(util.format(d) + '\n');
log_stdout.write(util.format(d) + '\n');
};
@ryanhamley
ryanhamley / time-polyfill.js
Created July 24, 2015 17:39
* This directive acts as a polyfill for displaying input type time on IE, Firefox and Safari. For now, it is display only and does not change the behavior, which still acts as a simple text input. The directive acts as a two-way filter converting the model from strings like "19:00:00" to "07:00 PM" for display and converting "03:00 PM" back to …
angular.module('managerApp')
.directive('time', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function postLink(scope, element, attrs, ngModelController) {
//convert data from view format to model format
ngModelController.$parsers.push(function(data) {
if(window.ui.browser !== 'Chrome') {
var amPM = data.substring(data.length-2, data.length);
@ryanhamley
ryanhamley / verifyCreditCardNumber.js
Created May 26, 2015 21:06
Javascript implementation of the Luhn algorithm to verify credit card numbers
/**
* [verifyCardNumber implements the Luhn algorithm (http://en.wikipedia.org/wiki/Luhn_algorithm), which is a checksum algorithm to determine card validity. This implementation was developed by BrainJar (http://www.brainjar.com/js/validation/default2.asp)]
* @param {[string]} _num [the credit card number in string format]
* @return {Boolean} [true if valid, else false]
*/
function verifyCardNumber (_num) {
var i, sum, digit, reverse, modified;
// First, reverse the string and remove any non-numeric characters.
reverse = '';
@ryanhamley
ryanhamley / verifyCardType.js
Last active August 29, 2015 14:21
Javascript function to determine a credit card type by its number.
//This function makes use of the getDigitAt() method which is found in a gist here: https://gist.github.com/ryanhamley/73140d5e9710a9a71fa0
function verifyCardType (_num) {
//cast primitive number _num to a Number object to allow for using the getDigitAt() method
var num = Number(_num);
if (num.getDigitAt(0) === 4) {
return 'Visa';
}
switch (num.getDigitAt(0, 2)) {
@ryanhamley
ryanhamley / ignoremousewheel.js
Last active August 29, 2015 14:21
An Angular directive for use with number inputs to prevent mousewheel events from changing the input.
angular.module('myApp')
.directive('ignoreMouseWheel', function () {
return {
restrict: 'A',
link: function (scope, element) {
element.bind('mousewheel', function() {
element[0].blur();
});
}
};
@ryanhamley
ryanhamley / templateCompile.js
Created April 17, 2015 15:16
Snippet for including a template in an Angular attribute directive. This is useful for creating autocompletes, datepickers and other functionality that requires dynamically adding DOM elements to an existing element. This code would go inside of the directive's linking function.
$http.get('/views/includes/datepicker.html', {
cache: $templateCache
}).success(function(template) {
element.after($compile(template)(scope));
});