Skip to content

Instantly share code, notes, and snippets.

@craigmaslowski
craigmaslowski / compress-images.ps1
Last active May 28, 2023 16:57
Powershell script to compress all jpg, gif, or png files recursively in the given path with ImageMagick
param([string]$path = ".\", [int]$minSize = 0, [switch]$jpg, [switch]$png, [switch]$gif, [switch]$verbose, [switch]$report)
function Get-Size
{
param([string]$pth)
"{0:n2}" -f ((gci -path $pth -recurse | measure-object -property length -sum).sum /1mb) + " mb"
}
function Get-Size-Kb
{
@craigmaslowski
craigmaslowski / detect-card-type.js
Last active July 10, 2019 18:03
Credit Card Type Detection
// card type prefixes sourced from: https://en.wikipedia.org/wiki/Bank_card_number#Issuer_identification_number_.28IIN.29
var cardTypes = {
'American Express': [
[34],
[37]
],
'Diners Club Intl': [
[300, 305],
[309],
[36],
@craigmaslowski
craigmaslowski / Readme.md
Last active May 4, 2018 15:27
ES6/Javascript UK phone formatting

This function will format a UK phone number according to the rules at https://www.area-codes.org.uk/formatting.php

Note: The order of the 08 and 0800 numbers in the list was swapped from the directions at the link above. As written in the linked page, 0800 would never be formatted properly.

Backbone.mixin = function (obj, mixin) {
var source = obj.prototype || obj;
if (!mixin)
throw new Error('Backbone.mixin: The mixin specified is undefined.');
// merge all properties in the mixin into the view's prototype
_.defaults(source, mixin);
// merge object literal properties (ensure's events, bindings, and channels)
@craigmaslowski
craigmaslowski / backbone.view.example.js
Created February 5, 2016 18:22
Arbitrary Timeout Counter Using Moment.js
Backbone.View.extend({
events: {
'click .continue': 'extendSession'
},
initialize: function () {
this.warnIntervalInSeconds = 60;
this.inactivityTimeoutInMinutes = 2;
this.sessionTimingOutMessageShown = false;
this.template = '<p>Your session will timeout shortly. Please click Continue to resume your session.</p><button class="button continue">Continue</button>';
@craigmaslowski
craigmaslowski / example.js
Created December 15, 2013 07:25
Function for defining a Meteor template.
// pass n template name parameters
// the last parameter contains the object that defines the template.
// pass either an object literal or a function that returns an object for the last parameter
Templates.define('postSubmit', 'postEdit', {
// assign either an object literal or return an object from a function
helpers: {
post: function (id) {
Posts.findOne(id);
}
},
@craigmaslowski
craigmaslowski / p01.scala
Last active December 23, 2015 15:09
S-99: Ninety-Nine Scala Problems - http://aperiodic.net/phil/scala/s-99/
def last(xs: List[Int]): Int = {
if (xs.tail.isEmpty) xs.head
else last(xs.tail)
} //> last: (xs: List[Int])Int
@craigmaslowski
craigmaslowski / euler1.hs
Created August 29, 2013 02:53
Euler Solutions in Haskell
sum [x | x <- [1..999], x `mod` 3 == 0 || x `mod` 5 == 0]
@craigmaslowski
craigmaslowski / backbone.error.js
Created August 28, 2013 18:58
backbone.error.js
Backbone.Error = function (message, obj) {
var type;
// TODO: Clean this up.
if (obj instanceof Backbone.Model) {
type = 'model';
} else if (obj instanceof Backbone.View) {
type = 'view';
} else if (obj instanceof Backbone.Collection) {
type = 'collection';
@craigmaslowski
craigmaslowski / euler11.js
Created June 6, 2013 20:33
Project Euler Problem #11
var grid = [
[08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 08],
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00],
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65],
[52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91],
[22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
[24, 47, 32, 60, 99, 03, 45, 02, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
[32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
[67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 94, 39, 63, 08, 40, 91, 66, 49, 94, 21],
[24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],