Bez popisu

index.js 977B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 'use strict';
  2. var BEEP_DELAY = 500;
  3. function beep() {
  4. process.stdout.write('\u0007');
  5. }
  6. function melodicalBeep(val, cb) {
  7. if (val.length === 0) {
  8. cb();
  9. return;
  10. }
  11. setTimeout(function () {
  12. if (val.shift() === '*') {
  13. beep();
  14. }
  15. melodicalBeep(val, cb);
  16. }, BEEP_DELAY);
  17. }
  18. module.exports = function (val, cb) {
  19. if (!process.stdout.isTTY ||
  20. process.argv.indexOf('--no-beep') !== -1 ||
  21. process.argv.indexOf('--beep=false') !== -1) {
  22. return;
  23. }
  24. cb = cb || function () {};
  25. if (val === parseInt(val)) {
  26. if (val < 0) {
  27. throw new TypeError('Negative numbers are not accepted');
  28. }
  29. if (val === 0) {
  30. cb();
  31. return;
  32. }
  33. for (var i = 0; i < val; i++) {
  34. setTimeout(function (i) {
  35. beep();
  36. if (i === val - 1) {
  37. cb();
  38. }
  39. }, BEEP_DELAY * i, i);
  40. }
  41. } else if (!val) {
  42. beep();
  43. cb();
  44. } else if (typeof val === 'string') {
  45. melodicalBeep(val.split(''), cb);
  46. } else {
  47. throw new TypeError('Not an accepted type');
  48. }
  49. };