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 🙏

© 2026 – Pkg Stats / Ryan Hefner

contextual-trim

v0.1.0

Published

String manipulation utilities that preserve position and context information.

Readme

contextual-trim

String manipulation utilities that preserve position and context information.

Installation

npm install contextual-trim

Features

  • Context-aware trimming: Remove whitespace while tracking what was removed and where
  • Position tracking: Know the exact position of processed strings in the original text
  • Chainable operations: All functions accept both strings and StringResult objects
  • TypeScript support: Full type definitions included

Usage

Basic Trimming

import { cTrim, cTrimLeft, cTrimRight } from 'contextual-trim';

// Trim both ends
const result = cTrim('  hello world  ');
console.log(result);
// {
//   value: 'hello world',
//   left: '  ',
//   right: '  ',
//   position: 2
// }

// Trim left only
const leftResult = cTrimLeft('  hello world  ');
console.log(leftResult);
// {
//   value: 'hello world  ',
//   left: '  ',
//   right: '',
//   position: 2
// }

// Trim right only
const rightResult = cTrimRight('  hello world  ');
console.log(rightResult);
// {
//   value: '  hello world',
//   left: '',
//   right: '  ',
//   position: 0
// }

String Splitting with Context

import { cSplit } from 'contextual-trim';

const parts = cSplit('apple,banana,cherry', ',');
console.log(parts);
// [
//   { value: 'apple', position: 0, left: '', right: ',banana,cherry' },
//   { value: 'banana', position: 6, left: 'apple,', right: ',cherry' },
//   { value: 'cherry', position: 13, left: 'apple,banana,', right: '' }
// ]

Chaining Operations

All functions accept StringResult objects, allowing you to chain operations while maintaining accurate position tracking:

import { cTrim, cSplit } from 'contextual-trim';

// Chain trim and split operations
const text = '  apple, banana, cherry  ';
const trimmed = cTrim(text);
const parts = cSplit(trimmed, ',');

console.log(parts);
// Each part maintains position relative to the original text
// [
//   { value: 'apple', position: 2, left: '  ', right: ', banana, cherry  ' },
//   { value: ' banana', position: 8, left: '  apple,', right: ', cherry  ' },
//   { value: ' cherry', position: 16, left: '  apple, banana,', right: '  ' }
// ]

// Further process each part
const cleanParts = parts.map(part => cTrim(part));
console.log(cleanParts);
// [
//   { value: 'apple', position: 2, left: '  ', right: ', banana, cherry  ' },
//   { value: 'banana', position: 9, left: '  apple, ', right: ', cherry  ' },
//   { value: 'cherry', position: 17, left: '  apple, banana, ', right: '  ' }
// ]

API Reference

Types

StringResult

interface StringResult {
  value: string;      // The processed string value
  left: string;       // Text that was to the left (removed or context)
  right: string;      // Text that was to the right (removed or context)
  position: number;   // Position in the original string
}

Functions

cTrim(text: string | StringResult): StringResult

Removes whitespace from both ends of a string.

Parameters:

  • text: The string or StringResult to trim

Returns: StringResult with trimmed value and context information


cTrimLeft(text: string | StringResult): StringResult

Removes whitespace from the left end of a string.

Parameters:

  • text: The string or StringResult to trim

Returns: StringResult with left-trimmed value and context information


cTrimRight(text: string | StringResult): StringResult

Removes whitespace from the right end of a string.

Parameters:

  • text: The string or StringResult to trim

Returns: StringResult with right-trimmed value and context information


cSplit(text: string | StringResult, delimiter: string): StringResult[]

Splits a string by delimiter while tracking positions and context.

Parameters:

  • text: The string or StringResult to split
  • delimiter: The delimiter to split by

Returns: Array of StringResult objects, one for each split part

License

MIT