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

motd-json

v0.1.1

Published

Library for retrieving a random motd from a json input with filter support

Downloads

1

Readme

motd-json

npm version npm downloads Circle CI Codecov

Library for retrieving a random motd from a json input with filter support

Features

  • validation of json against a JSON Schema
  • filter motd's by region
  • filter motd's by period / holidays
  • filter motd's by custom tags

Messages definition

[
  {
    "message": <string>, // required, the message of the day
    "regions": [<string>], // optional, the regions/territories to show this message for
    "periods": [{ // optional, use 0000 as year for a yearly recurring period
      "from": <date-time>, // the start date-time this message could be shown
      "till": <date-time> // the end date-time this message could be shown
    }],
    "tags": { // optional
      {
        "name": <string>,
        "type": ["semver", "number", "boolean", "set"],
        "value":
          <string> // for semver
          <number> // for number, matches only equal or greater than
          <boolean> // for boolean
          Array<any> // for set
    }
  }
]

For easy validation a JSON Schema and validate export is included:

The peer dependency ajv needs to be installed for validate to work

import { validate } from 'motd-json'
import yourMessages from './your-messages.json'

await validate(yourMessages) // resolves to true or false

Options

These are the options you could pass to the motd export for retrieving a message

  • region string
  • regional boolean

If region is empty and regional is true, the users region is determined from the territory string of their os locale using os-locale

Messages are matched when they either dont list any region or the user region is included in the list of regions for a message

  • date string

If not empty then only messages are matched that either dont have a period listed or where the provided date falls within the configured period for the message

  • tags object

A key/value mapping of tag values for the current user. If a message contains a list of tags, then the name of those tags is used as key to lookup the value. The mapped value is then compared to the message tag values to determine whether the message should be included or not

Usage

Basic with validation

import { motd, validate } from 'motd-json'
import messages from './my-messages.json'

if (validate(messages)) {
  const options = {
    regional: true,
    tags: {
      typescript: false,
      version: 'v2.2.3',
      modules: ['axios', 'i18n']
    }
  }

  console.log(motd(messages, options))
}

Create a motd generator

import { filter, motd } from 'motd-json'
import messages from './my-messages.json'

const options = {
  regional: true,
  tags: {
    typescript: false,
    version: 'v2.2.3',
    modules: ['axios', 'i18n']
  }
}

const filteredMessages = filter(messages, options)
const motdGenerator = () => motd(filteredMessages)

motdGenerator()
motdGenerator()

Example messages

// messages.json
[
  {
    message: 'some message'
  },
  {
    message: 'my multi-region message',
    regions: ['en', 'nl']
  },
  {
    message: 'mijn test bericht',
    regions: ['nl']
  },
  {
    message: 'Happy New Year',
    periods: [{
      from: '0000-12-28',
      till: '0000-12-31'
    }, {
      from: '0000-01-01',
      till: '0000-01-05'
    }]
  },
  {
    message: 'Merry Christmas',
    period: {
      from: '0000-12-01',
      till: '0000-12-27'
    }
  },
  {
    message: 'Fijne Sinterklaas',
    regions: ['nl'],
    period: {
      from: '2019-11-16',
      till: '2019-12-05'
    }
  },
  {
    message: 'You are using v2.x',
    tags: [
      {
        name: 'version',
        type: 'semver',
        value: 'v2.x'
      }
    ]
  },
  {
    message: 'You are using v3.x',
    tags: [
      {
        name: 'version',
        type: 'semver',
        value: 'v3.x'
      }
    ]
  },
  {
    message: 'You are using 10 modules or more',
    tags: [
      {
        name: 'modulesCount',
        type: 'number',
        value: 10
      }
    ]
  },
  {
    message: 'You are using the axios or http module',
    tags: [
      {
        name: 'modules',
        type: 'set',
        value: ['axios', 'http']
      },
    ]
  },
  {
    message: 'You are using typescript',
    tags: [
      {
        name: 'typescript',
        type: 'boolean',
        value: true
      }
    ]
  }
]

TODO

  • [ ] Add support for different comparison operators for tags

License

MIT