Skip to content

Instantly share code, notes, and snippets.

Avatar

Martin Eneqvist marlun78

  • Garkbit AB
  • Lund, Sweden
View GitHub Profile
@marlun78
marlun78 / flat-map.ts
Last active Oct 21, 2020
TypeScript flatMap
View flat-map.ts
export const flatMap = <T, U>(
array: T[],
callback: (value: T, index: number, array: T[]) => U
): U[] => {
return Array.prototype.concat(...array.map(callback));
};
@marlun78
marlun78 / react-switch-component.js
Last active Nov 7, 2018
Idea for a React <Switch> component
View react-switch-component.js
/**
* react-switch-component.js
* Copyright (c) 2018 marlun78
* MIT License, https://gist.github.com/marlun78/bd0800cf5e8053ba9f83
*/
import { Children, createElement } from 'react';
/**
* Switch Component
*
@marlun78
marlun78 / .vimrc
Created Jul 25, 2018
My VIM config
View .vimrc
filetype plugin indent on
syntax on
set number
set tabstop=4
set shiftwidth=4
set expandtab
@marlun78
marlun78 / sample.js
Created Jul 19, 2018
Samples a given number of characters from a given string
View sample.js
// 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 / interpolate.js
Created Jul 19, 2018
Replaces placeholders in a string template with passed values
View interpolate.js
// 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 / make-cancelable.js
Created Oct 26, 2017
An idea for promise cancelation (it doesn’t really cancel the promise, just make sure it never resolves or rejects)
View make-cancelable.js
// 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)
);
});
View git-sync-masters.md
git fetch upstream && git checkout master && git rebase upstream/master && git push origin master
@marlun78
marlun78 / move-props.js
Last active Feb 16, 2017
Move properties and their values from one object into another by mutating the source object.
View move-props.js
// 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 Apr 26, 2016
Fiddling around with some kind of observable values
View observableValue.js
/**
* 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
*/
View IndexedDB.js
var idb = (function () {
'use strict';
var IDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
var IDBTransactionMode = {
readOnly: 'readonly',
readWrite: 'readwrite',
versionChange: 'versionchange'
};