2.1.0 • Published 6 years ago

functions.js v2.1.0

Weekly downloads
75
License
MIT
Repository
github
Last release
6 years ago

npm.io

functions.js

npm npm Build Status GitHub stars PRs Welcome Open Source Love GitHub license

functions.js is a hub of various functions or methods which act differently according to their functionality.

  • Please feel free to contribute, read the contributing guide.
  • The code is written in ES6.

Install

The project is up on npm and can be used in a project by:

npm install --save functions.js

Usage

Below is an example on how to use the library into your projects.

const func = require('functions.js');

const algo = new func.algorithms();
const mathematics = new func.mathematics();
const strings = new func.strings();

console.log(strings.camelize('john doe'));
//returns 'johnDoe'

console.log(mathematics.factorialOneLine(5));
//returns 120

console.log(algo.selectionSort([5,10,3,1]));
//returns [1,3,5,10]

Client Side

The library can be loaded as a standalone script.

<script type="text/javascript" src="https://unpkg.com/functions.js/umd/FJS.min.js"></script>
<script type="text/javascript">
	const algo = new FJS.algorithms();
	console.log(algo.nthLargest([5,6,9,8,4,2,1]));
	//=> returns 9
</script>

Methods

Method NameCategoryDescriptionReturn Type
anagram(str1, str2)stringBoth strings are anagramsboolean
average(arr)mathCalculates the average of an arraynumber
binarySearch(arr, elem)algorithmSearch an element using binary search tehcniquenumber(position)
bubbleSort(arr)algorithmSort an array using bubble sort techniquearray
camelize(str)stringCamelize a string i.e CamelCasestring
catalan(number)mathCatalan Numbernumber
changeBase(number, original, final)mathConvert a base of a number to any base.number
chunk(arr, size)mathSplit array into chunks of smaller arrays of the desired size.array
clamp(number, min, max)mathClamp a number.number
collatz(number)algortihmCollatz Algorithm.number
combination(number,rep)mathCalculates simple combination.number
concat(...arrs)algorithmConcatenates into a single array.arr
cosineRule(sideOne, sideTwo, theta)mathCosine Rule.number
countPrimeFactors(n)mathA function to count all prime factors of a given number.number
countVowels(str)stringCount the number of vowels in a string.number
degToRad(deg)mathConvert degrees to radians.number
differenceBetweenDates(date1, date2)mathSimple division function which returns an array of quotient & remainder.array
digitalRoot(number)mathDigital Root of a numbernumber
division(dividend, divisor)mathSimple division function which returns an array of quotient & remainder.array
doublefactorial(n)mathPrint semi-factorial or double factorial of number using the iterative approach.number
escape(str)stringEscape characters from stringstring
euclideanDistance(initial, final)mathCalculates the distance between two points in any plane. initial takes an array of initial points & final takes an array of final points.number
factorialRecursive(n)mathPrint factorial of a number using recursive function approach.number
factorialIterative(n)mathPrint factorial of a number using iterative approach.number
factorialOneLine(n)mathPrint factorial of a number using a ternary operator.number
factCountTrailingZeroes(n)mathPrint the count of trailing zeroes in n!.number
fibRecursion(n)mathPrint nth fibonacci number using recursion.number
fibDynamic(n)mathPrint nth fibonacci number using dynamic programming approach.number
filter(arr, elem)stringFilter out an element from an arrayarray
filterFalsy(arr)stringFilter out falsy values from an arrayarray
fisherYates(arr)mathShuffle an array using the Fisher Yates Algorithm using a while loop.array
fizzBuzz(n)algorithmFizzBuzz Algorithm: most famous asked question.string
flatten(arr, givenArr)algorithmFlatten/Merge two arrays.array
geometricVariance(probability)mathCalculate geometric variance.number
gcd(numb1, numb2)mathCalculate the greatest common divisor among the two numbers using Euclideans algorithm.number
generateHexColor()stringGenerate random hex color.string
generateRGBColor()stringGenerate random RGB color.string
guid(len)stringGenerate a Global Unique Identifierstring
hammingDistance(number1, number2)mathCalculate hamming distance.number
hasAtleastOneUpperCaseLetter(str)stringString has atleast one upper-cased letter.boolean
hasAtleastOneLowerCaseLetter(str)stringString has atleast one lower-cased letter.boolean
hexToRgb(hex)stringConverts hex color to RGB.array
humanizeNumber(num)stringHumanize a number.string
insertionSort(arr)algorithmSort an array using the insertion sort technique.array
integerReversal(integer)mathematicsReverse an integer.number
isASCII(str)stringString contains ASCII charactersboolean
isBool(str)stringString contains true or falseboolean
isDataURI(str)stringString is a data uri format.boolean
isDivisible(dividend, divisor)mathCheck if the first number is divisible by second one.boolean
isEmoji(str)stringString is an emoji.boolean
isEmpty(str)stringString is empty.boolean
isEndWith(str, target)stringString ends with target.boolean
isEquals(str, elem)stringCompares the two strings entered.boolean
isEven(number)mathTrue if a number is even, false otherwise.boolean
isFunction(val)stringValue is a function.boolean
isHexaDecimal(str)stringString is hexadecimal.boolean
isHexColor(str)stringString is a hex color.boolean
isIPAddress(str)stringString is an IP address.boolean
isJSON(str)stringString is a JSON object.boolean
isLowerCase(str)stringString is all lower-cased.boolean
isMACAddress(str)stringString is a MAC Address.boolean
isMD5(str)stringString is a a MD5 type.boolean
initials(str)stringGet the initials of a persons name.string
isOdd(number)mathTrue if a number is odd, false otherwise.boolean
ISO8601WeekNumber(dateObj)stringGet ISO-8601 week number of a year.number
isPalindrome(str)stringString is a palindrome.boolean
isPrime(number)stringNumber is a prime.boolean
isUpperCase(str)stringString is all upper-cased.boolean
isValidEmail(str)stringString is a valid email id.boolean
kebabCase(str)stringto Kebab Case.string
lcm(arr)mathCalculate LCM of an array of numbersnumber
leibniz(limit)mathPredict the value of PInumber
linearSearch(arr, elem)algorithmSearch an element using linear search techniquenumber(position)
lineFromPoints(initial, final)mathCreate a line from x1, y1 & x2, y2 in XY plane.string
maxOrMin(arr, task)mathGet maximum or minimum of an arraynumber
median(arr)mathCalculate median of a sorted number arraynumber
modularExponential(base, power, mod)algorithmModular Exponential Algorithmnumber
nthRoot(number, n)mathCalculates the nth root of a positive numbernumber
nthSGonalNumber(sides, number)mathCalculates the nth s-gonal number of 'sides' polygonnumber
nthLargest(arr, nth)algorithmFind the nth largest number from an unsorted arraynumber
permutation(num,rep)mathCalculates simple permutation.number
pythagorasDistance(arg1, arg2)mathCalculate the pythogorean distance if arg1 and arg2 are 2 sides of a right-angled triangle.number
radToDeg(rad)mathConvert radians to degreesnumber
remove(arr)algorithmRemove an element from an arrayarr
removeDuplicate(arr)algorithmRemove duplicates from an arrayarr
removeTags(str)stringRemove tags from an HTML statementstring
reverse(arr)algorithmReverse an arrayarr
reverseInPlace(str)stringReverse words of a sentence in placestring
rgbToHex(red,green,blue)stringRGB to Hex formatstring
romanize(num)stringRomanize a numberstring
round(number)mathRound a number to two placesnumber
secureEmail(str)stringSecures your email using asterisksstring
securePassword(str)stringSecure password using astrisksstring
segregate(arr)algorithmSegregate 0's and 1's in an arrayarray
selectionSort(arr)algorithmSort an array using the selection sort techniquearr
sigmoid(number)mathSigmoid Functionnumber
sineRule(sideOne, angleOne, angleTwo)mathSine Rule using two angles & one sidenumber
slopeOfALine(arg)mathCalculates the slope of a line using points and angle of inclination. arg takes angle of inclination or an array of initial points and final points. Example arg = 60 degrees or x0,y0,x1,y1number
snakeCase(str)stringto Snake Casestring
sortChar(str)stringSort string characters in alphabetical orderstring
splitName(str)stringReturn the first name and the last name of a person's name.array
stringHammingDistance(str1, str2)stringString Hamming Distancenumber
subtract(firstNum, secondNum)mathSubtract a number from anothernumber
sumOfArray(arr)mathAddition of all the integers in an arraynumber
thousandSeperator(num)stringSeperate thousands using commasnumber
toFloat(str)stringParse string to float valuenumber
toInt(str)stringParse string to integer valuenumber
toTitleCase(str)stringConvert string to titleCasestring
truncate(str,pos)stringTruncate a string at provided positionstring
trailingAndLeading(arr)stringRemove leading and trailing zeroes from an arrayarray
unescape(str)stringUnescape the string.string
uuid()stringGenerate a UUID i.e universally unique identifier.number
zeroFill(number, len)stringZerofill a number to the given length.number

Documentation

You can find the documentation of the package over here

Test

Run the following command:

npm run test

About

The project is a continuation to a package previously owned by me named okay-string. I thought of renaming it to functions & learn more by actually practicing functions. That's how functions.js was created.😍

FAQ🙋

Here are some solutions to frequently asked questions:

Q. How to use ?

Take a look over here.

Q. Another lodash or underscore ?

These two libraries are industry standards & are widely used. They have a ton of great, useful functions to use in a project. There is a difference. functions.js was created to learn predefined methods and make something out of it by creating a new method. So i think there is no comparison between this & the other two libraries.

Q. How do i contribute ?

Read the guidelines mentioned here

License📃

The project is licensed under MIT

2.1.0

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago