
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
string-left-right
Advanced tools
Looks up the first non-whitespace character to the left/right of a given index
The string-left-right npm package provides utilities for trimming whitespace from the left and right sides of strings, as well as other string manipulation functionalities.
trimLeft
The trimLeft function removes whitespace from the left side of a string.
const { trimLeft } = require('string-left-right');
const result = trimLeft(' Hello World! ');
console.log(result); // 'Hello World! '
trimRight
The trimRight function removes whitespace from the right side of a string.
const { trimRight } = require('string-left-right');
const result = trimRight(' Hello World! ');
console.log(result); // ' Hello World!'
trim
The trim function removes whitespace from both sides of a string.
const { trim } = require('string-left-right');
const result = trim(' Hello World! ');
console.log(result); // 'Hello World!'
left
The left function returns the index of the first non-whitespace character from the left.
const { left } = require('string-left-right');
const result = left(' Hello World! ');
console.log(result); // 3
right
The right function returns the index of the first non-whitespace character from the right.
const { right } = require('string-left-right');
const result = right(' Hello World! ');
console.log(result); // 15
Lodash is a popular utility library that provides a wide range of functions for manipulating arrays, objects, and strings. It includes functions like _.trim, _.trimStart, and _.trimEnd which are similar to the trim, trimLeft, and trimRight functions in string-left-right.
Underscore is another utility library similar to Lodash. It provides a variety of functions for working with arrays, objects, and strings. The _.trim function in Underscore is comparable to the trim function in string-left-right.
Validator is a library for string validation and sanitization. It includes functions like trim, ltrim, and rtrim which are similar to the trim, trimLeft, and trimRight functions in string-left-right. However, Validator focuses more on validation and sanitization rather than general string manipulation.
Looks up the first non-whitespace character to the left/right of a given index
This package is pure ESM. If you're not ready yet, install an older version of this program, 4.1.0 (npm i string-left-right@4.1.0
).
npm i string-left-right
import { strict as assert } from "assert";
import {
left,
right,
leftSeq,
rightSeq,
chompLeft,
chompRight,
leftStopAtNewLines,
rightStopAtNewLines,
} from "string-left-right";
// get the closest non-whitespace character to the left of "d" (which itself
// is at string index 6)
const str = "abc def";
// | |
// 012345678
assert.equal(
`next non-whitespace character to the left of ${str[6]} (index 6) is ${
str[left(str, 6)]
} (index ${left(str, 6)})`,
"next non-whitespace character to the left of d (index 6) is c (index 2)",
);
Please visit codsen.com for a full description of the API.
To report bugs or request features or assistance, raise an issue on GitHub.
MIT License.
Copyright © 2010-2025 Roy Revelt and other contributors.
FAQs
Looks up the first non-whitespace character to the left/right of a given index
The npm package string-left-right receives a total of 119,449 weekly downloads. As such, string-left-right popularity was classified as popular.
We found that string-left-right demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.