Skip to content

Instantly share code, notes, and snippets.

View marlun78's full-sized avatar

Martin Eneqvist marlun78

View GitHub Profile
@marlun78
marlun78 / move-props.js
Last active February 16, 2017 16:43
Move properties and their values from one object into another by mutating the source object.
// const source = {name: 'Martin', age: 38};
// const target = moveProps(/name/, source);
// console.log(source, target); => {age: 38}, {name: 'Martin'}
export default function moveProps(pattern, source, target = {}) {
return Object.keys(source).reduce((accumulator, key) => {
if (pattern.test(key)) {
accumulator[key] = source[key];
delete source[key];
}
return accumulator;
@marlun78
marlun78 / observableValue.js
Last active April 26, 2016 10:04
Fiddling around with some kind of observable values
/**
* An ObservableValue are like events for values. One important difference from
* events is that you are guaranteed to always get the current value when you
* subscribe. So there is no chance you subscribe “too late”. It will also
* pass you the new value together withthe old value.
* ObservableValue are similar to Angular’s `Scope.$watch`.
*
* http://jsbin.com/hokagosaru/edit?js,console
*/
var idb = (function () {
'use strict';
var IDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
var IDBTransactionMode = {
readOnly: 'readonly',
readWrite: 'readwrite',
versionChange: 'versionchange'
};
@marlun78
marlun78 / server.js
Last active December 15, 2015 11:07
/**
* server.js
* Copyright (c) 2015 marlun78
* MIT License, https://gist.github.com/marlun78/bd0800cf5e8053ba9f83
* Requires Node version >= 4.x
*/
'use strict';
const fs = require('fs');
const http = require('http');
var angular = require('angular');
module.exports = angular.module('models.Locale', [])
.value('Locale', Locale);
/**
* @typedef Locale
* @property {string} language
* @property {string|null} country
* @method {boolean} equals
@marlun78
marlun78 / me.repeat.js
Created November 20, 2015 14:19
A set of directives that works with ngRepeate
angular.module('me.repeat', [])
.directive('meRepeat', repeatDirective)
.directive('meRepeateven', repeatEvenDirective)
.directive('meRepeatodd', repeatOddDirective)
.directive('meRepeatstart', repeatStartDirective)
.directive('meRepeatmiddle', repeatMiddleDirective)
.directive('meRepeatend', repeatEndDirective);
function repeatDirective() {
@marlun78
marlun78 / toRatio.js
Created May 18, 2015 08:00
Takes a screen dimension and return its ratio as width:height
/**
* toRatio.js
* Copyright (c) 2015 marlun78
* MIT License, https://gist.github.com/marlun78/bd0800cf5e8053ba9f83
*
* Takes a screen dimension and return its ratio as width:height.
*
* @example
* toRatio(1280, 720); //=> 16:9
* toRatio(800, 600); //=> 4:3
@marlun78
marlun78 / Intercept.js
Last active August 29, 2015 14:08
Simple meta-programming for methods
/**
* Intercept.js
* Simple meta-programming for methods
* Copyright (c) 2014 marlun78
* MIT License, https://gist.github.com/marlun78/bd0800cf5e8053ba9f83
*
* The intercept-function provides a pre and a post hook that
* will run before and after the original implementation
* respectively. In the pre-hook you have access to the
* arguments passed, and in the post-hook you have access to
@marlun78
marlun78 / Enum.js
Last active August 29, 2015 14:06
Provides an immutable enum type
/**
* Enum.js
* Provides an immutable Enum type
* Copyright (c) 2014 marlun78
* MIT License, https://gist.github.com/marlun78/bd0800cf5e8053ba9f83
*
* @example:
* var colors = new Enum({
* RED: '#f00',
* GREEN: '#0f0',
@marlun78
marlun78 / UniqueMap.js
Last active August 29, 2015 14:06
Provides an immutable enum-like type where both keys and values must be unique.
/**
* UniqueMap.js
* Provides an immutable enum-like type where both keys and values must be unique.
* Copyright (c) 2014 marlun78
* MIT License, https://gist.github.com/marlun78/bd0800cf5e8053ba9f83
*/
(function (undefined) {
'use strict';