Js Utils
Small JS functions, to help you during the development process. Here, we are just importing functions from this package.
Files to import
import { calculateAverage, calculateTotalSum, camelToCapitalize, capitalize, convertDate, convertDateShort, convertPrice, convertToEmail, convertToPascal, convertYoutube, deleteDuplicates, detectLanguage, disableScroll, emailRegex, enableScroll, formatHour, generateNumbers, getFirstName, getLastName, getPercentage, getRandom, getRandomAvatar, getRandomDate, getRandomNumber, getRandomString, getRandomTime, getTimeNow, getToday, getTomorrow, passwordRegex, scrollToTop, slugify, sortByFrequency, stringifyPx, unslugify, uuid } from "tsx-library-julseb"
Name | Effect | Example | Result |
---|---|---|---|
calculateAverage | Calculates the average of an array of numbers | calculateAverage([1, 2, 3, 4, 5]) | 3 |
calculateTotalSum | Calculates the total sum of an array of numbers | calculateTotalSum([3, 30, 42, 2]) | 77 |
camelToCapitalize | Transforms a camelCase or PascalCase to a Capitalized String | camelToCapitalize("helloWorld") | Hello World |
capitalize | Capitalizes the first letter of a string | capitalize("hello") | Hello |
convertDate | Converts a date in the format yyyy-mm-dd to dd fullMonth yyyy | convertDate(new Date("2022-01-28")) | 28 January 2022 |
convertDateShort | Converts a date in the format yyyy-mm-dd to dd shortMonth yyyy | convertDateShort(new Date("2022-01-28")) | 28 Jan 2022 |
convertToEmail | Converts a string to an email address. | convertToEmail("Julien Sebag", "me.com") | julien.sebag@me.com |
convertToPascal | Converts a string to PascalCase | convertToPascal("hello world") | HelloWorld |
convertYoutube | Converts a YouTube url to an embed link for iframes | convertYoutube("https://www.youtube.com/watch?v=2EaxYi31ips") | https://www.youtube.com/embed/2EaxYi31ips |
convertPrice | Converts an amount to a formatted price | convertPrice(1000, "EUR") | €1,000.00 |
deleteDuplicates | Delete all duplicates inside an array (also works with an array of objects) | deleteDuplicates([1, 1, 2, 3, 4, 5]).join(", ") | 1, 2, 3, 4, 5 |
detectLanguage | Search for lang or language in localStorage, and if it returns null both times detects the browser language | detectLanguage() | en-US |
disableScroll | Disable scrolling by adding a class stop-scrolling to the body. Do not forget to add our CSS file! | disableScroll() | |
enableScroll | Removes the class stop-scrolling on the body. | enableScroll() | |
formatHour | Formats an hour from a given number | formatHour(9.5) | 09:30 |
generateNumbers | Generate an array of numbers | generateNumbers(0, 5).join(", ") | 0, 1, 2, 3, 4 |
getFirstName | Returns the first name of a string | getFirstName("Julien Sebag") | Julien |
getLastName | Returns the last name of a string | getLastName("Julien Sebag") | Sebag |
getPercentage | Returns the percentage of two given values | getPercentage(24, 140) | 17.142857142857142% |
getRandom | Returns a random value inside an array | getRandom(["Hello", "world", "how", "are", "you"]) | you |
getRandomAvatar | Returns a random avatar from cartoon-avatar library. Can be male , female , or other for random gender. | getRandomAvatar("male") | |
getRandomDate | Returns a random date. You need to specify the minimum and maximum year | getRandomDate(1970, 2022) | 1971-09-26 |
getRandomNumber | Returns a random number in a range. You need to specify the min and max | getRandomNumber(1, 20) | 3 |
getRandomString | Returns a random string of letters and numbers. You need to specify the length | getRandomString(20) | ppDmS0tZniQ55klZ4FfI |
getRandomTime | Returns a random time. You need to specify the min hour and max hour. | getRandomTime(0, 23) | 10:26 |
getTimeNow | Returns the time | getTimeNow() | 13:36 |
getToday | Returns today's date | getToday() | 2025-04-19 |
getTomorrow | Returns tomorrow's date | getTomorrow() | 2025-03-20 |
passwordRegex | Tests if a password contains at least 6 characters, one number, one uppercase letter and one lowercase letter | passwordRegex.test("Password42") | true |
emailRegex | Tests if an email address is valid | emailRegex.test("a@b.c") | false |
scrollToTop | 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-you |
sortByFrequency | Sort array by frequency of its values | sortByFrequency(["Hello", "Hello", "foo", "baz", "baz", "hello", "world", "world"]) | hello, baz, world, foo |
stringifyPx | Returns a value in pixels if you enter a number | stringifyPx(80) | 80px |
unslugify | 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 you |
uuid | Generates a random id | uuid() | 985660675099 |