Update and rename rainbow.js to rainbow.ts

This commit is contained in:
l198881 2021-05-29 18:47:46 -03:00 committed by GitHub
parent 83742e2773
commit 0356032002
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 38 deletions

View file

@ -1,38 +0,0 @@
'use strict';
const chalk = require('..');
const ignoreChars = /[^!-~]/g;
function rainbow(str, offset) {
if (!str || str.length === 0) {
return str;
}
const hueStep = 360 / str.replace(ignoreChars, '').length;
let hue = offset % 360;
const chars = [];
for (const c of str) {
if (c.match(ignoreChars)) {
chars.push(c);
} else {
chars.push(chalk.hsl(hue, 100, 50)(c));
hue = (hue + hueStep) % 360;
}
}
return chars.join('');
}
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
async function animateString(str) {
console.log();
for (let i = 0; i < 360 * 5; i++) {
console.log('\u001B[1F\u001B[G ', rainbow(str, i));
await sleep(2); // eslint-disable-line no-await-in-loop
}
}
console.log();
animateString('We hope you enjoy the new version of Chalk 2! <3').then(() => console.log());

38
examples/rainbow.ts Normal file
View file

@ -0,0 +1,38 @@
'use strict';
chalk = require('..');
ignoreChars = /[^!-~]/g;
rainbow(str, offset) {
(!str || str.length === 0) {
str;
}
hueStep = 360 / str.replace(ignoreChars, '').length;
hue = offset % 360;
chars = [];
( c str) {
(c.match(ignoreChars)) {
chars.push(c);
} {
chars.push(chalk.hsl(hue, 100, 50)(c));
hue = (hue + hueStep) % 360;
}
}
chars.join('');
}
sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
M animateString(str) {
console.log();
( i = 0; i < 360 * 5; i++) {
console.log('\u001B[1F\u001B[G ', rainbow(str, i));
sleep(2); // eslint-disable-line no-await-in-loop
}
}
console.log();
animateString('We hope you enjoy the new version of Chalk 2! <3').then(() => console.log());