Skip to content

Instantly share code, notes, and snippets.

@craigmaslowski
craigmaslowski / compress-images.ps1
Last active July 1, 2024 21:41
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 / 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.

@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 / 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 / _Mithril.js_Blog.md
Last active August 29, 2015 14:20
Mithril Blog

I recently discovered Mithril.js and wanted to give it a shot with an application that's slightly more complicated than the standard ToDo example. I decided on making a simple blogging software. The requirements for the software were as follows:

  • A listing of all blog posts as the front page of the app
  • The ability to add, edit, and remove posts.
  • A combined Add/Edit Form for managing posts
  • The usage of localstorage
  • Accessing localstorage in a faux asynchronous manner to simulate REST type requests

What follows is the result.

@craigmaslowski
craigmaslowski / buhbye.js
Last active August 29, 2015 14:02
Upworthy Blogspam Killer
var iframes = document.getElementsByTagName('iframe'),
src;
for (var i = 0; i < iframes.length; i++) {
src = iframes[i].src;
if (/\/embed\//.test(src)) {
var videoId = src.substring(src.indexOf('embed/')+6, src.indexOf('?'));
window.location.href = 'http://youtube.com/watch?v=' + videoId;
}
}
@craigmaslowski
craigmaslowski / pw
Last active August 29, 2015 14:00
Quick and dirty password generator
#!/usr/bin/env node
var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiklmnopqrstuvwxyz',
length = !isNaN(process.argv[2]) ? process.argv[2] : 12;
string = '';
for (var i = 0; i < length; i++) {
var randomNumber = Math.floor(Math.random() * chars.length);
string += chars.substring(randomNumber, randomNumber + 1);
}
@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]