Skip to content

Commit 18d1ebc

Browse files
👕 refactor: Lint all JavaScript files.
1 parent f01d238 commit 18d1ebc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+559
-670
lines changed

doc/scripts/header.js

+18-22
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
1-
var domReady = function(callback) {
2-
var state = document.readyState ;
3-
if ( state === 'interactive' || state === 'complete' ) {
4-
callback() ;
5-
}
6-
else {
1+
const domReady = function (callback) {
2+
const state = document.readyState;
3+
if (state === 'interactive' || state === 'complete') {
4+
callback();
5+
} else {
76
document.addEventListener('DOMContentLoaded', callback);
87
}
9-
} ;
10-
8+
};
119

12-
domReady(function(){
13-
14-
var projectname = document.createElement('a');
10+
domReady(() => {
11+
const projectname = document.createElement('a');
1512
projectname.classList.add('project-name');
1613
projectname.text = 'aureooms/js-red-black-tree';
17-
projectname.href = './index.html' ;
14+
projectname.href = './index.html';
1815

19-
var header = document.getElementsByTagName('header')[0] ;
20-
header.insertBefore(projectname,header.firstChild);
16+
const header = document.querySelectorAll('header')[0];
17+
header.insertBefore(projectname, header.firstChild);
2118

22-
var testlink = document.querySelector('header > a[data-ice="testLink"]') ;
23-
testlink.href = 'https://coveralls.io/github/aureooms/js-red-black-tree' ;
24-
testlink.target = '_BLANK' ;
19+
const testlink = document.querySelector('header > a[data-ice="testLink"]');
20+
testlink.href = 'https://coveralls.io/github/aureooms/js-red-black-tree';
21+
testlink.target = '_BLANK';
2522

26-
var searchBox = document.querySelector('.search-box');
27-
var input = document.querySelector('.search-input');
23+
const searchBox = document.querySelector('.search-box');
24+
const input = document.querySelector('.search-input');
2825

29-
// active search box when focus on searchBox.
30-
input.addEventListener('focus', function(){
26+
// Active search box when focus on searchBox.
27+
input.addEventListener('focus', () => {
3128
searchBox.classList.add('active');
3229
});
33-
3430
});

src/adt/Leaf.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BLACK } from '../index.js' ;
1+
import {BLACK} from '../index.js';
22

33
/**
44
* A black leaf node.
@@ -7,9 +7,9 @@ import { BLACK } from '../index.js' ;
77
* @param {Node} parent - The parent node in the tree.
88
* @returns {Leaf}
99
*/
10-
export function Leaf ( parent ) {
11-
this.color = BLACK ;
12-
this.parent = parent ;
10+
export function Leaf(parent) {
11+
this.color = BLACK;
12+
this.parent = parent;
1313
}
1414

1515
/**
@@ -18,4 +18,6 @@ export function Leaf ( parent ) {
1818
*
1919
* @returns {Boolean}
2020
*/
21-
Leaf.prototype.isleaf = function ( ) { return true ; } ;
21+
Leaf.prototype.isleaf = function () {
22+
return true;
23+
};

src/adt/Node.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Leaf } from "./Leaf.js" ;
1+
import {Leaf} from './Leaf.js';
22

33
/**
44
* An internal node. This node can be red or black.
@@ -8,12 +8,12 @@ import { Leaf } from "./Leaf.js" ;
88
* @param {Key} key - The key of the node.
99
* @returns {Node}
1010
*/
11-
export function Node ( color , key ) {
12-
this.color = color ;
13-
this.left = new Leaf( this ) ;
14-
this.right = new Leaf( this ) ;
15-
this.parent = null ;
16-
this.key = key ;
11+
export function Node(color, key) {
12+
this.color = color;
13+
this.left = new Leaf(this);
14+
this.right = new Leaf(this);
15+
this.parent = null;
16+
this.key = key;
1717
}
1818

1919
/**
@@ -22,4 +22,6 @@ export function Node ( color , key ) {
2222
*
2323
* @returns {Boolean}
2424
*/
25-
Node.prototype.isleaf = function ( ) { return false ; } ;
25+
Node.prototype.isleaf = function () {
26+
return false;
27+
};

src/adt/RedBlackTree.js

+63-81
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
2-
import { Node } from "./Node.js" ;
3-
import { RED , BLACK } from '../index.js' ;
4-
import { predecessor } from '../index.js' ;
5-
import { insert , insert_case2 } from '../index.js' ;
6-
import { delete_one_child } from '../index.js' ;
7-
import { search } from '../index.js' ;
8-
import { inordertraversal , rangetraversal } from '../index.js' ;
1+
import {Node} from './Node.js';
2+
import {
3+
RED,
4+
BLACK,
5+
predecessor,
6+
insert,
7+
insert_case2,
8+
delete_one_child,
9+
search,
10+
inordertraversal,
11+
rangetraversal,
12+
} from '../index.js';
913

1014
/**
1115
* A RedBlackTree with key-only nodes.
1216
*
1317
*/
1418
export class RedBlackTree {
15-
1619
/**
1720
* Constructs a new empty red-black tree.
1821
*
1922
* @param {Function} compare - The comparison function for node keys.
2023
* @returns {RedBlackTree}
2124
*/
22-
constructor ( compare ) {
23-
24-
this.compare = compare ;
25-
this.root = null ;
26-
25+
constructor(compare) {
26+
this.compare = compare;
27+
this.root = null;
2728
}
2829

2930
/**
3031
* Adds a key to the tree.
3132
*
3233
* @param {Key} key - The key to add.
3334
*/
34-
add ( key ) {
35-
if ( this.root === null ) {
36-
this.root = new Node( BLACK , key ) ;
37-
}
38-
else {
39-
const node = new Node( RED , key ) ;
40-
insert( this.compare , this.root , node ) ;
41-
insert_case2( node ) ;
35+
add(key) {
36+
if (this.root === null) {
37+
this.root = new Node(BLACK, key);
38+
} else {
39+
const node = new Node(RED, key);
40+
insert(this.compare, this.root, node);
41+
insert_case2(node);
4242
}
4343
}
4444

@@ -50,9 +50,9 @@ export class RedBlackTree {
5050
* @param {Key} key - The input key.
5151
* @returns {Node}
5252
*/
53-
_search ( key ) {
54-
if ( this.root === null ) return null ;
55-
return search( this.compare , this.root , key ) ;
53+
_search(key) {
54+
if (this.root === null) return null;
55+
return search(this.compare, this.root, key);
5656
}
5757

5858
/**
@@ -63,9 +63,9 @@ export class RedBlackTree {
6363
* @param {Key} key - The input key.
6464
* @returns {Key}
6565
*/
66-
get ( key ) {
67-
const node = this._search( key ) ;
68-
return node === null ? null : node.key ;
66+
get(key) {
67+
const node = this._search(key);
68+
return node === null ? null : node.key;
6969
}
7070

7171
/**
@@ -75,47 +75,39 @@ export class RedBlackTree {
7575
* @param {Key} key - The input key.
7676
* @returns {Boolean}
7777
*/
78-
has ( key ) {
79-
return this._search( key ) !== null ;
78+
has(key) {
79+
return this._search(key) !== null;
8080
}
8181

8282
/**
8383
* Deletes the input node from the tree.
8484
*
8585
* @param {Node} node - The input node to delete.
8686
*/
87-
_delete ( node ) {
88-
89-
if ( !node.left.isleaf() ) {
90-
// replace node's key with predecessor's key
91-
const pred = predecessor( node ) ;
92-
node.key = pred.key ;
93-
// delete predecessor node
87+
_delete(node) {
88+
if (!node.left.isleaf()) {
89+
// Replace node's key with predecessor's key
90+
const pred = predecessor(node);
91+
node.key = pred.key;
92+
// Delete predecessor node
9493
// note: this node can only have one non-leaf child
9594
// because the tree is a red-black tree
96-
delete_one_child( pred ) ;
97-
}
98-
99-
else if ( !node.right.isleaf() ) {
100-
// replace node's key with successor's key
95+
delete_one_child(pred);
96+
} else if (!node.right.isleaf()) {
97+
// Replace node's key with successor's key
10198
// If there is no left child, then there can only be one right
10299
// child.
103-
const succ = node.right ;
104-
node.key = succ.key ;
105-
// delete successor node
100+
const succ = node.right;
101+
node.key = succ.key;
102+
// Delete successor node
106103
// note: this node can only have one non-leaf child
107104
// because the tree is a red-black tree
108-
delete_one_child( succ ) ;
109-
}
110-
111-
else if ( node === this.root ) {
112-
this.root = null ;
113-
}
114-
115-
else {
116-
delete_one_child( node ) ;
105+
delete_one_child(succ);
106+
} else if (node === this.root) {
107+
this.root = null;
108+
} else {
109+
delete_one_child(node);
117110
}
118-
119111
}
120112

121113
/**
@@ -127,14 +119,12 @@ export class RedBlackTree {
127119
* @param {Key} key - The input key.
128120
* @returns {Boolean} - Whether the key existed in the tree before removal.
129121
*/
130-
remove ( key ) {
131-
132-
const node = this._search( key ) ;
133-
if ( node === null ) return false ;
134-
135-
this._delete( node ) ;
136-
return true ;
122+
remove(key) {
123+
const node = this._search(key);
124+
if (node === null) return false;
137125

126+
this._delete(node);
127+
return true;
138128
}
139129

140130
/**
@@ -144,30 +134,25 @@ export class RedBlackTree {
144134
* @param {Key} right - The right bound of the interval.
145135
* @returns {Iterator}
146136
*/
147-
*range ( left , right ) {
148-
149-
if ( this.root !== null ) yield* rangetraversal( this.compare , this.root , left , right ) ;
150-
137+
*range(left, right) {
138+
if (this.root !== null)
139+
yield* rangetraversal(this.compare, this.root, left, right);
151140
}
152141

153142
/**
154143
* Returns an in order iterator over the keys of the tree.
155144
*
156145
* @returns {Iterator}
157146
*/
158-
*items ( ) {
159-
160-
if ( this.root !== null ) yield* inordertraversal( this.root ) ;
161-
147+
*items() {
148+
if (this.root !== null) yield* inordertraversal(this.root);
162149
}
163150

164151
/**
165152
* Same as {@link RedBlackTree#items}.
166153
*/
167-
[Symbol.iterator] ( ) {
168-
169-
return this.items() ;
170-
154+
[Symbol.iterator]() {
155+
return this.items();
171156
}
172157

173158
/**
@@ -177,14 +162,11 @@ export class RedBlackTree {
177162
* @param {Iterbale} iterable - The input iterable.
178163
* @returns {RedBlackTree}
179164
*/
180-
static from ( compare , iterable ) {
165+
static from(compare, iterable) {
166+
const tree = new RedBlackTree(compare);
181167

182-
const tree = new RedBlackTree( compare ) ;
183-
184-
for ( const element of iterable ) tree.add( element ) ;
185-
186-
return tree ;
168+
for (const element of iterable) tree.add(element);
187169

170+
return tree;
188171
}
189-
190172
}

src/adt/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export * from "./Leaf.js" ;
2-
export * from "./Node.js" ;
3-
export * from "./RedBlackTree.js" ;
1+
export * from './Leaf.js';
2+
export * from './Node.js';
3+
export * from './RedBlackTree.js';

src/color/BLACK.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/**
22
* Constant for black.
33
*/
4-
export const BLACK = 0 ;
4+
export const BLACK = 0;

src/color/RED.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/**
22
* Constant for red.
33
*/
4-
export const RED = 1 ;
4+
export const RED = 1;

src/color/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from "./BLACK.js" ;
2-
export * from "./RED.js" ;
1+
export * from './BLACK.js';
2+
export * from './RED.js';

0 commit comments

Comments
 (0)