CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-autorest--common

AutoRest common utilities for logging, configuration merging, exceptions, and styling

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

utils.mddocs/

Utility Functions

General-purpose utilities for type checking, array manipulation, and terminal text styling with markdown-like syntax support.

Capabilities

Type Checking Utilities

Type guard functions for safe type checking and filtering.

isDefined

Type guard to check if a value is not null or undefined.

function isDefined<T>(value: T | undefined | null): value is T;

Usage Examples:

import { isDefined } from "@autorest/common";

// Filter out null and undefined values
const items = [1, null, 3, undefined, 5, 0, "", false];
const definedItems = items.filter(isDefined); 
// Result: [1, 3, 5, 0, "", false] - only null and undefined are filtered

// Type-safe array processing
const maybeNumbers: (number | undefined)[] = [1, 2, undefined, 4];
const numbers: number[] = maybeNumbers.filter(isDefined);
// TypeScript knows numbers is number[], not (number | undefined)[]

// Safe property access
function processUser(user: { name?: string; age?: number }) {
  if (isDefined(user.name)) {
    // TypeScript knows user.name is string here
    console.log(`User name: ${user.name.toUpperCase()}`);
  }
  
  if (isDefined(user.age)) {
    // TypeScript knows user.age is number here
    console.log(`User age: ${user.age + 1}`);
  }
}

Array Manipulation Utilities

Utilities for converting values between different array representations.

arrayify

Converts a single value, array, or undefined to an array of values.

function arrayify<T>(value: T | T[] | undefined): T[];

Usage Examples:

import { arrayify } from "@autorest/common";

// Convert single values to arrays
arrayify("hello"); // ["hello"]
arrayify(42); // [42]
arrayify({ key: "value" }); // [{ key: "value" }]

// Arrays pass through unchanged
arrayify(["a", "b", "c"]); // ["a", "b", "c"]
arrayify([1, 2, 3]); // [1, 2, 3]

// undefined becomes empty array
arrayify(undefined); // []

// Practical usage in configuration
interface Config {
  plugins?: string | string[];
  include?: string | string[];
}

function normalizeConfig(config: Config) {
  return {
    plugins: arrayify(config.plugins),
    include: arrayify(config.include)
  };
}

const config1 = { plugins: "single-plugin" };
const config2 = { plugins: ["plugin1", "plugin2"] };
const config3 = { plugins: undefined };

console.log(normalizeConfig(config1)); // { plugins: ["single-plugin"], include: [] }
console.log(normalizeConfig(config2)); // { plugins: ["plugin1", "plugin2"], include: [] }
console.log(normalizeConfig(config3)); // { plugins: [], include: [] }

Text Styling Utilities

Terminal text formatting with markdown-like syntax support using chalk.

color

Applies chalk-based coloring to text with markdown-like syntax support.

function color(text: string): string;

Supported Syntax:

  • [text] - Yellow bold
  • **text** - Bold
  • # text - Green bright (header)
  • ## text - Green (subheader)
  • ### text - Cyan bright (subsubheader)
  • URLs - Blue bold underlined
  • _text_ - Italic
  • > text - Cyan blockquote
  • ! text - Red bold warning
  • code - Gray code blocks
  • code - Gray inline code
  • "text" or 'text' - Gray quoted strings

Usage Examples:

import { color } from "@autorest/common";

// Basic styling
console.log(color("Processing **complete** successfully"));
console.log(color("Warning: [deprecated] function used"));
console.log(color("Error: `validateConfig` failed"));

// Headers and structure
console.log(color("# AutoRest Code Generation"));
console.log(color("## Configuration Processing"));
console.log(color("### Plugin Execution"));

// Code and quotes
console.log(color("Run `npm install` to get started"));
console.log(color("Set the 'debug' option to true"));
console.log(color("Check ```/src/config.json``` for settings"));

// URLs and links
console.log(color("Visit https://github.com/Azure/autorest for docs"));

// Warnings and blockquotes
console.log(color("! This operation cannot be undone"));
console.log(color("> Note: Configuration will be merged automatically"));

// Combined styling
const message = color(`
# Processing Results
- Status: **Success**
- Time: \`2.3s\`
- Config: '/path/to/config.json'
- Details: > See log for more information
`);
console.log(message);

Combined Utility Usage

import { isDefined, arrayify, color } from "@autorest/common";

function processConfiguration(configs: (Config | undefined)[]) {
  // Filter out undefined configs and flatten arrays
  const validConfigs = configs
    .filter(isDefined)
    .map(config => ({
      ...config,
      plugins: arrayify(config.plugins),
      includes: arrayify(config.includes)
    }));
  
  console.log(color(`Found **${validConfigs.length}** valid configurations`));
  
  validConfigs.forEach((config, index) => {
    console.log(color(`## Configuration ${index + 1}`));
    console.log(color(`- Plugins: \`${config.plugins.join(', ')}\``));
    console.log(color(`- Includes: \`${config.includes.join(', ')}\``));
  });
  
  return validConfigs;
}

// Usage
const configs = [
  { plugins: "swagger", includes: "src/" },
  undefined,
  { plugins: ["openapi", "typescript"], includes: ["lib/", "dist/"] },
  null
];

const processed = processConfiguration(configs);

Type Definitions

// Type guard return type for isDefined
function isDefined<T>(value: T | undefined | null): value is T;

// Arrayify return type - always returns an array
function arrayify<T>(value: T | T[] | undefined): T[];

// Color function processes strings and returns styled strings
function color(text: string): string;

docs

exceptions.md

index.md

logging.md

merging.md

utils.md

tile.json