JavaScript Environment Capability Detection

0.2.5 · abandoned · verified Sun Apr 19

capability.js is a lightweight JavaScript library designed for detecting specific environmental capabilities, primarily focusing on browser features. Released in 2016, its current stable version is 0.2.5, indicating it has not seen significant development since its initial release. The library allows developers to define custom capability tests using a simple API and then query or enforce these capabilities. Unlike comprehensive feature detection libraries like Modernizr, `capability.js` provides a minimalist approach for defining and checking individual features such as `Object.create`, `Array.prototype.forEach`, or `Function.prototype.bind`. Its release cadence appears to be inactive, with no recent updates. It relies on CommonJS modules and requires a bundler like Webpack or Browserify for use in browser environments, as native ES modules were not widely supported when it was developed. This makes it less suitable for modern projects that typically use native ES modules and rely on built-in browser feature detection or more actively maintained polyfill solutions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to define, test, and enforce capabilities, including using the shorthand `capability()` and loading specific capability modules.

const capability = require('capability');

// Define a custom capability
capability.define('XMLHttpRequest', function () {
  return typeof XMLHttpRequest !== 'undefined';
});

// Test if a capability is supported
const hasObjectCreate = capability.test('Object.create');
console.log(`Object.create is supported: ${hasObjectCreate}`);

const hasCustomFeature = capability('XMLHttpRequest'); // Shorthand for capability.test
console.log(`XMLHttpRequest is supported: ${hasCustomFeature}`);

// Enforce a capability (will throw an error if not supported)
try {
  capability.check('Array.prototype.forEach');
  console.log('Array.prototype.forEach is available.');
} catch (error) {
  console.error(`Error: ${error.message}`);
}

// Load a specific capability group to enforce its requirements
try {
  require('capability/es5'); // This will run checks for all ES5 capabilities defined in the library
  console.log('Environment meets ES5 requirements (via /es5 module).');
} catch (error) {
  console.error(`Error loading ES5 capabilities: ${error.message}`);
}

view raw JSON →