type
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The "type" package is a highly valuable tool used for the runtime validation and processing of JavaScript types. The package caters to the requirements of the JavaScript language, acknowledging its quirks and ensuring strict adherence to type consistency. The type package does not enforce transpilation, making it compatible with ECMAScript 3 and later editions. A notable feature of this package is that it enables restrained type coercion, rejecting any invalid inputs while normalizing minor type deviations that are acceptable. One of the primary uses of "type" is to validate arguments input in public API endpoints.
The "type" package can be used seamlessly by installing it using npm with the command npm install type
and then by importing it within your JavaScript file. Here is an example of how to use the type package:
const ensureString = require('type/string/ensure')
, ensureDate = require('type/date/ensure')
, ensureNaturalNumber = require('type/natural-number/ensure')
, isObject = require('type/object/is');
module.exports = (path, options = { min: 0 }) {
path = ensureString(path, { errorMessage: "%v is not a path" });
if (!isObject(options)) options = {};
const min = ensureNaturalNumber(options.min, { default: 0 })
, max = ensureNaturalNumber(options.max, { isOptional: true })
, startTime = ensureDate(options.startTime, { isOptional: true });
// ...logic
};
In the example above, the ensureString
function is used to confirm that the 'path' variable is indeed a string, displaying an error message if it is not. ensureDate
validates that the 'startTime' is a legitimate Date object and if the 'options' variable is not an object, it is set to an empty object.
For in-depth usage instructions, documentation on utilities and extensive examples, you can refer to the documentation on GitHub. This includes general utilities like ensure
, is
and coerce
, type-specific utilities for values like String, Number, BigInt, Array, Map, Date, Function, Error, Prototype, and others. Each of these utilities includes specific usage instructions and examples for a more comprehensive understanding.