Skip to content

Instantly share code, notes, and snippets.

View amoilanen's full-sized avatar
💭
"Simplicity is prerequisite for reliability." Edsger W. Dijkstra

Anton Moilanen amoilanen

💭
"Simplicity is prerequisite for reliability." Edsger W. Dijkstra
View GitHub Profile
@amoilanen
amoilanen / object_delve.js
Created February 4, 2016 12:56
Adds utility 'delve' function to Object.prototype that allows accessing nested object properties
/*
* Adds 'delve' to the Object prototype for accesing nested properties.
*/
if (!Object.prototype.delve) {
Object.prototype.delve = function(compositeKey) {
return compositeKey
.split('.')
.reduce((obj, key) => obj[key], this);
}
}
@amoilanen
amoilanen / tabs2spaces.py
Created January 29, 2016 15:50
Python script to replace tabs with spaces recursively for all files matching a specified file mask regex, the number of spaces if configurable
#
# Usage:
#
# python tabs2spaces.py . 2 .*\.js$
#
import argparse
import os
import re
parser = argparse.ArgumentParser(description='Replace tabs with spaces.')
@amoilanen
amoilanen / yield_generator_from_generator.js
Created January 6, 2016 16:13
Yielding generator from a generator: seemless to the client code
function* func1() {
var y = yield 'b';
console.log('y = ', y);
return 'c';
}
function* func() {
var x = yield 'a'
console.log("x = ", x);
var z = yield* func1();
@amoilanen
amoilanen / annotations.js
Last active December 21, 2015 22:05
Demonstrates how easy it is to add annotations support in JavaScript, although they are not that useful like in statically typed languages
/**
* Example how annotations can be easily added to JavaScript functions, however there is no need in this
* as unlike in statically typed languages in JavaScript it is already possible to add various fields to functions
*/
(function() {
if (Function.prototype._annotations) {
return;
}
@amoilanen
amoilanen / generators_demo.js
Last active December 9, 2015 13:02
Demonstrates how ES6 generator function works
function* func() {
var x = yield 'a'
console.log("x = ", x); //1
var y = yield 'b'
console.log("y = ", y); //2
return 'c';
}
var it = func();
//Showcases how arrays can be easily concatenated with values in ES6
var x = [1, 2, 3];
var y = [4, 5, 6];
var z = 7;
var arr = [...x, ...y, z];
console.log(arr);
@amoilanen
amoilanen / app.tsx
Created September 22, 2015 14:13 — forked from tomaskikutis/app.tsx
react typescript browserify
// npm install browserify -g
// npm install tsd -g
// npm install react tsify
// tsd install react
// browserify app.tsx -p [tsify --jsx=react] -o bundle.js
/// <reference path="typings/react/react.d.ts" />
import React = require("react");
interface HelloWorldComponentProps extends React.Props<any> {
@amoilanen
amoilanen / constructor.arguments.expansion.js
Created September 16, 2015 19:47
Demonstrates how constructor arguments can be expanded from an array in JavaScript when using 'new'.
/*
* Demonstrates how constructor arguments can be expanded if supplied as an array.
*/
function createWithArgs(constructor, args) {
var boundArgs = [null].concat(args);
var boundConstructor = Function.prototype.bind.apply(constructor, boundArgs);
//When 'boundConstructor' is invoked with new,
//'this' will point to object being constructed, not 'null'
return new boundConstructor();
@amoilanen
amoilanen / taming_function_arguments.js
Created June 30, 2015 22:08
Produces a real array from function arguments array-like object and places it in the function context while the function is being executed
/*
* One way to make arguments of a function an array in JavaScript with less boilerplate.
*/
Function.prototype.enhance = Function.prototype.enhance || function() {
var self = this;
return function() {
var oldValue = this._args;
try {
@amoilanen
amoilanen / equivalent_transparent_color.js
Last active August 29, 2015 14:18
JavaScript function to compute what color should be specified in CSS given desired transparency value and non-transparent color
/**
* Computes the color to be specified in CSS which will be visually equivalent to
* <b>desiredColor</b> when the transparency is <b>alpha</b>.
* Based on the method suggested in http://stackoverflow.com/questions/12228548/findinq-equivalent-color-with-opacity
*
* @options.backgroundColor -
* background color against which the computed background color will be overlayed
* @options.desiredColor -
* desired color appearance in case alpha is 1
* @options.alpha -