Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/stdlib-js/string-base

Base (i.e., lower-level) string functions.
https://github.com/stdlib-js/string-base

base javascript node node-js nodejs stdlib str string util utilities utility utils

Last synced: about 2 months ago
JSON representation

Base (i.e., lower-level) string functions.

Awesome Lists containing this project

README

        


About stdlib...

We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.


The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.


When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.


To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!

# Base String

[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url]

> Base (i.e., lower-level) string functions.

## Installation

```bash
npm install @stdlib/string-base
```

Alternatively,

- To load the package in a website via a `script` tag without installation and bundlers, use the [ES Module][es-module] available on the [`esm`][esm-url] branch (see [README][esm-readme]).
- If you are using Deno, visit the [`deno`][deno-url] branch (see [README][deno-readme] for usage intructions).
- For use in Observable, or in browser/node environments, use the [Universal Module Definition (UMD)][umd] build available on the [`umd`][umd-url] branch (see [README][umd-readme]).

The [branches.md][branches-url] file summarizes the available branches and displays a diagram illustrating their relationships.

To view installation and usage instructions specific to each branch build, be sure to explicitly navigate to the respective README files on each branch, as linked to above.

## Usage

```javascript
var string = require( '@stdlib/string-base' );
```

#### string

Namespace containing "base" (i.e., lower-level) string functions.

```javascript
var ns = string;
// returns {...}
```

The namespace contains the following functions:

