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

implement-js

v0.0.31

Published

JavaScript interface library

Downloads

2,160

Readme

Implement.js npm version js-standard-style

  • Create interfaces to enforce, remove, or rename properties of objects
  • Use in testing to easily verify object shapes and property types
  • Effortlessly and safely parse API responses by renaming or removing properties
  • Errors and warnings are suppressed when process.env.NODE_ENV === 'production'

Docs

What is Implement.js?

Implement.js is a library that attempts to bring interfaces to JavaScript in the form of runtime type-checking.

Simply define an interface using Interface and call implement on an object to check if it implements the given interface.

const Hello = {
    greeting: 'hello'
    wave () {}
}
const Introduction = Interface('Introduction')({
    greeting: type('string')
    handshake: type('function')
}, { error: true })

const HelloIntroduction = implement(Introduction)(Hello) // throws an error!

Setup

Install

yarn add implement-js

Build

yarn build

API

Implement

Accepts an Interface and an object, then checks to see if the object implements the given Interface.

implement(Interface)(object) -> object
Implementing classes

Since class is just a constructor function waiting to be called and not truly an object, we cannot check if it implements a given Interface. Also, due to the dynamic nature of class properties, even once instantiated we cannot reliably implement interfaces against them.

Interface

Takes a string to be used as a name, if none is provided it generates a uuid, returns a function that accepts an object where all the keys are type objects, and returns an Interface. The Interface is to be used by implement.

Interface([name])(object[, options]) -> Interface object
Options
{
    // when true, errors and warnings are triggered when properties
    // other than those on the Interface are found, is suppressed if
    // trim is set to true - default: false
    strict: true,

    // remove methods that don’t match the Interface - default: false
    trim: true,

    // throws an error when Interface isn’t implemented - default: false
    error: true,

    // warns when Interface isn’t implemented - default: true
    warn: false,

    // accepts an Interface to extend, the new Interface must also
    // implement the extended Interface
    extend: Interface,

    // accepts an object where all property values are strings used to
    // rename the corresponding properties on the given object
    rename: { seats: 'chairs' }
}

Type

Accepts a string matching any JavaScript types, plus ‘array’ and 'any'.

If ‘array’ is passed, a second argument can be passed denoting the type of the elements of the array, if none is passed then the types of the elements will not be checked. The second argument should be an array containing type objects.

If ‘object’ is passed, a second argument can be passed containing an Interface for the object, if none is passed then the properties of the object will not be checked. The second argument should be an Interface.

type(string[, Array<type>|Interface]) -> Type

ES6 Modules / CommonJS

// ES6
import implement, { Interface, type } from 'implement-js'

// CommonJS modules
const implementjs = require('implement-js')
const implement = implementjs.default
const { Interface, type } = implementjs

Examples

Standard usage
import implement, { Interface, type } from 'implement-js'

const Passenger = Interface('Passenger')({
    name: type(‘string’),
    height: type(‘number’)
})

const ChildPassenger = Interface('ChildPassenger')({
    hasBabySeat: type(‘boolean’)
}, {
    extend: Passenger
})

const Car = Interface('Car')({
    speed: type(’number’),
    passengers: type(‘array’, [type('object', Passenger), type('object', ChildPassenger)]),
    beep: type(‘function’)
}, { error: true })

// Successful implementation
const MyCar = implement(Car)({
    speed: 0,
    passengers: [],
    beep () {}
})

// Bad implementation - does not implement the beep method
const AnotherCar = implement(Car)({
    speed: 0,
    passengers: []
})
Unit tests
import implement from 'implement-js'
import CarService from '../services/CarService'
import { Vehicle } from '../Interfaces'

describe('CarService', () => {
    describe('getCar', () => {
        it('should implement the Vehicle Interface', done => {
            const someCar = CarService.getCar()

            // Ensure someCar implements Vehicle Interface
            implement(Vehicle)(someCar)

            done()
        })
    })
})
Renaming and refactoring API responses:
import { store } from ‘../store’
import { fetchUsers } from ‘../services/userService’
import implement, { Interface, type } from 'implement-js'

const User = Interface('User')({
    name: type(‘string’),
    id: type(‘number’)
}, { trim: true })

const Users = Interface('Users')({
    users: type(‘array’, [type('object', User)])
}, {
    trim: true,
    rename: { API_RESPONSE_USERS_LIST: 'users' }
})

const updateUsers = () => async dispatch => {
    dispatch(fetchUsersBegin())

    try {
        const users = await fetchUsers()
        const MyUsers = implement(Users)(users)

        dispatch(updateUsersSuccess(MyUsers))
    } catch (err) {
        dispatch(updateUsersError(err))
    }
}

Code Style

eslint-config-standard

js-standard-style