npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@remotex-labs/xmap

v1.0.3

Published

A library with a sourcemap parser and TypeScript code formatter for the CLI

Downloads

336

Readme

xMap

Description

xMap is a library designed to help with sourcemap parsing and TypeScript code formatting for the CLI.
It includes components for parsing error stack traces, formatting code, and providing syntax highlighting, as well as a service for handling source maps.

Installation

To install the package, use npm or yarn:

npm install @remotex-labs/xmap

or

yarn add @remotex-labs/xmap

Usage

Parsing Error Stack Traces

The parseErrorStack function parses an error stack trace and returns an array of stack entries.
Each entry contains information about the function call, file, line number, column number, and if applicable, details about the eval context.

Example

import { parseErrorStack } from '@remotex-labs/xmap';

// Example stack trace string
const stackTrace = `
Error: Example error
    at Object.<anonymous> (/path/to/file.js:10:15)
    at Module._compile (node:internal/modules/cjs/loader:1217:14)
    at node:internal/modules/cjs/loader:1308:14
    at node:internal/modules/cjs/loader:1425:5
    at node:internal/modules/cjs/loader:1425:5
    at node:internal/modules/cjs/loader:1483:3
    at node:internal/modules/cjs/loader:1700:8
    at node:internal/modules/cjs/loader:1760:3
    at /path/to/file.js:10:15
    at Object.<anonymous> (/path/to/file.js:10:15)
    at eval (eval at <anonymous> (/path/to/file.js:10:15), <anonymous>:1:1)
`;

// Parsing the stack trace
const parsedStack = parseErrorStack(stackTrace);
console.log(parsedStack);

Formatting Code

The formatCode function formats a code snippet with optional line padding and custom actions.
It applies padding to line numbers and can trigger custom actions for specific lines.

Function Signature

export function formatCode(code: string, options: FormatCodeInterface = {}): string;

Parameters

  • code (string): The source code or stack to be formatted.
  • options (FormatCodeInterface, optional): Configuration options for formatting the code.
    • padding (number, optional): Number of characters for line number padding. Defaults to 10.
    • startLine (number, optional): The starting line number for formatting. Defaults to 1.
    • action (object, optional): Custom actions to apply to specific lines.
    • triggerLine (number): The line number where the action should be triggered.
    • callback (function): A callback function to format the line string when triggerLine is matched. The callback receives the formatted line string, the padding value, and the current line number as arguments.
import { formatCode } from '@remotex-labs/xmap';

// Example TypeScript code
const code = `
function helloWorld() {
    console.log('Hello, world!');
}
`;

// Formatting the code
const formattedCode = formatCode(code, {
    padding: 8,
    startLine: 5,
    action: {
        triggerLine: 7,
        callback: (lineString, padding, lineNumber) => {
            return `Custom formatting for line ${lineNumber}: ${lineString}`;
        }
    }
});
console.log(formattedCode);
`;

// Formatting the code
const formattedCode = formatCode(code);
console.log(formattedCode);

Formatting Code with Error Location

The formatErrorCode function formats a code snippet around an error location with special highlighting.
It highlights the relevant code snippet around the error location, including special formatting for the error line and column.

Function Signature

export function formatErrorCode(sourcePosition: PositionSourceInterface, ansiOption?: AnsiOptionInterface): string;

Example

import { formatErrorCode } from '@remotex-labs/xmap';

// Example source position
const sourcePosition = {
    code: `
function helloWorld() {
    console.log('Hello, world!');
}
`,
    line: 2,
    column: 15,
    startLine: 1
};

// Optional ANSI color configuration
const ansiOption = {
    color: '\x1b[38;5;160m', // Red color
    reset: '\x1b[0m' // Reset color
};

// Formatting the error code
const formattedErrorCode = formatErrorCode(sourcePosition, ansiOption);
console.log(formattedErrorCode);

Highlighting Syntax

To apply syntax highlighting to TypeScript or JavaScript code, use the highlighter component:

import { highlightCode } from '@remotex-labs/xmap';

// Example TypeScript code
const code = `
function helloWorld() {
    console.log('Hello, world!');
}
`;

// Highlighting the code
const highlightedCode = highlightCode(code);
console.log(highlightedCode);