- [`altcase( str )`][@stdlib/string/base/altcase]: convert a string to alternate case.
- [`camelcase( str )`][@stdlib/string/base/camelcase]: convert a string to camel case.
- [`capitalize( str )`][@stdlib/string/base/capitalize]: capitalize the first character in a string.
- [`codePointAt( string, position, backward )`][@stdlib/string/base/code-point-at]: return a Unicode code point from a string at a specified position.
- [`constantcase( str )`][@stdlib/string/base/constantcase]: convert a string to constant case.
- [`distances`][@stdlib/string/base/distances]: implementations of various string similarity metrics.
- [`dotcase( str )`][@stdlib/string/base/dotcase]: convert a string to dot case.
- [`endsWith( str, search, len )`][@stdlib/string/base/ends-with]: test if a string ends with the characters of another string.
- [`firstCodePoint( str, n )`][@stdlib/string/base/first-code-point]: return the first `n` Unicode code points of a string.
- [`firstGraphemeCluster( str, n )`][@stdlib/string/base/first-grapheme-cluster]: return the first `n` grapheme clusters (i.e., user-perceived characters) of a string.
- [`first( str, n )`][@stdlib/string/base/first]: return the first `n` UTF-16 code units of a string.
- [`forEachCodePointRight( str, clbk[, thisArg ] )`][@stdlib/string/base/for-each-code-point-right]: invokes a function for each Unicode code point in a string, iterating from right to left.
- [`forEachCodePoint( str, clbk[, thisArg ] )`][@stdlib/string/base/for-each-code-point]: invokes a function for each Unicode code point in a string.
- [`forEachGraphemeCluster( str, clbk[, thisArg ] )`][@stdlib/string/base/for-each-grapheme-cluster]: invokes a function for each grapheme cluster (i.e., user-perceived character) in a string.
- [`forEachRight( str, clbk[, thisArg ] )`][@stdlib/string/base/for-each-right]: invokes a function for each UTF-16 code unit in a string, iterating from right to left.
- [`forEach( str, clbk[, thisArg ] )`][@stdlib/string/base/for-each]: invokes a function for each UTF-16 code unit in a string.
- [`formatInterpolate( tokens, ...args )`][@stdlib/string/base/format-interpolate]: generate string from a token array by interpolating values.
- [`formatTokenize( str )`][@stdlib/string/base/format-tokenize]: tokenize a string into an array of string parts and format identifier objects.
- [`headercase( str )`][@stdlib/string/base/headercase]: convert a string to HTTP header case.
- [`invcase( str )`][@stdlib/string/base/invcase]: convert a string to inverse case.
- [`kebabcase( str )`][@stdlib/string/base/kebabcase]: convert a string to kebab case.
- [`lastCodePoint( str, n )`][@stdlib/string/base/last-code-point]: return the last `n` Unicode code points of a string.
- [`lastGraphemeCluster( str, n )`][@stdlib/string/base/last-grapheme-cluster]: return the last `n` grapheme clusters (i.e., user-perceived characters) of a string.
- [`last( str, n )`][@stdlib/string/base/last]: return the last `n` UTF-16 code units of a string.
- [`lpad( str, len, pad )`][@stdlib/string/base/left-pad]: left pad a string.
- [`ltrim( str )`][@stdlib/string/base/left-trim]: trim whitespace characters from the beginning of a string.
- [`lowercase( str )`][@stdlib/string/base/lowercase]: convert a string to lowercase.
- [`pascalcase( str )`][@stdlib/string/base/pascalcase]: convert a string to Pascal case.
- [`percentEncode( str )`][@stdlib/string/base/percent-encode]: percent-encode a UTF-16 encoded string according to RFC 3986.
- [`removeFirstCodePoint( str, n )`][@stdlib/string/base/remove-first-code-point]: remove the first `n` Unicode code points of a string.
- [`removeFirstGraphemeCluster( str, n )`][@stdlib/string/base/remove-first-grapheme-cluster]: remove the first `n` grapheme clusters (i.e., user-perceived characters) of a string.
- [`removeFirst( str, n )`][@stdlib/string/base/remove-first]: remove the first `n` UTF-16 code units of a string.
- [`removeLastCodePoint( str, n )`][@stdlib/string/base/remove-last-code-point]: remove the last `n` Unicode code points of a string.
- [`removeLastGraphemeCluster( str, n )`][@stdlib/string/base/remove-last-grapheme-cluster]: remove the last `n` grapheme clusters (i.e., user-perceived characters) of a string.
- [`removeLast( str, n )`][@stdlib/string/base/remove-last]: remove the last `n` UTF-16 code units of a string.
- [`repeat( str, n )`][@stdlib/string/base/repeat]: repeat a string a specified number of times and return the concatenated result.
- [`replaceAfterLast( str, search, replacement, fromIndex )`][@stdlib/string/base/replace-after-last]: replace the substring after the last occurrence of a specified search string.
- [`replaceAfter( str, search, replacement, fromIndex )`][@stdlib/string/base/replace-after]: replace the substring after the first occurrence of a specified search string.
- [`replaceBeforeLast( str, search, replacement, fromIndex )`][@stdlib/string/base/replace-before-last]: replace the substring before the last occurrence of a specified search string.
- [`replaceBefore( str, search, replacement, fromIndex )`][@stdlib/string/base/replace-before]: replace the substring before the first occurrence of a specified search string.
- [`replace( str, search, newval )`][@stdlib/string/base/replace]: replace search occurrences with a replacement string.
- [`reverseCodePoints( str )`][@stdlib/string/base/reverse-code-points]: reverse the Unicode code points of a string.
- [`reverseGraphemeClusters( str )`][@stdlib/string/base/reverse-grapheme-clusters]: reverse the grapheme clusters (i.e., user-perceived characters) of a string.
- [`reverse( str )`][@stdlib/string/base/reverse]: reverse the UTF-16 code units of a string.
- [`rpad( str, len, pad )`][@stdlib/string/base/right-pad]: right pad a string.
- [`rtrim( str )`][@stdlib/string/base/right-trim]: trim whitespace characters from the end of a string.
- [`snakecase( str )`][@stdlib/string/base/snakecase]: convert a string to snake case.
- [`startcase( str )`][@stdlib/string/base/startcase]: capitalize the first letter of each word in a string.
- [`startsWith( str, search, position )`][@stdlib/string/base/starts-with]: test if a string starts with the characters of another string.
- [`stickycase( str[, p] )`][@stdlib/string/base/stickycase]: convert a string to sticky case.
- [`trim( str )`][@stdlib/string/base/trim]: trim whitespace characters from the beginning and end of a string.
- [`truncateMiddle( str, len, seq )`][@stdlib/string/base/truncate-middle]: truncate the middle UTF-16 code units of a string to return a string having a specified length.
- [`uncapitalize( str )`][@stdlib/string/base/uncapitalize]: uncapitalize the first character of a string.
- [`uppercase( str )`][@stdlib/string/base/uppercase]: convert a string to uppercase.

