-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Export template function This facilitates wrapping template behavior in other functions, and not needing to reference the tag function in many places. * Add tests and types Additionally add typescript as a dev dependency * Update README
- Loading branch information
1 parent
bc92e93
commit e5a2993
Showing
6 changed files
with
106 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
import {expectType} from 'tsd'; | ||
import chalk from 'chalk'; | ||
import chalkTemplate from './index.js'; | ||
import chalkTemplate, {template} from './index.js'; | ||
|
||
// -- Template literal -- | ||
expectType<string>(chalkTemplate``); | ||
const name = 'John'; | ||
expectType<string>(chalkTemplate`Hello {bold.red ${name}}`); | ||
expectType<string>(chalkTemplate`Works with numbers {bold.red ${1}}`); | ||
|
||
expectType<string>(template('Today is {bold.red hot}')); | ||
|
||
// -- Complex template literal -- | ||
expectType<string>(chalk.red.bgGreen.bold(chalkTemplate`Hello {italic.blue ${name}}`)); | ||
expectType<string>(chalk.strikethrough.cyanBright.bgBlack(chalkTemplate`Works with {reset {bold numbers}} {bold.red ${1}}`)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import test from 'ava'; | ||
import {template} from '../index.js'; | ||
|
||
test('correctly parse and evaluate color-convert functions', t => { | ||
t.is(template('{bold.rgb(144,10,178).inverse Hello, {~inverse there!}}'), | ||
'\u001B[1m\u001B[38;2;144;10;178m\u001B[7mHello, ' | ||
+ '\u001B[27m\u001B[39m\u001B[22m\u001B[1m' | ||
+ '\u001B[38;2;144;10;178mthere!\u001B[39m\u001B[22m'); | ||
|
||
t.is(template('{bold.bgRgb(144,10,178).inverse Hello, {~inverse there!}}'), | ||
'\u001B[1m\u001B[48;2;144;10;178m\u001B[7mHello, ' | ||
+ '\u001B[27m\u001B[49m\u001B[22m\u001B[1m' | ||
+ '\u001B[48;2;144;10;178mthere!\u001B[49m\u001B[22m'); | ||
}); | ||
|
||
test('properly handle escapes', t => { | ||
t.is(template('{bold hello \\{in brackets\\}}'), | ||
'\u001B[1mhello {in brackets}\u001B[22m'); | ||
}); | ||
|
||
test('throw if there is an unclosed block', t => { | ||
t.throws(() => { | ||
template('{bold this shouldn\'t work ever\\}'); | ||
}, { | ||
message: 'Chalk template literal is missing 1 closing bracket (`}`)', | ||
}); | ||
|
||
t.throws(() => { | ||
template('{bold this shouldn\'t {inverse appear {underline ever\\} :) \\}'); | ||
}, { | ||
message: 'Chalk template literal is missing 3 closing brackets (`}`)', | ||
}); | ||
}); | ||
|
||
test('throw if there is an invalid style', t => { | ||
t.throws(() => { | ||
template('{abadstylethatdoesntexist this shouldn\'t work ever}'); | ||
}, { | ||
message: 'Unknown Chalk style: abadstylethatdoesntexist', | ||
}); | ||
}); | ||
|
||
test('properly style multiline color blocks', t => { | ||
t.is( | ||
template(`{bold | ||
Hello! This is a | ||
${'multiline'} block! | ||
:) | ||
} {underline | ||
I hope you enjoy | ||
}`), | ||
'\u001B[1m\u001B[22m\n' | ||
+ '\u001B[1m\t\t\tHello! This is a\u001B[22m\n' | ||
+ '\u001B[1m\t\t\tmultiline block!\u001B[22m\n' | ||
+ '\u001B[1m\t\t\t:)\u001B[22m\n' | ||
+ '\u001B[1m\t\t\u001B[22m \u001B[4m\u001B[24m\n' | ||
+ '\u001B[4m\t\t\tI hope you enjoy\u001B[24m\n' | ||
+ '\u001B[4m\t\t\u001B[24m', | ||
); | ||
}); | ||
|
||
test('should allow bracketed Unicode escapes', t => { | ||
t.is(template('\u{AB}'), '\u{AB}'); | ||
t.is(template('This is a {bold \u{AB681}} test'), 'This is a \u001B[1m\u{AB681}\u001B[22m test'); | ||
t.is(template('This is a {bold \u{10FFFF}} test'), 'This is a \u001B[1m\u{10FFFF}\u001B[22m test'); | ||
}); | ||
|
||
test('should handle special hex case', t => { | ||
t.is(template('{#FF0000 hello}'), '\u001B[38;2;255;0;0mhello\u001B[39m'); | ||
t.is(template('{#:FF0000 hello}'), '\u001B[48;2;255;0;0mhello\u001B[49m'); | ||
t.is(template('{#00FF00:FF0000 hello}'), '\u001B[38;2;0;255;0m\u001B[48;2;255;0;0mhello\u001B[49m\u001B[39m'); | ||
t.is(template('{bold.#FF0000 hello}'), '\u001B[1m\u001B[38;2;255;0;0mhello\u001B[39m\u001B[22m'); | ||
t.is(template('{bold.#:FF0000 hello}'), '\u001B[1m\u001B[48;2;255;0;0mhello\u001B[49m\u001B[22m'); | ||
t.is(template('{bold.#00FF00:FF0000 hello}'), '\u001B[1m\u001B[38;2;0;255;0m\u001B[48;2;255;0;0mhello\u001B[49m\u001B[39m\u001B[22m'); | ||
t.is(template('{#FF0000.bold hello}'), '\u001B[38;2;255;0;0m\u001B[1mhello\u001B[22m\u001B[39m'); | ||
t.is(template('{#:FF0000.bold hello}'), '\u001B[48;2;255;0;0m\u001B[1mhello\u001B[22m\u001B[49m'); | ||
t.is(template('{#00FF00:FF0000.bold hello}'), '\u001B[38;2;0;255;0m\u001B[48;2;255;0;0m\u001B[1mhello\u001B[22m\u001B[49m\u001B[39m'); | ||
}); |