Socket
Socket
Sign inDemoInstall

js-messages

Package Overview
Dependencies
0
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    js-messages

A small library to handle messages, message types and message creators (also known as "actions", "action types" and "action creators" in redux context). js-messages is written in TypeScript, everything is completely type safe.


Version published
Weekly downloads
28
increased by460%
Maintainers
1
Install size
78.2 kB
Created
Weekly downloads
 

Readme

Source

js-messages

A small library to handle messages, message types and message creators (also known as "actions", "action types" and "action creators" in redux context). js-messages is written in TypeScript, everything is completely type safe.

Licence npm version Build status Coverage status

Installation

npm install --save js-messages

Usage

js-messages consists of three functions:

  • defineMessage: To define one type of message / to create one message creator
  • defineMessages: To define multiple related messages represented by message creators
  • props: A helper function to define a message creator where all message properties must be declared explicitly in an object.

Example 1 (using defineMessage)

import { defineMessage } from 'js-messages'

const increment = defineMessage('increment')
// for messages of shape { type: 'increment' }

const decrement = defineMessage('decrement')
// for messages of shape { type: 'decrement' }

const resetTo = defineMessage('resetTo', (value: number) => ({ value }))
// for messages of shape { type: 'resetTo', value: number }

Example 2 (using defineMessage and props)

const saveToStorage = defineMessage(
  'saveToStorage',
  props<{ tasks: Task[]; storageKey: string }>()
)

// for messages of shape
//   {
//     type: 'saveToStorage',
//     tasks: Task[],
//     storageKey: string
//   }

Example 3 (using defineMessages, without namespace):

import { defineMessages } from 'js-messages'

const Actions = defineMessages({
  increment: null,
  // for messages of shape { type: 'increment' }

  decrement: null,
  // for messages of shape { type: 'decrement' }

  resetTo: (value: number) => ({ value })
  // for messages of shape { type: 'resetTo', value: number }

  log: (value: number, message: string = null) => ({ value, message })
  // for messages of shape { type: 'log', value: number, message: string }
})

Example 4 (using defineMessages, with namespace):

import { defineMessages } from 'js-messages'

const Actions = defineMessages('counter', {
  increment: null,
  // for messages of shape { type: 'counter.increment' }

  decrement: null,
  // for messages of shape { type: 'counter.decrement' }

  resetTo: (value: number) => ({ value })
  // for messages of shape { type: 'counter.resetTo', value: number }

  log: (value: number, message: string = null) => ({ value, message })
  // for messages of shape { type: 'counter.log', value: number, message: string }
})

License

"js-messages" is licensed under LGPLv3.

Project status

"js-messages" is currently in beta status.

FAQs

Last updated on 31 Dec 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc