advance-javascript

a collection of javascript prototype extensions that make your javascript coding very easy and hanndy.

Usage no npm install needed!

<script type="module">
  import advanceJavascript from 'https://cdn.skypack.dev/advance-javascript';
</script>

README

AdvanceJavascript.js

a collection of javascript prototype extensions that make your javascript coding very easy and hanndy.

support both node.js and web browser

How to use...

download: AdvanceJavascript.js

download: AdvanceJavascript.min.js

download: Source Code GitHub

npm install advance-javascript

How to Install

copy following code and run on CLI

npm install advance-javascript

Include in your main.js/script.js/index.html page

it automaically initiated and extends JavaScript

NODE-JS :

require("advance-javascript");

HTML

import "advance-javascript";

or

<script src="./js/AdvanceJavascript.js"></script>

or

<script src="./js/AdvanceJavascript.min.js"></script>

after it you can use Advance Javascript Extension methods

Demo

React

Extension Methods

Extends String/Number/Date/Array/Set/Object

String Prototype Extensions

|String.methods|Description| |-|-| |.trimAll() | Remove all extra spaces, new line and tabs from string and return single line text having single space between words. | |.toCamelCase() | Convert sentence to camel case. eg.
"this is normal string".toCamelCase()
-> "thisIsNormalString"| |.toKebabCase() | Convert sentence to kebab case. eg.
"this is normal string".toKebabCase()
-> "this-is-normal-string"| |.toPascalCase()| Convert sentence to pascal case. eg.
"this is normal string".toPascalCase()
-> "ThisIsNormalString"| |.toSnakeCase() | Convert sentence to snake case. eg.
"this is normal string".toSnakeCase()
-> "this_is_normal_string"| |.left(n) | Extract number of characters from left. eg.
"this is normal string".left(6)
-> "this i"| |.right(n) | Extract number of characters from right. eg.
"this is normal string".right(4)
-> "ring" | |.mid(m,n) | Extract number of characters (n) from given index (m). eg.
"this is normal string".mid(8,6)
-> "normal"| |.reverse() | Reverses the string. eg.
"this is normal string".reverse()
-> "gnirts lamron si siht"| |.sort() | Sort string letters in assending order. eg.
"this is normal string".sort()
-> " aghiiilmnnorrssstt"| |.sort(fn) | Sort string letters according to compare function (fn) passed. . eg.
"this is normal string".sort((a, b) => (a < b ? 1 : -1))
-> "ttsssrronnmliiihga "| |.sortAsc() | Sort string letters in assending order. eg.
"this is normal string".sortAsc()
-> " aghiiilmnnorrssstt"| |.sortDesc() | Sort string letters in descending order. eg.
"this is normal string".sortDesc()
-> "ttsssrronnmliiihga "| |.distinct() | Extract unique/distinct characters from string and return string. eg.
"this is normal string".distinct()
-> "this normalg"| |.toNumber()| Convert string number to numeric value or NaN. eg.
"123.45".toNumber()
-> 123.45| |.toNumber()| Convert string number to numeric value or NaN. eg.
"a123.45".toNumber()
-> NaN| |.toNumber(NaN_Value)| Convert string number to numeric value. on fail return NaN_Value. eg.
"a123.45".toNumber(0)
-> 0| |.toNumber(itself)| Convert string number to numeric value. on fail return original string itself. eg.
"a123.45".toNumber(itself)
-> "a123.45"| |.parseInt()| Convert string to integer value and round up the fraction value. eg.
"123.45".parseInt()
-> 123| |.parseInt()| Convert string to integer value and round up the fraction value. eg.
"a123.45".parseInt()
-> NaN| |.parseInt(redix)| Convert string to integer value on the basis of basevalue/redix. eg.
"AF2F".parseInt(16)
-> 44847| |.parseInt(redix,NaN_Value)| Convert string to integer value on the basis of basevalue/redix. eg.
"AF2F".parseInt(10,0)
-> 0| |.parseInt(redix,itself)| Convert string to integer value on the basis of basevalue/redix. eg.
"AF2F".parseInt(8,itself)
-> "AF2F"| |.parseFloat()| Convert string number to numeric value or NaN. eg.
"123.45".parseFloat()
-> 123.45| |.parseFloat()| Convert string number to numeric value or NaN. eg.
"a123.45".parseFloat()
-> NaN| |.parseFloat(NaN_Value)| Convert string number to numeric value. on fail return NaN_Value. eg.
"a123.45".parseFloat(0)
-> 0| |.parseFloat(itself)| Convert string number to numeric value. on fail return original string itself. eg.
"a123.45".parseFloat(itself)
-> "a123.45"| |.toInt()| Convert string to integer value and round up the fraction value. eg.
"123.45".toInt()
-> 123| |.toInt()| Convert string to integer value and round up the fraction value. eg.
"a123.45".toInt()
-> NaN| |.toInt(redix)| Convert string to integer value on the basis of basevalue/redix. eg.
"AF2F".toInt(16)
-> 44847| |.toInt(redix,NaN_Value)| Convert string to integer value on the basis of basevalue/redix. eg.
"AF2F".toInt(10,0)
-> 0| |.toInt(redix,itself)| Convert string to integer value on the basis of basevalue/redix. eg.
"AF2F".toInt(8,itself)
-> "AF2F"| |.toFloat()| Convert string number to numeric value or NaN. eg.
"123.45".toFloat()
-> 123.45| |.toFloat()| Convert string number to numeric value or NaN. eg.
"a123.45".toFloat()
-> NaN| |.toFloat(NaN_Value)| Convert string number to numeric value. on fail return NaN_Value. eg.
"a123.45".toFloat(0)
-> 0| |.toFloat(itself)| Convert string number to numeric value. on fail return original string itself. eg.
"a123.45".toFloat(itself)
-> "a123.45"| |.isNaN()| Check wheter string can be convert to numeric or not. return true/false. eg.
"1234.56".isNaN()
-> false| |.isNaN()| Check wheter string can be convert to numeric or not. return true/false. eg.
"a1234.56".isNaN()
-> true| |.isNaN(NaN_Value)| Convert string to numeric value. eg.
"1234.56".isNaN(0)
-> 1234.56| |.isNaN(NaN_Value)| Convert string to numeric value. eg.
"a1234.56".isNaN(0)
-> 0| |.isNaN(itself)| Convert string to numeric value. eg.
"a1234.56".isNaN(itself)
-> "a1234.56"| |.toArray()| Convert strint to characters array. eg.
"abcd".toArray()
-> ["a", "b", "c", "d"]| |.toCodeArray()| Convert string to array of character ascii/unicode code. eg.
"abcde".toCodeArray()
-> [97, 98, 99, 100, 101]| |.toCharCodeArray()| Convert string to array of object {char:'char',code:number}. eg.
"abcde".toCharCodeArray()
-> [{char: "a", code: 97},{char: "b", code: 98},{char: "c", code: 99},{char: "d", code: 100},{char: "e", code: 101}]| |.toCharCode()|Convert string to object {char:code,char:code} eg.
"abcd".toCharCode()
-> { a : 97, b : 98, c : 99, d : 100 }| |.frequency()|Calculate frequency of charactes in string and return object having character as key and frequency as value. eg.
"this is a sample string".frequency()
-> {" ": 4, a: 2, e: 1, g: 1, h: 1, i: 3, l: 1, m: 1, n: 1, p: 1, r: 1, s: 4, t: 2}
"thisIsASampleString".frequency()
-> {A: 1,I: 1,S: 2,a: 1,e: 1,g: 1,h: 1,i: 2,l: 1,m: 1,n: 1,p: 1,r: 1,s: 2,t: 2}| |.frequency(isInSensitive)|Optional if true then it matches without case check(ignore case) eg.
"thisIsASampleString".frequency(true)
-> {a: 2,e: 1,g: 1,h: 1,i: 3,l: 1,m: 1,n: 1,p: 1,r: 1,s: 4,t: 2}| |.frequency(char)|Optional Character if passed then return only that char frequency. eg
"thisIsASampleString".frequency('i')
-> 2| |.frequency(char,isInSensitive)|return frequency of char with ignore case. eg.
"thisIsASampleString".frequency('i', true)
-> 3| |.toDateTime()|Convert any valid string date to Date data type. If unable to convert the given string to date return date value of "1-JAN-1900" date data type. eg.
"1-jan-2021".toDateTime()
-> Fri Jan 01 2021 00:00:00 GMT+0530 (India Standard Time)
"1-Mar".toDateTime()
-> Mon Mar 01 2021 00:00:00 GMT+0530 (India Standard Time)
"Mar,2022".toDateTime()
-> Tue Mar 01 2022 00:00:00 GMT+0530 (India Standard Time)
"2021".toDateTime()
-> Fri Jan 01 2021 05:30:00 GMT+0530 (India Standard Time)
"1/5/2021".toDateTime()
-> Sat May 01 2021 00:00:00 GMT+0530 (India Standard Time)
"01/05/2021 5:55 AM".toDateTime()
-> Sat May 01 2021 05:55:00 GMT+0530 (India Standard Time)
"01/08/2021 5:15:16 PM".toDateTime()
-> Sun Aug 01 2021 17:15:16 GMT+0530 (India Standard Time)
"12:15:16 AM".toDateTime()
-> Thu Aug 05 2021 00:15:16 GMT+0530 (India Standard Time)
//current date with given time
"invalid date string".toDateTime()
-> Mon Jan 01 1900 00:00:00 GMT+0521 (India Standard Time)| |.toDate()|Convert String to only Date value exclude time value. eg.
"05-Aug-2021 05:25:15 AM".toDate()
-> Thu Aug 05 2021 00:00:00 GMT+0530 (India Standard Time)| |.isString|[Boolean Property] return true if variable value is string else false. eg.
"abcd".isString
-> true
let a="string";
a.isString
->true
let b=34;
b.isString;
-> false|

