Skip to content

Instantly share code, notes, and snippets.

View .js
const create = sessionUser => data => Promise.try(() => transaction(model, async (modelTransaction) => {
const { CAN_CREATE_ORGANIZATIONS } = getPermissions(sessionUser.permissions);
const { userId } = sessionUser;
const { avatar, ...orgData } = data;
if (!CAN_CREATE_ORGANIZATIONS) {
throw Unauthorized({ message: 'Sie haben nicht das Recht Organisationen zu erstellen' });
}
const insert = params => modelTransaction.query()
View .js
let estate;
let signed;
let watches;
function getImagesAndServePages() {
estate = require('fs').readdirSync('./public/imgs/estate');
signed = require('fs').readdirSync('./public/imgs/signed');
watches = require('fs').readdirSync('./public/imgs/watches');
View .js
const get = (array, currentKey, direction) => {
const k = currentKey ? currentKey : array[0];
const delta = direction == 'inc' ? 1 : -1
const index = array.indexOf(k);
const nextIndex = (index + delta + array.length) % array.length;
console.log(array[nextIndex]);
};
View .js
import React, { useState } from 'react';
import styled from '@emotion/styled';
import { Col, Row } from './Flex';
const splitBasis = (percent) => {
const SCALE = 10000;
const p = Math.floor(percent * SCALE);
const shared = { flexShrink: 1 };
return {
a: { flexGrow: String(p), ...shared },
View .js
export async function json_to_sheet(...args) {
const XLSX = await import('xlsx')
return XLSX.utils.json_to_sheet(...args);
}
View .js
function on(eventTarget, eventType, handler) {
// Distinct but identical event handler
const handler2 = (event) => handler(event);
eventTarget.addEventListener(eventType, hander2);
const unsub = () => {
eventTarget.removeEventListener(eventType, hander2);
};
return unsub;
@brigand
brigand / promiseMap.js
Created Feb 17, 2021
A slow async/await version of promiseMap
View promiseMap.js
async function promiseMap(array, mapper, concurrency) {
const promises = [];
const results = Array.from({ length: array.length });
const drainOne = async () => {
const [pj, { index, resolved, rejected }] = await Promise.race(promises.map((p, pi) => p.then((v) => [pi, v])));
promises.splice(pj, 1);
if (resolved) {
results[index] = resolved[0];
@brigand
brigand / .js
Last active Sep 1, 2020
domOn, domOnce
View .js
function domOn(subject, eventName, handler, options = false) {
// This function is useless except in the case domOn(x, 'e', f); domOn(x, 'e', f); is run
// where cleanup can remove the distinct 'inner' function from each call, instead of both 'f' listeners.
function inner(event) {
return handler.call(this, event);
}
subject.addEventListener(eventName, inner, options);
const cleanup = () => subject.removeEventListener(eventName, inner, options);
@brigand
brigand / .js
Created Dec 17, 2019
Concept for flexible pattern matching in ES6
View .js
import match, { matches, Instance, TypeOf, PropEq, Eq, Check, Index } from '@brigand/unnamed-match-thing';
// Simple matching.
// Note that this is an object literal, so all of these need to
// return something with a toString, and match needs to convert that
// back to the pattern. This implies we need to store Eq('production') in
// module level storage and somehow avoid leaking memory for every pattern ever used.
// Something every toString called here allocates the pattern, and when match() is called
// it can claim all patterns before it (since the toString calls run before match()), removing
// them from the global storage, and then execute the patterns.
@brigand
brigand / README.md
Last active Nov 14, 2019
A gist npm package for using @types/stripe-v3
View README.md

To install, add this gist as a dependency, and @types/stripe-v3 as a dev dependency.

yarn add --dev @types/stripe-v3
yarn add https://gist.github.com/955eafd92692f4ba54d50d447cef6327.git

Or with npm