kindled-utils
Version:
A few functions to assist in development
102 lines (90 loc) • 2.98 kB
JavaScript
const fs = require('fs-extra')
const { writeFile } = require('fs-extra');
const BitlyClient = require('bitly').BitlyClient;
const request = require('request')
const cheerio = require('cheerio')
exports.getUUID = function(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
exports.includesText = function(file, text) {
var data = fs.readFileSync(file, 'utf-8');
if(data.includes(text)){
return true;
}
else{
return false;
}
}
exports.removeText = function(file, text) {
var data = fs.readFileSync(file, 'utf-8');
var replace = data.replace(text, "");
fs.writeFileSync(file, replace);
console.log(`\n[UTILS] Removed text\nText: ${text}\nFile: ${file}`)
}
exports.date = function() {
return(new Date().toLocaleDateString())
}
exports.time = function() {
var d = new Date()
return (d.getUTCHours + ":" + d.getUTCMinutes)
}
exports.appendNewLine = function(file, text){
fs.appendFileSync(file, text + "\n");
console.log(`\n[UTILS] Appended text\nText: ${text}\nFile: ${file}`)
}
exports.toJSONFile = function(path, object){
let data = JSON.stringify(object);
writeFile(path, data, (err) => {
if (err) throw err;
console.log(`\n[UTILS] Object writted to ${path}`);
});
}
exports.getSiteTitle = function(url_){
request({
method: 'GET',
url: url_
}, (err, res, body) => {
if (err) return console.error(err);
let $ = cheerio.load(body);
let title = $('title');
console.log(title.text());
});
}
/*/
exports.shorten = function(key, link) {
const bitly = new BitlyClient(key);
async function shorten(url) {
const response = await bitly.shorten(url);
console.log(`[UTILS] Shortner\nURL: ${url}\nShortened URL: ${response.link}`);
return response.link;
}
return(shorten(link))
}
/*/
function logFeature(name, desc){
console.log(`<kindled-utils>.${name} <- ${desc}`)
}
exports.features = function() {
console.log("KINDLED UTILS")
console.log("FEATURES:")
console.log("")
console.log("File management")
console.log("")
logFeature("includesText(file, text)", "Checks if a file includes text")
logFeature("removeText(file, text)", "Remove text from a file.")
logFeature("appendNewLine(file, text)", "Appends a new line to a file")
logFeature("toJSONFile(path, object)", "Generates a JSON file from a JS Object")
console.log("")
console.log("Random")
console.log("")
logFeature("date()", "Gets the date")
logFeature("time()", "Gets the time")
logFeature("getUUID(length)", "Generates a random UUID")
logFeature("shorten(api-key, link)", "Shortens a link using the bit.ly API")
}