Simple utilities for your JavaScript/Typescript project!
Installation: npm i @netsu/js-utils
NPM: https://www.npmjs.com/package/@netsu/js-utils
Removes all 'undefined' values from an array
removeUndefinedFromArray([1, undefined, 2, undefined, 3]); // [1, 2, 3]
Removes all 'null' from an array
removeNullFromArray([1, null, 2, null, 3]); // [1, 2, 3]
Removes all 'undefined' and 'null' values from an array
removeVoidValuesFromArray([1, null, 2, undefined, 3]); // [1, 2, 3]
Returns TRUE if the first specified array contains all elements from the second one. FALSE otherwise.
arrayContainsArray([1, 2, 3], [1, 2]); // true
arrayContainsArray([1, 3], [1, 2]); // false
Check if an array has duplicate values.
arrayHasDuplicates(["Hello", "World", "Hello"]); // true
arrayHasDuplicates(["Hello", "World", "!"]); // false
Removes duplicate values from array.
removeDuplicatesFromArray([1, 1, 2, 3, 4, 4]); // [1, 2, 3, 4]
removeDuplicatesFromArray(["my", "my", "you"]); // ["my", "you"]
Convert the first letter of a piece of text to uppercase
capitalizeFirstLetter("mike is cool"); // Mike is cool
Limits a piece of text, if the text is longer than the limit, then the text will end with '...' after the limited amount of characters has been reached. Default limit is 50 characters.
NOTE: This will trim the text of useless white space!
// Hello world
limitText("Hello world");
// Hello...
limitText("Hello world", 5);
// Lorem ipsum dolor, sit amet consectetur adipisicin...
limitText("Lorem ipsum dolor, sit amet consectetur adipisicing elit.");
Remove the spaces in a given string
removeSpacesFromStr("I am cool"); // Iamcool
Calculates how similar 2 strings are to each other.
calculateStringSimilarity("mike", "mike"); // 1 (100% similar)
calculateStringSimilarity("jackie chan", "jackie chon"); // 0.9090
calculateStringSimilarity("jakeiscool", "jakecool"); // 0.8
calculateStringSimilarity("mark", "nicole"); // 0
Changes all links in a text to an anchor tag.
// Cannot be manually closed. Click this: <a href="https://www.youtube.com/stevesteacher" target="_blank">https://www.youtube.com/stevesteacher</a> and <a href="https://www.youtube.com" target="_blank">https://www.youtube.com</a>
enableTextLinks(
"Cannot be manually closed. Click this: https://www.youtube.com/stevesteacher and https://www.youtube.com"
);
Search for text in string, this will ignore casing.
regexStringSearch("cool", "I am cool"); // true
regexStringSearch("cool", "I am drool", "ig"); // true
regexStringSearch("cool", "yup"); // false
Search for text in string, this will ignore casing. Will be true if at least one of the searches returned true.
regexStringListSearch("cool", ["I am cool"]); // true
regexStringListSearch("cool", ["I am drool", "you are cool"], "ig"); // true
regexStringListSearch("cool", ["yup", "nothing"]); // false
This will check if the value passed in is a valid number, mainly useful on strings.
Note: Infinity and -Infinity will be marked as NOT valid numbers
isValidNumber(20); // true
isValidNumber("20.3"); // true
isValidNumber("lol"); // false
Checks if string is empty, if it is, returns true. It will also check if passed in value is of type "string".
checkStrEmpty(" "); // true
checkStrEmpty("lol"); // false
checkStrEmpty(23); // true because not a string
Format JS date to date value that calendar inputs can use (YYYY-MM-DD)
formatToCalendarDate(new Date()); // 2023-04-28
Format JS date to date and time that people can easily understand (defaults to D MMM YYYY HH:mm
)
formatToHumanDate(new Date()) // 28 Apr 2023 10:18
formatToHumanDate(new Date(), "D-MM-YY hh:mm") // 28-04-23 10:18
Converts number of minutes to time format (hh:mm)
formatMin(100); // 01:40
formatMin(120); // 02:00
formatMin(9999); // 66:39
Converts number of seconds to time format (hh:mm:ss)
formatSec(100); // 00:01:40
formatSec(600); // 00:10:00
formatSec(9999); // 02:46:39
Format timestamp (HH:MM) into minutes.
formatTimestamp("1:00"); // 60
formatTimestamp("10:52"); // 652
formatTimestamp("2:22"); // 142
Format amount to currency format
formatMoneyStr(560); // 560.00 ZAR
formatMoneyStr(560, "USD"); // 560.00 USD
formatMoneyStr(560, "USD", false); // 560.00
This will format the amount to the currency code passed in
currencyFormatter(420.69); // R 420,69
currencyFormatter(420.69, "USD"); // US$420,69
Not functions, but useful constants
Check if value is a digit
digitRegex.test("23"); // true
digitRegex.test("23.4"); // true
digitRegex.test("nope"); // false
Check if email is valid
emailRegex.test("mike"); // false
emailRegex.test("mike@gmail.com"); // true
emailRegex.test("mike09+cool@gmail.com"); // true
emailRegex.test("mike09+cool@gmail"); // false
emailRegex.test("mike09+cool.com"); // false
Regex that can find links in a string
emailRegex.test("https://www.youtube.com/stevesteacher"); // true
emailRegex.test("mike@gmail.com"); // false
These are functions that either does not fit in one of the specified categories, or now does, but would break programs if moved elsewhere.
Will generate a url with the given query parameters.
Note that if the value in data is undefined or null the parameter will be ignored.
urlQueryBuilder("http://test.com/wow", {
cool: true,
name: "jack",
}); // http://test.com/wow?cool=true&name=jack
Search for text in string, this will ignore casing. Will be true if at least one of the searches returned true.
NOTE: this uses .include() to search, so will only work on individual words and not text in words. Use regexStringSearch() or regexStringListSearch() instead if you want a more powerful search
includeSearch("cool", "I am cool"); // true
includeSearch("cool", "I am drool", "you are cool"); // true
includeSearch("cool", "yup", "nothing"); // false
If you want to support the work I do, please consider donating to me on one of these platforms: