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

tv4-mutant

v0.0.2

Published

Fork of tv4 by Geraint Luff which adds simple JSON reformatting

Downloads

3

Readme

tv4-mutant: is a fork of tv4

It adds the ability to reformat fields in the source POJO, based on definitions given in a JSON Schema.

It adds two calls to the tv-4 API, addReformatter and reformat

Apologies for doing examples in coffeescript, but I'm in a rush...

Tv4 = require './tv4'

# formats work in the same way as usual
Tv4.addFormat(
    'uk-mobile-phone': (data, schema) ->
        if not data? or /^(447\d{7,11})$/.test(data)
            return null
        else
            return "'#{data}' is not a uk mobile number"

    'lowercase': (data, schema) ->
        if not data? or not /[A-Z]/.test(data)
            return null
        else
            return "'#{data}' must be lowercase"
    )

# a reformatter will raise an error if it doesn't like its input
# it returns a reformatted value if it wants to reformat the data
# or it returns null if it wishes to do nothing
Tv4.addReformatter 'date-time', (data, schema) ->
    if not data?
        return null                     # should pass a null data (nullness is to be decided by the schema)
    else if data instanceof Date
        if isNaN data.getTime()
            throw new Error("invalid date object")
        else
            return null                 # valid date objects should also pass
    else if typeof data is 'string'
        reformatted = new Date(data)
        if isNaN reformatted.getTime()
            throw new Error("invalid time format")
        return reformatted
    else
        throw new Error("'#{data}' cannot be reformatted to date")

# an example schema
ScheduleEvent =
    type: "object"
    additionalProperties: false
    properties:
        index:
            type: "number" 
        openAt:
            type: "object"
            format: "date-time"
        dueAt:
            type: "object"
            format: "date-time"
        timeoutAt:
            type: "object"
            format: "date-time"
        previousEventTimeoutAt:     #this is required, and should be null for the first event
            type: ["object", "null"]
            format: "date-time"
        status:
            type: "string"
            enum: "EarlyTime, Due".split(', ')
        lastPromptSentAt:
            type: ["object", "null"]
            format: "date-time"
        promptsRemaining:
            type: "integer"
    required: 'index,openAt,dueAt,timeoutAt,previousEventTimeoutAt,status,lastPromptSentAt,promptsRemaining'.split(',')

# an example instance
scheduleEventInstance =
    index: 1
    openAt: '2011-02-01T01:01:01.123Z'
    dueAt: '2011-02-01T01:01:01.123Z'
    timeoutAt: '2011-02-01T01:01:01.123Z'
    previousEventTimeoutAt: null
    status: 'Due'
    lastPromptSentAt: '2011-02-01T01:01:01.123Z'
    promptsRemaining: 1

# run reformat on the instance.
console.log Tv4.reformat(scheduleEventInstance, ScheduleEvent)

produces:

{ index: 1,
  openAt: Tue Feb 01 2011 01:01:01 GMT+0000 (GMT),
  dueAt: Tue Feb 01 2011 01:01:01 GMT+0000 (GMT),
  timeoutAt: Tue Feb 01 2011 01:01:01 GMT+0000 (GMT),
  previousEventTimeoutAt: null,
  status: 'Due',
  lastPromptSentAt: Tue Feb 01 2011 01:01:01 GMT+0000 (GMT),
  promptsRemaining: 1 }