Skip to content

Commit

Permalink
Move away from rxjs to iterables.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed May 16, 2019
1 parent c290fe9 commit 9b3e670
Show file tree
Hide file tree
Showing 44 changed files with 608 additions and 567 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -10,7 +10,6 @@ node_js:
- "node"
- "12"
- "10"
- "8"

install:
# the prepare script run during install currently does clean-build
Expand Down
1 change: 0 additions & 1 deletion appveyor.yml
Expand Up @@ -9,7 +9,6 @@ environment:
# - nodejs_version: "12"
- nodejs_version: "Current"
- nodejs_version: "10"
- nodejs_version: "8"

# Install scripts. (runs after repo cloning)
install:
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -47,7 +47,7 @@
"@types/minimatch": "^3.0.3",
"@types/mocha": "^5.2.6",
"@types/mock-require": "^2.0.0",
"@types/node": "^8.10.48",
"@types/node": "^10.14.6",
"@types/xregexp": "^3.0.29",
"chai": "^4.2.0",
"coveralls": "^3.0.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-glob/package.json
Expand Up @@ -56,7 +56,7 @@
]
},
"engines": {
"node": ">=8.0.0"
"node": ">=10.0.0"
},
"dependencies": {
"micromatch": "^4.0.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-lib/.vscode/launch.json
Expand Up @@ -18,7 +18,7 @@
"type": "node",
"request": "launch",
"name": "Run Mocha Current File",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"program": "${workspaceRoot}/../../node_modules/mocha/bin/_mocha",
"runtimeArgs": [
],
"args": [
Expand Down
3 changes: 3 additions & 0 deletions packages/cspell-lib/CHANGELOG.md
@@ -1,5 +1,8 @@
# Change log

## [4.0.0]
* **Breaking Changes** drops dependency upon rxjs and moves to [AsyncIterator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator)

## [3.05]
* Update dependencies and use `rxjs-stream` for reading files.

Expand Down
54 changes: 49 additions & 5 deletions packages/cspell-lib/package-lock.json

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

13 changes: 6 additions & 7 deletions packages/cspell-lib/package.json
@@ -1,6 +1,6 @@
{
"name": "cspell-lib",
"version": "3.0.8",
"version": "4.0.0",
"description": "A library of useful functions used across various cspell tools.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand All @@ -16,7 +16,7 @@
"clean": "rimraf dist",
"clean-build": "npm run clean && npm run build",
"coverage": "NODE_ENV=test nyc --silent --no-clean --temp-dir=../../.nyc_output npm run test-ts",
"test-ts": "rimraf temp ; NODE_ENV=test mocha --require ts-node/register --recursive --bail \"src/**/*.test.ts\"",
"test-ts": "../../node_modules/.bin/rimraf temp ; NODE_ENV=test ../../node_modules/.bin/mocha --require ts-node/register --recursive \"src/**/*.test.ts\"",
"test-watch": "../../node_modules/.bin/mocha --require ts-node/register --watch --recursive \"src/**/*.test.ts\"",
"test": "rimraf temp ; mocha --recursive ./dist/**/*.test.js"
},
Expand All @@ -35,11 +35,10 @@
"homepage": "https://github.com/streetsidesoftware/cspell#readme",
"dependencies": {
"iconv-lite": "^0.4.24",
"rxjs-stream": "^3.0.1"
},
"peerDependencies": {
"rxjs": "^6.0.0"
"iterable-to-stream": "^1.0.1",
"stream-to-iterator": "^3.0.2-0"
},
"peerDependencies": {},
"nyc": {
"include": [
"src/**/*.ts"
Expand All @@ -59,6 +58,6 @@
]
},
"engines": {
"node": ">=8.0.0"
"node": ">=10.0.0"
}
}
13 changes: 13 additions & 0 deletions packages/cspell-lib/src/async/asyncIterable.ts
@@ -0,0 +1,13 @@

/**
* Reads an entire iterable and converts it into a promise.
* @param asyncIterable the async iterable to wait for.
*/
export async function toArray<T>(asyncIterable: AsyncIterable<T> | Iterable<T> | Iterable<Promise<T>>): Promise<T[]> {
const data: T[] = [];
for await (const item of asyncIterable) {
data.push(item);
}
return data;
}

107 changes: 52 additions & 55 deletions packages/cspell-lib/src/file/fileReader.test.ts
@@ -1,9 +1,9 @@
import { expect } from 'chai';
import * as fReader from './fileReader';
import { of } from 'rxjs';
import { toArray } from 'rxjs/operators';
import * as fs from 'fs-extra';
import * as path from 'path';
import { Readable } from 'stream';
import * as asyncIterable from '../async/asyncIterable';

describe('Validate the fileReader', () => {
const samplePath = path.join(__dirname, '..', '..', 'samples');
Expand All @@ -14,82 +14,79 @@ describe('Validate the fileReader', () => {
'cities.noEOL.txt',
].map(f => path.join(samplePath, f));

it('tests stringsToLines', () => {
const strings = of('a1\n2\n3\n4', '5\n6');
return fReader.stringsToLinesRx(strings).pipe(toArray())
.toPromise().then((a) => {
expect(a).to.be.deep.equal(['a1', '2', '3', '45', '6']);
});
it('tests reading a file', async () => {
const expected = await fs.readFile(__filename, 'utf8');
const result = await fReader.readFile(__filename, 'utf8');
expect(result).to.be.equal(expected);
});

it('tests stringsToLines trailing new line', () => {
const strings = of('a1\n2\n3\n4', '5\n6\n');
return fReader.stringsToLinesRx(strings).pipe(toArray()).toPromise().then((a) => {
expect(a).to.be.deep.equal(['a1', '2', '3', '45', '6', '']);
});
it('tests stringsToLines', async () => {
const strings = stringToStream('a1\n2\n3\n4', '5\n6');
const a = await asyncIterable.toArray(fReader.streamLineByLineAsync(strings));
expect(a).to.be.deep.equal(['a1', '2', '3', '45', '6']);
});

it('test the file reader', () => {
return fReader.stringsToLinesRx(fReader.textFileStreamRx(__filename))
.pipe(toArray())
.toPromise()
.then(lines => {
const actual = lines.join('\n');
const expected = fs.readFileSync(__filename, 'UTF-8');
expect(actual).to.equal(expected);
});
it('tests stringsToLines trailing new line', async () => {
const strings = stringToStream('a1\n2\n3\n4', '5\n6\n');
const a = await asyncIterable.toArray(fReader.streamLineByLineAsync(strings));
expect(a).to.be.deep.equal(['a1', '2', '3', '45', '6', '']);
});

it('test the lineReaderRx', () => {
return fReader.lineReaderRx(__filename)
.pipe(toArray())
.toPromise()
.then(lines => {
const expected = fs.readFileSync(__filename, 'UTF-8').split('\n');
expect(lines).to.deep.equal(expected);
});
it('test the file reader', async () => {
const lines = await asyncIterable.toArray(fReader.streamFileLineByLineAsync(__filename));
const actual = lines.join('\n');
const expected = fs.readFileSync(__filename, 'UTF-8');
expect(actual).to.equal(expected);
});

it('test the lineReaderAsync', async () => {
const lines = await asyncIterable.toArray(fReader.lineReaderAsync(__filename));
const expected = fs.readFileSync(__filename, 'UTF-8').split('\n');
expect(lines).to.deep.equal(expected);
});

it('tests reading the cities sample', async () => {
const lines = await fReader.lineReaderRx(fileCities)
.pipe(toArray())
.toPromise();
const lines = await asyncIterable.toArray(fReader.lineReaderAsync(fileCities));
const file = await fs.readFile(fileCities, 'utf8');
expect(lines).to.be.deep.equal(file.split('\n'));
});

it('tests streamFileLineByLineRx', async () => {
it('tests streamFileLineByLineAsync', async () => {
await Promise.all(sampleFiles
.map(async filename => {
const lines = await fReader.streamFileLineByLineRx(filename)
.pipe(toArray())
.toPromise();
const lines = await asyncIterable.toArray(fReader.streamFileLineByLineAsync(filename));
const file = await fs.readFile(filename, 'utf8');
expect(lines, `compare to file: ${filename}`).to.be.deep.equal(file.split(/\r?\n/));
}));
});

it('tests streamFileLineByLineRx 2', async () => {
const lines = await fReader.streamFileLineByLineRx(__filename)
.pipe(toArray())
.toPromise();
it('tests streamFileLineByLineAsync 2', async () => {
const lines = await asyncIterable.toArray(fReader.streamFileLineByLineAsync(__filename));
const file = await fs.readFile(__filename, 'utf8');
expect(lines).to.be.deep.equal(file.split('\n'));
});

it('test missing file', () => {
return fReader.lineReaderRx(__filename + 'not.found')
.pipe(toArray())
.toPromise()
.then(
() => {
expect('not to be here').to.be.true;
},
(e) => {
expect(e).to.be.instanceof(Error);
expect(e.code).to.be.equal('ENOENT');
}
);
it('test missing file', async () => {
const result = asyncIterable.toArray(fReader.lineReaderAsync(__filename + 'not.found'));
return result.then(
() => {
expect('not to be here').to.be.true;
},
(e) => {
expect(e).to.be.instanceof(Error);
expect(e.code).to.be.equal('ENOENT');
}
);
});

});

function stringToStream(...strings: string[]): NodeJS.ReadableStream {
return new Readable({
read: function() {
for (const s of strings) {
this.push(s);
}
this.push(null);
}
});
}

0 comments on commit 9b3e670

Please sign in to comment.