Skip to content

Instantly share code, notes, and snippets.

@danielberndt
danielberndt / sequences.ts
Created October 20, 2023 08:07
Sequences
const lettersToSequence = (
letters: string,
{startVal = 0, implicitZero = false}: {startVal: number; implicitZero: boolean} = {
startVal: 0,
implicitZero: false,
}
) => {
const length = letters.length;
const letterToIndex = letters.split("").reduce((memo, letter, index) => {
memo[letter] = index;
@danielberndt
danielberndt / snippet-to-be-pasted-into-browser.js
Last active October 24, 2022 11:54
extract all cards via api
let subdomain = "MYSUBDOMAIN"; // for e.g. MYSUBDOMAIN.codecks.io
let cardQuery = {cardCreatedAt: {op: "gt", value: "2022-10-20"}};
// build up the nested query and define which models and fields we want to receive
let query = {
_root: [
{
account: [
{
@danielberndt
danielberndt / snippet-to-be-pasted-into-browser.js
Last active September 22, 2022 12:01
Codeck.io-Api: get info about card by it's three letter id
let subdomain = YOUR_SUBDOMAIN;
let CARD_ID='123'
// turns the string representation of a card id into a number.
// `111` will become 1, `15z` will become 140
let convertIdToNumber = (seq) => {
let startVal = 28 * 29 - 1;
let implicitZero= true;
let letters = "123456789acefghijkoqrsuvwxyz";
let length = letters.length;
@danielberndt
danielberndt / Group.ts
Last active September 18, 2022 20:09
React Suspense Abstraction for PouchDb (or other async ressources)
import {Nominal} from "../types";
import PouchDB from "pouchdb-browser";
import LRU from "lru-cache";
export type GroupId = Nominal<string, "GroupId">;
export type GroupOverviewEntry = {
name: string;
createdAt: Date;
};
let subdomain = SUBDOMAIN;
let segmentQuery = {
$and: [
{ startedAt: { op: "gte", value: "2020-11-03T23:00:00.000Z" } },
{ startedAt: { op: "lt", value: "2020-12-01T23:00:00.000Z" } },
],
user: { name: ["daniel","tom","Alex"] },
};
@danielberndt
danielberndt / useLocalStorageState.js
Last active August 19, 2021 10:57
useLocalStorageState
import {useEffect, useMemo, useRef, useState} from "react";
const getStorageMap = (storageGetter) => {
let storage;
try {
storage = storageGetter();
} catch {
return {
storageGet() {
return null;
@danielberndt
danielberndt / README.md
Last active October 26, 2022 09:45
Codecks Api Guide

Quick Guide to Tinkering with the Codecks API

Version: Jan 2021

The Codecks API is the source for powering the web app, so expect it to be fairly stable and expansive. We can't yet make any guarantees regarding the stability of all schema definitions and endpoints at this point. So tread with care!

To get started you need to extract your access token. This token will allow anyone who holds it to impersonate you. This also means they have access to all the same Organizations and contents as you do via the web app. So be careful who you share it with. You might want to create an observer user and use their token if this helps your use case (and the observer limitations suit your use case) you might also create a new non-observer user but this will count towards your user quota.

To extract the token, check requests going to the codecks api (https://api.codecks.io). There you'll find a cookie called at containing your token.

@danielberndt
danielberndt / IsIntersecting.jsx
Created January 7, 2019 11:20
IsIntersecting Component using IntersectionObserver
import React from "react";
export default class IsIntersecting extends React.Component {
state = {isIntersecting: false};
observer = null;
rootRef = React.createRef();
elementRef = React.createRef();
componentDidMount() {
@danielberndt
danielberndt / app.ts
Last active December 31, 2017 14:54
This is the result of some prototyping with zeit/micro and creating type-safe handlers that progressively enhance the `req` variable via function composition
import * as http from "http";
interface Handler<T extends http.IncomingMessage> {
(req: T, res: http.ServerResponse): any;
}
type WithDB = {db: number};
function withPg<T extends http.IncomingMessage>(handler: Handler<T & WithDB>): Handler<T> {
return (req, res) => {
const newReq = req as T & WithDB;
@danielberndt
danielberndt / Ui.js
Last active October 16, 2017 14:19
Glamorous Ui Library Example
import React from "react";
import B from "glamorous";
import {Link} from "react-router-dom";
import col from "./colors";
const FullHeight = B.div({
display: "flex",
flexDirection: "column",
flex: "auto",
minWidth: 0,