DEV Community

Cover image for # new stuff dropped in duckkit 🦆
Zura Japoshvili
Zura Japoshvili

Posted on

# new stuff dropped in duckkit 🦆

new stuff dropped in duckkit 🦆

typed event emitter. function composition. partial application.

here's what it looks like.


before

emitter.on('win', (amount) => {
  // amount is any 😬
})

emitter.emit('win', 'oops')  // no error
emitter.emit('wiiin', 500)   // typo. also no error.
Enter fullscreen mode Exit fullscreen mode

after

const emitter = createEmitter<{
  win: number
  spin: void
  error: { code: number; message: string }
}>()

emitter.emit('win', 500)       // ✅
emitter.emit('win', 'oops')    // ❌ TypeScript error
emitter.emit('wiiin', 500)     // ❌ TypeScript error
emitter.emit('win')            // ❌ payload missing
Enter fullscreen mode Exit fullscreen mode

define the map once. TypeScript does the rest.


pipeline — reusable composed functions

const process = pipeline(
  (s: string) => s.trim(),
  s => s.toUpperCase(),
  s => s.split(' '),
)

process('  hello world  ')  // ["HELLO", "WORLD"]
process('  foo bar  ')      // ["FOO", "BAR"]
Enter fullscreen mode Exit fullscreen mode

async steps work too — mix sync and async freely.


curry

const multiply = curry((factor: number, value: number) => value * factor)

[1, 2, 3].map(multiply(2))   // [2, 4, 6]
[1, 2, 3].map(multiply(10))  // [10, 20, 30]
Enter fullscreen mode Exit fullscreen mode

npm install duckkit
Enter fullscreen mode Exit fullscreen mode

npm · GitHub

do you use typed emitters? or just wing it with strings? 👇

Top comments (0)