Skip to content

Instantly share code, notes, and snippets.

Troels Knak-Nielsen troelskn

Block or report user

Report or block troelskn

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@troelskn
troelskn / dk-tax.js
Last active Dec 19, 2019
Danish tax calculations for Google sheets
View dk-tax.js
function DK_TAX_PARSE_OPTIONS(opts) {
var options = {
hasChildren: true, // Do you receive "børnepenge"?
isFamily: true, // Are you married (and your spouse without income)?
interests: 44256, // Deductible interests paid (typically on a mortgage)
churchTax: 0.00867, // Set your church tax rate here, if you are a member of the church. Otherwise set to 0.0
municipalityTaxRate: 0.24926 // Find you tax rate here: https://www.skm.dk/skattetal/satser/kommuneskatter
};
var applyOption = function(key, value) {
var key = key.trim();
@troelskn
troelskn / gist:1287893
Created Oct 14, 2011
Luhn's algorithm in php
View gist:1287893
<?php
function is_valid_luhn($number) {
settype($number, 'string');
$sumTable = array(
array(0,1,2,3,4,5,6,7,8,9),
array(0,2,4,6,8,1,3,5,7,9));
$sum = 0;
$flip = 0;
for ($i = strlen($number) - 1; $i >= 0; $i--) {
$sum += $sumTable[$flip++ & 0x1][$number[$i]];
@troelskn
troelskn / underscore.php
Created Dec 22, 2010
camelize + underscore in php
View underscore.php
<?php
/**
* Transforms an under_scored_string to a camelCasedOne
*/
function camelize($scored) {
return lcfirst(
implode(
'',
array_map(
@troelskn
troelskn / app.rb
Last active Jul 14, 2019 — forked from dstrelau/app.rb
Gollum protected by HTTP Basic
View app.rb
require 'gollum/frontend/app'
require 'digest/sha1'
class App < Precious::App
User = Struct.new(:name, :email, :password_hash, :can_write)
before { authenticate! }
before /^\/(edit|create|delete|livepreview|revert)/ do authorize_write! ; end
helpers do
View table_print.rb
# takes an array of hashes and prints as a table on the console
def table_print(rows)
table = [rows.first.keys.map(&:to_s)] + rows.map(&:values).map { |v| v.map(&:to_s) }
sizes = table.map { |row| row.map(&:size) }.transpose.map(&:max)
puts table.map { |row| row.each_with_index.map { |txt, idx| txt.rjust(sizes[idx]) }.join("\t") }.join("\n")
end
@troelskn
troelskn / ruby_print.rb
Created Apr 26, 2019
Ruby pretty print with rubocop standard syntax
View ruby_print.rb
class RubyPrint
def self.pp(mixed)
puts new.transform(mixed)
end
def transform(mixed, indentation = 1)
if mixed.is_a?(Hash)
transform_hash mixed, indentation
elsif mixed.is_a?(Array)
View just_and_exactly.rb
# frozen_string_literal: true
require "active_record"
module JustAndExactly
module ActiveRecordExtensions
MultipleRecordsFound = Class.new(ActiveRecord::ActiveRecordError)
# Like +first!+, but will also fail if the query would match more than
# one record.
View docker-machine-auth
PS1='\[\e[1m\]\[\e[34m\]\u@\h$([ "$DOCKER_MACHINE_NAME" = "" ] || echo " \[\e[33m\][docker-machine:$DOCKER_MACHINE_NAME]\[\e[34m\]"): \[\e[35m\]\w\[\e[33m\]$(__git_ps1) \[\e[34m\]$\[\e[0m\] '
docker-machine-auth () {
MACHINE_NAME=$1
if [ "$MACHINE_NAME" = "" ]
then
eval "$(docker-machine env -u)"
else
eval "$(docker-machine env $MACHINE_NAME)"
fi
}
@troelskn
troelskn / db.rake
Last active Jan 28, 2019
Wait for db. Useful in a dockerized setup
View db.rake
namespace :db do
desc "Wait for db. Used for initial boot up"
task wait: :environment do
puts "*** Giving DB time to boot"
catch :done do
10.times do
Timeout.timeout(1) do
begin
ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection.execute("SELECT NOW()")
@troelskn
troelskn / raven.rb
Last active Nov 21, 2018
config/initializers/raven.rb
View raven.rb
require 'raven'
require 'rake/task'
if Rails.env.production?
Raven.configure do |config|
config.dsn = 'http://xxx:xxx@app.getsentry.com/xxxxx'
end
module Rake
class Task
You can’t perform that action at this time.