mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-24 21:46:13 +08:00
Add jest tests.
This commit is contained in:
parent
eb3eadaf10
commit
ad0921d05e
10
.babelrc
10
.babelrc
@ -1,11 +1,3 @@
|
||||
{
|
||||
"presets": [
|
||||
[
|
||||
"env", {
|
||||
"targets": {
|
||||
"node": "current"
|
||||
}
|
||||
}
|
||||
]
|
||||
]
|
||||
"presets": ["env"]
|
||||
}
|
||||
|
8
.eslintrc
Normal file
8
.eslintrc
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"root": true,
|
||||
"extends": "airbnb",
|
||||
"plugins": ["jest"],
|
||||
"env": {
|
||||
"jest/globals": true
|
||||
}
|
||||
}
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,2 @@
|
||||
node_modules
|
||||
build
|
||||
.idea
|
||||
|
5681
package-lock.json
generated
5681
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
14
package.json
14
package.json
@ -4,8 +4,8 @@
|
||||
"description": "Data structures on JavaScript",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"build": "babel src -d build",
|
||||
"babel-node": "babel-node"
|
||||
"lint": "eslint ./src/*",
|
||||
"test": "jest"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -19,7 +19,15 @@
|
||||
},
|
||||
"homepage": "https://github.com/trekhleb/data-structures#readme",
|
||||
"devDependencies": {
|
||||
"@types/jest": "^22.2.2",
|
||||
"babel-cli": "^6.26.0",
|
||||
"babel-preset-env": "^1.6.1"
|
||||
"babel-preset-env": "^1.6.1",
|
||||
"eslint": "^4.19.1",
|
||||
"eslint-config-airbnb": "^16.1.0",
|
||||
"eslint-plugin-import": "^2.9.0",
|
||||
"eslint-plugin-jest": "^21.15.0",
|
||||
"eslint-plugin-jsx-a11y": "^6.0.3",
|
||||
"eslint-plugin-react": "^7.7.0",
|
||||
"jest": "^22.4.3"
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { Node } from './Node';
|
||||
import Node from './Node';
|
||||
|
||||
export class LinkedList {
|
||||
export default class LinkedList {
|
||||
constructor() {
|
||||
this.head = null;
|
||||
}
|
||||
@ -17,12 +17,12 @@ export class LinkedList {
|
||||
|
||||
// Rewind to last node.
|
||||
let currentNode = this.head;
|
||||
while (currentNode.nextNode !== null) {
|
||||
currentNode = currentNode.nextNode;
|
||||
while (currentNode.next !== null) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
// Attach new node to the end of linked list.
|
||||
currentNode.nextNode = newNode;
|
||||
currentNode.next = newNode;
|
||||
|
||||
return newNode;
|
||||
}
|
||||
@ -31,21 +31,19 @@ export class LinkedList {
|
||||
this.head = new Node(value, this.head);
|
||||
}
|
||||
|
||||
deleteWithValue(value) {
|
||||
|
||||
}
|
||||
|
||||
render() {
|
||||
toArray() {
|
||||
const listArray = [];
|
||||
let currentNode = this.head;
|
||||
let index = 0;
|
||||
|
||||
while(currentNode.nextNode !== null) {
|
||||
console.log(`Node #${index} data: ${currentNode.value}`);
|
||||
currentNode = currentNode.nextNode;
|
||||
index++;
|
||||
while (currentNode) {
|
||||
listArray.push(currentNode.value);
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
// Print the tail.
|
||||
console.log(`Node #${index} data: ${currentNode.value}`);
|
||||
return listArray;
|
||||
}
|
||||
|
||||
toString() {
|
||||
return this.toArray().toString();
|
||||
}
|
||||
}
|
6
src/data-structures/linked-list/Node.js
Normal file
6
src/data-structures/linked-list/Node.js
Normal file
@ -0,0 +1,6 @@
|
||||
export default class Node {
|
||||
constructor(value, next = null) {
|
||||
this.value = value;
|
||||
this.next = next;
|
||||
}
|
||||
}
|
23
src/data-structures/linked-list/__test__/LinkedList.test.js
Normal file
23
src/data-structures/linked-list/__test__/LinkedList.test.js
Normal file
@ -0,0 +1,23 @@
|
||||
import LinkedList from '../LinkedList';
|
||||
|
||||
describe('LinkedList', () => {
|
||||
it('should create empty linked list', () => {
|
||||
const linkedList = new LinkedList();
|
||||
expect(linkedList).toBeDefined();
|
||||
});
|
||||
|
||||
it('should append node to linked list', () => {
|
||||
const linkedList = new LinkedList();
|
||||
linkedList.append(1);
|
||||
linkedList.append(2);
|
||||
expect(linkedList.toString()).toBe('1,2');
|
||||
});
|
||||
|
||||
it('should prepend node to linked list', () => {
|
||||
const linkedList = new LinkedList();
|
||||
linkedList.append(1);
|
||||
linkedList.append(2);
|
||||
linkedList.prepend(3);
|
||||
expect(linkedList.toString()).toBe('3,1,2');
|
||||
});
|
||||
});
|
9
src/data-structures/linked-list/__test__/Node.test.js
Normal file
9
src/data-structures/linked-list/__test__/Node.test.js
Normal file
@ -0,0 +1,9 @@
|
||||
import Node from '../Node';
|
||||
|
||||
describe('Node', () => {
|
||||
it('should create list node with value', () => {
|
||||
const node = new Node(1);
|
||||
expect(node.value).toBe(1);
|
||||
expect(node.next).toBeNull();
|
||||
});
|
||||
});
|
@ -1,6 +0,0 @@
|
||||
export class Node {
|
||||
constructor(value, nextNode = null) {
|
||||
this.value = value;
|
||||
this.nextNode = nextNode;
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
import { LinkedList } from "./LinkedList";
|
||||
|
||||
const linkedList = new LinkedList();
|
||||
|
||||
linkedList.append(1);
|
||||
linkedList.append(2);
|
||||
linkedList.append(3);
|
||||
linkedList.prepend(4);
|
||||
linkedList.prepend(5);
|
||||
|
||||
linkedList.render();
|
Loading…
Reference in New Issue
Block a user