Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/richie-south/javascript-rich-string-parser

Find tokens in your strings
https://github.com/richie-south/javascript-rich-string-parser

parser string-parser string-parsing

Last synced: about 5 hours ago
JSON representation

Find tokens in your strings

Awesome Lists containing this project

README

        

# rich-string-parser

Finds rich text in strings, ex: links, mentions, emails, your own parser, in an non overlapping way to prevent dubble matching.

**Example**

```typescript
import {richStringParser} from 'rich-string-parser'
import {emailParser} from 'rich-string-parser/lib/parsers/email-parser'
import {linkParser} from 'rich-string-parser/lib/parsers/link-parser'

const result = richStringParser(
'https://www.typescriptlang.org/ text [email protected] more text',
[emailParser(), linkParser()],
)

// log result
/* [
{
type: 'LinkParser',
match: 'https://www.typescriptlang.org/',
index: 0,
subIndex: 0
},
' text ',
{
type: 'EmailParser',
match: '[email protected]',
index: 37,
subIndex: 37
},
' more text'
] */
```

## Parser result

required parameters

```jsonc
{
"type": "", // uniq for each parser
"match": "", // result from parser
"index": 0, // where match is found based on whole string
"subIndex": 0 // where match is found based on parsed substring
}
```

## Built in parsers

**MentionParser**

Matches on `@(number|string)`, @(9712|John)

Result:

Without mention type:

```typescript
{
type: 'MentionParser',
match: '@(9712|John)',
id: 9712,
name: 'John',
index: 0,
subIndex: 0,
target: undefined
}
```

With mention type provided:

```typescript
{
type: 'MentionParser',
match: '@(0|Testgroupchatroom|group)',
id: 0,
name: 'Testgroupchatroom',
index: 0,
subIndex: 0,
target: 'group'
}
```

**EmailParser**

Matches on `[email protected]`,

Result:

```typescript
{
type: 'EmailParser',
match: '[email protected]',
index: 0,
subIndex: 0,
}
```

**LinkParser**

Matches on `https://example.com`,

Result:

```typescript
{
type: 'LinkParser',
match: 'https://example.com',
index: 0,
subIndex: 0,
}
```

## Create your own parser

Create a function that retrurns a `Parser` interface with a custom string in generic type, example `Parser<'HashtagParser'>`.
Implemente the required return object function `parse`.

**Example**

```typescript
function hashtagParser(): Parser<'HashtagParser'> {
const regex: RegExp = /(#[a-z\d-]+)/gi

return {
parse: (text, index) => {
const result = regex.exec(text)
if (result === null) return null

return {
type: 'HashtagParser',
match: result[0],
index: index + result.index, // don't forget to add the global index
subIndex: result.index,
}
},
}
}
```