Skip to content

Instantly share code, notes, and snippets.

View davidkpiano's full-sized avatar
🎹
Working on XState Dev Tools

David Khourshid davidkpiano

🎹
Working on XState Dev Tools
View GitHub Profile
@busypeoples
busypeoples / Helpers.ts
Last active June 17, 2021 17:14
Helper Functions in TS
/**
* TS Helper Functions
*/
type ConvertTo<Type, From, To> = {
[Key in keyof Type]: Required<Type>[Key] extends From ? To : Type[Key];
};
type FilterByType<T, U> = {
[Key in keyof Required<T>]: Required<T>[Key] extends U ? Key : never;
}[keyof T];
@chrisdmacrae
chrisdmacrae / Comparisons.ts
Last active February 10, 2020 03:36
Builder patterns for XState
// Compare fluent to object notation
const pedestrianMachine = Machine({
id: "pedestrian",
initial: 'walk',
states: {
walk: {
on: {
PED_TIMER: 'wait'
}
@tPl0ch
tPl0ch / Main.sc
Last active February 15, 2022 18:16
Message-Driven Finite-State-Transducer Domain-Driven-Design Aggregate
import cats.instances.either._
import Transducer.run
import UserRegistration._
object Main extends App {
private val commands = List(GDPRDeletion, StartRegistration, StartRegistration, ConfirmAccount, GDPRDeletion)
run(userRegistration)(commands).foreach(println)
// OUTPUT
@chris-sev
chris-sev / setup.sh
Last active August 13, 2026 08:41
Mac Setup
# how to run this thingy
# create a file on your mac called setup.sh
# run it from terminal with: sh setup.sh
# heavily inspired by https://twitter.com/damcclean
# https://github.com/damcclean/dotfiles/blob/master/install.sh
# faster dock hiding/showing (run in terminal)
# defaults write com.apple.dock autohide-delay -float 0; defaults write com.apple.dock autohide-time-modifier -int 0;killall Dock
@Spiralis
Spiralis / XState Handball Match Machine.js
Last active April 10, 2019 20:39
A simplified XState statecharts state-machine for running a handball game. Typically used with a control app for the match officials. Note: There are still parts missing in this sample, like disciplinary actions (2 min, yellow and red cards), as well as who made the goals, and adjusting the match-clock (adjusting the data on the fly). But, it is…
// Available variables:
// Machine (machine factory function)
// XState (all XState exports)
function pad(n, width) {
var n = n + "";
return n.length >= width ? n : new Array(width - n.length + 1).join("0") + n;
}
function displayTime(ticksInSecs) {
@tomraithel
tomraithel / munchkin-statechart.js
Last active April 10, 2019 20:40
Statechart for munchkin rules
// Copy-Paste this Chart into the visualizer: https://statecharts.github.io/xstate-viz/
// Available variables:
// Machine (machine factory function)
// XState (all XState exports)
const { assign } = XState.actions;
const fightStates = {
initial: "fightOrRun",
@GoNZooo
GoNZooo / text.ts
Created December 24, 2018 21:48
Phantom type variant in TypeScript
interface PlainText extends String {
__plaintext__: never
}
interface Base64Encoded extends String {
__base64Encoded__: never
}
interface Encrypted extends String {
__encrypted__: never
}
// Available variables:
// Machine (machine factory function)
// XState (all XState exports)
const {sendParent} = XState.actions
const geoMachine = Machine({
id: "geolocation",
initial: "check_geolocation_api",

Strings

String.prototype.*

None of the string methods modify this – they always return fresh strings.

  • charAt(pos: number): string ES1

    Returns the character at index pos, as a string (JavaScript does not have a datatype for characters). str[i] is equivalent to str.charAt(i) and more concise (caveat: may not work on old engines).

@notoriousb1t
notoriousb1t / 1. simple.js
Last active March 15, 2018 13:40
Just Animate 3 (API ideas)
/**
* This shortcut function returns a timeline that you could build upon.
* a single value is assumed to be the end value
* this autoplays as it is intended to be fire and forget
*/
JA.to('.target', 800, { opacity: 0 })