xrtlibrary-linesplit
v1.2.8
Published
Line split utilities module of XRT library.
Downloads
4
Readme
XRTLibrary-LineSplit
Introduction
A library that can split text lines synchronously or asynchronously.
Installation
To install this package, you can use NPM by typing following command:
npm install xrtlibrary-linesplit --save
Then you can import this library in your JavaScript code:
const XRTLibLineSplit = require("xrtlibrary-linesplit");
API
(Class) LineReader
Line reader.
new LineReader()
Construct a new object.
reader.read([cancellator])
Read a line.
Exception(s):
- LineReader.EndOfStreamError: Raised if the stream was already ended.
- LineReader.OperationCancelledError: Raised if the cancellator was activated.
Parameter(s):
- cancellator (ConditionalSynchronizer): (Optional) The cancellator.
Return value:
- (Promise<String>) The promise object (resolves with the the line if succeed, rejects if error occurred).
reader.write(data)
Write data.
Exception(s):
- LineReader.EndOfStreamError: Raised if the stream was already ended.
Parameter(s):
- data (String): The data.
reader.isEnded()
Get whether the stream was ended.
Return value:
- (Boolean) True if so.
reader.end()
End the stream.
Exception(s):
- LineReader.EndOfStreamError: Raised if the stream was already ended.
(Class) LineReader.Error
Line reader error.
Extend(s):
- Error
(Class) LineReader.EndOfStreamError
Line reader end of stream error.
Extend(s):
- LineReader.Error
(Class) LineReader.OperationCancelledError
Line reader operation cancelled error.
Extend(s):
- LineReader.Error
(Class) BufferLineReader
Buffer line reader.
new BufferLineReader()
Construct a new object.
reader.read([cancellator])
Read a line.
Exception(s):
- BufferLineReader.EndOfStreamError: Raised if the stream was already ended.
- BufferLineReader.OperationCancelledError: Raised if the cancellator was activated.
Parameter(s):
- cancellator (ConditionalSynchronizer): (Optional) The cancellator.
Return value:
- (Promise<Buffer>) The promise object (resolves with the buffer of the line if succeed, rejects if error occurred).
reader.write(data)
Write data.
Exception(s):
- BufferLineReader.EndOfStreamError: Raised if the stream was already ended.
Parameter(s):
- data (Buffer): The data.
reader.isEnded()
Get whether the stream was ended.
Return value:
- (Boolean) True if so.
reader.end()
End the stream.
Exception(s):
- BufferLineReader.EndOfStreamError: Raised if the stream was already ended.
(Class) BufferLineReader.Error
Buffer line reader error.
Extend(s):
- Error
(Class) BufferLineReader.EndOfStreamError
Buffer line reader end of stream error.
Extend(s):
- BufferLineReader.Error
(Class) BufferLineReader.OperationCancelledError
Buffer line reader operation cancelled error.
Extend(s):
- BufferLineReader.Error
SplitLines(text)
Split text into lines.
Parameter(s):
- text (String): The text.
Return value:
- (String[]) The lines.
SplitBufferLines(input)
Split buffer into lines.
Parameter(s):
- input (Buffer): The input buffer.
Return value:
- (Buffer[]) The lines.
Example
Example 1: Use synchronous mechanism.
See following example:
console.log(XRTLibLineSplit.SplitLines("Line 1\r\nLine 2\n\nLine 4"));
Output:
[ 'Line 1', 'Line 2', '', 'Line 4' ]
Example 2: Use asynchronous mechanism.
See following example:
let reader = new XRTLibLineSplit.LineReader();
reader.read().then(function(line) {
console.log("First line: " + line);
return reader.read();
}).then(function(line) {
console.log("Second line: " + line);
return reader.read();
}).then(function(line) {
console.log("Third line: " + line);
return reader.read();
}).catch(function(error) {
if (error instanceof XRTLibLineSplit.LineReader.EndOfStreamError) {
console.log("Stream ended.");
} else {
throw error;
}
});
reader.write("Hello wor");
reader.write("ld!\r");
reader.write("\nJavaScript\nLast line");
reader.end();
Output:
First line: Hello world!
Second line: JavaScript
Third line: Last line
Stream ended.