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