gg-utils

0.2.2 • Public • Published

GG Utils NPM version

GG Utils collects utils for daily struggle

Usage

var gg = require('gg-utils');
 
console.log(gg.Network.isIPV4("172.19.30.21"));     // true
console.log(gg.Network.isIPV4("172.19.256.21"));    // false
 
var array = [6,3,7,2,4,0]
var sortedArray = gg.Algorithm.quickSort(array) 
console.log(sortedArray)    // [0,2,3,4,6,7]

Installation

npm install gg-utils --save

API Reference

Algorithm

var algorithm = require('gg-utils').Algorithm;

bubbleSort(array)

Sorts an array from the minimum to the maximum.

Reference: https://en.wikipedia.org/wiki/Bubble_sort

var array = [3,7,1,2,5,8,12]
console.log(algorithm.bubbleSort(array))    // [1,2,3,5,7,8,12]

cocktailShakerSort(array)

Sorts an array from the minimum to the maximum.

Reference: https://en.wikipedia.org/wiki/Cocktail_shaker_sort

var array = [3,7,1,2,5,8,12]
console.log(algorithm.cocktailShakerSort(array))    // [1,2,3,5,7,8,12]

combSort(array)

Sorts an array from the minimum to the maximum.

Reference: https://en.wikipedia.org/wiki/Comb_sort

var array = [3,7,1,2,5,8,12]
console.log(algorithm.combSort(array))  //[1,2,3,5,7,8,12]

fibonacciNumber(n)

Returns the nth fibonacci number.

Reference: https://en.wikipedia.org/wiki/Fibonacci_number

console.log(algorithm.fibonacciNumber(20))  // 6765
console.log(algorithm.fibonacciNumber(40))  // 102334155

fibonacciNumberRecursive(n)

Returns the nth fibonacci number using the recursive algorithm (slower than the iterative).

Reference: https://en.wikipedia.org/wiki/Fibonacci_number

console.log(algorithm.fibonacciNumberRecursive(20)) //6765
console.log(algorithm.fibonacciNumberRecursive(40)) //102334155

gnomeSort(array)

Sorts an array from the minimum to the maximum.

Reference: https://en.wikipedia.org/wiki/Gnome_sort

var array = [6,3,7,2,4,0]
console.log(algorithm.gnomeSort(array)) // [0,2,3,4,6,7]

quickSort(array)

Sorts an array from the minimum to the maximum.

Reference: https://en.wikipedia.org/wiki/Quicksort

var array = [6,3,7,2,4,0]
console.log(algorithm.quickSort(array)) // [0,2,3,4,6,7]

sieveOfEratosthenes(n)

Produces an array with all primes not greater than n.

Reference: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

console.log(algorithm.quickSort(10))    // [0,1,2,3,5,7]
console.log(algorithm.quickSort(30))    // [0,1,2,3,5,7,11,13,17,19,23,29]
console.log(algorithm.quickSort(1))     // false

trialDivision(n)

Produces an array with prime factors of n.

Reference: https://en.wikipedia.org/wiki/Trial_division

console.log(algorithm.trialDivision(12))    // [2,2,3]
console.log(algorithm.trialDivision(17))    // [17]
console.log(algorithm.trialDivision(784))   // [2,2,2,2,7,7]

Date

var date = require('gg-utils').Date;

isLeapYear(year)

Checks if year is a leap year.

Reference: https://en.wikipedia.org/wiki/Leap_year

console.log(date.isLeapYear(2012))   //true
console.log(date.isLeapYear(2013))   //false
console.log(date.isLeapYear(2016))   //true
console.log(date.isLeapYear(2017))   //false

timeDiff(from, to[,timezone])

Returns an object with the fields: days, hours, minutes and seconds of the time difference between from and to

console.log(date.timeDiff(new Date(2015, 5, 1, 22, 14, 7),new Date(2018, 8, 17, 15, 8, 32))) // {days: 1203, hours: 16, minutes: 54,seconds: 25}
console.log(date.timeDiff(new Date(2015, 1, 1, 22, 14, 7),new Date(2018, 8, 17, 15, 8, 32), true)) //{days: 1323, hours: 15, minutes: 54, seconds: 25}

Network

var network = require('gg-utils').Network;

isMACAddress(MAC)

Checks if the string provided is a valid MAC address.

console.log(network.isMACAddress("01-23-45-67-89-AB"))  //true
console.log(network.isMACAddress("G1-23-45-67-89-AB"))  //false

isIPV4(IPV4)

Checks if the string provided is a valid IPV4.

console.log(network.isIPV4("172.19.2.4"))       // true
console.log(network.isIPV4("192.168.4.256"))    // false

isIPV6(IPV6[,specialCheck])

Checks if the string provided is a valid IPV6. If specialCheck is true (default is false), the following IPV6 address will be treated as valid:

  • ::/128
  • ::1/128
  • ::/96
  • ::ffff:0:0/96
  • fe80::/10
  • fec0::/10
  • fc00::/7
  • ff00::/8"
console.log(network.isIPV6("2001:0db8:85a3:0000:0000:8a2e:0370:7334"))  // true
console.log(network.isIPV6("2001:db8:85a3:0:0:8a2e:370:7334"))          // true
console.log(network.isIPV6("::ffff:192.168.89.9"))                      // true
console.log(network.isIPV6("fec0::/10", true))                          // true
console.log(network.isIPV6("2001:0gb8:85a3:0000:0000:8a2e:0370:7334"))  // false
console.log(network.isIPV6("192.168.89.9"))                             // false

simplifyIPV6(IPV6)

Simpliefies a valid IPV6.

console.log(network.simplifyIPV6("2001:0db8:0000:0000:0000:0000:1428:57ab"))    // 2001:db8::1428:57ab
console.log(network.simplifyIPV6("2001:0db8:85a3:0:1319:8a2e:0370:7344"))       // 2001:db8:85a3::1319:8a2e:370:7344
console.log(network.simplifyIPV6("0000:0000:0000:0000:0000:0000:0000:0001"))    // ::1
console.log(network.simplifyIPV6("2001:0gb8:85a3:0000:0000:8a2e:0370:7334"))    // false

Random

var random = require('gg-utils').Random;

arrayGeneratorInt(min,max,n)

Returns a random array of integers.

  • min is minimum value
  • max is the maximum value
  • n is the size of the array (limit 10000000)
console.log(random.arrayGeneratorInt(0,10,5))           // [3,5,8,0,1]
console.log(random.arrayGeneratorInt(-30,30,10))        // [-29,-27,-28,13,-29,10,14,7,13,-5]
console.log(random.arrayGeneratorInt(30,29,10))         // false
console.log(random.arrayGeneratorInt(-30,30,10000001))  // false

getRandomIntInclusive(min,max)

Returns a random integer between min and max (both inclusive)

console.log(random.arrayGeneratorInt(0,10))     // 4
console.log(random.arrayGeneratorInt(0,10))     // 10
console.log(random.arrayGeneratorInt(-10,10))   // -8
console.log(random.arrayGeneratorInt(10,9))     // false

getRandomString(n)

Returns a random string with length n. Available chars are [a-zA-Z0-9].

console.log(random.getRandomString(10)) // 2jIPIcUMCv
console.log(random.getRandomString(4))  // 65Bi
console.log(random.getRandomString(0))  // ""
console.log(random.getRandomString(-1)) // ""

License

MIT

Package Sidebar

Install

npm i gg-utils

Weekly Downloads

1

Version

0.2.2

License

MIT

Last publish

Collaborators

  • ithurts