Using
Darkness Tools is a Node.JS module, that provides 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
Installation
npm i darkness-tools
or using yarn
:
yarn add darkness-tools
Usage
import tools from 'darkness-tools'
Methods
random(min, max)
Returns random entities
tools // random integer between 0 and 10tools // random integer between 10 and 100 tools // randomly true or false tools // random element from array tools // random key from object
timeInMs
Returns object with time frames in milliseconds
Available millisecond
, second
, minute
, hour
and day
toolstimeInMshour // 3600000 toolstimeInMsday // 86400000
updateClasses(...prototypes)
Function that adds new method to global classes
Available String
, Array
, Object
, Number
and Date
methods
// Give all prototypes of available classes in which u want to add new methods tools // updates Date, Number and Array classes
Methods of updated classes
String
replaceAll(substring, replacer)
Returns a new string with all matches of a pattern or substring replaced by a replacement
'Round apples and juicy Apples' // Round oranges and juicy oranges 'Round apples and juicy apples' // Round oranges and juicy oranges
deleteAll(substring)
Returns a new string with deleted all matches of a pattern or substring
'Darkness tools is not the best' // Darkness tools is the best
setData(data)
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.' // My name is John, I am 20 y.o
firstToUpperCase()
Returns a new string with first symbol at upper case
'darkness' // Darkness
toBoolean()
Converts string to boolean
'true' // true '0' // false 'str' // true
Array
randomElement()
Returns random element from array
1 2 3 // Randomly 1, 2 or 3 const names = 'Jonh' 'Helen' 'Fluffy'name // Random name from names
lastElement()
Returns last element from array
1 2 3 // 3 const names = 'Jonh' 'Helen' 'Fluffy'names // Fluffy
removeElement(element)
Removes specified element from array Note, that this will not work with arrays and objects
5 4 3 2 1 // [5, 4, 3, 1] const names = 'Jonh' 'Helen' 'Fluffy'names // ['John', 'Fluffy']
sum()
Returns sum of all elements of array
5 4 3 2 1 // 15 18 2 5 '$' // 25$
Number
in_range(a, b)
Returns true
or false
corresponding to the existence of a number in the interval [a;b]
const number = 115 number // falsenumber // truenumber // true
Object
toShowString()
Converts object to string, placing tabs
const user = name: 'Fluffy' age: 20 friends: 'John' 'Helen'//native method:JSON // "{"name":"Fluffy","age":20,"friends":["John","Helen"]}" //new methoduser // result will be: "name": "Fluffy" "age": 20 "friends": 'John' 'Helen'
createCopy()
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 = user1user1age++user2age // 21 intead of 20 // copying with new methodconst user2 = user1user1age++user1age // 21user2age // 20
Date
leftTimeTo(date)
Returns object with time to specified date in days, hours, minutes, seconds and milliseconds
const date = 'December 02, 2020 12:36:11:16' nextDate = 'December 31, 2020 24:00:00:00' date // { days: 29, hours: 11, minutes: 23, seconds: 48, milliseconds: 984 }