3.5.16 • Published 4 years ago

darkness-tools v3.5.16

Weekly downloads
7
License
MIT
Repository
github
Last release
4 years ago

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(10) // random integer between 0 and 10
tools.random(10, 100) // random integer between 10 and 100

tools.randomBoolean() // randomly true or false

tools.random([...]) // random element from array

tools.random({...}) // random key from object

timeInMs

Returns object with time frames in milliseconds

Available millisecond, second, minute, hour and day

tools.timeInMs.hour // 3600000

tools.timeInMs.day // 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.updateClasses(Date.prototype, Number.prototype, Array.prototype) // 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'.replaceAll(/apples/gi, 'oranges') // Round oranges and juicy oranges

'Round apples and juicy apples'.replaceAll('apples', 'oranges') // 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'.deleteAll('not') // 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.'.setData(user) // My name is John, I am 20 y.o

firstToUpperCase()

Returns a new string with first symbol at upper case

'darkness'.firstToUpperCase() // Darkness

toBoolean()

Converts string to boolean

'true'.toBoolean() // true

'0'.toBoolean() // false

'str'.toBoolean() // true

Array

randomElement()

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

lastElement()

Returns last element from array

[1, 2, 3].lastElement() // 3

const names = ['Jonh', 'Helen', 'Fluffy']
names.lastElement() // Fluffy

removeElement(element)

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']

sum()

Returns sum of all elements of array

[5, 4, 3, 2, 1].sum() // 15

[18, 2, 5, '$'].sum() // 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.in_range(0, 100) // false
number.in_range(0, 200) // true
number.in_range(100, 115) // true

Object

toShowString()

Converts object to string, placing tabs

const user = {
    name: 'Fluffy',
    age: 20,
    friends: ['John', 'Helen']
}
//native method:
JSON.stringify(user)  // "{"name":"Fluffy","age":20,"friends":["John","Helen"]}"

//new method
user.toShowString() // 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 = user1
user1.age++
user2.age // 21 intead of 20

// copying  with new method
const user2 = user1.createCopy()
user1.age++
user1.age // 21
user2.age // 20

Date

leftTimeTo(date)

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'),
    nextDate = new Date('December 31, 2020 24:00:00:00')
    
date.leftTimeTo(nextDate) // { days: 29, hours: 11, minutes: 23, seconds: 48, milliseconds: 984 }

License

MIT

3.5.16

4 years ago

3.5.14

4 years ago

3.5.13

4 years ago

3.5.12

4 years ago

3.5.11

4 years ago

3.5.10

4 years ago

3.5.15

4 years ago

3.5.9

4 years ago

3.5.8

4 years ago

3.5.7

4 years ago

3.5.6

4 years ago

3.5.5

4 years ago

3.5.4

4 years ago

3.5.3

4 years ago

3.5.2

4 years ago

3.5.1

4 years ago

3.5.0

4 years ago

3.4.19

4 years ago

3.4.18

4 years ago

3.4.17

4 years ago

3.4.16

4 years ago

3.4.15

4 years ago

3.4.14

4 years ago

3.4.12

4 years ago

3.4.13

4 years ago

3.4.11

4 years ago

3.4.9

4 years ago

3.4.8

4 years ago

3.4.7

4 years ago

3.4.6

4 years ago

3.4.5

4 years ago

3.4.4

4 years ago

3.4.3

4 years ago

3.4.2

4 years ago

3.3.10

4 years ago

3.3.11

4 years ago

3.3.9

4 years ago

3.3.8

4 years ago

3.3.7

4 years ago

3.3.6

4 years ago

3.4.0

4 years ago

3.4.1

4 years ago

3.3.5

4 years ago

3.3.4

4 years ago

3.3.3

4 years ago

3.3.2

4 years ago

3.3.1

4 years ago

3.3.0

4 years ago

3.2.6

4 years ago

3.2.5

4 years ago

3.2.4

4 years ago

3.2.3

4 years ago

3.2.2

4 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.0

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

2.3.0

4 years ago

2.3.2

4 years ago

2.3.1

4 years ago

2.3.3

4 years ago

2.2.4

4 years ago

2.2.3

4 years ago

2.2.2

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago