2.0.7 • Published 11 months ago

@bsgbryan/madul v2.0.7

Weekly downloads
12
License
MIT
Repository
github
Last release
11 months ago

Mädūl

Madul is a simple set of tools that help you craft clean functional code that's straightforward to test - and fun to write & maintain

Docs

You can find a Getting Started Guide and more here - enjoy! 🤘🏻

tl;dr

Madul Definition: MessageReciever.ts

export const dependencies = () => ({
  '+/db': ['connectAs', 'getAllMessagesBefore', 'getMessagesFrom']
})

export const $init = async ({ connectAs, username }) => await connectAs({ username })

export const getMessagesFrom = async ({
  friend,
  getAllMessagesBefore,
  getMessagesFrom,
  sentBefore,
}) => {
  const allMessages  = await getAllMessagesBefore({ timestamp: sentBefore })
  const fromMyFriend = await getMessagesFrom({ friend })

  return allMessages.filter(m => fromMyFriend.includes(m))
}

In the above code:

  1. A madul is just a plain old node module
  2. The db dependency is loaded asynchronously
  3. Dependencies are passed as named parameters to methods
  4. The $init method is guaranteed to be executed after all dependencies have been loaded, but before the madul is available for use; so you know that the db will be properly setup and connected to as username

Madul Usage GetMessagesFromAda.ts

import madul from '@bsgbryan/madul'

const receiver = await madul('+/MessageReciever', { username: 'KatherineJohnson' })

const oneHour    = 1000 * 60 * 60
const sentBefore = Date.now() - oneHour

const messages = await receiver.getMessagesFrom({ friend: 'Ada', sentBefore })

console.log('My messages from Ada!', messages)

In the above code:

  1. We pass the username used for connecting to the db when creating our madul
  2. We don't call $init directly; that's handled for us as part of madul
  3. We don't pass the db dependency to getMessagesFrom; that's handled for us
2.0.3

12 months ago

2.0.2

12 months ago

2.0.5

12 months ago

2.0.4

12 months ago

2.0.7

11 months ago

2.0.6

12 months ago

2.0.1

12 months ago

2.0.0

2 years ago

1.3.4

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago