Re: [whatwg/streams] Add an example of a transform stream that replaces template tags (#846)

ricea commented on this pull request.



> @@ -4572,6 +4572,67 @@ and similarly, closing or aborting the <code>writable</code> side will implicitl
 
 </div>
 
+<h3 id="example-ts-lipfuzz">A transform stream that replaces template tags</h3>
+
+It's often useful to substitute tags with variables on a stream of data, where the parts that need to be replaced are
+small compared to the overall data size. This example presents a simple way to do that. It maps strings to strings,
+replacing a template like <code>"Time: <span>{</span>{time}} Message: <span>{</span>{message}}"</code> with <code>"Time:
+15:36 Message: hello"</code> assuming that <code>{ time: "15:36", message: "hello" }</code> was passed in the
+<code>substitutions</code> parameter to <code>LipFuzzTransform(substitutions)</code>.
+
+This example also demonstrates one way to deal with a situation where a chunk contains partial data that cannot be
+transformed until more data is received. In this case, a partial template tag will be accumulated in the
+<code>partialChunk</code> variable until either the end of the tag is found or the end of the stream is reached.
+
+<pre><code class="lang-javascript">
+  function LipFuzzTransform(substitutions) {

Done.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/pull/846#discussion_r147398321

Received on Friday, 27 October 2017 12:32:28 UTC