Date Prototype Extensions

|Date.methods|Description| |-|-| |.format( strFormat )|Convert Date to String in given format string.
Format String Description:

specifierdescription
done or two digit date value
ddtwo digit date value leading zero if one digit date
dddthree char week day name
ddddfull week day name
Mone or two digit month value
MMtwo digit month value. leading zero if one digit month
MMMthree char month name
MMMMfull month name
yytwo digit year value
yyyyfour digit year value
hone or two digit hour value
hhtwo digit hour value. leading zero if one digit hour
Hone or two digit hour value in 24 hour format
HHtwo digit hour value in 24 hour format
mone or two digit minute value
mmtwo digit minute value
sone or two digit second value
sstwo digit second value
fffmiliseconds value
ttAM/PM value
Examples : let date = "8-April-2021 11:35:48 PM".toDateTime()
date
-> Thu Apr 08 2021 23:35:48 GMT+0530 (India Standard Time)
date.format("ddd dd-MMM-yyyy hh:mm:ss:fff tt")
-> "Thu 08-Apr-2021 11:35:48:000 PM"
date.format("dddd dd/MMM/yyyy")
-> "Thursday 08/Apr/2021"
date.format("HH:mm:ss:fff")
-> "23:35:48:000"
date.format("dd/MM/yy")
-> "08/04/21"
date.format("d/M/y")
-> "8/4/21"
| |.addDays(num)|This is Date extension method that adds number of days in Date value
'1 jan 2022'.toDateTime().addDays(10)
-> Tue Jan 11 2022 00:00:00 GMT+0530 (India Standard Time)
'1 jan 2022'.toDateTime().addDays(-10)
-> Wed Dec 22 2021 00:00:00 GMT+0530 (India Standard Time)| |.addMonths(num)| This is Date extension method that adds number of months in Date value
'1 jan 2022'.toDateTime().addMonths(10)
-> Tue Nov 01 2022 00:00:00 GMT+0530 (India Standard Time)
'1 jan 2022'.toDateTime().addMonths(-10)
-> Mon Mar 01 2021 00:00:00 GMT+0530 (India Standard Time) | |.addYears(num) | This is Date extension method that adds number of years in Date value
'1 jan 2022'.toDateTime().addYears(10)
-> Thu Jan 01 2032 00:00:00 GMT+0530 (India Standard Time)
'1 jan 2022'.toDateTime().addYears(-10)
-> Sun Jan 01 2012 00:00:00 GMT+0530 (India Standard Time)| |.addHours(num) | This is Date extension method that adds number of hours in Date value
'1 jan 2022 08:12:18 AM'.toDateTime().addHours(10)
-> Sat Jan 01 2022 18:12:18 GMT+0530 (India Standard Time)
'1 jan 2022 08:12:18 AM'.toDateTime().addHours(-10)
-> Fri Dec 31 2021 22:12:18 GMT+0530 (India Standard Time) | |.addMinutes(num)|This is Date extension method that adds number of minutes in Date value
'1 jan 2022 08:12:18 AM'.toDateTime().addMinutes(10)
-> Sat Jan 01 2022 08:22:18 GMT+0530 (India Standard Time)
'1 jan 2022 08:12:18 AM'.toDateTime().addMinutes(-10)
-> Sat Jan 01 2022 08:02:18 GMT+0530 (India Standard Time) | |.addSeconds(num)| This is Date extension method that adds number of seconds in Date value
'1 jan 2022 08:12:18 AM'.toDateTime().addSeconds(10)
-> Sat Jan 01 2022 08:12:28 GMT+0530 (India Standard Time)
'1 jan 2022 08:12:18 AM'.toDateTime().addSeconds(-10)
-> Sat Jan 01 2022 08:12:08 GMT+0530 (India Standard Time)| |.dateDiff(date)| This is Date extension method that subtract given date and return difference in days
let a = '1 jan 2022'.toDateTime()
let b = '1 jan 2020'.toDateTime()
a.dateDiff(b)
-> -731
let a = '1 jan 2022 10:20 AM'.toDateTime()
let b = '1 jan 2025 10:20 PM'.toDateTime()
a.dateDiff(b)
-> 1096.5| |.toNumber()|Convert Date to Numeric value in milliseconds. eg.
'15 Aug 2021 8:30 AM'.toDateTime().toNumber();
-> 1628996400000| |.isDate|[boolean property] return true if a variable holds date value else false|

