CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-inferno-shared

Helpers functions for Inferno

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

Inferno Shared

Inferno Shared provides essential utility functions for the Inferno React-like library ecosystem. This zero-dependency package offers type guards, validation functions, error handling utilities, and component helper functions designed for maximum performance and reusability within the Inferno framework.

Package Information

  • Package Name: inferno-shared
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install inferno-shared

Core Imports

import { isArray, isString, isNumber, isFunction, isNull, isUndefined, isNullOrUndef, isInvalid, isStringOrNumber, throwError, warning, hoistStaticProperties } from "inferno-shared";

For CommonJS:

const { isArray, isString, isNumber, isFunction, isNull, isUndefined, isNullOrUndef, isInvalid, isStringOrNumber, throwError, warning, hoistStaticProperties } = require("inferno-shared");

Basic Usage

import { isString, isNumber, throwError, hoistStaticProperties } from "inferno-shared";

// Type checking
if (isString(userInput)) {
  // TypeScript knows userInput is string here
  console.log(userInput.toUpperCase());
}

if (isNumber(value)) {
  // TypeScript knows value is number here
  console.log(value.toFixed(2));
}

// Error handling
if (invalidCondition) {
  throwError("Custom error message");
}

// Component utilities
class MyComponent {}
MyComponent.displayName = "MyComponent";

class ExtendedComponent {}
hoistStaticProperties(ExtendedComponent, MyComponent);
// ExtendedComponent now has displayName property

Capabilities

Type Guard Functions

Runtime type checking functions with TypeScript type narrowing support.

/**
 * Checks if value is an array (alias for Array.isArray)
 */
const isArray: (arg: any) => arg is any[];

/**
 * Type guard to check if value is string or number
 * @param o - Value to check
 * @returns true if value is string or number
 */
function isStringOrNumber(o: unknown): o is string | number;

/**
 * Type guard to check if value is null or undefined
 * @param o - Value to check  
 * @returns true if value is null or undefined
 */
function isNullOrUndef(o: unknown): o is undefined | null;

/**
 * Type guard to check if value is invalid (null, boolean, or undefined)
 * @param o - Value to check
 * @returns true if value is null, true, false, or undefined
 */
function isInvalid(o: unknown): o is null | boolean | undefined;

/**
 * Type guard to check if value is a function
 * @param o - Value to check
 * @returns true if value is a function
 */
function isFunction(o: unknown): o is Function;

/**
 * Type guard to check if value is a string
 * @param o - Value to check
 * @returns true if value is a string
 */
function isString(o: unknown): o is string;

/**
 * Type guard to check if value is a number
 * @param o - Value to check
 * @returns true if value is a number
 */
function isNumber(o: unknown): o is number;

/**
 * Type guard to check if value is null
 * @param o - Value to check
 * @returns true if value is null
 */
function isNull(o: unknown): o is null;

/**
 * Type guard to check if value is undefined
 * @param o - Value to check
 * @returns true if value is undefined
 */
function isUndefined(o: unknown): o is undefined;

Error Handling

Error throwing and warning utilities with standardized Inferno formatting.

/**
 * Default error message constant for runtime errors
 */
const ERROR_MSG: string;

/**
 * Throws an error with Inferno-specific formatting
 * @param message - Optional custom error message. Defaults to ERROR_MSG if not provided
 * @throws Error with message prefixed by "Inferno Error: "
 */
function throwError(message?: string): void;

/**
 * Outputs a warning message to console
 * @param message - Warning message to display
 */
function warning(message: string): void;

Component Utilities

Helper functions for component property management and static property copying.

/**
 * Copies static properties from source component to target component
 * Skips standard React/component properties like displayName, propTypes, etc.
 * @param targetComponent - Component to receive the static properties
 * @param sourceComponent - Component from which to copy static properties
 */
function hoistStaticProperties(
  targetComponent: any,
  sourceComponent: any,
): void;

Usage Examples

Type Guards with TypeScript

import { isString, isNumber, isArray, isFunction, isNullOrUndef } from "inferno-shared";

function processUserInput(input: unknown) {
  if (isString(input)) {
    // TypeScript knows input is string
    return input.trim().toLowerCase();
  }
  
  if (isNumber(input)) {
    // TypeScript knows input is number
    return input.toString();
  }
  
  if (isArray(input)) {
    // TypeScript knows input is array
    return input.join(', ');
  }
  
  if (isNullOrUndef(input)) {
    return "No value provided";
  }
  
  return "Unknown type";
}

// Runtime validation
function safeExecute(fn: unknown, ...args: any[]) {
  if (isFunction(fn)) {
    // TypeScript knows fn is Function
    return fn(...args);
  }
  throw new Error("Expected a function");
}

Error Handling

import { throwError, warning, ERROR_MSG } from "inferno-shared";

function validateAge(age: number) {
  if (age < 0) {
    throwError("Age cannot be negative");
    // Throws: Error: Inferno Error: Age cannot be negative
  }
  
  if (age > 150) {
    warning("Age seems unusually high");
    // Logs warning to console.error
  }
}

// Using default error message
function criticalFailure() {
  throwError(); // Uses ERROR_MSG constant
  // Throws: Error: Inferno Error: a runtime error occured! Use Inferno in development environment to find the error.
}

Component Property Hoisting

import { hoistStaticProperties } from "inferno-shared";

// Source component with static properties
function OriginalComponent() {
  return <div>Original</div>;
}
OriginalComponent.displayName = "OriginalComponent";
OriginalComponent.propTypes = { /* ... */ };
OriginalComponent.customStatic = "custom value";

// Target component
function EnhancedComponent() {
  return <div>Enhanced</div>;
}

// Copy static properties (excluding known React statics)
hoistStaticProperties(EnhancedComponent, OriginalComponent);

// EnhancedComponent now has:
// - customStatic: "custom value" ✓ (copied)
// - displayName, propTypes are NOT copied (filtered out)

console.log(EnhancedComponent.customStatic); // "custom value"

Validation Patterns

import { isInvalid, isStringOrNumber, isNullOrUndef } from "inferno-shared";

interface UserData {
  id: string | number;
  name?: string;
  email?: string;
}

function validateUserData(data: any): UserData | null {
  // Check for completely invalid values
  if (isInvalid(data) || typeof data !== 'object') {
    return null;
  }
  
  // Validate required ID field
  if (!isStringOrNumber(data.id)) {
    return null;
  }
  
  // Handle optional fields
  const result: UserData = { id: data.id };
  
  if (!isNullOrUndef(data.name) && isString(data.name)) {
    result.name = data.name;
  }
  
  if (!isNullOrUndef(data.email) && isString(data.email)) {
    result.email = data.email;
  }
  
  return result;
}

docs

index.md

tile.json