@hugoalh/string-dissect
v4.0.3
Published
A module to dissect the string; Safe with the emojis, URLs, and words.
Downloads
259
Readme
String Dissect (ES)
🔗 DistBoard @hugoalh ● GitHub ● JSR ● NPM
An ECMAScript module to dissect the string; Safe with the emojis, URLs, and words.
🎯 Runtime Targets
Any runtime which support ECMAScript should able to use this; These runtimes are officially supported:
🛡️ Runtime Permissions
This does not request any runtime permission.
#️⃣ Sources & Entrypoints
- GitHub Raw
https://raw.githubusercontent.com/hugoalh/string-dissect-es/{Tag}/mod.ts - JSR
jsr:@hugoalh/string-dissect[@{Tag}] - NPM
npm:@hugoalh/string-dissect[@{Tag}]
| Name | Path | Description |
|:--|:--|:--|
| . | ./mod.ts | Default. |
[!NOTE]
- Different runtimes have vary support for the sources and entrypoints, visit the runtime documentation for more information.
- It is recommended to include tag for immutability.
- These are not part of the public APIs hence should not be used:
- Benchmark/Test file (e.g.:
example.bench.ts,example.test.ts).- Entrypoint name or path include any underscore prefix (e.g.:
_example.ts,foo/_example.ts).- Identifier/Namespace/Symbol include any underscore prefix (e.g.:
_example,Foo._example).
🧩 APIs
class StringDissector { constructor(options?: StringDissectorOptions); dissect(item: string): Generator<StringSegmentDescriptor>; }interface StringDissectorOptions { locales?: Intl.LocalesArgument; outputANSI?: boolean; safeURLs?: boolean; safeWords?: boolean; }interface StringSegmentDescriptor { indexEnd: number; indexStart: number; type: StringSegmentType; value: string; }type StringSegmentType = | "ansi" | "character" | "emoji" | "url" | "word";
[!NOTE]
- For the full or prettier documentation, can visit via:
✍️ Examples
const sample1 = "Vel ex sit est sit est tempor enim et voluptua consetetur gubergren gubergren ut."; Array.from(new StringDissector().dissect(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 // ] Array.from(new StringDissector({ safeWords: false }).dissect(sample1)); //=> // [ // { 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 // ]Array.from(new StringDissector().dissect("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" } // ]Array.from(new StringDissector().dissect("🤝💑💏👪👨👩👧👦👩👦👩👧👦🧑🤝🧑"), ({ value }) => { return value; }); //=> [ "🤝", "💑", "💏", "👪", "👨👩👧👦", "👩👦", "👩👧👦", "🧑🤝🧑" ]
