Skip to content

Commit

Permalink
First commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
rauschma committed Apr 6, 2018
1 parent 5612078 commit 698f54d
Show file tree
Hide file tree
Showing 9 changed files with 352 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
.DS_Store
node_modules/
dist/
2 changes: 2 additions & 0 deletions .npmignore
@@ -0,0 +1,2 @@
ts/

33 changes: 33 additions & 0 deletions README.md
@@ -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.
206 changes: 206 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions package.json
@@ -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"
}
}
34 changes: 34 additions & 0 deletions ts/src/index.ts
@@ -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);
});
});
}
29 changes: 29 additions & 0 deletions ts/test/index_test.ts
@@ -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);
}
*/
1 change: 1 addition & 0 deletions ts/test/index_test_file.txt
@@ -0,0 +1 @@
This is a test!
22 changes: 22 additions & 0 deletions tsconfig.json
@@ -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"
]
}

0 comments on commit 698f54d

Please sign in to comment.