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.
- Host: GitHub
- URL: https://github.com/hugoalh-studio/string-dissect-js
- Owner: hugoalh-studio
- License: other
- Created: 2023-03-07T08:00:28.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-31T06:55:09.000Z (about 2 years ago)
- Last Synced: 2024-03-31T07:37:53.169Z (about 2 years ago)
- Topics: dissect, javascript, js, string
- Language: TypeScript
- Homepage:
- Size: 264 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Citation: CITATION.cff
- Codeowners: .github/CODEOWNERS
- Security: SECURITY.md
Awesome Lists containing this project
README
# String Dissect (JavaScript)
[**โ๏ธ** MIT](./LICENSE.md)
**๐๏ธ**
[](https://github.com/hugoalh-studio/string-dissect-js)
[](https://www.npmjs.com/package/@hugoalh/string-dissect)
**๐**  ()
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; });
//=> [ "๐ค", "๐", "๐", "๐ช", "๐จโ๐ฉโ๐งโ๐ฆ", "๐ฉโ๐ฆ", "๐ฉโ๐งโ๐ฆ", "๐งโ๐คโ๐ง" ]
```