Skip to content

Instantly share code, notes, and snippets.

View marlun78's full-sized avatar

Martin Eneqvist marlun78

View GitHub Profile
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 / 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 idb = (function () {
'use strict';
var IDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
var IDBTransactionMode = {
readOnly: 'readonly',
readWrite: 'readwrite',
versionChange: 'versionchange'
};
@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
*/
@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;
git fetch upstream && git checkout master && git rebase upstream/master && git push origin master
@marlun78
marlun78 / make-cancelable.js
Created October 26, 2017 07:07
An idea for promise cancelation (it doesn’t really cancel the promise, just make sure it never resolves or rejects)
// Just an idea, completely untested!
function makeCancelable(promise) {
let canceled = false;
const proxy = new Promise((resolve, reject) => {
promise.then(
(value) => canceled === false && resolve(value),
(error) => canceled === false && reject(error)
);
});
@marlun78
marlun78 / interpolate.js
Created July 19, 2018 12:25
Replaces placeholders in a string template with passed values
// interpolate.js
// Based on Crockford’s supplant (http://www.crockford.com/javascript/remedial.html)
// Example;
// interpolate('Hello {name}!', { name: 'Martin' }); // 'Hello Martin!'
// interpolate('Hello {0}!', ['Vanja']); // 'Hello Vanja!'
function interpolate(template, values) {
return template.replace(/\{([^{}]*)\}/g, function(match, key) {
const value = values[key];
const type = typeof value;
@marlun78
marlun78 / sample.js
Created July 19, 2018 12:45
Samples a given number of characters from a given string
// sample.js
// Samples a given number of characters from a given string.
// Example;
// sample('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', 4); // Eg. 'uY7m'
function sample(characters, length) {
var result = '';
for (var i = 0; i < length; i++) {
result += getRandomChar(characters);
}
return result;
@marlun78
marlun78 / .vimrc
Created July 25, 2018 11:45
My VIM config
filetype plugin indent on
syntax on
set number
set tabstop=4
set shiftwidth=4
set expandtab