Satanic.JS is a Node.JS module, that provides Telegraf.JS wrapper and convenient functions, that make coding easier
P.S. There will be no conflicts with native methods in new methods of global classes - if one of them already exists, it will not be replaced by a new one
npm i satanic
or using yarn
:
yarn add satanic
import tools from 'satanic'
{
"key 1": "value",
"key 2": "value 1",
"key 3": 100
}
const path = 'data/file.json'
const file = new tools.JSONFile(path)
Convert file data to object
await file.toObject() // {"key 1": "value","key 2": "value 1","key 3": 100}
Get value by key from json file
await file.get('key 2') // 'value 1'
Set value by key in json file
await file.set('key 2', 'new value') // 'new value'
Add value to existing value by key in json file
await file.get('key 2') // 'value 1'
await file.add('key 2', ', 2')
await file.get('key 2') // 'value 1, 2'
Check if json file includes key
await file.includes('key 3') // true
await file.includes('key 2') // false
Add task to queue
const logQueue = new Queue(data => console.log(data))
logQueue.addTask('Some data to log')
Returns random entities
tools.random(10) // random integer between 0 and 10
tools.random(10, 100) // random integer between 10 and 100
tools.random(10, 100, true) // random decimal between 10 and 100
tools.random([...]) // random element from array
tools.random({...}) // random key from object
Returns object with time frames in milliseconds
Available millisecond
, second
, minute
, hour
and day
tools.timeInMs.hour // 3600000
tools.timeInMs.day // 86400000
Function that adds new method of library to global classes
Available String
, Array
, Object
, Number
and Date
methods
tools.updateClasses(Date.prototype, Number.prototype, Array.prototype)
Write logs to logs.txt
and log data to console
Use silent = true
to log without sending data to console
Use error = true
to log data like error
tools.log('Debug data', true)
tools.log('Error', false, true)
tools.log(['Array', 'of', 'data'])
Read .env config Returns object
const env = readEnv() // default path is './config.env'
or
const env = readEnv('path/to/config.env')
Write JSON to local file
tools.saveJSON({
key: 'value'
}, 'data/file.json')
Get JSON from local file
tools.saveJSON({ key: 'value' }, 'data/file.json')
const data = tools.getJSON('data/file.json')
console.log(data) // { "key": "value" }
Returns Date object by cron
const cron = '* */2 * *'
const date = tools.getDate(cron)
console.log(date instanceof Date) // true
Get random Boolean value
Get random item with chance
tools.randomWithChance({
'key 1': 30, // key: percent of chance
'key 2': 50,
'key 3': 20
})
Run code at date (also supports cron time)
tools.doAtDate(new Date(1595425476085), () => {
console.log('Now is Wed Jul 22 2020 16:44:36')
})
tools.doAtDate('*/1 * * * *', () => {
console.log('Console log every minute')
})
Async sleep
console.log('Log now')
tools.sleep(1000)
console.log('Log after a second')
Repeat callback
function count
times
tools.repeat(4, i => console.log(i))
// 0
// 1
// 2
// 3
Iterate over object
const animalsSounds = {
cat: 'Meow',
dog: 'Woof',
fish: null,
cow: 'Moo'
}
forObject(animalsSounds, (animal, sound) => {
if (sound === null) {
return 'continue'
}
console.log(`${animal} says ${sound}!`)
})
U can return continue
or break
from callback function to go to next iteration immediately or stop cycle.
Invokes Telegram bot instance(wrapper for Telegraf) More in Telegraf docks
const token = '****************************'
const bot = tools.bot(token, true, () => {
console.log('Bot will be launched after this function')
})
Remove all substrings from string
'a b c a b c'.removeAll(' c') // a b a b
'nananas'.removeAll(/^n/) // ananas
Returns a new string with replaced %key%
with specified value
const user = {
name: 'John',
age: 20
}
'My name is %name%, I am %age% y.o.'.setData(user) // My name is John, I am 20 y.o
Returns a new string with first symbol at upper case
'darkness'.firstToUpperCase() // Darkness
Converts string to boolean
'true'.toBoolean() // true
'0'.toBoolean() // false
'str'.toBoolean() // true
Returns random element from array
[1, 2, 3].randomElement() // Randomly 1, 2 or 3
const names = ['Jonh', 'Helen', 'Fluffy']
name.randomElement() // Random name from names
Returns last element from array
[1, 2, 3].lastElement() // 3
const names = ['Jonh', 'Helen', 'Fluffy']
names.lastElement() // Fluffy
Removes specified element from array Note, that this will not work with arrays and objects
[5, 4, 3, 2, 1].removeElement(2) // [5, 4, 3, 1]
const names = ['Jonh', 'Helen', 'Fluffy']
names.removeElement('Helen') // ['John', 'Fluffy']
Returns sum of all elements of array
[5, 4, 3, 2, 1].sum() // 15
[18, 2, 5, '$'].sum() // 25$
Returns shallow copy of array
const a = [1, 2, 3, 4, 5]
const b1 = a
const b2 = a.createCopy()
a[0] = 99
console.log(b1[0]) // 99
console.log(b2[0]) // 1
Set callback_data of Telegram bot inline keyboard
const inlineButton = [{
text: 'Button',
callback_data: 'id_%id%'
}]
inlineButton.setCbData({
id: 312
})
Check if array has no items
[1, 2, 3].isEmpty() // false
[].isEmpty() // true
Randomly shuffles array elements
const array = [1, 2, 3, 4, 5]
array.shuffle()
console.log(array) // [2, 1, 4, 5, 3]
Returns true
or false
corresponding to the existence of a number in the interval (a;b)
(or [a;b]
for inclusive = true
)
const number = 115
number.inRange(0, 100) // false
number.inRange(100, 115, false) // false
number.inRange(100, 115, true) // true
number.inRange(100, 200) // true
Converts number to boolean
. false
if number = 0
, otherwise true
const [number1, number2, number3] = [0, 125, -312]
number1.toBoolean() // false
number2.toBoolean() // true
number3.toBoolean() // true
Shorts number with K
(1.000), M
(1.000.000) and T
(1.000.000.000.000)
const [number1, number2, number3, number4] = [1234, 1234567, 1234567890123, 12345678901231300]
number1.shortWithK() // 1.23K
number2.shortWithK() // 1.23M
number3.shortWithK() // 1.23T
number4.shortWithK() // 12.35KKKKK
Converts object to string, placing tabs
const user = {
name: 'Fluffy',
age: 20,
friends: ['John', 'Helen']
}
JSON.stringify(user) // "{"name":"Fluffy","age":20,"friends":["John","Helen"]}"
user.toShowString() // result will be:
{
"name": "Fluffy",
"age": 20,
"friends": [
'John',
'Helen'
]
}
Returns a copy of object. Also, all values of keys of object will be also copies, not a links to it Note, that all class instances will be converted to simple Objects
const user1 = {
name: 'Fluffy',
age: 20,
friends: ['John', 'Helen']
}
const user2 = user1
const user3 = user1.createCopy()
user1.age++
user2.age // 21
user3.age // 20
Find pair key-value in object by function. Returns null if not found
const object = {
key1: 15,
key2: 33,
key3: 14,
key5: 122
}
object.find((key, value) => value % 2 === 0) // ['key3', 14]
Returns object with time to specified date in days, hours, minutes, seconds and milliseconds
const date = new Date('December 02, 2020 12:36:11:16')
const nextDate = new Date('December 31, 2020 24:00:00:00')
date.leftTimeTo(nextDate) // { days: 29, hours: 11, minutes: 23, seconds: 48, milliseconds: 984 }