fj-js
Version:
Facilitation of JavaScript (FJ) - A simplified, powerful JS-based scripting language.
335 lines (261 loc) • 5.58 kB
JavaScript
const fs = require("fs")
const os = require("os")
const path = require("path")
const crypto = require("crypto")
const { execSync } = require("child_process")
const { v4: uuidv4 } = require("uuid")
const axios = require("axios")
function print(...args) {
console.log(...args)
}
function say(...args) {
console.log(...args)
}
function pause(seconds) {
return new Promise(resolve => setTimeout(resolve, seconds * 1000))
}
function uuid() {
return uuidv4()
}
function rep(text, times = 1) {
for (let i = 0; i < times; i++) print(text)
}
function random(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min
}
function now() {
return new Date().toLocaleString()
}
function capitalize(str) {
return str ? str[0].toUpperCase() + str.slice(1) : ""
}
function upper(str) {
return String(str).toUpperCase()
}
function lower(str) {
return String(str).toLowerCase()
}
function reverse(str) {
return String(str).split("").reverse().join("")
}
function need(mod) {
try {
return require(mod)
} catch {
print(`Error cargando módulo '${mod}'`)
return null
}
}
function clear() {
process.stdout.write("\x1Bc")
}
function hash(text, algo = "sha256") {
return crypto.createHash(algo).update(String(text)).digest("hex")
}
function input(promptText = "> ") {
return new Promise(resolve => {
process.stdin.resume()
process.stdout.write(promptText)
process.stdin.once("data", data => {
process.stdin.pause()
resolve(data.toString().trim())
})
})
}
function file(pathname) {
return fs.readFileSync(pathname, "utf-8")
}
function save(pathname, content) {
fs.writeFileSync(pathname, content)
}
function exists(pathname) {
return fs.existsSync(pathname)
}
function files(dir = ".") {
return fs.readdirSync(dir)
}
function remove(pathname) {
if (fs.existsSync(pathname)) fs.unlinkSync(pathname)
}
function rename(oldName, newName) {
fs.renameSync(oldName, newName)
}
function size(pathname) {
return fs.statSync(pathname).size
}
function user() {
return os.userInfo().username
}
function platform() {
return os.platform()
}
function cpu() {
return os.cpus()[0].model
}
function mem() {
return `${Math.round(os.totalmem() / 1024 / 1024)} MB`
}
function exec(cmd) {
try {
return execSync(cmd).toString()
} catch (e) {
return e.message
}
}
function fetch(url) {
return axios.get(url).then(res => res.data).catch(err => err.message)
}
function json(str) {
try {
return JSON.parse(str)
} catch {
return null
}
}
function str(obj) {
try {
return JSON.stringify(obj, null, 2)
} catch {
return ""
}
}
function range(start, end) {
return Array.from({ length: end - start + 1 }, (_, i) => i + start)
}
function table(obj) {
console.table(obj)
}
function isEven(num) {
return num % 2 === 0
}
function isOdd(num) {
return num % 2 !== 0
}
function sum(...nums) {
return nums.reduce((a, b) => a + b, 0)
}
function avg(...nums) {
return sum(...nums) / nums.length
}
function min(...nums) {
return Math.min(...nums)
}
function max(...nums) {
return Math.max(...nums)
}
function sqrt(n) {
return Math.sqrt(n)
}
function pow(x, y) {
return Math.pow(x, y)
}
function abs(n) {
return Math.abs(n)
}
function round(n) {
return Math.round(n)
}
function floor(n) {
return Math.floor(n)
}
function ceil(n) {
return Math.ceil(n)
}
function log(n) {
return Math.log(n)
}
function sin(n) {
return Math.sin(n)
}
function cos(n) {
return Math.cos(n)
}
function tan(n) {
return Math.tan(n)
}
function deg(rad) {
return rad * (180 / Math.PI)
}
function rad(deg) {
return deg * (Math.PI / 180)
}
function sleep(ms) {
return new Promise(r => setTimeout(r, ms))
}
function error(msg) {
throw new Error(msg)
}
function timestamp() {
return Date.now()
}
function date() {
return new Date().toISOString()
}
function clamp(n, min, max) {
return Math.min(Math.max(n, min), max)
}
function repeat(str, times) {
return String(str).repeat(times)
}
function lines(str) {
return String(str).split("\n")
}
function words(str) {
return String(str).trim().split(/\s+/)
}
function chars(str) {
return Array.from(String(str))
}
function includes(str, val) {
return String(str).includes(val)
}
function starts(str, val) {
return String(str).startsWith(val)
}
function ends(str, val) {
return String(str).endsWith(val)
}
function replace(str, a, b) {
return String(str).replaceAll(a, b)
}
function count(arr) {
return Array.isArray(arr) ? arr.length : 0
}
function uniq(arr) {
return [...new Set(arr)]
}
function sort(arr) {
return [...arr].sort()
}
function shuffle(arr) {
return [...arr].sort(() => Math.random() - 0.5)
}
function first(arr) {
return arr[0]
}
function last(arr) {
return arr[arr.length - 1]
}
function join(arr, sep = ",") {
return arr.join(sep)
}
function split(str, sep = ",") {
return String(str).split(sep)
}
function talvez(callback) {
try {
callback()
} catch (e) {
print("Error:", e.message)
}
}
module.exports = {
print, say, pause, uuid, rep, random, now, capitalize, upper, lower,
reverse, need, clear, hash, input, file, save, exists, files, remove,
rename, size, user, platform, cpu, mem, exec, fetch, json, str,
range, table, isEven, isOdd, sum, avg, min, max, sqrt, pow, abs,
round, floor, ceil, log, sin, cos, tan, deg, rad, sleep, error,
timestamp, date, clamp, repeat, lines, words, chars, includes,
starts, ends, replace, count, uniq, sort, shuffle, first, last,
join, split, talvez
}