Transform uses array of regex's to run exec() on each part and pass on capture results to next stream.
npm install each-part-capture --save
# get builder functionbuildCapturer = require 'each-part-capture'# build capturer transform with array of regular expressionscapturer = buildCapturer /=/# write a string which will match the regular expressioncapturerwrite 'num=12345'# capturer will push an object, `result` to the next stream with# the `capture` property containing the regex.exec(string) result:result =capture:# capture = 'num'# capture = 12345# write a string which does *NOT* match any of the regex'scapturerwrite 'wont match any pattern'# *nothing* is pushed to the next stream. non-matching strings are *ignored*# by default. override this via the `ignore` option at creation time:nonIgnoringCapturer = buildCapturer /some regex/ignore:false# this capturer will provide unmatchable strings in a `unknown` property on `result`result =unknown: 'wont match any pattern'
Usage: Stream Pipeline
# get the each-part module to breakup the partsbuildEacher = require 'each-part'# make an eacher transformeacher = buildEacher# get this modulebuildCapturer = require 'each-part-capture'# get module to enhance regex's exec() to use names for the capture groupsbuildNameCapture = require 'regex-named-groups'# build a capturer transform with an array of regular expressions which are# enhanced to use capture group namescapturer = buildCapturer# three words separated by spacesbuildNameCapture /^\s+\s+$/'first''second''third'# three numbers separated by spacesbuildNameCapture /^\s+\s+$/'d1''d2''d3'# test strings to write thru transforms:words = 'one two three'nums = '-1.23 +81742.2153 7028261.3'nomatch = 'some non-matching text which gets ignored'testString = words + '\n' + nomatch + '\n' + nums# create a target stream, as a sample, to receive the capture resultstarget = thruobj# first result is the words capture:result.capture.first = 'one'result.capture.second = 'two'result.capture.third = 'three'# NOTE: the 'nomatch' string is ignored because it didn't match any regex# second result (next time this function is called) is the nums.result.capture.d1 = -1.23result.capture.d2 = 81742.2153result.capture.d3 = 7028261.3# pipe them togethereacherpipecapturerpipetarget# write our test string to the pipelineeacherend testString