Skip to content

Instantly share code, notes, and snippets.

// ------------------------------------------------------------
// Datamodell
// ------------------------------------------------------------
const fruitBasket = {
apple: 27,
banana: 1,
grape: 22,
pear: 14,
};
@leifoolsen
leifoolsen / mdl-rgb-string-to-numbers.md
Last active June 11, 2016 20:23
MDL RGB string to RGB numbers

MDL defines many RGB values as a string, so $my-color: rgba($input-text-error-color, 0.9); will result in an error: "Argument '$color' of 'rgba($color, $amount)' must be a color"

What we could do instead:

  • Strip "rgb" or "rgba", "(", ")" and spaces from MDL SASS variable, e.g.
    $input-text-error-color: unquote("rgb ( 213, 0,0)") => "213,0,0"
  • Split the comma separated string into a number list
    "213,0,0" => [213, 0, 0]
  • Then feed the numbers into the wanted SASS function, e.g:
@leifoolsen
leifoolsen / valueAndUnit.js
Last active September 17, 2015 08:19
Split a string into a numeric value and a trailing char sequence
var reUnit = /([a-z]*|%)$/;
var value = "120px"
var numericValue = parseInt(value.phone, 10);
var unit = value.match(reUnit)[1] || 'px';
@leifoolsen
leifoolsen / queryParameterByName.js
Last active September 3, 2015 11:22
Get query parameter by name from given URL
function queryParameterByName(name, querystring) {
// See: http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(querystring);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
};
(function(){
@leifoolsen
leifoolsen / polymer-1.0-create-custom-element.md
Last active March 9, 2018 19:53
Polymer 1.0 : Custom elements

Polymer 1.0 : Custom elements

Simplest possible workflow

Minimal steps required to create a custom elemenet in Polymer 1.0, without any aid from tools like Yeoman and Gulp.

I have used the Quick tour of Polymer as a reference.

Prerequisites

@leifoolsen
leifoolsen / FindClassWithAnnotation.java
Last active August 29, 2015 14:21
Find class annotated with a given annotation using com.google.common.reflect.ClassPath
// Find class annotated with @ApplicationPath and extract value from annotation: @ApplicationPath("/api/*")
ClassPath cp = ClassPath.from(Thread.currentThread().getContextClassLoader());
String appPath = "";
for (ClassPath.ClassInfo classInfo : cp.getTopLevelClassesRecursive("com.github.leifoolsen")) {
Class<?> clazz = classInfo.load();
if(clazz.isAnnotationPresent(ApplicationPath.class)) {
appPath = clazz.getAnnotation(ApplicationPath.class).value();
break;
}
}
DO:
for (Entry<K, V> entry : map.entrySet()) {
K key = entry.getKey();
V value = entry.getValue();
}
DO NOT:
for (K key : map.keySet()) {
V value : map.get(key);
@leifoolsen
leifoolsen / ComparisonChain.java
Created May 15, 2015 21:23
The Guava ComparisonChain is a utility that provides a fluent API to write clean compareTo methods
public class Fruit implements Comparable<Fruit> {
private String name;
private String family;
private int calories;
@Override
public int compareTo( Fruit otherFruit ) {
return ComparisonChain.start()
.compare( name, otherFruit.name )
@leifoolsen
leifoolsen / charmatcher.java
Created April 20, 2015 13:26
Check if a string contains only a given character using Guava CharMatcher
CharMatcher.is('0').matchesAllOf("0000000") => true
CharMatcher.is('0').matchesAllOf("0000001") => false