Skip to content

Instantly share code, notes, and snippets.

View nicolashery's full-sized avatar

Nicolas Hery nicolashery

View GitHub Profile
@nicolashery
nicolashery / typescript-jsdoc-enum.md
Last active August 9, 2024 16:06
Emulating "enums" in JSDoc version of TypeScript

Emulating "enums" in JSDoc version of TypeScript

Problem

TypeScript has support for type-checking plain JavaScript files, which is very useful if you have an existing JS codebase and you want to test the waters and gradually add types.

There are some limitations in what you can do in JSDoc, but a lot of them can be worked-around by using type-definition files .d.ts (for example in a types/ directory). These files don't generate any JavaScript code, they are just there to provide extra type definitions to the compiler.

One thing you can't do in those .d.ts files though, is use enums. You could define them of course, but you won't get the runtime representation since the files don't generate JS code.

@nicolashery
nicolashery / typed-event-driven.ts
Last active July 23, 2024 17:22
Sketchpad: Event-driven design in TypeScript
// Playing around with TypeScript
// Inspiration: https://github.com/abdullin/omni
// core.event
interface DomainEventType {
name: string;
}
interface DomainEvent {
_type: DomainEventType;
@nicolashery
nicolashery / example.js
Last active May 28, 2024 16:53
Combine a pipe of multiple Node.js streams into one stream
var util = require('util')
, Transform = require('stream').Transform
, StreamCombiner = require('./streamcombiner');
var chunks1 = [];
var stream1 = new Transform();
var soFar = '';
stream1._transform = function(chunk, encoding, done) {
chunks1.push(chunk.toString());
var pieces = (soFar + chunk).split('\n');
@nicolashery
nicolashery / poisson.js
Created June 28, 2013 14:52
Quick JavaScript implementation of Exponential and Geometric random number generators
/* Quick implementation of Exponential and Geometric random number generators
For example you can use it to simulate when an event is going to happen next, given its average rate:
Buses arrive every 30 minutes on average, so that's an average rate of 2 per hour.
I arrive at the bus station, I can use this to generate the next bus ETA:
randomExponential(2); // => 0.3213031016466269 hours, i.e. 19 minutes
*/
@nicolashery
nicolashery / elasticsearch.md
Last active December 30, 2023 19:03
Elasticsearch: updating the mappings and settings of an existing index

Elasticsearch: updating the mappings and settings of an existing index

Note: This was written using elasticsearch 0.9.

Elasticsearch will automatically create an index (with basic settings and mappings) for you if you post a first document:

$ curl -X POST 'http://localhost:9200/thegame/weapons/1' -d \
'{
  "_id": 1,

Nesting APIs and ReaderT environments in Haskell's Servant

Environments, from parent to child (or base to extended):

  • App (ReaderT AppEnv IO):
    • HasLogFunc env
    • HasDatabase env
    • HasTracing env
  • AppAuthenticated (ReaderT AppAuthenticatedEnv IO):
  • HasApp env (everything from App)

App-wide vs. Handler-specific environments in Haskell's Servant

See files:

Client requests:

Using MonadLogger without LoggingT in Haskell

@nicolashery
nicolashery / .gitignore
Last active September 17, 2023 16:58
TypeScript React JSDoc
node_modules
dist

Using RIO with Servant in Haskell, with nested environments.

Port of servant-nested-apis Gist.