Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
352 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.DS_Store | ||
node_modules/ | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ts/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# stringio: convert strings to Node.js streams and vice versa | ||
|
||
```js | ||
import * as assert from 'assert'; | ||
import { StringStream, readableToString } from 'stringio'; | ||
|
||
test('From string to stream to string', async () => { | ||
const str = 'Hello!\nHow are you?\n'; | ||
const stringStream = new StringStream(str); // (A) | ||
const result = await readableToString(stringStream); // (B) | ||
assert.strictEqual(result, str); | ||
}); | ||
``` | ||
|
||
## `stringio.StringStream`: from string to stream | ||
|
||
```typescript | ||
declare class StringStream extends Readable { | ||
constructor(str: string); | ||
} | ||
``` | ||
|
||
Used in line A. | ||
|
||
## `stringio.readableToString`: from stream to string | ||
|
||
```typescript | ||
declare function readableToString(readable: Readable, encoding?: string): Promise<string>; | ||
``` | ||
|
||
Default encoding is `'utf-8'`. | ||
|
||
Used in line B. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "@rauschma/stringio", | ||
"version": "1.0.0", | ||
"author": "Axel Rauschmayer", | ||
"main": "./dist/src/index.js", | ||
"types": "./dist/src/index.d.ts", | ||
"scripts": { | ||
"build": "tsc", | ||
"watch": "tsc --watch", | ||
"test": "mocha --ui qunit", | ||
"testall": "mocha --ui qunit dist/test", | ||
"prepublishOnly": "tsc" | ||
}, | ||
"//": "@types/node being a normal dependency is not ideal, but it ensures it’s installed automatically", | ||
"dependencies": { | ||
"@types/node": "^9.4.7" | ||
}, | ||
"devDependencies": { | ||
"@types/mocha": "^2.2.48", | ||
"mocha": "^5.0.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Readable } from 'stream'; | ||
|
||
export class StringStream extends Readable { | ||
private _done: boolean; | ||
private _str: string; | ||
constructor(str: string) { | ||
super(); | ||
this._str = str; | ||
this._done = false; | ||
} | ||
_read() { | ||
if (!this._done) { | ||
this._done = true; | ||
this.push(this._str); | ||
this.push(null); | ||
} | ||
} | ||
} | ||
|
||
export function readableToString(readable: Readable, encoding='utf8'): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
readable.setEncoding(encoding); | ||
let data = ''; | ||
readable.on('data', function (chunk) { | ||
data += chunk; | ||
}); | ||
readable.on('end', function () { | ||
resolve(data); | ||
}); | ||
readable.on('error', function (err) { | ||
reject(err); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as assert from 'assert'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import { StringStream, readableToString } from '../src'; | ||
|
||
test('From string to stream to string', async () => { | ||
const str = 'Hello!\nHow are you?\n'; | ||
const stringStream = new StringStream(str); | ||
const result = await readableToString(stringStream); | ||
assert.strictEqual(result, str); | ||
}); | ||
|
||
test('File stream stream to string', async () => { | ||
const PATH = path.resolve(__dirname, '../../ts/test/index_test_file.txt'); | ||
const stream = fs.createReadStream(PATH); | ||
const str = await readableToString(stream); | ||
assert.strictEqual(str.trim(), 'This is a test!'); | ||
}); | ||
|
||
/* | ||
async function mainTest() { | ||
const str = await readableToString(); | ||
console.log('STR: '+str); | ||
} | ||
async function mainStdin() { | ||
const str = await readableToString(process.stdin); | ||
console.log('STR: '+str); | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is a test! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"esModuleInterop": true, | ||
"target": "es2015", | ||
"lib": [ | ||
"es2016", | ||
"es2017", | ||
], | ||
"sourceMap": true, | ||
"outDir": "dist", | ||
"strict": true, | ||
"declaration": true | ||
}, | ||
"include": [ | ||
"ts/**/*" | ||
], | ||
"exclude": [ | ||
"node_modules", | ||
"dist" | ||
] | ||
} |