Skip to content

Instantly share code, notes, and snippets.

@lski
lski / array.find-polyfill.js
Last active October 30, 2017 09:50
Array.find polyfill created by MDN, made available to use with NPM
/*! Created by MDN {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find} */
if (!Array.prototype.find) {
Array.prototype.find = function(predicate) {
if (this === null) {
throw new TypeError('Array.prototype.find called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
@lski
lski / promise-all-success.js
Created April 1, 2017 17:18
A variation on promise all, where instead of stopping on first fail, it will continue and resolves with a list of all successful responses.
module.exports = function all(promises) {
let completed = 0;
let count = promises.length;
let results = [];
return new Promise((resolve) => {
for (let p of promises) {
p.then(_success).catch(_next);
@lski
lski / Router.jsx
Created April 24, 2017 13:47
Shows two different ways to easily add a lazy loaded HashRouter fallback to a React with React Router v4 app
import React, { PureComponent } from 'react';
import { supportsHistory } from 'history/DOMUtils';
import { BrowserRouter } from 'react-router-dom';
const requiresHash = !supportsHistory();
/**
* Option 1:
* A Router that by default uses a BrowserRouter, but if not supported fallsback and lazy loads HashRouter
*/
@lski
lski / withFallback.js
Created April 26, 2017 11:15
Load the default component unless the result of check is false(y) then run the getFallback function that resolves to a Component
import React, { PureComponent } from 'react';
/**
* Load the default component unless the result of check is false(y) then run the getFallback function that resolves to a Component
*
* @param {React.Component} DefaultComponent
* @param {function} check
* @param {function} getFallback
*/
export default function withFallback(DefaultComponent, check, getFallback) {
@lski
lski / asyncComponent.jsx
Created April 26, 2017 11:23
Slightly altered React asyncComponent wrapper that doesnt require the need for babel-plugin-transform-class-properties.
import React, { PureComponent } from 'react';
/**
* Slightly altered asyncComponent that doesnt require the need for babel-plugin-transform-class-properties.
*
* Accepts a function that returns a Promise, that resolves to a React component.
* Only loads it when the asyncComponent is mounted
*
* @param {function} getComponent
*/
@lski
lski / Arg.cs
Last active September 21, 2017 13:54
Quick and dirty args utility
/// <summary>
/// An argument and its values (if any) extracted from a list of arguments
/// </summary>
public class Arg
{
public Arg(string key, IEnumerable<string> values)
{
Key = key;
Values = values;
}
@lski
lski / parseTime.js
Created December 31, 2017 16:20
Date time functions
const timeRegex = /^([\d]{2}):([\d]{2}):?([\d]{2})?\.?([\d]{0,3})?$/;
function parseTime(time) {
const matches = timeRegex.exec(time);
if(matches) {
return {
hours: parseInt(matches[1], 10),
mins: parseInt(matches[2], 10),
@lski
lski / is-func.js
Created April 7, 2018 18:58
Simple array replace function that takes an array, a search function and a value to replace it with
export default function isFunction(fun) {
return typeof fun === 'function' && fun.call;
};
@lski
lski / date-difference.js
Created April 7, 2018 19:02
Accepts two dates and calculates the difference between them and returns it as a breakdown.
/**
* Accepts two dates and calculates the difference between them and returns it as a breakdown.
*
* TODO: Works but requires some work for if start is greater than end
*
* @param {Date} start
* @param {Date} end
*/
const difference = (start, end) => {
@lski
lski / Readme.md
Last active September 25, 2018 13:16
Install colortool for windows on WSL