This repository has been archived by the owner on Nov 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli.js
executable file
·84 lines (65 loc) · 2.21 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env node
'use strict';
const parseJson = require('parse-json');
const jsonFile = require('jsonfile');
const color = require('cli-color');
const argv = require('minimist')(process.argv.slice(2));
const meow = require('meow');
const path = require('path');
const os = require('os');
const cli = meow(`
Usage
$ mega-sena <numbers>
Examples
$ mega-sena 05 06 12 19 30 60
😱 Você escolheu os números que foram sorteados no dia 04/06/2016!
`);
let wrongNumber = (argv._.length !== 6);
if (wrongNumber) {
console.error(color.red('Você precisa informar seis números.'));
process.exit(1);
};
let sequence = argv._;
jsonFile.readFile(path.resolve(__dirname) + '/results.json', function(err, response) {
let results = JSON.parse(JSON.stringify(response));
let wasFound = false;
var emoji = (os.platform() == 'win32') ? '\u00D6' : '😱';
results.forEach((result) => {
let totalDifference = sequence.diffCounter(result.numbers);
if (totalDifference <= 2) {
wasFound = true;
switch(totalDifference) {
case 0:
console.log(color.cyan(emoji + ' Você escolheu os números que foram sorteados no dia ' + result.date + '!'));
process.exit(0);
break;
case 1:
console.log(color.yellow(emoji + ' Você escolheu os números que foram quina no dia ' + result.date + '!'));
process.exit(0);
break;
case 2:
console.log(color.magenta(emoji + ' Você escolheu os números que foram quadra no dia ' + result.date + '!'));
process.exit(0);
break;
}
}
});
if (false === wasFound) {
emoji = (os.platform() == 'win32') ? '\u2665' : '❤️';
console.log(color.green(' Ihh, essa sequência nunca foi sorteada!\n Se for tentar a sorte e ganhar algo, lembre de mim... ' + emoji));
};
});
/**
* Returns the number of differences between the arrays
*/
Array.prototype.diffCounter = function(arr) {
let diffs = 6;
this.sort();
arr.sort();
for (var i = 0; i < this.length; i++) {
if (arr.indexOf(this[i]) !== -1 && this[i] != this[i+1] ) {//avoid duplicated numbers
diffs --;
};
};
return diffs;
};