npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

correspondent

v1.0.1

Published

Keep text consistent across your app.

Downloads

7

Readme

What?

Correspondent helps you keep user-facing text consistent across your application by allowing you to store that text in a single JSON file.

This approach works especially well for:

  • API response messages
  • Notification text
  • Error/success messages

Why?

More specifically....

Why do I need this?

I made this because I need to keep text consistent across a large app that I'm working on. If you don't have that need, you might not need this!

Why not just import a JSON file?

Correspondent introduces a few advantages over a static JSON files:

  • Variables, Prefixes & Postfixes!
  • Safety. If you reference a message that doesn't exist, Correspondent will fallback to an default message.
  • Multiple message types in a single file.

Installation

npm install correspondent --save

Usage & Demo

The idea behind Correspondent is best expressed through a demo.

Let's say you want to create some response messages for your API. You could define them in a file called success.json like this:

{
  "prefix":"Success! ",
  "messages":[
    {"USER_CREATED":"User: {0} was created"},
    {"USER_UPDATED":"User: {0} was updated"}
  ],
  "postfix": ".",
  "unknown":"The action was completed"
}

By keeping all user-facing success messages in this file, it's easy to ensure that they maintain consistent content and style.

Accessing the messages you've just defined is easy:

//Note that the path to success.json is relative to your project, not the location of the Correspondent module.
let success = require('correspondent')('./success.json')

success('USER_CREATED','alex') // Returns: "Success! User: Alex was created."

While the gist of the message we're sending is clear to the programmer, its actual text is handled by Correspondent through the JSON file defined outside of the code.

What if you wanted to store more than just success messages with Correspondent?

You could just create another instance of Correspondent for that new message type. For example, building off of our earlier code:

//What we had before
let success = require('correspondent')('./success.json')
let err = require('correspondent')('./error.json')

success('USER_CREATED','alex') // Returns: "Success! User: Alex was created."

err('NOT_FOUND') // Returns: the message in error.json that has the key 'NOT_FOUND'

Or better yet, you can keep both success and error messages in a single JSON file. To do that, we need to modify our JSON just a bit. Let's make a new file called messages.json

{
  "success":{
    "prefix":"Success! ",
    "messages":[
      {"USER_CREATED":"User: {0} was created"},
      {"USER_UPDATED":"User: {0} was updated"}
    ],
    "postfix": ".",
    "unknown":"The action was completed"
  },
  "error":{
    "prefix":"Error! ",
    "messages":[
      {"NOT_FOUND":"The user could not be found"}
    ],
    "postfix": ".",
    "unknown":"Something went wrong"
  }
}

Now, here's how we'd access both message types:

let msg = require('correspondent')('./messages.json')

msg('success','USER_CREATED','alex') // Returns: "Success! User: Alex was created."

msg('error','NOT_FOUND') // Returns: "Error! The user could not be found."

You can use either method mentioned above to store an arbitrary number of message types.

Etc.

Philosophy

As I mentioned above, Correspondent was created to meet my own needs. Any updates to the module will be consistent with that goal, as I am using it in production.

Because Correspondent is basically an incremental improvement over using a plain JSON file, I don't want to introduce the problems associated with additional dependencies including lack of control, stability, bloat, etc. Thus, it will never make us of any dependencies.

Coming Eventually

  • Tests, so that its more production-friendly.
  • Probably nothing else... there's just not that much to it.

License

Correspondent is distributed under the MIT license by Alex Arena.