-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolbox.js
70 lines (67 loc) · 2.08 KB
/
toolbox.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
/**
* This is the toolbox. Here we have the tools we need across all the platform. These functions prepare the data.
* @module toolbox
* @exports module:toolbox.cleanTitle
* @exports module:toolbox.hashGenerator
* @exports module:toolbox.validGamecode
* @exports module:toolbox.iGamecodeGenerator
*/
var crypto = require('crypto');
const request = require ("request");
/**
* Returns the title cleaned and prepared for database
* @module module:toolbox.cleanTitle
* @param {string} title - Title of a game.
* @returns {string} string cleaned
*/
function cleanTitle(title){
title = title.toLowerCase();
var re = /\W/g;
return title.replace(re, "");
}
/**
* Returns a hashed string by md5
* @requires module:crypto
* @param {string} string - String to hash
* @returns {string}
* @module module:toolbox.hasGenerator
*/
function hashGenerator (string) {
return crypto.createHash('md5').update(string).digest("hex");
}
/**
* Returns true or false depending is the gamecode is valid or not
* @module module:toolbox.validGamecode
* @param {string} gamecode - raw gamecode of the continent
* @returns {boolean}
*/
function validGamecode(gamecode) {
return typeof(gamecode) === "string" && gamecode.length > 3;
}
/**
* Returns the 4 digit international gamecode
* @module module:toolbox.iGamecodeGenerator
* @param {string} gamecode - raw gamecode of the continent
* @returns {string} The international gamecode
*/
function iGamecodeGenerator(gamecode) {
var char = gamecode.length
var start = char -1 ;
var finish = start - 4;
var intl_gamecode = gamecode.substring(start, finish);
return intl_gamecode;
}
//@see: https://stackoverflow.com/a/38428075
// @valkiriann: Sacar a la libreria (toolbox) de utilidades....
function promiseRequest(url) {
return new Promise(function (resolve, reject) {
request(url, function (error, res, body) {
if (!error && res.statusCode == 200) {
resolve(body);
} else {
reject(error);
}
});
});
}
module.exports = { hashGenerator, iGamecodeGenerator, validGamecode, cleanTitle, promiseRequest };