Skip to content

Commit

Permalink
chore: set up TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
yangshun committed Jul 5, 2020
1 parent 434baa7 commit 83dca6c
Show file tree
Hide file tree
Showing 18 changed files with 374 additions and 152 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"presets": ["@babel/preset-env"],
"presets": ["@babel/preset-env", "@babel/preset-typescript"],
"plugins": [
[
"babel-plugin-transform-builtin-extend",
Expand Down
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
coverage

# TypeScript compiled files
lib
18 changes: 16 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
module.exports = {
extends: ['airbnb-base', 'prettier'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'airbnb-base',
'prettier',
],
env: {
browser: true,
jest: true,
node: true,
},
plugins: ['prettier'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'prettier'],
rules: {
'no-param-reassign': 'off', // Needed for swapping elements within an array.
'no-plusplus': 'off',
'no-underscore-dangle': 'off', // Indicate private methods.
'no-unused-vars': ['error', { argsIgnorePattern: '^_$' }], // Ignore variables called _.
'prettier/prettier': 'error',
},
settings: {
'import/resolver': {
node: {
extensions: ['.js', '.ts'],
},
},
},
};
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ typings/
# dotenv environment variables file
.env

# TypeScript compiles files
lib
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# TypeScript compiled files
lib
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"check-all": "yarn prettier && yarn lint && yarn test",
"check-all:ci": "yarn prettier && yarn lint && yarn run test:ci",
"lint": "eslint .",
"prettier": "prettier --config .prettierrc --write 'src/**/*.js'",
"prettier:diff": "prettier --config .prettierrc --list-different 'src/**/*.js'",
"prettier": "prettier --config .prettierrc --write 'src/**/*.{js,ts}'",
"prettier:diff": "prettier --config .prettierrc --list-different 'src/**/*.{js,ts}'",
"test": "jest --coverage",
"test:ci": "jest --coverage --runInBand",
"start": "jest --coverage --watch"
Expand All @@ -27,6 +27,9 @@
"homepage": "https://github.com/yangshun/lago#readme",
"devDependencies": {
"@babel/preset-env": "^7.10.4",
"@babel/preset-typescript": "^7.10.4",
"@typescript-eslint/eslint-plugin": "^3.5.0",
"@typescript-eslint/parser": "^3.5.0",
"babel-jest": "^26.1.0",
"babel-plugin-transform-builtin-extend": "^1.1.2",
"eslint": "^7.4.0",
Expand All @@ -36,6 +39,7 @@
"eslint-plugin-prettier": "^3.1.4",
"jest": "^26.1.0",
"lodash": "^4.17.4",
"prettier": "^2.0.5"
"prettier": "^2.0.5",
"typescript": "^3.9.6"
}
}
111 changes: 73 additions & 38 deletions src/algorithms/__tests__/breadthFirstSearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,79 +6,90 @@ describe('breadthFirstSearch', () => {
});

test('graphs with one node', () => {
expect(breadthFirstSearch({ '1': [] }, '1')).toEqual(['1']);
expect(breadthFirstSearch({ '1': ['1'] }, '1')).toEqual(['1']);
expect(breadthFirstSearch({ 1: [] }, '1')).toEqual(['1']);
expect(breadthFirstSearch({ 1: ['1'] }, '1')).toEqual(['1']);
});

test('graphs with two nodes', () => {
expect(breadthFirstSearch({ '1': ['2'], '2': [] }, '1')).toEqual([
expect(breadthFirstSearch({ 1: ['2'], 2: [] }, '1')).toEqual(['1', '2']);
expect(breadthFirstSearch({ 1: ['1', '2'], 2: [] }, '1')).toEqual([
'1',
'2',
]);
expect(breadthFirstSearch({ '1': ['1', '2'], '2': [] }, '1')).toEqual([
'1',
'2',
]);
expect(breadthFirstSearch({ '1': ['1', '2'], '2': [] }, '2')).toEqual([
'2',
]);
expect(breadthFirstSearch({ '1': ['1', '2'], '2': ['1'] }, '2')).toEqual([
expect(breadthFirstSearch({ 1: ['1', '2'], 2: [] }, '2')).toEqual(['2']);
expect(breadthFirstSearch({ 1: ['1', '2'], 2: ['1'] }, '2')).toEqual([
'2',
'1',
]);
});

test('graphs with multiple nodes', () => {
expect(
breadthFirstSearch({ '1': ['2'], '2': ['3'], '3': [] }, '1'),
).toEqual(['1', '2', '3']);
expect(
breadthFirstSearch({ '1': ['2', '3'], '2': [], '3': [] }, '1'),
).toEqual(['1', '2', '3']);
expect(breadthFirstSearch({ 1: ['2'], 2: ['3'], 3: [] }, '1')).toEqual([
'1',
'2',
'3',
]);
expect(breadthFirstSearch({ 1: ['2', '3'], 2: [], 3: [] }, '1')).toEqual([
'1',
'2',
'3',
]);
expect(
breadthFirstSearch(
{ '1': ['2', '3'], '2': [], '3': [], '4': ['2'], '5': ['3'] },
{
1: ['2', '3'],
2: [],
3: [],
4: ['2'],
5: ['3'],
},
'1',
),
).toEqual(['1', '2', '3']);
expect(
breadthFirstSearch(
{ '1': ['4', '5'], '2': [], '3': [], '4': ['2'], '5': ['3'] },
{
1: ['4', '5'],
2: [],
3: [],
4: ['2'],
5: ['3'],
},
'1',
),
).toEqual(['1', '4', '5', '2', '3']);
expect(
breadthFirstSearch(
{
'1': ['4', '5'],
'2': ['1', '2', '3', '4', '5'],
'3': [],
'4': ['2'],
'5': ['3'],
1: ['4', '5'],
2: ['1', '2', '3', '4', '5'],
3: [],
4: ['2'],
5: ['3'],
},
'1',
),
).toEqual(['1', '4', '5', '2', '3']);
expect(
breadthFirstSearch(
{
'1': ['1', '2', '3', '4', '5'],
'2': [],
'3': [],
'4': ['2'],
'5': ['3'],
1: ['1', '2', '3', '4', '5'],
2: [],
3: [],
4: ['2'],
5: ['3'],
},
'1',
),
).toEqual(['1', '2', '3', '4', '5']);
// Graph taken from https://www.geeksforgeeks.org/breadth-first-traversal-for-a-graph/
const graph = {
'1': ['2', '3'],
'2': ['1', '4', '5'],
'3': ['1', '5'],
'4': ['2', '5', '6'],
'5': ['2', '3', '4', '6'],
'6': ['4', '5'],
1: ['2', '3'],
2: ['1', '4', '5'],
3: ['1', '5'],
4: ['2', '5', '6'],
5: ['2', '3', '4', '6'],
6: ['4', '5'],
};
expect(breadthFirstSearch(graph, '1')).toEqual([
'1',
Expand Down Expand Up @@ -132,13 +143,37 @@ describe('breadthFirstSearch', () => {

test('disjoint graphs', () => {
expect(
breadthFirstSearch({ '1': ['2'], '2': [], '3': [], '4': ['3'] }, '1'),
breadthFirstSearch(
{
1: ['2'],
2: [],
3: [],
4: ['3'],
},
'1',
),
).toEqual(['1', '2']);
expect(
breadthFirstSearch({ '1': ['2'], '2': [], '3': [], '4': ['3'] }, '3'),
breadthFirstSearch(
{
1: ['2'],
2: [],
3: [],
4: ['3'],
},
'3',
),
).toEqual(['3']);
expect(
breadthFirstSearch({ '1': ['2'], '2': [], '3': [], '4': ['3'] }, '4'),
breadthFirstSearch(
{
1: ['2'],
2: [],
3: [],
4: ['3'],
},
'4',
),
).toEqual(['4', '3']);
});
});
Loading

0 comments on commit 83dca6c

Please sign in to comment.