Should.js Type Adaptors
The `should-type-adaptors` library (current stable version 1.1.0) provides low-level utility functions specifically designed to enhance the `should.js` BDD-style assertion library. Its core purpose is to enable consistent deep comparison, property traversal, and accurate type inspection across diverse JavaScript data structures, particularly those that might deviate from standard object or array behaviors. The library allows for the registration of custom type adaptors, facilitating intelligent handling of complex, custom, or "adapted" types during `should.js` assertions. Maintained as part of the `should.js` ecosystem, its latest updates were for dependency management, suggesting it is in a maintenance state rather than active development. Its key differentiator is offering an extensible mechanism for `should.js` to correctly interpret and assert against non-standard or complex type structures.
Warnings
- gotcha This package (`should-type-adaptors`) is a low-level utility for `should.js` and is not typically used for direct assertions. Its functions are primarily intended to extend or modify how `should.js` processes different data types during its own assertion logic.
- gotcha The package's `package.json` does not specify `"type": "module"`, indicating it is primarily a CommonJS module. While modern bundlers can often handle ESM imports, direct usage in Node.js without specific configuration might require `require()`.
- deprecated The `should-type-adaptors` package has seen infrequent updates, with its last commit related to dependency updates in late 2019. While it remains functional within the `should.js` ecosystem, active development on this specific utility has significantly slowed.
Install
-
npm install should-type-adaptors -
yarn add should-type-adaptors -
pnpm add should-type-adaptors
Imports
- addTypeAdaptor
const { addTypeAdaptor } = require('should-type-adaptors');import { addTypeAdaptor } from 'should-type-adaptors'; - convert
const { convert } = require('should-type-adaptors');import { convert } from 'should-type-adaptors'; - isAdapted
const { isAdapted } = require('should-type-adaptors');import { isAdapted } from 'should-type-adaptors';
Quickstart
import should from 'should';
import { addTypeAdaptor } from 'should-type-adaptors';
// Define a custom class
class MyCustomType {
constructor(id, value) {
this.customId = id;
this.data = value;
}
// A method that might be important for comparison
getFormattedData() {
return `ID: ${this.customId}, Data: ${this.data}`;
}
}
// Register a type adaptor for MyCustomType to define how 'should.js' should compare it
// This adaptor tells should.js to use 'getFormattedData' for deep comparisons.
addTypeAdaptor(MyCustomType, {
valueOf: (instance) => instance.getFormattedData(),
// You can also define custom 'eql' or 'inspect' methods here
// eql: (a, b) => a.customId === b.customId && a.data === b.data,
// inspect: (instance, level) => `MyCustomType { id: ${instance.customId}, data: '${instance.data}' }`
});
const instance1 = new MyCustomType(1, 'hello');
const instance2 = new MyCustomType(1, 'hello');
const instance3 = new MyCustomType(2, 'world');
// Now should.js can compare custom types based on their adapted value
should(instance1).eql(instance2); // This assertion will pass because valueOf makes them deeply equal
should(instance1).not.eql(instance3); // This assertion will pass
console.log('Assertions passed successfully!');
// Example of direct usage of convert (less common)
// import { convert } from 'should-type-adaptors';
// const convertedValue = convert(instance1); // Will be 'ID: 1, Data: hello'
// console.log(convertedValue);