Number Prototype Extensions

|Number.methods|Description| |-|-| |.isNaN()|[boolean] return true or false if variable holds NaN value or Infinity then true else false eg.
let a = "45".toNumber();
a.isNaN();
-> false
let b = "a45".toNumber();
b.isNaN();
-> true| |.isNaN( nanValue )|check variable value if NaN or Infinity then nanValue else the numeric value itself. eg.
let a = "45".toNumber();
a.isNaN(0);
-> 45
let b = "a45".toNumber();
b.isNaN(0);
-> 0| |.isNaN( itself )|check variable value if NaN or Infinity then variable's value else the numeric value. eg.
let a = "45".toNumber();
a.isNaN(itself);
-> 45
let b = "a45".toNumber();
b.isNaN(itself);
-> NaN| |..toChar()|Convert Numeric value to character string according to their ASCII/UNICODE. eg.
(65).toChar();
-> 'A'
(9829).toChar();
-> "♥"
(9786).toChar();
-> "☺"| |.format( strFormat )|Convert numeric value to string with comma separated or with pad zero. depends of format string that is passed to it as argument.
Invalid format string then the number convert to string with local number format.

Format SpecifierDescription
#Number or Blank
0Number or Zero
,Specify Comma Places
.Decimal Position

example:
ExampleOutput
(12345679).format("#,##,##0.00")"1,23,45,679.00"
(.123).format("#,##,##0.00")"0.12"
(123).format("000000")"000123"
(123412341234).format("####,####,####").replaceAll("," , "-")"1234-1234-1234"
| |.toDate()|Convert milliseconds numeric value to Date value. eg.
(1628996400000).toDate();
-> Sun Aug 15 2021 08:30:00 GMT+0530 (India Standard Time)| |.isNumber|[boolean property] return true if a variable holds numeric value else false|