## Examples

```javascript
var ns = require( '@stdlib/string-base' );

// Generate a Pascal case string...
var str = ns.pascalcase( 'beep boop' );
// returns 'BeepBoop'

// Tokenize a string into an array of string parts and format identifier objects...
str = 'The %d %s foxes jumped over the %d %s dogs.';
var tokens = ns.formatTokenize( str );
// returns [ 'The ', {...}, ' ', {...}, ' foxes jumped over the ', {...}, ' ', {...}, ' dogs.' ]

// Generate a string from a token array by interpolating values...
str = ns.formatInterpolate( tokens, 3, 'quick', 4, 'lazy' );
// returns 'The 3 quick foxes jumped over the 4 lazy dogs.'

// Test whether a string starts with the characters of another string...
str = 'Lorem ipsum dolor sit amet';
var bool = ns.startsWith( str, 'Lorem' );
// returns true

// Test whether a string ends with the characters of another string...
bool = ns.endsWith( str, 'amet' );
// returns true

// Trim whitespace characters from the beginning and end of a string...
str = ' \t\n Lorem ipsum dolor sit amet \n\t ';
str = ns.trim( str );
// returns 'Lorem ipsum dolor sit amet'
```

* * *

## Notice

This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib].

#### Community

[![Chat][chat-image]][chat-url]

---

## License

See [LICENSE][stdlib-license].

## Copyright

Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].

[npm-image]: http://img.shields.io/npm/v/@stdlib/string-base.svg
[npm-url]: https://npmjs.org/package/@stdlib/string-base

[test-image]: https://github.com/stdlib-js/string-base/actions/workflows/test.yml/badge.svg?branch=main
[test-url]: https://github.com/stdlib-js/string-base/actions/workflows/test.yml?query=branch:main

