Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 74bfd9e

Browse files
committed
Adding Wikipedia and Word Counter.
1 parent 3f5ab1a commit 74bfd9e

File tree

5 files changed

+60
-18
lines changed

5 files changed

+60
-18
lines changed

node/README.md

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,23 @@ First of all, install all dependencies with:
99
npm install
1010
```
1111

12-
Then, you can run each of the listed [examples](#examples) with the following command:
12+
Then, you can run each of the listed [examples](#examples) with the following command from the this project root folder:
1313
```bash
14-
npm run example ts example-name
14+
ts-node ./examples/example-name.ts
1515
```
1616

1717
To run the HTTPS server example, just:
1818
```bash
19-
npm run example ts HttpServer
19+
ts-node ./examples/HttpServer.ts
2020
```
2121

22-
Under the hood, this examples are running through [ts-node](https://github.com/TypeStrong/ts-node), which is not recommended in production environments. You can also build those examples with:
22+
This examples are running through [ts-node](https://github.com/TypeStrong/ts-node), which is not recommended in production environments. You can also build those examples with:
2323
```bash
2424
npm run build
2525
```
2626

2727
And then running the compiled JavaScript (JS) example file with:
2828
```bash
29-
npm run example js example-name
30-
```
31-
32-
Or even trough node directly:
33-
```bash
3429
node ./dist/example-name.js
3530
```
3631

@@ -50,9 +45,3 @@ A modified version of the [Microsoft Linter Standards](https://github.com/Micros
5045

5146
## Git Hooks
5247
Due to [Husky](https://github.com/typicode/husky) integration, before any push to this Github repository, [TSLint](https://github.com/palantir/tslint) will run and then point out all the fixes that needs to be done to follow the set of code [standards](#standards); if nothing needs to be corrected, you then can push it :)
53-
54-
## Tests
55-
Tests are a resourceful tool to add it, they serve as examples and also as guarantee that your code is doing what needs to be bone. [TDD](https://en.wikipedia.org/wiki/Test-driven_development) is a great example of this.
56-
```bash
57-
npm test
58-
```

node/lorem.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"

node/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
"description": "Node examples using TypeScript",
55
"scripts": {
66
"build": "tsc",
7-
"example": "ts-node ./index.ts",
8-
"lint": "tslint --config tslint.json --project .",
9-
"test": "jest --config jest.config.json --ci --runInBand --detectOpenHandles --forceExit --no-cache"
7+
"lint": "tslint --config tslint.json --project ."
108
},
119
"contributors": [
1210
{

node/src/Wikipedia.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Searches Wikipedia database
3+
*/
4+
import { IncomingMessage } from 'http';
5+
import { request } from 'https';
6+
7+
const response = (res: IncomingMessage) => {
8+
const { statusCode } = res;
9+
let chunk = '';
10+
11+
if (statusCode !== 200) {
12+
console.error(new Error(`[Request status ${statusCode}] Not accepted`));
13+
} else {
14+
res.on('error', console.error)
15+
.on('uncaughtException', console.error)
16+
.on('data', (data: string) => chunk += data)
17+
.on('end', () => {
18+
const result = JSON.parse(chunk);
19+
20+
console.log(result);
21+
});
22+
}
23+
};
24+
25+
const search = 'microsoft';
26+
const query = `action=query&format=json&list=search&srsearch=${encodeURI(search)}`;
27+
const path = `/w/api.php?${query}`;
28+
const options = {
29+
path,
30+
method: 'GET',
31+
hostname: 'en.wikipedia.org',
32+
headers: {
33+
Accept: 'application/json',
34+
'Content-Type': 'text/html'
35+
}
36+
};
37+
const wikipedia = request(options);
38+
39+
wikipedia.on('error', console.error)
40+
.on('response', response)
41+
.end();

node/src/WordCounter.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Reads file then shows how many matching occurrences of a word is found
3+
*/
4+
import { readFileSync } from 'fs';
5+
import { join } from 'path';
6+
7+
const path = join(__dirname, '../lorem.txt');
8+
const ipsum = readFileSync(path, { encoding: 'utf8' });
9+
const word = 'ut';
10+
const re = new RegExp(word, 'gm');
11+
const matching = <RegExpMatchArray> ipsum.match(re);
12+
13+
console.log(`Number of occurrences of the \"${word}\" word is: ${matching.length}`);

0 commit comments

Comments
 (0)