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

https://github.com/hugoalh-studio/string-dissect-js

A JavaScript module to dissect the string; Safe with the emojis, URLs, and words.
https://github.com/hugoalh-studio/string-dissect-js

dissect javascript js string

Last synced: 11 months ago
JSON representation

A JavaScript module to dissect the string; Safe with the emojis, URLs, and words.

Awesome Lists containing this project

README

          

# String Dissect (JavaScript)

[**โš–๏ธ** MIT](./LICENSE.md)

**๐Ÿ—‚๏ธ**
[![GitHub: hugoalh-studio/string-dissect-js](https://img.shields.io/badge/hugoalh--studio/string--dissect--js-181717?logo=github&logoColor=ffffff&style=flat "GitHub: hugoalh-studio/string-dissect-js")](https://github.com/hugoalh-studio/string-dissect-js)
[![NPM: @hugoalh/string-dissect](https://img.shields.io/badge/@hugoalh/string--dissect-CB3837?logo=npm&logoColor=ffffff&style=flat "NPM: @hugoalh/string-dissect")](https://www.npmjs.com/package/@hugoalh/string-dissect)

**๐Ÿ†™** ![Latest Release Version](https://img.shields.io/github/release/hugoalh-studio/string-dissect-js?sort=semver&color=2187C0&label=&style=flat "Latest Release Version") (![Latest Release Date](https://img.shields.io/github/release-date/hugoalh-studio/string-dissect-js?color=2187C0&label=&style=flat "Latest Release Date"))

A JavaScript module to dissect the string; Safe with the emojis, URLs, and words.

## ๐ŸŽฏ Target

- Bun ^ v1.0.0
- Cloudflare Workers
- Deno >= v1.34.0
> **๐Ÿ›ก๏ธ Require Permission**
>
> *N/A*
- NodeJS >= v20.9.0

### ๐Ÿ”— Other Edition

- [TypeScript](https://github.com/hugoalh-studio/string-dissect-ts)

## ๐Ÿ”ฐ Usage

### Via Installation

> **๐ŸŽฏ Supported Target**
>
> - Cloudflare Workers
> - NodeJS

1. Install via console/shell/terminal:
- Via NPM
```sh
npm install @hugoalh/string-dissect[@]
```
- Via PNPM
```sh
pnpm add @hugoalh/string-dissect[@]
```
- Via Yarn
```sh
yarn add @hugoalh/string-dissect[@]
```
2. Import at the script (`.js`):
```js
import ... from "@hugoalh/string-dissect";
```
> **โ„น๏ธ Note**
>
> Although it is recommended to import the entire module, it is also able to import part of the module with sub path if available, please visit [file `package.json`](./package.json) property `exports` for available sub paths.

### Via NPM Specifier

> **๐ŸŽฏ Supported Target**
>
> - Bun
> - Deno

1. Import at the script (`.js`):
```js
import ... from "npm:@hugoalh/string-dissect[@]";
```
> **โ„น๏ธ Note**
>
> Although it is recommended to import the entire module, it is also able to import part of the module with sub path if available, please visit [file `package.json`](./package.json) property `exports` for available sub paths.

## ๐Ÿงฉ API

- ```ts
class StringDissector {
constructor(options: StringDissectorOptions = {}): StringDissector;
dissect(item: string, optionsOverride: StringDissectorOptions = {}): Generator;
dissectExtend(item: string, optionsOverride: StringDissectorOptions = {}): Generator;
static dissect(item: string, options: StringDissectorOptions = {}): Generator;
static dissectExtend(item: string, options: StringDissectorOptions = {}): Generator;
}
```
- ```ts
function dissectString(item: string, options: StringDissectorOptions = {}): Generator;
```
- ```ts
function dissectStringExtend(item: string, options: StringDissectorOptions = {}): Generator;
```
- ```ts
enum StringSegmentType {
ansi = "ansi",
ANSI = "ansi",
character = "character",
Character = "character",
emoji = "emoji",
Emoji = "emoji",
url = "url",
Url = "url",
URL = "url",
word = "word",
Word = "word"
}
```
- ```ts
interface StringDissectorOptions {
/**
* The locale(s) to use in the operation; The JavaScript implementation examines locales, and then computes a locale it understands that comes closest to satisfying the expressed preference. By default, the implementation's default locale will be used. For more information, please visit https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument.
* @default undefined
*/
locales?: StringDissectorLocales;
/**
* Whether to remove ANSI escape codes.
* @default false
*/
removeANSI?: boolean;
/**
* Whether to prevent URLs get splitted.
* @default true
*/
safeURLs?: boolean;
/**
* Whether to prevent words get splitted.
* @default true
*/
safeWords?: boolean;
}
```
- ```ts
interface StringSegmentDescriptor {
type: StringSegmentType;
value: string;
}
```
- ```ts
interface StringSegmentDescriptorExtend extends StringSegmentDescriptor {
indexEnd: number;
indexStart: number;
}
```
- ```ts
type StringDissectorLocales = ConstructorParameters[0];
```

## โœ๏ธ Example

- ```js
const sample1 = "Vel ex sit est sit est tempor enim et voluptua consetetur gubergren gubergren ut.";

/* Either */
Array.from(new StringDissector().dissect(sample1));
Array.from(dissectString(sample1));
/*=>
[
{ value: "Vel", type: "word" },
{ value: " ", type: "character" },
{ value: "ex", type: "word" },
{ value: " ", type: "character" },
{ value: "sit", type: "word" },
{ value: " ", type: "character" },
{ value: "est", type: "word" },
{ value: " ", type: "character" },
... +20
]
*/

/* Either */
Array.from(new StringDissector({ safeWords: false }).dissect(sample1));
Array.from(dissectString(sample1, { safeWords: false }));
/*=>
[
{ value: "V", type: "character" },
{ value: "e", type: "character" },
{ value: "l", type: "character" },
{ value: " ", type: "character" },
{ value: "e", type: "character" },
{ value: "x", type: "character" },
{ value: " ", type: "character" },
{ value: "s", type: "character" },
... +73
]
*/
```
- ```js
/* Either */
Array.from(new StringDissector().dissect("GitHub homepage is https://github.com."));
Array.from(dissectString("GitHub homepage is https://github.com."));
/*=>
[
{ value: "GitHub", type: "word" },
{ value: " ", type: "character" },
{ value: "homepage", type: "word" },
{ value: " ", type: "character" },
{ value: "is", type: "word" },
{ value: " ", type: "character" },
{ value: "https://github.com", type: "url" },
{ value: ".", type: "character" }
]
*/
```
- ```js
/* Either */
Array.from(new StringDissector().dissect("๐Ÿค๐Ÿ’‘๐Ÿ’๐Ÿ‘ช๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿ‘ฉโ€๐Ÿ‘ฆ๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘")).map((element) => { return element.value; });
Array.from(dissectString("๐Ÿค๐Ÿ’‘๐Ÿ’๐Ÿ‘ช๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿ‘ฉโ€๐Ÿ‘ฆ๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘")).map((element) => { return element.value; });
//=> [ "๐Ÿค", "๐Ÿ’‘", "๐Ÿ’", "๐Ÿ‘ช", "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ", "๐Ÿ‘ฉโ€๐Ÿ‘ฆ", "๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ", "๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘" ]
```