Skip to content

Instantly share code, notes, and snippets.

View kripod's full-sized avatar

Kristóf Poduszló kripod

View GitHub Profile
@kripod
kripod / murmur3a.ts
Last active August 2, 2020 14:56
MurmurHash3_x86_32 in TypeScript
// Inspired by: https://github.com/levitation/murmurhash-js
function murmur3a(data: string, seed: number = 0): number {
const len = data.length;
const nBlocks = len >>> 2;
const tailOffset = nBlocks << 2;
let k1;
/* Body */
@kripod
kripod / xxh32.ts
Created February 18, 2020 00:00
xxHash fast digest algorithm
// ~~: Math.floor
// >>> 0: Clamp to Uint32
const PRIME32_1 = 2654435761; // 0b10011110001101110111100110110001
const PRIME32_2 = 2246822519; // 0b10000101111010111100101001110111
const PRIME32_3 = 3266489917; // 0b11000010101100101010111000111101
const PRIME32_4 = 668265263; // 0b00100111110101001110101100101111
const PRIME32_5 = 374761393; // 0b00010110010101100110011110110001
function xxh32(message: Uint8Array, seed: number = 0): number {
@kripod
kripod / Box.tsx
Last active May 19, 2024 02:01
Superseded by https://www.kripod.dev/blog/behind-the-as-prop-polymorphism-done-well/ – Polymorphic `as` prop for React components with TypeScript
import React from 'react';
// Source: https://github.com/emotion-js/emotion/blob/master/packages/styled-base/types/helper.d.ts
type PropsOf<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
E extends keyof JSX.IntrinsicElements | React.JSXElementConstructor<any>
> = JSX.LibraryManagedAttributes<E, React.ComponentPropsWithRef<E>>;
export interface BoxOwnProps<E extends React.ElementType = React.ElementType> {
as?: E;
@kripod
kripod / NumberInput.tsx
Last active January 4, 2020 16:07
React NumberInput with a value of type Number
// Demo: https://codesandbox.io/s/react-numberinput-v5-7lqtb
import React, { useEffect, useState } from 'react';
function clamp(
x: number,
lower: number = Number.NEGATIVE_INFINITY,
upper: number = Number.POSITIVE_INFINITY,
): number {
return Math.min(Math.max(x, lower), upper);
@kripod
kripod / engine.c
Created May 24, 2017 14:00
Quant cup contest for fastest matching engine.
/*****************************************************************************
* QuantCup 1: Price-Time Matching Engine
*
* Submitted by: voyager
*
* Design Overview:
* In this implementation, the limit order book is represented using
* a flat linear array (pricePoints), indexed by the numeric price value.
* Each entry in this array corresponds to a specific price point and holds
* an instance of struct pricePoint. This data structure maintains a list
@kripod
kripod / socket-handler.js
Created June 17, 2016 09:29
SocketHandler base class for Primus
class SocketHandler {
get room() {
return this.primus.rooms(this.roomName);
}
constructor(primus, roomName, actionHandlers, disconnectionHandler) {
this.primus = primus;
this.roomName = roomName;
primus.on('connection', (spark) => {
@kripod
kripod / dir-selector.js
Created April 26, 2016 19:17
Node.js - ES6 directory selector
var nodeVersion = parseInt(process.versions.node.split('.')[0]);
if (nodeVersion >= 6) {
// Use the untranspiled ES6 code
require('./src');
} else {
// Use the transpiled ES5 code
require('./lib');
}