Array Prototype Extensions

|Array.methods|Description| |-|-| |.toSet()| Convert Array to new Set. Set of unique values.| |.contains( val )|This Array extension method searches value in array and return boolean true/false
return true if value found in array with exact matches. object with same properties and value. eg.
[1, 'a', '2.5', 2.6, {a:false}, {a:1,b:2}].contains('a')
-> true
[1, 'a', '2.5', 2.6, {a:false}, {a:1,b:2}].contains('b')
-> false
[1, 'a', '2.5', 2.6, {a:false}, {a:1,b:2}].contains(2.5)
-> false
[1, 'a', '2.5', 2.6, {a:false}, {a:1,b:2}].contains('2.5')
-> true
[1, 'a', '2.5', 2.6, {a:false}, {a:1,b:2}].contains({a:1})
-> false
[1, 'a', '2.5', 2.6, {a:false}, {a:1,b:2}].contains({b:2,a:1})
-> true
| |.unique()| This array extension method return new array having distinct/unique values
this compare array value deep search and check equality for finding unique value
note : it not clone the array so both array reference is same object.
return new array with unique values reference to original. eg.
[1, 2, 5, 4, 1, 2, 5, 8, 2, 5].unique();
-> [1, 2, 5, 4, 8]
[{a:true, b:false}, {a:true, b:false}, {a:false, b:false}, {a:true, b:false}].unique();
-> [{a:true, b:false}, {a:false, b:false}]| |isArray| [boolean property] return true if a variable holds Array value else false|

Set Prototype Extensions

|Set.methods|Description| |-|-| |.toArray()| This Set extension method that convert set to Array.

[1,2,3,1,5,4,2,3].toSet().toArray()
-> [1, 2, 3, 5, 4]|

Object Prototype Extensions

Common to All Data Type Vaiables

|Object.methods|Description| |-|-| |.clone()| This extension method make clone of any object/array/Date/other values| |.equals( val )| This extension method checks for equality of object in deep.

let obj1 = {a:true, b:44, c:[1, 2, 3]};
let obj2 = {a:true, b:44,c:[1, 2, 3]};
obj1.equals(obj2);
-> true

let obj1 = {a:true, b:44, c:[1, 2, 3, 4]};
let obj2 = {a:true, b:44, c:[1, 2, 3]};
obj1.equals(obj2);
-> false| |.typeof()| This extension method return typeof variable| |isObject| [boolean property] return true if a variable holds JS Object value else false|