or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-util-deprecate

Cross-platform implementation of Node.js util.deprecate() function with browser support

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/util-deprecate@1.0.x

To install, run

npx @tessl/cli install tessl/npm-util-deprecate@1.0.0

index.mddocs/

util-deprecate

The Node.js util.deprecate() function with browser support. Provides a cross-platform implementation for marking functions as deprecated with configurable warning behavior.

Package Information

  • Package Name: util-deprecate
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install util-deprecate

Core Imports

const deprecate = require('util-deprecate');

For ES modules (using a bundler like webpack or browserify):

import deprecate from 'util-deprecate';

Basic Usage

const deprecate = require('util-deprecate');

// Mark a function as deprecated
const oldFunction = function() {
  return 'This function works but is deprecated';
};

// Create a deprecated version with a custom warning message
const deprecatedFunction = deprecate(
  oldFunction, 
  'oldFunction() is deprecated, use newFunction() instead'
);

// Export the deprecated function
exports.oldFunction = deprecatedFunction;

// When users call the function, they'll see the warning once:
// deprecatedFunction(); // Logs: "oldFunction() is deprecated, use newFunction() instead"
// deprecatedFunction(); // No warning on subsequent calls

Architecture

util-deprecate uses a dual-implementation approach to provide cross-platform compatibility:

  • Node.js Environment: Directly uses the native util.deprecate() function from Node.js core, ensuring full compatibility with Node.js deprecation system and environment variables
  • Browser Environment: Provides a custom implementation that mimics Node.js behavior while safely handling browser-specific constraints like localStorage access and sandboxed environments
  • Automatic Selection: The appropriate implementation is selected automatically via package.json browser field when using bundlers like browserify or webpack

Capabilities

Function Deprecation

Mark functions as deprecated with customizable warning messages and behavior control through environment configuration.

/**
 * Mark that a method should not be used.
 * Returns a modified function which warns once by default.
 * 
 * @param {Function} fn - the function to deprecate
 * @param {String} msg - the string to print to the console when fn is invoked
 * @returns {Function} a new "deprecated" version of fn
 */
function deprecate(fn, msg);

Parameters:

  • fn (Function): The function to deprecate
  • msg (String): The warning message to display when the deprecated function is invoked

Returns:

  • Function: A new "deprecated" wrapper version of the original function that:
    • Executes the original function with the same arguments and context
    • Shows a deprecation warning only on the first invocation
    • Respects configuration flags for warning behavior

Warning Behavior:

The deprecation warning behavior can be controlled through different mechanisms depending on the environment:

Node.js Environment:

  • Uses Node.js built-in util.deprecate() function
  • Respects standard Node.js environment variables and process configuration

Browser Environment:

  • Uses localStorage configuration flags:
    • localStorage.noDeprecation = 'true': Disables all deprecation warnings
    • localStorage.throwDeprecation = 'true': Throws an Error instead of showing a warning
    • localStorage.traceDeprecation = 'true': Uses console.trace() to show stack trace instead of console.warn()

Usage Examples:

const deprecate = require('util-deprecate');

// Simple deprecation
const oldApi = deprecate(
  function(data) { return data.toUpperCase(); },
  'oldApi() is deprecated, use newApi() instead'
);

// Deprecate a class method
class MyClass {
  constructor() {
    this.newMethod = this.newMethod.bind(this);
    this.oldMethod = deprecate(
      this.oldMethod.bind(this),
      'MyClass.oldMethod() is deprecated, use MyClass.newMethod() instead'
    );
  }
  
  newMethod(data) {
    return data.toLowerCase();
  }
  
  oldMethod(data) {
    return this.newMethod(data);
  }
}

// Configure warning behavior in browser
if (typeof localStorage !== 'undefined') {
  // Disable all deprecation warnings
  localStorage.noDeprecation = 'true';
  
  // Or throw errors instead of warnings
  localStorage.throwDeprecation = 'true';
  
  // Or show stack traces
  localStorage.traceDeprecation = 'true';
}

Environment-Specific Implementation

The package provides different implementations based on the runtime environment:

Node.js (node.js):

  • Re-exports the native util.deprecate function from Node.js core
  • Full compatibility with Node.js deprecation system
  • Supports all Node.js deprecation environment variables and configuration

Browser (browser.js):

  • Custom implementation for browser compatibility via browserify
  • Safe localStorage access with error handling for sandboxed environments
  • Configurable warning output: console.warn(), console.trace(), or thrown Error
  • Once-only warning behavior maintained across function calls

The appropriate implementation is automatically selected based on the environment when using a bundler like browserify or webpack that respects the browser field in package.json.