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]); // falseCheck if an array has duplicate values.
arrayHasDuplicates(['Hello', 'World', 'Hello']); // true
arrayHasDuplicates(['Hello', 'World', '!']); // falseRemoves 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 coolEscape regex inside a string
// this example assumes you are receiving a raw input, so this is not a valid JS example
escapeRegex('mike is cool'); // Mike is \\ coolLimits 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'); // IamcoolCalculates 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'); // 0Changes 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'); // falseSearch 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']); // falseRemove all fields with an undefined key.
const result = removeUndefinedFromObject({
a: 1,
b: undefined,
c: 'hello',
}); // result is { a: 1, c: "hello" }
const result = removeUndefinedFromObject({
a: ['some', 'value'],
b: undefined,
c: undefined,
d: null,
}); // result is { a: ["some", "value"], d: null }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'); // falseThis will check if the value passed in is a valid email.
isValidEmail('mark@gmail.com'); // true
isValidEmail('nick'); // false
isValidEmail('jack@mac@test.com'); // falseChecks if string is not empty, if it has at least 1 NON SPACE character, returns true. It will also check if passed in value is of type "string".
isNonEmptyStr(' '); // false
isNonEmptyStr('lol'); // true
isNonEmptyStr(23); // false because not a stringChecks if all strings in an array is not empty. It will also check if passed in value is of type "string[]".
isNonEmptyArrStr([' ', 'some text']); // false, 1 item is an empty string
isNonEmptyArrStr(['lol', 'some text']); // true
isNonEmptyArrStr(23); // false because not an array of strings
isNonEmptyArrStr([23]); // false because not an array of stringsChecks if the value provided can be found inside the provided enum
enum Sizes {
MIN = 'min',
MAX = 'max',
}
isOfEnum('min', Sizes); // true
isOfEnum(23, Sizes); // false, 23 is not part of the Sizes enum
isOfEnum('small', Sizes); // false, small is not part of the Sizes enumFormat JS date to date value that calendar inputs can use (YYYY-MM-DD)
formatToCalendarDate(new Date()); // 2023-04-28Format 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:18Converts number of minutes to time format (hh:mm)
formatMin(100); // 01:40
formatMin(120); // 02:00
formatMin(9999); // 66:39Converts number of seconds to time format (hh:mm:ss)
formatSec(100); // 00:01:40
formatSec(600); // 00:10:00
formatSec(9999); // 02:46:39Format timestamp (HH:MM) into minutes.
formatTimestamp('1:00'); // 60
formatTimestamp('10:52'); // 652
formatTimestamp('2:22'); // 142Format amount to currency format
formatMoneyStr(560); // 560.00 ZAR
formatMoneyStr(560, 'USD'); // 560.00 USD
formatMoneyStr(560, 'USD', false); // 560.00This will format the amount to the currency code passed in
currencyFormatter(420.69); // R 420,69
currencyFormatter(420.69, 'USD'); // US$420,69Not functions, but useful constants
Check if value is a digit
digitRegex.test('23'); // true
digitRegex.test('23.4'); // true
digitRegex.test('nope'); // falseCheck 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'); // falseRegex that can find links in a string
emailRegex.test('https://www.youtube.com/stevesteacher'); // true
emailRegex.test('mike@gmail.com'); // falseThese 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=jackSearch 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'); // falseIf you want to support the work I do, please consider donating to me on one of these platforms: