Skip to content

Commit

Permalink
fix, document and test mapWithInput
Browse files Browse the repository at this point in the history
  • Loading branch information
fponticelli committed Nov 26, 2018
1 parent fc75c21 commit 340937f
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 32 deletions.
2 changes: 1 addition & 1 deletion docs/assets/js/search.js

Large diffs are not rendered by default.

104 changes: 82 additions & 22 deletions docs/classes/decoder.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ <h3>Methods</h3>
<li class="tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter"><a href="decoder.html#manywithseparator" class="tsd-kind-icon">many<wbr>With<wbr>Separator</a></li>
<li class="tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter"><a href="decoder.html#map" class="tsd-kind-icon">map</a></li>
<li class="tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter"><a href="decoder.html#maperror" class="tsd-kind-icon">map<wbr>Error</a></li>
<li class="tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter"><a href="decoder.html#mapwithinput" class="tsd-kind-icon">map<wbr>With<wbr>Input</a></li>
<li class="tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter"><a href="decoder.html#or" class="tsd-kind-icon">or</a></li>
<li class="tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter"><a href="decoder.html#picknext" class="tsd-kind-icon">pick<wbr>Next</a></li>
<li class="tsd-kind-method tsd-parent-kind-class"><a href="decoder.html#probe" class="tsd-kind-icon">probe</a></li>
Expand Down Expand Up @@ -226,7 +227,7 @@ <h3>at<wbr>Least</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L210">core/decoder.ts:210</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L225">core/decoder.ts:225</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -255,7 +256,7 @@ <h3>at<wbr>Least<wbr>With<wbr>Separator</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L281">core/decoder.ts:281</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L296">core/decoder.ts:296</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -294,7 +295,7 @@ <h3>at<wbr>Most</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L272">core/decoder.ts:272</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L287">core/decoder.ts:287</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -324,7 +325,7 @@ <h3>at<wbr>Most<wbr>With<wbr>Separator</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L290">core/decoder.ts:290</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L305">core/decoder.ts:305</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -363,7 +364,7 @@ <h3>between</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L237">core/decoder.ts:237</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L252">core/decoder.ts:252</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -394,7 +395,7 @@ <h3>between<wbr>With<wbr>Separator</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L315">core/decoder.ts:315</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L330">core/decoder.ts:330</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -489,7 +490,7 @@ <h3>flat<wbr>Map<wbr>Error</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L138">core/decoder.ts:138</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L153">core/decoder.ts:153</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -542,7 +543,7 @@ <h3>join</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L179">core/decoder.ts:179</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L194">core/decoder.ts:194</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -577,7 +578,7 @@ <h3>many</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L230">core/decoder.ts:230</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L245">core/decoder.ts:245</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -599,7 +600,7 @@ <h3>many<wbr>With<wbr>Separator</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L298">core/decoder.ts:298</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L313">core/decoder.ts:313</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -690,7 +691,7 @@ <h3>map<wbr>Error</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L150">core/decoder.ts:150</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L165">core/decoder.ts:165</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -732,6 +733,62 @@ <h4 class="tsd-returns-title">Returns <a href="decoder.html" class="tsd-signatur
</li>
</ul>
</section>
<section class="tsd-panel tsd-member tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter">
<a name="mapwithinput" class="tsd-anchor"></a>
<h3>map<wbr>With<wbr>Input</h3>
<ul class="tsd-signatures tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter">
<li class="tsd-signature tsd-kind-icon">map<wbr>With<wbr>Input&lt;Out2&gt;<span class="tsd-signature-symbol">(</span>fun<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">function</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="decoder.html" class="tsd-signature-type">Decoder</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">In</span><span class="tsd-signature-symbol">, </span><span class="tsd-signature-type">Out2</span><span class="tsd-signature-symbol">, </span><span class="tsd-signature-type">Err</span><span class="tsd-signature-symbol">&gt;</span></li>
</ul>
<ul class="tsd-descriptions">
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L116">core/decoder.ts:116</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
<div class="lead">
<p>Similar to <a href="decoder.html#map">map</a> but passes a tuple of <code>res: Out</code> and <code>input: In</code>. This is mostly useful when decoding
tokens to preserve the original position in the input.</p>
</div>
</div>
<h4 class="tsd-type-parameters-title">Type parameters</h4>
<ul class="tsd-type-parameters">
<li>
<h4>Out2</h4>
</li>
</ul>
<h4 class="tsd-parameters-title">Parameters</h4>
<ul class="tsd-parameters">
<li>
<h5>fun: <span class="tsd-signature-type">function</span></h5>
<ul class="tsd-parameters">
<li class="tsd-parameter-siganture">
<ul class="tsd-signatures tsd-kind-type-literal tsd-is-not-exported">
<li class="tsd-signature tsd-kind-icon"><span class="tsd-signature-symbol">(</span>res<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">Out</span>, input<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">In</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">Out2</span></li>
</ul>
<ul class="tsd-descriptions">
<li class="tsd-description">
<h4 class="tsd-parameters-title">Parameters</h4>
<ul class="tsd-parameters">
<li>
<h5>res: <span class="tsd-signature-type">Out</span></h5>
</li>
<li>
<h5>input: <span class="tsd-signature-type">In</span></h5>
</li>
</ul>
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">Out2</span></h4>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h4 class="tsd-returns-title">Returns <a href="decoder.html" class="tsd-signature-type">Decoder</a><span class="tsd-signature-symbol">&lt;</span><span class="tsd-signature-type">In</span><span class="tsd-signature-symbol">, </span><span class="tsd-signature-type">Out2</span><span class="tsd-signature-symbol">, </span><span class="tsd-signature-type">Err</span><span class="tsd-signature-symbol">&gt;</span></h4>
</li>
</ul>
</section>
<section class="tsd-panel tsd-member tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter">
<a name="or" class="tsd-anchor"></a>
<h3>or</h3>
Expand All @@ -742,7 +799,7 @@ <h3>or</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L187">core/decoder.ts:187</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L202">core/decoder.ts:202</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -779,7 +836,7 @@ <h3>pick<wbr>Next</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L163">core/decoder.ts:163</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L178">core/decoder.ts:178</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -814,7 +871,7 @@ <h3>probe</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L401">core/decoder.ts:401</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L416">core/decoder.ts:416</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -863,7 +920,7 @@ <h3>repeat</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L262">core/decoder.ts:262</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L277">core/decoder.ts:277</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -891,7 +948,7 @@ <h3>repeat<wbr>With<wbr>Separator</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L307">core/decoder.ts:307</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L322">core/decoder.ts:322</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -930,7 +987,7 @@ <h3>skip<wbr>Next</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L171">core/decoder.ts:171</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L186">core/decoder.ts:186</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -965,7 +1022,7 @@ <h3>sub</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L116">core/decoder.ts:116</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L131">core/decoder.ts:131</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -1048,7 +1105,7 @@ <h3>surrounded<wbr>By</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L369">core/decoder.ts:369</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L384">core/decoder.ts:384</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -1086,7 +1143,7 @@ <h3>test</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L380">core/decoder.ts:380</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L395">core/decoder.ts:395</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -1137,7 +1194,7 @@ <h3>with<wbr>Failure</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L420">core/decoder.ts:420</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L435">core/decoder.ts:435</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -1171,7 +1228,7 @@ <h3>with<wbr>Result</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L413">core/decoder.ts:413</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L428">core/decoder.ts:428</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -1301,6 +1358,9 @@ <h4 class="tsd-returns-title">Returns <a href="decoder.html" class="tsd-signatur
<li class=" tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter">
<a href="decoder.html#maperror" class="tsd-kind-icon">map<wbr>Error</a>
</li>
<li class=" tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter">
<a href="decoder.html#mapwithinput" class="tsd-kind-icon">map<wbr>With<wbr>Input</a>
</li>
<li class=" tsd-kind-method tsd-parent-kind-class tsd-has-type-parameter">
<a href="decoder.html#or" class="tsd-kind-icon">or</a>
</li>
Expand Down
10 changes: 5 additions & 5 deletions docs/globals.html
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> fail</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L489">core/decoder.ts:489</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L504">core/decoder.ts:504</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -1236,7 +1236,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> lazy</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L500">core/decoder.ts:500</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L515">core/decoder.ts:515</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -1733,7 +1733,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> one<wbr>Of</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L457">core/decoder.ts:457</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L472">core/decoder.ts:472</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -1871,7 +1871,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> sequence</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L430">core/decoder.ts:430</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L445">core/decoder.ts:445</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -1910,7 +1910,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> succeed</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L484">core/decoder.ts:484</a></li>
<li>Defined in <a href="https://github.com/fponticelli/partsing/blob/master/src/core/decoder.ts#L499">core/decoder.ts:499</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down
2 changes: 1 addition & 1 deletion package.npm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "partsing",
"version": "0.4.0",
"version": "0.4.1",
"license": "Apache-2.0",
"homepage": "https://github.com/fponticelli/partsing",
"bugs": {
Expand Down
8 changes: 6 additions & 2 deletions src/core/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,17 @@ export class Decoder<In, Out, Err> {
return this.flatMap<Out2>(r => Decoder.of<In, Out2, Err>((input: In) => success(input, fun(r))))
}

/**
* Similar to {@link map} but passes a tuple of `res: Out` and `input: In`. This is mostly useful when decoding
* tokens to preserve the original position in the input.
*/
mapWithInput<Out2>(fun: (res: Out, input: In) => Out2): Decoder<In, Out2, Err> {
return Decoder.of<In, Out2, Err>((input: In) => {
const result = this.run(input)
if (result.isSuccess()) {
return new DecodeSuccess(result.input, fun(result.value, input))
return success(result.input, fun(result.value, input))
} else {
return new DecodeFailure(input, result.failure)
return failure(input, ...result.failures)
}
})
}
Expand Down
Loading

0 comments on commit 340937f

Please sign in to comment.