johngrimm-utils
Version:
Module created for facilitated your life ;) Bringing some native PHP modules to the NODE and Create new modules
253 lines (208 loc) • 7.52 kB
JavaScript
const request = require('request')
const fs = require('fs')
let open = fs.readFileSync(__dirname + '/bins.csv', 'utf-8')
module.exports = {
/**
* function for exploding strings
*
* @param {String} date a String
* @param {String} start a String initial to split
* @param {Stringe} end a String final to split
* @param {Number} i an index number
*/
GetStr(date, start, end, i = 0) {
return date.split(start)[1].split(end)[i];
},
/**
* function async/await for sleeping your application for some time
*
* @param {Number} ms a number in ms
*/
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
},
/**
* function async/await for famous Curl (for request in web or applications)
*
* @param {Object} config a Object with your config to request
* @param {Boolean} req a value Boolean
* @return {Promise} Return a Promise
*/
curl(config, req = null) {
request.defaults({ rejectUnauthorized: false });
if (req) {
return new Promise(resolve => request(config, (error, res, body) => resolve(res)));
} else {
return new Promise(resolve => request(config, (error, res, body) => resolve(body)));
}
},
/**
* function native of PHP to randomize numbers
*
* @param {Number} min
* @param {Number} max
*/
rand(min, max) {
if (min == 0) {
return Math.floor((Math.random() * max) + 0);
} else {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
},
/**
* function for randomize strings or keys in array
*
* @param {Array} keys an array to return a random value
*/
randomizador(keys) {
var keys = keys
var random = Math.floor(Math.random() * keys.length);
return keys[random]
},
/**
* function for generate CPF valid
*
* @param {Boolean} comPontos a value Boolean for generate CPF with punctuation
*/
gerarCpf(comPontos = false) {
var numbers = []
for (var i = 0; i < 11; i++) {
numbers.push(Math.round(Math.random() * 9));
}
var [n1, n2, n3, n4, n5, n6, n7, n8, n9] = numbers;
var d1 = n9 * 2 + n8 * 3 + n7 * 4 + n6 * 5 + n5 * 6 + n4 * 7 + n3 * 8 + n2 * 9 + n1 * 10;
d1 = 11 - (Math.round(d1 - (Math.floor(d1 / 11) * 11)));
if (d1 >= 10) d1 = 0;
var d2 = d1 * 2 + n9 * 3 + n8 * 4 + n7 * 5 + n6 * 6 + n5 * 7 + n4 * 8 + n3 * 9 + n2 * 10 + n1 * 11;
d2 = 11 - (Math.round(d2 - (Math.floor(d2 / 11) * 11)));
if (d2 >= 10) d2 = 0;
if (comPontos) return '' + n1 + n2 + n3 + '.' + n4 + n5 + n6 + '.' + n7 + n8 + n9 + '-' + d1 + d2;
else return '' + n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + d1 + d2;
},
/**
* function for testing your proxy in server lumtest
*
* @param {String} proxy proxy
*/
proxy(proxy) {
return new Promise(resolve => request({ url: 'http://lumtest.com/myip.json', method: 'GET', proxy: `${proxy}`, timeout: 10000 }, (error, res, body) => resolve(body)));
},
/**
* function for testing your proxy in server Google
*
* @param {String} proxy proxy
*/
proxy1(proxy) {
return new Promise(resolve => request({ url: 'https://google.com', method: 'GET', proxy: `${proxy}`, timeout: 10000 }, (error, res, body) => resolve(body)));
},
/**
* function to include code from another file
*
* @param {*} file
*/
include(file) {
return eval(fs.readFileSync(file) + '');
},
/**
* function equivalent to substr_count in php
* @param {String} haystack
* @param {String} needle
* @param {Number} offset
* @param {Number} length
*/
substr_count(haystack, needle, offset, length) {
// example 1: substr_count('Kevin van Zonneveld', 'e')
// returns 1: 3
// example 2: substr_count('Kevin van Zonneveld', 'K', 1)
// returns 2: 0
// example 3: substr_count('Kevin van Zonneveld', 'Z', 0, 10)
// returns 3: false
var cnt = 0
haystack += ''
needle += ''
if (isNaN(offset)) {
offset = 0
}
if (isNaN(length)) {
length = 0
}
if (needle.length === 0) {
return false
}
offset--
while ((offset = haystack.indexOf(needle, offset + 1)) !== -1) {
if (length > 0 && (offset + needle.length) > length) {
return false
}
cnt++
}
return cnt
},
/**
* function to generate Gift
*
* @param {String} string a String with various "x"
*/
gift(string) {
return string.replace(/[x]/gi, () => Math.random().toString(26)[5].toUpperCase())
},
/**
* function to check bin
*
* @param {[Number, String]} cc a Number or String of card
* @return {Object} Return a Object with: Brand, Type, Nivel, Bank and Country
*/
bin(cc) {
cc = cc.substr(0, 6)
let lines = open.split(/\r?\n/g)
if (open.includes(cc)) {
for (let index = 0; index < lines.length; index++) {
const element = lines[index].split(';')
if (element[0] == cc) {
var bandeira = element[1];
var tipo = element[2];
var nivel = element[3];
var pais = element[4];
var banco = element[5];
}
}
}
return {
brand: bandeira,
type: tipo,
nivel: nivel,
bank: banco,
country: pais
}
},
/**
* function to Generate Person BR
*
* @return {Object} Return a Object with: nome, idade, cpf, rg, data_nasc, sexo, signo, mae, pai, email, senha, cep, endereco, numero, bairro, cidade, estado, telefone_fixo, celular, altura, peso, tipo_sanguineo and cor
*/
async dados() {
function curl(config, req = null) {
request.defaults({ rejectUnauthorized: false });
if (req) {
return new Promise(resolve => request(config, (error, res, body) => resolve(res)));
} else {
return new Promise(resolve => request(config, (error, res, body) => resolve(body)));
}
}
const options = {
url: 'https://www.4devs.com.br/ferramentas_online.php',
headers: {
'Host': 'www.4devs.com.br',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0',
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Content-Type': 'application/x-www-form-urlencoded'
},
body: 'acao=gerar_pessoa&sexo=I&pontuacao=N&idade=0&cep_estado=&txt_qtde=1&cep_cidade=',
method: 'POST',
gzip: true
}
let Person = JSON.parse(await curl(options))
return Person
}
}