Skip to content

Instantly share code, notes, and snippets.

View marlun78's full-sized avatar

Martin Eneqvist marlun78

View GitHub Profile
@marlun78
marlun78 / react-switch-component.js
Last active November 7, 2018 08:27
Idea for a React <Switch> component
/**
* 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 / license.txt
Last active November 7, 2018 08:23
MIT License
Copyright (C) 2009-2018 marlun78
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
@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
@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 / 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 / $once.js
Last active February 28, 2018 19:19
Angular $rootScope.Scope.$once
/**
* Angular $rootScope.Scope.$once
* Copyright (c) 2014 marlun78
* MIT License, https://gist.github.com/marlun78/bd0800cf5e8053ba9f83
*/
$provide.decorator('$rootScope', function ($delegate) {
var Scope = $delegate.__proto__.constructor;
Scope.prototype.$once = function (name, listener) {
var deregister = this.$on(name, function () {
deregister();
@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)
);
});
git fetch upstream && git checkout master && git rebase upstream/master && git push origin master
@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 / Number.isFinite.js
Last active February 10, 2017 08:39
Number.isFinite polyfill
/**
* Number.isFinite
* Copyright (c) 2014 marlun78
* MIT License, https://gist.github.com/marlun78/bd0800cf5e8053ba9f83
*
* Spec: http://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.isfinite
*/
if (typeof Number.isFinite !== 'function') {
Number.isFinite = function isFinite(value) {
// 1. If Type(number) is not Number, return false.