Skip to content

Instantly share code, notes, and snippets.

View ryanhamley's full-sized avatar

Ryan Hamley ryanhamley

View GitHub Profile
@ryanhamley
ryanhamley / customResourceAction.js
Created April 8, 2015 15:40
An example of adding custom functionality to an Angular $resource in order to create an action which accepts variables and a callback.
Event.getDateRange = function (start, end, cb){
var params = {
q: {
filters: [
{
name: 'occurrences',
op: 'any',
val: {
'name': 'date',
'op': 'gte',
@ryanhamley
ryanhamley / checkForDuplicateObjects.js
Created April 9, 2015 16:59
Recursive function for Angular.js to determine if an array of objects contains duplicates
/**
* [checkForDuplicateObjects recursively checks an array of objects for duplicate values using angular.equals() to deeply compare objects.]
* @param {[array]} _array [the array of objects to be checked]
* @return {[boolean]} true or falsey [the function will return true if duplicates are found]
*/
function checkForDuplicateObjects(_array) {
if (_array.length > 1) {
var first = _array[0];
for (var i = 1; i < _array.length; i++) {
@ryanhamley
ryanhamley / getDigitAt.js
Last active August 29, 2015 14:18
Number prototype method to return a digit at a specified position or a slice between two specified positions
/**
* [getDigitAt method for numbers that will return either the digit at a specified position or the section of the number between two positions]
* @param {[number]} position1 [either the position to search for or the starting position of the slice]
* @param {[number]} position2 [*optional*, if supplied, this creates a slice and acts as the end position]
* @return {[number]} [either the digit at the specified position or the slice between the specified positions]
*/
Number.prototype.getDigitAt = function (position1, position2) {
//convert the number to a string to allow array-like access
var num = this.valueOf().toString();
@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));
});
@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 / 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 / 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 / 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 / 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');
};

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: