Skip to content

Instantly share code, notes, and snippets.

View MartinSvarrer's full-sized avatar

Martin Svarrer Christensen MartinSvarrer

  • Sitecore
  • Copenhagen, Denmark
  • 20:41 (UTC +02:00)
View GitHub Profile
@MartinSvarrer
MartinSvarrer / layouts.html
Last active July 31, 2017 18:34
CSS grid layout
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>CSS Grid</title>
<style>
html, body {
@MartinSvarrer
MartinSvarrer / finddefine.js
Last active August 29, 2015 14:16
Requirejs define function regex
// Regex breakdown:
// Group: define([
// Group: dependencies
// Group: ], function (
// Group: arguments
// Group: ) {
var defineFuncBeginning = /(define\s*?\(\s*?\[)([\S|\s]*?)(\]\s*,\s*function\s*\()([\s|\S]*?)(\)\s*?{)/gmi,
defineFuncBeginningResult = defineFuncBeginning.exec(file);
console.log(defineFuncBeginningResult[2]); // output: string with all dependencies
@MartinSvarrer
MartinSvarrer / FizzBuzz
Created November 20, 2014 10:51
Based on the article http://blog.codinghorror.com/why-cant-programmers-program/ i wanted to try to write the fizzbuzz test. This is my result of 2 min work.
var output;
for (var i = 1; i <= 100; i++) {
output = "";
if (i % 3 == 0)
output = "Fizz";
if (i % 5 == 0)
output += "Buzz";
if (output === "")
function fib (n) {
var sqrt5 = Math.sqrt(5)
return Math.round(1/sqrt5 *(Math.pow((1+sqrt5)/2, n) - Math.pow((1-sqrt5)/2, n)));
}
// output large fib number
console.log(fib2(1000));
function fib (n) {
return (n >= 2) ? fib(n-1) + fib(n-2) : n;
}
// Example of getting Nth fibonacci number
fib(10);
// Generate a sequense of fibonacci
for (var i = 0; i < 20; i++) {
console.log(fib(i));
@MartinSvarrer
MartinSvarrer / es5_inheritance
Created February 19, 2014 14:01
ES5 way of doing inheritance
function SuperClass () {};
SuperClass.prototype = {
constructor: SuperClass,
a: 'Hello',
b: 'super',
c: function () {
return this.a + ', ' + this.b + '!';
}
};
@MartinSvarrer
MartinSvarrer / pre-wrap
Last active December 28, 2015 15:59
Escape text. And make sure whitespace and line breaks are preserved. This should output the same result as a user input in a textarea. This will work in IE7 and newer, unlike whitespace: pre-wrap. Snippet is using the underscore libs escape method.
var escapeText = _.escape(insertText),
preserveWhiteSpace = escapeText.replace(/ /g, '\u00a0'),
preserveWhiteSpaceAndLineBreaks = preserveWhiteSpace.replace(/\n/g, '<br />');
@MartinSvarrer
MartinSvarrer / logStackTrace
Last active December 27, 2015 13:29
callstack log
function logStackTrace(levels) {
var callstack = [];
var isCallstackPopulated = false;
try {
i.dont.exist += 0; //doesn't exist- that's the point
} catch (e) {
if (e.stack) { //Firefox / chrome
var lines = e.stack.split('\n');
for (var i = 0, len = lines.length; i < len; i++) {
callstack.push(lines[i]);
@MartinSvarrer
MartinSvarrer / fit to object calculation
Last active December 24, 2015 10:29
Calculate sizes for one object to fit another and top/left info for alignment. Usage: var sizeObj = fit( 3000, 3500, $('.box').width(), $('.box').height(), { cover: false, align: 'middle center' }); Returns an object like: {scale: 1.2, width: 120, height: 120, top: -10, left: 0 }; Takes two optional params: cover, and align. It works without JQu…
function fit (targetWidth, targetHeight, containerWidth, containerHeight, options) {
var settings = {
cover: options && options.cover !== undefined ? options.cover : true,
align: options && options.align !== undefined ? options.align : 'center middle'
}
var ratioWidth = containerWidth / targetWidth,
ratioHeight = containerHeight / targetHeight,
ratioTarget = targetWidth / targetHeight,
ratioContainer = containerWidth / containerHeight;