motd-json
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>,
"regions": [<string>],
"periods": [{
"from": <date-time>,
"till": <date-time>
}],
"tags": {
{
"name": <string>,
"type": ["semver", "number", "boolean", "set"],
"value":
<string>
<number>
<boolean>
Array<any>
}
}
]
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)
Options
These are the options you could pass to the motd
export for retrieving a message
region
stringregional
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
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
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
[
{
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
License
MIT