js-utils-julseb v2.0.3
JS utils
Small JavaScript functions
allCities
JSON file with all cities and countries in the world. Structure:
{
"country": "Germany",
"name": "Berlin"
},calculateTotalSum
Calculates the total sum of an array of numbers
calculateTotalSum([3, 30, 42, 2]) # 77capitalize
Capitalizes the first letter of a string
capitalize("hello") # HelloconvertDate
Converts a date in the format yyyy-mm-dd to dd fullMonth yyyy
convertDate("2022-01-28") # 28 January 2022convertDateShort
Converts a date in the format yyyy-mm-dd to dd shortMonth yyyy
convertDateShort("2022-01-28") # 28 Jan 2022convertToEmail
Converts a string to an email address. You can also specify the domain name
convertToEmail("Julien Sebag") # julien.sebag@email.com
convertToEmail("Julien Sebag", "me.com") # julien.sebag@me.comconvertToPascal
Converts a string to PascalCase
convertToPascal("hello world") # HelloWorldconvertYoutube
Converts a YouTube url to an embed link for iframes
convertYoutube("https://www.youtube.com/watch?v=2EaxYi31ips") # https://www.youtube.com/embed/2EaxYi31ipsconvertPrice
Converts an amount to a formatted price
convertPrice(1000, "EUR") # 1 000,00 €detectLanguage
Search for lang or language in localStorage, and if it returns null both times detects the browser language
detectLanguage() # "fr"getFirstName
Returns the first name of a string
getFirstName("Julien Sebag") # JuliengetLastName
Returns the last name of a string
getLasttName("Julien Sebag") # SebaggetRandom
Returns a random value inside an array
getRandom(["Hello", "world", "how", "are", "you"]) # howgetRandomAvatar
Returns a random avatar from cartoon-avatar library. Can be male, female, or other for random gender.
getRandomAvatar("male") # https://raw.githubusercontent.com/Ashwinvalento/cartoon-avatar/master/lib/images/male/86.pnggetRandomCity
Returns a random city from a specified country
getRandomCity("France") # Nîmes, FrancegetRandomDate
Returns a random date. You need to specify the minimum and maximum year
getRandomDate(1970, 2022) # 1989-03-12getRandomLocation
Returns a random location from the allCities JSON file
getRandomLocation() # Ailsworth, United KingdomgetRandomNumber
Returns a random number in a range. You need to specify the min and max
getRandomNumber(1, 20) # 12getRandomString
Returns a random string of letters and numbers. You need to specify the length
getRandomString(20) # ad98W9OYNHbBZt6xdz5TgetRandomTime
Returns a random time. You need to specify the min hour and max hour.
getRandomTime(0, 23) # 10:24getTimeNow
Returns the time
getTimeNow() # 14:42getToday
Returns today's date
2022-03-07passwordRegex
Tests if a password contains at least 6 characters, one number, one uppercase letter and one lowercase letter
passwordRegex.test("Password42") # trueemailRegex
Tests if an email address is valid
emailRegex.test("a@b.c") # falsescrollToTop
Function to scroll to the top of the page on click
slugify
Replaces accents, spaces, uppercase letters, etc., in a string to convert it to a url friendly string
slugify("Hello world how are you?") # hello-world-how-are-youstringifyPx
Returns a value in pixels if you enter a number
stringifyPx(80) # 80pxunslugify
Replaces dashes and underscores to a space, and capitalize the first letter of a string
unslugify("hello-world-how-are-you") # Hello world how are you3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago