Lyra-JS (1.0.8)

Selectors / Dates / String Prototypes / Array Prototypes / Functions

Selectors

lr.i()

Returns the element that has the ID attribute with specified value. Takes in one argument that corresponds to the name of the ID.

lr.i("id")

lr.q()

Returns the first element that matches a specified ID, class name or tag name. Takes in one argument that corresponds to the name of the ID, class or tag.

lr.q(".className")

lr.qa()

Returns all elements that matches a specified ID, class or tag name. Takes in one argument that corresponds to the name of the ID, class or tag.

lr.qa("h1")

Dates

lr.convertDateAdvanced()

lr.convertDateAdvanced is an important and useful function as most date functions in JavaScript and other date libraries, including this one, expects a valid date string or date object as an argument.

lr.convertDateAdvanced can convert any date format to a valid date object or string. It takes in three arguments, first one is the format of the date you are passing on, second is the date and third is whether you want a date string or object.

If you would like a date object, pass a string "short" as the third argument, like in the third example below.

lr.convertDateAdvanced("dd, mm yy", "30, June 2018") //Returns Sat Jun 30 2018 00:00:00

lr.convertDateAdvanced("yy dd-mm hh:min:ss", " 2018 28-3 13:30:00") //Returns Wed Mar 28 2018 13:30:10

lr.convertDateAdvanced("yy-mm hh-min @dd th", "2018-07 15:10 @10 th", "short") //Returns date object
Date formatting keys:

yy (required) - Full year. Eg: 2018

mm (required) - Full Month name, half month name or month number. Eg: January, Sep or 7

dd (required) - Date number. Eg: 3

hh (optional*) - Hour number. Eg: 13, 3

min (optional) - Minute number. Eg: 25

ss (optional) - Second number. Eg: 5

*Hours is optional; however, it is required if min is mentioned in the format.

Note: Formatting keys are case sensitive and shouldn't have a letter or word attached to it. Eg: ddday for 15day is invalid.

lr.convertDate()

lr.convertDate is a simplified version of lr.convertDateAdvanced. Formatting keys can have only one separator eg: - , @ etc.

It takes in three arguments, first one is the format of the date your passing on, second is the date and third is whether you want a date string or object.

If you would like a date object, pass a string "short" as the third argument, like in the first example below.

If you would like to add time, pass it in with second argument and put a space between date and time, the time should be on the right hand side, no need to mention it in the first argument

lr.convertDate("dd-mm-yy", "12-05-2018", "short") //Returns date object

lr.convertDate("yy@mm@dd", "2018@3@3 13:30:00") //Returns Sat Mar 03 2018 13:30:00
Date formatting keys:

yy (required) - Full year. Eg: 2018

mm (required) - Full month name, half month name or month number. Eg: January, Sep or 7

dd (required) - Date number. Eg: 3

Note: Formatting keys are case sensitive and shouldn't have a letter or word attached to it. Eg: ddday for 15day is invalid.

lr.getDate()

Get a date in any format you like, essentially the opposite of lr.convertDateAdvanced. It takes in two arguments, first the format you would like the date to be in and second which is optional, a specific date that needs to be a valid date string or object.

lr.getDate("ddth, fm yy", "Sat Jun 30 2018") //Returns 30th, June 2018

lr.getDate("dd sm") //Returns current date in specified format (Eg: 18 Sep)
Formatting keys:

yy- Full year. Eg: 2018

mm- Month number. Eg: 12

fm- Full month name. Eg: February

sm- Short month name. Eg: May

dd- Day number. Eg: 5

dn- Day name. Eg: Tuesday

dsn- Short day name. Eg: Fri

hh- Hour number. Eg: 13

min- Minute number. Eg: 56

ss- Second number. Eg: 12

lr.getTimeByZone()

Get date and time of any time zone. The function takes one argument, which is the time zone ID of the time offset.

lr.getTimeByZone("JST") //Returns time in Japan (JST - Japanese Standard Time)

lr.getTimeByZone("IST") //Returns time in India (IST - Indian Standard Time)

If you would like to know time zone ID / time offset check out the link here

If you know the time offset or want to know the time zone ID of any device use lr.getCC

lr.convertTimeByZone()

Convert time from one country's / time zone's to another country's / time zone's time. The function takes in three arguments, first one is the initial time zone ID, second is the time zone ID you want to convert to, third is the date and time that needs to be a valid date string or object.

lr.convertTimeByZone("IST","JST","Sat, 30 Jun 2018 12:43:00") //Returns Sat Jun 30 2018 16:13:00

lr.getCC()

Get date and time of any time zone. The function takes one argument, which is the time zone ID of the time offset.

lr.getCC() //Retruns time zone ID for device

lr.getCC(4) //Returns time zone ID for time offset of +4:00 hours

lr.getCC(-3.5) //Returns the time zone ID for the time offset of -3:30 hours

lr.add()

Adds specified parameters to date. The function takes two arguments, first is by how much you want to move the date forward to, second is the date, second argument is optional. If a second argument is not provided it will use current date.

lr.add("Tuesday", "Sat Jun 30 2018 16:13:00") //Moves date to next Tuesday. Returns Tue Jul 03 2018 16:13:00

lr.add("5 days") //Adds 5 days to current date

Keys:

Days- Eg: 5 Days or 1 Day

Months-Eg: 2 Months

Years- Eg: 4 Years

Name of day- Eg: Wednesday

Hours- Eg: 5 Hours

Minutes-Eg: 2 Minutes

Seconds- Eg: 4 Seconds

lr.sub()

Subtracts specified parameters to date. The function takes two arguments, first is by how much you want to move the date back to, second is the date, second argument is optional. If a second argument is not provided it will use current date.

lr.sub("Wed", "Sat Jun 30 2018 16:13:00") //Moves date back to Wednesday. Returns Wed Jun 27 2018 16:13:00

lr.sub("5 days") //Subtracts 5 days from current date

Keys:

Days- Eg: 5 Days or 1 Day

Months-Eg: 2 Months

Years- Eg: 4 Years

Name of day- Eg: Wednesday

Hours- Eg: 5 Hours

Minutes-Eg: 2 Minutes

Seconds- Eg: 4 Seconds

lr.dateDifference()

Finds difference between two dates (Leap Year Included). The functions takes three parameters, first the date you want in the L.H.S, second the date you want in the R.H.S, third argument is if you want time too.

Pass string "long" in the third parameter if you want hours, minutes and seconds

lr.dateDifference("Fri Sep 14 2018 15:31:10", "Wed Jun 27 2018 16:13:00", "long")
//Returns 2 Months 16 Days 23 Hours 18 Minutes 10 Seconds

lr.dateDifference("Fri Dec 5 2018", "Wed May 2 2010") //Returns 8 Years 7 Months 4 Days

String Prototypes

String.prototype.replaceAll()

Replace every occurrence of a word in a string specified in the first argument with the word passed in the second argument.

"Hello world world".replaceAll("world", "echo") //Returns Hello echo echo

String.prototype.titleCase()

Capitalizes every word in string.

"this is a title".titleCase() //Returns This Is A Title

String.prototype.camelize()

Turns kebab case to camel case.

"background-text-color".camelize() //Returns backgroundTextColor

String.prototype.contains()

Checks if string contains argument that is passed on. It also takes a second argument which is a boolean to indicate case sensitivity.

"This library is awesome".contains("awesome") //Returns true

"This library is awesome".contains("this") //Returns false

"This library is awesome".contains("this", false) //Returns true

String.prototype.count()

Counts number of occurrences of a word in a string.

"There is hello twice in this sentence, Hello".count("hello") //Returns 1

"There is hello twice in this sentence, Hello".count("hello", false) //Returns 2

String.prototype.format()

Replace keys in a string for words.

"Hello {key1}, I think you are just {key2}".format({ key1: "World", key2: "great" })
//Returns Hello World, I think you are just great

String.prototype.shorten()

Shortens string to number of characters mentioned in the first argument and replaces it with characters in the second argument

"This is not a fairly long string".shorten(7, "...") //Returns This is...

String.prototype.forEach()

Loops for each character

"lyra-js".forEach(function(letter, i){
  console.log(letter+"=>"+i)
})
// Returns l=>0 y=>1 r=>2 a=>3 -=>4 j=>5 s=>6

String.prototype.forEachWord()

Loops for each word

"Loops for each word".forEachWord(function(word, i){
  console.log(word+"=>"+i)
})
// Returns Loops=>0 for=>1 each=>2 word=>3

String.prototype.indices()

Searches for a letter in a string and returns its position

"hello world".indices("l") //Returns Array(3) [ 2, 3, 9 ]

Array Prototypes

Array.prototype.indices()

Checks if array contains a specific value and gives its position. It also takes a second argument which is a boolean to indicate case sensitivity.

["It", "is", "wednesday", "my", "dudes"].indices("dudes") //Returns Array [4]

["It", "is", "wednesday", "my", "dudes"].indices("it") //Returns Array []

["It", "is", "wednesday", "my", "dudes"].indices("it", false) //Returns Array [0]

Array.prototype.indexOfMax()

Returns index of max value in an array.

[56, 78, 42, 10].indexOfMax() //Returns 1

Array.prototype.indexOfMin()

Returns index of min value in an array.

[56, 78, 42, 10].indexOfMin() //Returns 3

Array.prototype.maxArray()

Returns value of biggest value in an array.

[56, 78, 42, 10].maxArray() //Returns 78

Array.prototype.minArray()

Returns value of smallest value in an array.

[56, 78, 42, 10].minArray() //Returns 10

Array.prototype.toLowerCase()

Lower cases all values in an array.

["hElLo", "1", "wOrLd"].toLowerCase() //Returns Array(3) [ "hello", "1", "world" ]

Array.prototype.toUpperCase()

Upper cases all values in an array.

["hello", 1, "world"].toUpperCase() //Returns Array(3) [ "HELLO", 1, "WORLD" ]

Functions

lr.generateRand()

Returns a random string of character. The function takes in two arguments, the first one indicates how many characters you want and the second one takes the type of string you want.

lr.generateRand(12) //Returns 12 character number (Eg: 704903664315)

lr.generateRand(12, "alphanum") //Returns a 12 character alphanumeric string (Eg: GEtL3uWk2OA6)
Types

alphanum- For alphanumeric string

string- For string with only alphabets

*Leave second argument as blank to get a string with only numbers

lr.getAbsoluteUrl()

Gets absolute url and adds passed argument.

lr.getAbsoluteUrl("hello")

//Eg: www.xyz.com/hello or www.xyz.com/view/hello

lr.getURLargs()

Gets arguments from URL, follow format below.

//URL: www.xyx.com/view?hello&world

lr.getURLargs()

//Returns Array [ "hello", "world" ]