https://github.com/telenko/string-stream
https://github.com/telenko/string-stream
Last synced: 12 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/telenko/string-stream
- Owner: telenko
- License: mit
- Created: 2019-04-27T15:47:42.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-05-03T21:16:24.000Z (about 7 years ago)
- Last Synced: 2025-05-07T18:51:50.637Z (about 1 year ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
#General description
Library which allows to work with strings as with streams. Allows to write parsers, tokenizers etc.
#API
..tbd
#Example
Pseudo-code of writing html-parser
```Javascript
function readNode () {
var childs = [];
var tag = stream.readNext('<');
var tagName = stream.readNextWord();
stream.startRecording();
var endTag = stream.readNext('>');
var nodeTemplate = stream.stopRecording();
while (true) {
stream.startRecording();
stream.readNext('<');
var textNode = stream.stopRecording();
if (textNode.trim().length > 0) {
childs.push({type: 'text', template: textNode});
}
if (stream.next() === '/') {
break;
} else {
stream.moveBack(2);
childs.push(readNode());
}
}
var resp = {tagName: tagName, template: nodeTemplate, children: childs};
return resp;
}
var nodes = [];
while (!stream.isCompleted()) {
nodes.push(readNode());
}
```