dsa.js
Advanced tools
Comparing version 2.5.1 to 2.6.0
@@ -0,1 +1,14 @@ | ||
# [2.6.0](https://github.com/amejiarosario/dsa.js/compare/2.5.1...2.6.0) (2020-10-28) | ||
### Bug Fixes | ||
* **test:** refactor tests ([571834a](https://github.com/amejiarosario/dsa.js/commit/571834a848d3b4c7d0dd8a94957b73724f3756ac)) | ||
### Features | ||
* **book:** add chapter numbers ([0f13f90](https://github.com/amejiarosario/dsa.js/commit/0f13f907141d0ad9bb439d131aca6d1d882421ee)) | ||
* **book/linkedlist:** linked lists techniques and common patterns ([8cd126d](https://github.com/amejiarosario/dsa.js/commit/8cd126d71a31473fefdbf0f0a9780cd7b128bcd6)) | ||
## [2.5.1](https://github.com/amejiarosario/dsa.js/compare/2.5.0...2.5.1) (2020-10-23) | ||
@@ -2,0 +15,0 @@ |
{ | ||
"name": "dsa.js", | ||
"version": "2.5.1", | ||
"version": "2.6.0", | ||
"description": "Data Structures & Algorithms in JS", | ||
@@ -18,6 +18,7 @@ "author": "Adrian Mejia <hi+dsajs@adrianmejia.com> (https://adrianmejia.com)", | ||
"watch": "jest --watch --coverage", | ||
"ci": "npx eslint src/ && jest --coverage", | ||
"lint:base": "npx eslint --fix '{src,book/interview-questions}/**/*.js'", | ||
"lint": "npm run lint:base -- --format codeframe", | ||
"ci": "npm run lint:base && jest --coverage", | ||
"coverage": "jest --coverage && open coverage/lcov-report/index.html", | ||
"coverage:win": "jest --coverage && cmd.exe /C start coverage/lcov-report/index.html", | ||
"lint": "npx eslint --fix --format codeframe src/", | ||
"semantic-release": "semantic-release", | ||
@@ -44,6 +45,6 @@ "release:check": "semantic-release --dry-run" | ||
"cz-conventional-changelog": "3.2.0", | ||
"eslint": "7.0.0", | ||
"eslint": "7.12.1", | ||
"eslint-config-airbnb-base": "14.1.0", | ||
"eslint-plugin-import": "2.20.2", | ||
"eslint-plugin-jest": "23.11.0", | ||
"eslint-plugin-jest": "24.1.0", | ||
"handlebars": "4.7.6", | ||
@@ -50,0 +51,0 @@ "husky": "4.2.5", |
@@ -12,3 +12,3 @@ const LRUCache = require('./lru-cache-3'); | ||
it('should initialize', () => { | ||
it('should initialize with capacity', () => { | ||
c = new LRUCache(7); | ||
@@ -15,0 +15,0 @@ expect(c.capacity).toEqual(7); |
@@ -255,7 +255,7 @@ const { Graph } = require('../../index'); | ||
it('should return true if two nodes are connected', () => { | ||
it('should return true if two nodes are connected to itself', () => { | ||
expect(graph.areConnected('you', 'you')).toBe(true); | ||
}); | ||
it('should return true if two nodes are connected', () => { | ||
it('should return true if two nodes are connected to other', () => { | ||
expect(graph.areConnected('you', 'John')).toBe(false); | ||
@@ -262,0 +262,0 @@ }); |
@@ -65,3 +65,3 @@ const Node = require('./node'); | ||
it('should return true if they are adjacent', () => { | ||
it('should return true if they are adjacent on c', () => { | ||
const c = new Node('c'); | ||
@@ -68,0 +68,0 @@ expect(node.isAdjacent(c)).toBe(false); |
@@ -16,3 +16,3 @@ const MedianHeap = require('./median-heap'); | ||
it('should work', () => { | ||
it('should work with 2 additions', () => { | ||
expect(medianHeap.add(1)).toEqual(undefined); | ||
@@ -34,3 +34,3 @@ expect(medianHeap.add(1)).toEqual(undefined); | ||
it('should work', () => { | ||
it('should work with even numbers', () => { | ||
const values = [5, 15, 1, 3]; | ||
@@ -44,3 +44,3 @@ const medians = values.map((v) => { | ||
it('should work', () => { | ||
it('should work with odd numbers', () => { | ||
const values = [2, 4, 7, 1, 5, 3]; | ||
@@ -47,0 +47,0 @@ const medians = values.map((v) => { |
const util = require('util'); | ||
const Node = require('./node'); | ||
const Node = require('./node'); // Doubly | ||
@@ -10,6 +10,14 @@ // tag::constructor[] | ||
class LinkedList { | ||
constructor(iterable = []) { | ||
constructor( | ||
iterable = [], | ||
// end::constructor[] | ||
ListNode = Node, // Node class (e.g. singly, doubly, multilevel) | ||
// tag::constructor[] | ||
) { | ||
this.first = null; // head/root element | ||
this.last = null; // last element of the list | ||
this.size = 0; // total number of elements in the list | ||
// end::constructor[] | ||
this.ListNode = ListNode; // ListNode class | ||
// tag::constructor[] | ||
@@ -24,6 +32,6 @@ Array.from(iterable, (i) => this.addLast(i)); | ||
* Runtime: O(1) | ||
* @param {any} value | ||
* @param {Node} value | ||
*/ | ||
addFirst(value) { | ||
const newNode = new Node(value); | ||
const newNode = new this.ListNode(value); | ||
@@ -251,6 +259,4 @@ newNode.next = this.first; | ||
* [Symbol.iterator]() { | ||
for (let node = this.first, position = 0; | ||
node; | ||
position += 1, node = node.next) { | ||
yield { node, position }; | ||
for (let node = this.first; node; node = node.next) { | ||
yield node; | ||
} | ||
@@ -261,3 +267,3 @@ } | ||
const parts = [...this]; // see [Symbol.iterator]() | ||
return parts.map((n) => util.inspect(n.node.value)).join(' -> '); | ||
return parts.map((n) => util.inspect(n.value)).join(' -> '); | ||
} | ||
@@ -264,0 +270,0 @@ |
@@ -63,3 +63,3 @@ const { LinkedList } = require('../../index'); | ||
describe('#addFirst', () => { | ||
it('add element to the head/root of the list', () => { | ||
it('add 1 element to the head/root of the list', () => { | ||
linkedList.addFirst('a'); | ||
@@ -70,3 +70,3 @@ expect(linkedList.first.value).toBe('a'); | ||
it('add element to the head/root of the list', () => { | ||
it('add 2 elements to the head/root of the list', () => { | ||
linkedList.addFirst('a'); | ||
@@ -222,3 +222,3 @@ linkedList.addFirst('b'); | ||
it('should remove last element', () => { | ||
it('should remove first element', () => { | ||
expect(linkedList.length).toBe(2); | ||
@@ -435,3 +435,22 @@ expect(linkedList.removeByPosition(0)).toBe(0); | ||
}); | ||
describe('iterator', () => { | ||
let a; | ||
let b; | ||
let c; | ||
let d; | ||
beforeEach(() => { | ||
a = linkedList.addLast('a'); | ||
b = linkedList.addLast('b'); | ||
c = linkedList.addLast('c'); | ||
d = linkedList.addLast('d'); | ||
}); | ||
it('should convert to array of nodes', () => { | ||
expect([...linkedList]).toEqual([a, b, c, d]); | ||
expect(Array.from(linkedList)).toEqual([a, b, c, d]); | ||
}); | ||
}); | ||
}); | ||
}); |
// tag::snippet[] | ||
/** | ||
* Node with reference to next and previous element | ||
* Linked List Node | ||
*/ | ||
// tag::singly[] | ||
class Node { | ||
@@ -9,7 +10,10 @@ constructor(value = null) { | ||
this.next = null; | ||
this.previous = null; // for doubly linked list | ||
// end::singly[] | ||
this.previous = null; // if doubly linked list | ||
// tag::singly[] | ||
} | ||
} | ||
// end::singly[] | ||
// end::snippet[] | ||
module.exports = Node; |
@@ -108,3 +108,3 @@ const { BinarySearchTree } = require('../../index'); | ||
it('should find future parent of a node that doesnt exist yet', () => { | ||
it('should find future parent of a node that doesnt exist yet with -1', () => { | ||
bst.add(5); | ||
@@ -111,0 +111,0 @@ bst.add(1); |
@@ -107,3 +107,3 @@ const { BinaryTreeNode } = require('../../index'); | ||
it('true if is parent left child', () => { | ||
it('true if is parent left child for sibling', () => { | ||
expect(s.isParentLeftChild).toBe(true); | ||
@@ -113,3 +113,3 @@ expect(s.isParentRightChild).toBe(false); | ||
it('true if is parent left child', () => { | ||
it('true if is parent left child for child', () => { | ||
expect(c.isParentLeftChild).toBe(false); | ||
@@ -116,0 +116,0 @@ expect(c.isParentRightChild).toBe(true); |
// data structures | ||
const LinkedList = require('./data-structures/linked-lists/linked-list'); | ||
const ListNode = require('./data-structures/linked-lists/node'); | ||
const Queue = require('./data-structures/queues/queue'); | ||
@@ -32,2 +33,3 @@ const Stack = require('./data-structures/stacks/stack'); | ||
LinkedList, | ||
ListNode, | ||
Queue, | ||
@@ -34,0 +36,0 @@ Stack, |
289546
8163