Skip to content

Commit 9ec2241

Browse files
💥 BREAKING CHANGE: Remove chalk dependency.
Fixes #82.
1 parent 2954e7e commit 9ec2241

File tree

4 files changed

+427
-947
lines changed

4 files changed

+427
-947
lines changed

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
"bugs": {
2727
"url": "https://github.com/aureooms/js-red-black-tree/issues"
2828
},
29-
"dependencies": {
30-
"chalk": "^4.0.0"
31-
},
3229
"devDependencies": {
3330
"@aureooms/js-compare": "1.4.8",
3431
"@aureooms/js-itertools": "4.1.0",
@@ -39,6 +36,7 @@
3936
"@babel/preset-env": "7.12.11",
4037
"@babel/register": "7.12.10",
4138
"ava": "3.15.0",
39+
"chalk": "^4.1.0",
4240
"coveralls": "3.1.0",
4341
"esdoc": "1.1.0",
4442
"esdoc-inject-script-plugin": "1.0.0",

src/debug/debug.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import chalk from 'chalk' ;
2-
31
import { BLACK } from '..' ;
42

53
/**
@@ -9,12 +7,18 @@ import { BLACK } from '..' ;
97
* @param {Node} root - The root of the tree.
108
* @returns {String}
119
*/
12-
export function debug ( root ) {
10+
export function _debug ({red, black}) {
11+
12+
const debug = ( root ) => {
13+
14+
if (root.isleaf( )) return black('L');
15+
16+
const repr = root.color === BLACK ? black(root.key) : red(root.key);
1317

14-
if ( root.isleaf( ) ) return chalk.bgBlack('L') ;
18+
return `(${debug(root.left)}, ${repr}, ${debug(root.right)})`;
1519

16-
const repr = root.color === BLACK ? chalk.bgBlack(root.key) : chalk.bgRed( root.key ) ;
20+
};
1721

18-
return `(${debug(root.left)}, ${repr}, ${debug(root.right)})` ;
22+
return debug;
1923

2024
}

test/src/RedBlackTree/remove.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ import { list , range , sorted , head , iter , exhaust } from '@aureooms/js-iter
66

77
import { randint , shuffle } from '@aureooms/js-random' ;
88

9-
import { RedBlackTree , debug } from '../../../src' ;
9+
import { RedBlackTree , _debug } from '../../../src' ;
1010

11-
import chalk from 'chalk' ;
11+
import { bgRed, bgBlack } from 'chalk' ;
12+
13+
const red = bgRed;
14+
const black = bgBlack;
15+
const debug = _debug({red, black});
1216

1317
test( 'RedBlackTree::remove' , t => {
1418

@@ -72,15 +76,15 @@ test( 'delete root with right child' , t => {
7276
tree.add( 0 ) ;
7377
tree.add( 1 ) ;
7478

75-
const repr1 = `(${chalk.bgBlack('L')}, ${chalk.bgBlack(0)}, (${chalk.bgBlack('L')}, ${chalk.bgRed(1)}, ${chalk.bgBlack('L')}))` ;
79+
const repr1 = `(${black('L')}, ${black(0)}, (${black('L')}, ${red(1)}, ${black('L')}))` ;
7680

7781
t.deepEqual( debug( tree.root ) , repr1 , 'debug string 1 is correct' ) ;
7882

7983
tree.remove( 0 ) ;
8084

8185
t.deepEqual( list( tree ) , [ 1 ] ) ;
8286

83-
const repr2 = `(${chalk.bgBlack('L')}, ${chalk.bgBlack(1)}, ${chalk.bgBlack('L')})` ;
87+
const repr2 = `(${black('L')}, ${black(1)}, ${black('L')})` ;
8488

8589
t.deepEqual( debug( tree.root ) , repr2 , 'debug string 2 is correct' ) ;
8690

@@ -93,15 +97,15 @@ test( 'delete root with left child' , t => {
9397
tree.add( 0 ) ;
9498
tree.add( -1 ) ;
9599

96-
const repr1 = `((${chalk.bgBlack('L')}, ${chalk.bgRed(-1)}, ${chalk.bgBlack('L')}), ${chalk.bgBlack(0)}, ${chalk.bgBlack('L')})` ;
100+
const repr1 = `((${black('L')}, ${red(-1)}, ${black('L')}), ${black(0)}, ${black('L')})` ;
97101

98102
t.deepEqual( debug( tree.root ) , repr1 , 'debug string 1 is correct' ) ;
99103

100104
tree.remove( 0 ) ;
101105

102106
t.deepEqual( list( tree ) , [ -1 ] ) ;
103107

104-
const repr2 = `(${chalk.bgBlack('L')}, ${chalk.bgBlack(-1)}, ${chalk.bgBlack('L')})` ;
108+
const repr2 = `(${black('L')}, ${black(-1)}, ${black('L')})` ;
105109

106110
t.deepEqual( debug( tree.root ) , repr2 , 'debug string 2 is correct' ) ;
107111

0 commit comments

Comments
 (0)