https://github.com/mkloubert/js-strings
String helpers.
https://github.com/mkloubert/js-strings
browser format function helper javascript js nodejs string stringbuilder ts typescript
Last synced: 11 months ago
JSON representation
String helpers.
- Host: GitHub
- URL: https://github.com/mkloubert/js-strings
- Owner: mkloubert
- License: mit
- Created: 2022-04-27T09:13:19.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-04-28T00:54:30.000Z (almost 4 years ago)
- Last Synced: 2025-03-07T14:38:09.593Z (12 months ago)
- Topics: browser, format, function, helper, javascript, js, nodejs, string, stringbuilder, ts, typescript
- Language: TypeScript
- Homepage: https://mkloubert.github.io/js-strings/
- Size: 89.8 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/@marcelkloubert/strings)
[](https://github.com/mkloubert/js-strings/actions?query=workflow%3APublish)
[](https://github.com/mkloubert/js-strings/pulls)
# @marcelkloubert/strings
> String helpers, for [Node.js 14+](https://nodejs.org/en/blog/release/v14.0.0/) and the browser.
## Install
Execute the following command from your project folder, where your `package.json` file is stored:
```bash
npm i @marcelkloubert/strings
```
## Usage
### asString(value: any): string
```typescript
import { asString } from "@marcelkloubert/strings"
const myObject = {
toString: () => "!!!myObject!!!",
}
asString(12) // "12"
asString("") // ""
asString(null) // ""
asString(undefined) // ""
asString(myObject) // "!!!myObject!!!"
```
### format(formatStr: string. ...args: any[]): string
```typescript
import { format } from "@marcelkloubert/strings"
format("{1}, {0}", "Marcel", "Kloubert") // "Kloubert, Marcel"
format("{1:lower,trim}, {0:upper}", "Marcel", " kloubert ") // "kloubert, MARCEL"
format("{1}, {0} Joachim", null, undefined) // "{1}, Joachim"
```
### formatArray(formatStr: string, args: List): string
```typescript
import { formatArray } from "@marcelkloubert/strings"
function* asGenerator(...args: any[]) {
for (const a of args) {
yield a
}
}
formatArray("{1}, {0}", ["Marcel", "Kloubert"]) // "Kloubert, Marcel"
formatArray("{1:lower,trim}, {0:upper}", asGenerator("Marcel", " kloubert ")) // "kloubert, MARCEL"
formatArray("{1}, {0} Joachim", [null, undefined]) // "{1}, Joachim"
```
### StringBuilder
```typescript
import { StringBuilder } from "@marcelkloubert/strings"
const str = new StringBuilder("Bar") // "Bar"
.append("Foo") // "FooBar"
.prependFormat("{1}, {0:upper,trim} ", " Marcel ", "Klbert") // "Klbert, MARCEL FooBar"
.replace("MARCEL", "Tanja") // "Klbert, Tanja FooBar"
.insert(2, "ou") // "Kloubert, Tanja FooBar"
.clear() // ""
str.isEmpty // (true)
str.equals("") // (true)
str.equals(null) // (true)
str.equals(undefined) // (true)
```
## Documentation
The API documentation can be found [here](https://mkloubert.github.io/js-strings/).
## License
MIT © [Marcel Joachim Kloubert](https://github.com/mkloubert)
## Support and contribute
[Contribution guidelines](./CONTRIBUTE.md)