[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/string-base/main.svg
[coverage-url]: https://codecov.io/github/stdlib-js/string-base?branch=main

[chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
[chat-url]: https://app.gitter.im/#/room/#stdlib-js_stdlib:gitter.im

[stdlib]: https://github.com/stdlib-js/stdlib

[stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors

[umd]: https://github.com/umdjs/umd
[es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules

[deno-url]: https://github.com/stdlib-js/string-base/tree/deno
[deno-readme]: https://github.com/stdlib-js/string-base/blob/deno/README.md
[umd-url]: https://github.com/stdlib-js/string-base/tree/umd
[umd-readme]: https://github.com/stdlib-js/string-base/blob/umd/README.md
[esm-url]: https://github.com/stdlib-js/string-base/tree/esm
[esm-readme]: https://github.com/stdlib-js/string-base/blob/esm/README.md
[branches-url]: https://github.com/stdlib-js/string-base/blob/main/branches.md

[stdlib-license]: https://raw.githubusercontent.com/stdlib-js/string-base/main/LICENSE

[@stdlib/string/base/altcase]: https://github.com/stdlib-js/string-base-altcase

[@stdlib/string/base/camelcase]: https://github.com/stdlib-js/string-base-camelcase

[@stdlib/string/base/capitalize]: https://github.com/stdlib-js/string-base-capitalize

[@stdlib/string/base/code-point-at]: https://github.com/stdlib-js/string-base-code-point-at

[@stdlib/string/base/constantcase]: https://github.com/stdlib-js/string-base-constantcase

[@stdlib/string/base/distances]: https://github.com/stdlib-js/string-base-distances

[@stdlib/string/base/dotcase]: https://github.com/stdlib-js/string-base-dotcase

[@stdlib/string/base/ends-with]: https://github.com/stdlib-js/string-base-ends-with

[@stdlib/string/base/first-code-point]: https://github.com/stdlib-js/string-base-first-code-point

[@stdlib/string/base/first-grapheme-cluster]: https://github.com/stdlib-js/string-base-first-grapheme-cluster

[@stdlib/string/base/first]: https://github.com/stdlib-js/string-base-first

[@stdlib/string/base/for-each-code-point-right]: https://github.com/stdlib-js/string-base-for-each-code-point-right

[@stdlib/string/base/for-each-code-point]: https://github.com/stdlib-js/string-base-for-each-code-point

[@stdlib/string/base/for-each-grapheme-cluster]: https://github.com/stdlib-js/string-base-for-each-grapheme-cluster

[@stdlib/string/base/for-each-right]: https://github.com/stdlib-js/string-base-for-each-right

[@stdlib/string/base/for-each]: https://github.com/stdlib-js/string-base-for-each

[@stdlib/string/base/format-interpolate]: https://github.com/stdlib-js/string-base-format-interpolate

[@stdlib/string/base/format-tokenize]: https://github.com/stdlib-js/string-base-format-tokenize

[@stdlib/string/base/headercase]: https://github.com/stdlib-js/string-base-headercase

[@stdlib/string/base/invcase]: https://github.com/stdlib-js/string-base-invcase

[@stdlib/string/base/kebabcase]: https://github.com/stdlib-js/string-base-kebabcase

[@stdlib/string/base/last-code-point]: https://github.com/stdlib-js/string-base-last-code-point

[@stdlib/string/base/last-grapheme-cluster]: https://github.com/stdlib-js/string-base-last-grapheme-cluster

[@stdlib/string/base/last]: https://github.com/stdlib-js/string-base-last

[@stdlib/string/base/left-pad]: https://github.com/stdlib-js/string-base-left-pad

[@stdlib/string/base/left-trim]: https://github.com/stdlib-js/string-base-left-trim

[@stdlib/string/base/lowercase]: https://github.com/stdlib-js/string-base-lowercase

[@stdlib/string/base/pascalcase]: https://github.com/stdlib-js/string-base-pascalcase

[@stdlib/string/base/percent-encode]: https://github.com/stdlib-js/string-base-percent-encode

[@stdlib/string/base/remove-first-code-point]: https://github.com/stdlib-js/string-base-remove-first-code-point

[@stdlib/string/base/remove-first-grapheme-cluster]: https://github.com/stdlib-js/string-base-remove-first-grapheme-cluster

[@stdlib/string/base/remove-first]: https://github.com/stdlib-js/string-base-remove-first

[@stdlib/string/base/remove-last-code-point]: https://github.com/stdlib-js/string-base-remove-last-code-point

[@stdlib/string/base/remove-last-grapheme-cluster]: https://github.com/stdlib-js/string-base-remove-last-grapheme-cluster

[@stdlib/string/base/remove-last]: https://github.com/stdlib-js/string-base-remove-last

[@stdlib/string/base/repeat]: https://github.com/stdlib-js/string-base-repeat

[@stdlib/string/base/replace-after-last]: https://github.com/stdlib-js/string-base-replace-after-last

[@stdlib/string/base/replace-after]: https://github.com/stdlib-js/string-base-replace-after

[@stdlib/string/base/replace-before-last]: https://github.com/stdlib-js/string-base-replace-before-last

[@stdlib/string/base/replace-before]: https://github.com/stdlib-js/string-base-replace-before

[@stdlib/string/base/replace]: https://github.com/stdlib-js/string-base-replace

[@stdlib/string/base/reverse-code-points]: https://github.com/stdlib-js/string-base-reverse-code-points

[@stdlib/string/base/reverse-grapheme-clusters]: https://github.com/stdlib-js/string-base-reverse-grapheme-clusters

[@stdlib/string/base/reverse]: https://github.com/stdlib-js/string-base-reverse

[@stdlib/string/base/right-pad]: https://github.com/stdlib-js/string-base-right-pad

[@stdlib/string/base/right-trim]: https://github.com/stdlib-js/string-base-right-trim

[@stdlib/string/base/snakecase]: https://github.com/stdlib-js/string-base-snakecase

[@stdlib/string/base/startcase]: https://github.com/stdlib-js/string-base-startcase

[@stdlib/string/base/starts-with]: https://github.com/stdlib-js/string-base-starts-with

[@stdlib/string/base/stickycase]: https://github.com/stdlib-js/string-base-stickycase

[@stdlib/string/base/trim]: https://github.com/stdlib-js/string-base-trim

[@stdlib/string/base/truncate-middle]: https://github.com/stdlib-js/string-base-truncate-middle

[@stdlib/string/base/uncapitalize]: https://github.com/stdlib-js/string-base-uncapitalize

[@stdlib/string/base/uppercase]: https://github.com/stdlib-js/string-base-uppercase