-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
25 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,132 +1,37 @@ | ||
<h1 align="center">match-to-token</h1> | ||
<h1> | ||
match-to-token <a href="https://npmjs.org/package/match-to-token"><img src="https://img.shields.io/badge/npm-v3.0.0-F00.svg?colorA=000"/></a> <a href="src"><img src="https://img.shields.io/badge/loc-54-FFF.svg?colorA=000"/></a> <a href="https://cdn.jsdelivr.net/npm/match-to-token@3.0.0/dist/match-to-token.min.js"><img src="https://img.shields.io/badge/brotli-421b-333.svg?colorA=000"/></a> <a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-F0B.svg?colorA=000"/></a> | ||
</h1> | ||
|
||
<p align="center"> | ||
transform a RegExp named group match to a more useful object | ||
</p> | ||
|
||
<p align="center"> | ||
<a href="#install">🔧 <strong>Install</strong></a> | ||
· <a href="#example">🧩 <strong>Example</strong></a> | ||
· <a href="#api">📜 <strong>API docs</strong></a> | ||
· <a href="https://github.com/stagas/match-to-token/releases">🔥 <strong>Releases</strong></a> | ||
· <a href="#contribute">💪🏼 <strong>Contribute</strong></a> | ||
· <a href="https://github.com/stagas/match-to-token/issues">🖐️ <strong>Help</strong></a> | ||
</p> | ||
|
||
--- | ||
|
||
## Install | ||
|
||
```sh | ||
$ npm i match-to-token | ||
``` | ||
|
||
## What it does | ||
|
||
It picks up the first matching named group from a RegExp `string.match()` or `string.matchAll()` result and creates a [Token](#token) object with that value. | ||
|
||
## Why | ||
|
||
Extracting the name of the group that was captured from a match result isn't trivial and can be done incorrectly which can lead to errors. This module is tested and will (hopefully) not fail. | ||
|
||
## Example | ||
<p></p> | ||
|
||
```ts | ||
import { matchToToken } from 'match-to-token' | ||
|
||
const match = 'foo'.match(/(?<bar>[o])/)! | ||
const token = matchToToken(match) | ||
console.log(token) | ||
// => { group: 'bar', value: 'o', index: 1 } | ||
transform a RegExp named group match to a more useful object | ||
|
||
const matches = 'foo'.matchAll(/(?<bar>[o])/g) | ||
for (const match of matches) { | ||
const token = matchToToken(match) | ||
console.log(token) | ||
// => { group: 'bar', value: 'o', index: 1 } | ||
// => { group: 'bar', value: 'o', index: 2 } | ||
} | ||
``` | ||
<h4> | ||
<table><tr><td title="Triple click to select and copy paste"> | ||
<code>npm i match-to-token </code> | ||
</td><td title="Triple click to select and copy paste"> | ||
<code>pnpm add match-to-token </code> | ||
</td><td title="Triple click to select and copy paste"> | ||
<code>yarn add match-to-token</code> | ||
</td></tr></table> | ||
</h4> | ||
|
||
## API | ||
|
||
<!-- Generated by documentation.js. Update this documentation by updating the source code. --> | ||
|
||
#### Table of Contents | ||
|
||
- [matchToToken](#matchtotoken) | ||
- [Parameters](#parameters) | ||
- [Token](#token) | ||
- [Parameters](#parameters-1) | ||
- [group](#group) | ||
- [value](#value) | ||
- [index](#index) | ||
- [source](#source) | ||
|
||
### matchToToken | ||
|
||
[src/index.ts:20-28](https://github.com/stagas/match-to-token/blob/e2711d8f14f9d1fe3b5bdfa8f9e8883991c08256/src/index.ts#L20-L28 "Source code on GitHub") | ||
|
||
Convert a match object from `string.match()` or `string.matchAll()` | ||
with a named group RegExp such as `/(?<group>[a-z])/` to a more | ||
useful [Token](#token) object. | ||
|
||
#### Parameters | ||
|
||
- `match` **(RegExpMatchArray | null)** The match object | ||
|
||
Returns **(MatchToken | null)**  | ||
|
||
### Token | ||
|
||
[src/token.ts:6-44](https://github.com/stagas/match-to-token/blob/e2711d8f14f9d1fe3b5bdfa8f9e8883991c08256/src/token.ts#L6-L44 "Source code on GitHub") | ||
|
||
**Extends String** | ||
|
||
Token interface | ||
|
||
#### Parameters | ||
|
||
- `value` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**  | ||
- `group` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**  | ||
- `source` **RegExpMatchArrayLike**  | ||
|
||
#### group | ||
|
||
[src/token.ts:13-13](https://github.com/stagas/match-to-token/blob/e2711d8f14f9d1fe3b5bdfa8f9e8883991c08256/src/token.ts#L13-L13 "Source code on GitHub") | ||
|
||
The group it matched. | ||
|
||
Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | ||
|
||
#### value | ||
|
||
[src/token.ts:17-19](https://github.com/stagas/match-to-token/blob/e2711d8f14f9d1fe3b5bdfa8f9e8883991c08256/src/token.ts#L17-L19 "Source code on GitHub") | ||
|
||
The value of the match. | ||
|
||
#### index | ||
|
||
[src/token.ts:23-25](https://github.com/stagas/match-to-token/blob/e2711d8f14f9d1fe3b5bdfa8f9e8883991c08256/src/token.ts#L23-L25 "Source code on GitHub") | ||
|
||
The index position of the match. | ||
|
||
#### source | ||
|
||
[src/token.ts:29-29](https://github.com/stagas/match-to-token/blob/e2711d8f14f9d1fe3b5bdfa8f9e8883991c08256/src/token.ts#L29-L29 "Source code on GitHub") | ||
|
||
The input string. | ||
|
||
Type: RegExpMatchArrayLike | ||
<p> <details id="matchToToken$5" title="Function" ><summary><span><a href="#matchToToken$5">#</a></span> <code><strong>matchToToken</strong></code><em>(match)</em> – Convert a match object from <code>string.match()</code> or <code>string.matchAll()</code> | ||
with a named group RegExp such as <code>/(?<group>[a-z])/</code> to a more | ||
useful {@link Token} object.</summary> <a href="src/index.ts#L20">src/index.ts#L20</a> <ul> <p> <details id="match$7" title="Parameter" ><summary><span><a href="#match$7">#</a></span> <code><strong>match</strong></code> – The match object | ||
</summary> <ul><p><code>null</code> | <span>RegExpMatchArray</span></p> </ul></details> <p><strong>matchToToken</strong><em>(match)</em> => <ul><code>null</code> | <a href="#Token$19">Token</a></ul></p></p> </ul></details><details id="MatchToken$19" title="Class" ><summary><span><a href="#MatchToken$19">#</a></span> <code><strong>MatchToken</strong></code> – Token interface</summary> <a href="src/token.ts#L6">src/token.ts#L6</a> <ul> <p> <details id="constructor$25" title="Constructor" ><summary><span><a href="#constructor$25">#</a></span> <code><strong>constructor</strong></code><em>(value, group, source)</em> </summary> <a href="src/token.ts#L31">src/token.ts#L31</a> <ul> <p> <details id="new MatchToken$26" title="ConstructorSignature" ><summary><span><a href="#new MatchToken$26">#</a></span> <code><strong>new MatchToken</strong></code><em>()</em> </summary> <ul><p><a href="#Token$19">Token</a></p> <p> <details id="value$27" title="Parameter" ><summary><span><a href="#value$27">#</a></span> <code><strong>value</strong></code> </summary> <ul><p>string</p> </ul></details><details id="group$28" title="Parameter" ><summary><span><a href="#group$28">#</a></span> <code><strong>group</strong></code> </summary> <ul><p>string</p> </ul></details><details id="source$29" title="Parameter" ><summary><span><a href="#source$29">#</a></span> <code><strong>source</strong></code> </summary> <ul><p><a href="#RegExpMatchArrayLike$1">RegExpMatchArrayLike</a></p> </ul></details></p> </ul></details></p> </ul></details><details id="group$30" title="Property" ><summary><span><a href="#group$30">#</a></span> <code><strong>group</strong></code> – The group it matched.</summary> <a href="src/token.ts#L13">src/token.ts#L13</a> <ul><p>string</p> </ul></details><details id="source$35" title="Property" ><summary><span><a href="#source$35">#</a></span> <code><strong>source</strong></code> – The input string.</summary> <a href="src/token.ts#L29">src/token.ts#L29</a> <ul><p><a href="#RegExpMatchArrayLike$1">RegExpMatchArrayLike</a></p> </ul></details><details id="index$33" title="Accessor" ><summary><span><a href="#index$33">#</a></span> <code><strong>index</strong></code> </summary> <a href="src/token.ts#L23">src/token.ts#L23</a> <ul> </ul></details><details id="value$31" title="Accessor" ><summary><span><a href="#value$31">#</a></span> <code><strong>value</strong></code> </summary> <a href="src/token.ts#L17">src/token.ts#L17</a> <ul> </ul></details><details id="as$40" title="Method" ><summary><span><a href="#as$40">#</a></span> <code><strong>as</strong></code><em>(value, group)</em> </summary> <a href="src/token.ts#L41">src/token.ts#L41</a> <ul> <p> <details id="value$42" title="Parameter" ><summary><span><a href="#value$42">#</a></span> <code><strong>value</strong></code> </summary> <ul><p>string</p> </ul></details><details id="group$43" title="Parameter" ><summary><span><a href="#group$43">#</a></span> <code><strong>group</strong></code> <span><span> = </span> <code>...</code></span> </summary> <ul><p>string</p> </ul></details> <p><strong>as</strong><em>(value, group)</em> => <ul><a href="#Token$19">Token</a></ul></p></p> </ul></details><details id="is$36" title="Method" ><summary><span><a href="#is$36">#</a></span> <code><strong>is</strong></code><em>(group, value)</em> </summary> <a href="src/token.ts#L37">src/token.ts#L37</a> <ul> <p> <details id="group$38" title="Parameter" ><summary><span><a href="#group$38">#</a></span> <code><strong>group</strong></code> </summary> <ul><p>string</p> </ul></details><details id="value$39" title="Parameter" ><summary><span><a href="#value$39">#</a></span> <code><strong>value</strong></code> </summary> <ul><p>string</p> </ul></details> <p><strong>is</strong><em>(group, value)</em> => <ul>boolean</ul></p></p> </ul></details><details id="create$20" title="Method" ><summary><span><a href="#create$20">#</a></span> <code><strong>create</strong></code><em>(value, group, source)</em> </summary> <a href="src/token.ts#L7">src/token.ts#L7</a> <ul> <p> <details id="value$22" title="Parameter" ><summary><span><a href="#value$22">#</a></span> <code><strong>value</strong></code> </summary> <ul><p>string</p> </ul></details><details id="group$23" title="Parameter" ><summary><span><a href="#group$23">#</a></span> <code><strong>group</strong></code> </summary> <ul><p>string</p> </ul></details><details id="source$24" title="Parameter" ><summary><span><a href="#source$24">#</a></span> <code><strong>source</strong></code> </summary> <ul><p><a href="#RegExpMatchArrayLike$1">RegExpMatchArrayLike</a></p> </ul></details> <p><strong>create</strong><em>(value, group, source)</em> => <ul><a href="#Token$19">Token</a></ul></p></p> </ul></details></p></ul></details><details id="RegExpToken$8" title="Class" ><summary><span><a href="#RegExpToken$8">#</a></span> <code><strong>RegExpToken</strong></code> </summary> <a href="src/index.ts#L32">src/index.ts#L32</a> <ul> <p> <details id="constructor$9" title="Constructor" ><summary><span><a href="#constructor$9">#</a></span> <code><strong>constructor</strong></code><em>(pattern)</em> </summary> <ul> <p> <details id="new RegExpToken$10" title="ConstructorSignature" ><summary><span><a href="#new RegExpToken$10">#</a></span> <code><strong>new RegExpToken</strong></code><em>()</em> </summary> <ul><p><a href="#RegExpToken$8">RegExpToken</a></p> <p> <details id="pattern$11" title="Parameter" ><summary><span><a href="#pattern$11">#</a></span> <code><strong>pattern</strong></code> </summary> <ul><p>string | <span>RegExp</span></p> </ul></details></p> </ul></details><details id="new RegExpToken$12" title="ConstructorSignature" ><summary><span><a href="#new RegExpToken$12">#</a></span> <code><strong>new RegExpToken</strong></code><em>()</em> </summary> <ul><p><a href="#RegExpToken$8">RegExpToken</a></p> <p> <details id="pattern$13" title="Parameter" ><summary><span><a href="#pattern$13">#</a></span> <code><strong>pattern</strong></code> </summary> <ul><p>string</p> </ul></details><details id="flags$14" title="Parameter" ><summary><span><a href="#flags$14">#</a></span> <code><strong>flags</strong></code> </summary> <ul><p>string</p> </ul></details></p> </ul></details><details id="new RegExpToken$15" title="ConstructorSignature" ><summary><span><a href="#new RegExpToken$15">#</a></span> <code><strong>new RegExpToken</strong></code><em>()</em> </summary> <ul><p><a href="#RegExpToken$8">RegExpToken</a></p> <p> <details id="pattern$16" title="Parameter" ><summary><span><a href="#pattern$16">#</a></span> <code><strong>pattern</strong></code> </summary> <ul><p>string | <span>RegExp</span></p> </ul></details><details id="flags$17" title="Parameter" ><summary><span><a href="#flags$17">#</a></span> <code><strong>flags</strong></code> </summary> <ul><p>string</p> </ul></details></p> </ul></details></p> </ul></details></p></ul></details><details id="RegExpMatchArrayLike$1" title="Interface" ><summary><span><a href="#RegExpMatchArrayLike$1">#</a></span> <code><strong>RegExpMatchArrayLike</strong></code> </summary> <a href="src/index.ts#L4">src/index.ts#L4</a> <ul> <p> <details id="index$2" title="Property" ><summary><span><a href="#index$2">#</a></span> <code><strong>index</strong></code> </summary> <a href="src/index.ts#L5">src/index.ts#L5</a> <ul><p>number</p> </ul></details><details id="input$3" title="Property" ><summary><span><a href="#input$3">#</a></span> <code><strong>input</strong></code> </summary> <a href="src/index.ts#L6">src/index.ts#L6</a> <ul><p>string</p> </ul></details></p></ul></details><details id="Token$4" title="TypeAlias" ><summary><span><a href="#Token$4">#</a></span> <code><strong>Token</strong></code> </summary> <a href="src/index.ts#L9">src/index.ts#L9</a> <ul><p><a href="#MatchToken$19">MatchToken</a> & string</p> </ul></details><details id="matchToToken$5" title="Function" ><summary><span><a href="#matchToToken$5">#</a></span> <code><strong>matchToToken</strong></code><em>(match)</em> – Convert a match object from <code>string.match()</code> or <code>string.matchAll()</code> | ||
with a named group RegExp such as <code>/(?<group>[a-z])/</code> to a more | ||
useful {@link Token} object.</summary> <a href="src/index.ts#L20">src/index.ts#L20</a> <ul> <p> <details id="match$7" title="Parameter" ><summary><span><a href="#match$7">#</a></span> <code><strong>match</strong></code> – The match object | ||
</summary> <ul><p><code>null</code> | <span>RegExpMatchArray</span></p> </ul></details> <p><strong>matchToToken</strong><em>(match)</em> => <ul><code>null</code> | <a href="#Token$19">Token</a></ul></p></p> </ul></details></p> | ||
|
||
## Contribute | ||
## Contributing | ||
|
||
[Fork](https://github.com/stagas/match-to-token/fork) or | ||
[edit](https://github.dev/stagas/match-to-token) and submit a PR. | ||
[Fork](https://github.com/stagas/match-to-token/fork) or [edit](https://github.dev/stagas/match-to-token) and submit a PR. | ||
|
||
All contributions are welcome! | ||
|
||
## License | ||
|
||
MIT © 2021 | ||
[stagas](https://github.com/stagas) | ||
<a href="LICENSE">MIT</a> © 2022 [stagas](https://github.com/stagas) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters