- 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' {}const Introduction = greeting: handshake: error: true const HelloIntroduction = 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
.
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
.
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
ES6 Modules / CommonJS
// ES6 // CommonJS modulesconst implementjs = const implement = implementjsdefaultconst Interface type = implementjs
Examples
Standard usage
const Passenger = name: height: const ChildPassenger = hasBabySeat: extend: Passenger const Car = speed: passengers: beep: // Successful implementationconst MyCar = speed: 0 passengers: {} // Bad implementation - does not implement the beep methodconst AnotherCar = speed: 0 passengers:
Unit tests
Renaming and refactoring API responses:
const User = name: id: trim: true const Users = users: trim: true rename: API_RESPONSE_USERS_LIST: 'users' const updateUsers = async { try const users = await const MyUsers = users catch err }
Code Style
eslint-config-standard