@bolajiolajide/utils
Collection of utility functions/helpers I use in my everyday development.
Methods
capitalize
This method is used to capitalize a string.- value string to be capitalize
const { capitalize } = require('@bolajiolajide/utils');
const data = capitalize('bolaji');
console.log(data);
// 'Bolaji'
convertSliceToString
This method is used to convert a string slice into a string.- value string slice to convert
- separator string to use to separate the different items in the slice
const { convertSliceToString } = require('@bolajiolajide/utils');
const data = convertSliceToString('bolaji');
console.log(data);
// 'bolaji'
const data2 = convertSliceToString(['bol', 'aji']);
console.log(data2);
// 'bolaji'
const data3 = convertSliceToString(['bol', 'aji', 'pro', 'ton'], '**');
console.log(data3);
// 'bol**aji**pro**ton'
countries
This is a list of countries all over the world. It's a pretty long list.const { countries } = require('@bolajiolajide/utils');
console.log(countries);
// ['Afghanistan', ...]
delay
This method is used to add a delay to an async method. It takes in one argument which is the amount of milliseconds to delay.const { delay } = require('@bolajiolajide/utils');
await delay(10000); // delay for 10seconds
generateCSV
This method is used to generate a csv string from an array of objects. It takes in array of objects with a key-value type of string. The output is a string which will be the records in the array delimited by a comma.const { generateCSV } = require('@bolajiolajide/utils');
const data = [
{ name: 'John Doe', age: 20 },
{ name: 'Jane Doe', age: 23 }
];
const csv = generateCSV(data);
console.log(csv);
// name, age
// John Doe, 20
// Jane Doe, 23
generateShortCode
This method is used to generate a not so unique shortcode. The default shortcode length is 5.const { generateShortCode } = require('@bolajiolajide/utils');
const shortcode = generateShortCode(10);
console.log(shortcode);
// 637010000
isDict
This method returns a boolean depending on whether the argument supplied is a dictionary.- value literal to check type
const { isDict } = require('@bolajiolajide/utils');
const data = isDict('bolaji');
console.log(data);
// false
const data2 = isDict({ amount: 230329 });
console.log(data2);
// true
isString
This method returns a boolean depending on whether the argument supplied is a string.- value literal to check type
const { isString } = require('@bolajiolajide/utils');
const data = isString('bolaji');
console.log(data);
// true
const data2 = isString(230329);
console.log(data2);
// false
runOnce
This method is used to set a function to run only once. It's argumument is:- fn the function to be run only once
const { runOnce } = require('@bolajiolajide/utils');
const addTogether = function(num1:number, num2:number):number{
return num1+num2
}
const addTogetherOnce = runOnce(addTogether)
let firstResult = addTogetherOnce(0,1)
let secondResult = addTogetherOnce(0,1)
console.log(firstResult)
console.log(secondResult)
});
// firstResult: 1
// secondResult: undefined
paginate
This method is used to lazily paginate an array of items. It can be used for client-side pagination where no server exists. It's argumumets are:- limit how many items to receive per page
- page page number to be retrieved
- data the array of data to be paginated
const { paginate } = require('@bolajiolajide/utils');
const data = paginate(2, 2, [
'Jane',
'John',
'James',
'Bill',
'Steve',
'Melissa',
'Esther',
'Shannon'
]);
console.log(data);
// ['James', 'Bill']
sentencize
This method is used to capitalize several words in a phrase depending on the separator. It's argumumets are:- word the word/phrase to sentencize
- separator a string separating the words in a phrase, defaults to ' '
const { sentencize } = require('@bolajiolajide/utils');
const data = sentencize('APPLE_MUSIC', '_');
console.log(data);
// 'Apple Music'
const data = sentencize('SPOTIFY');
console.log(data);
// 'Spotify'
isUrl
This method is used to check if a string is a valid URL- url the url to be checked
const { isUrl } = require('@bolajiolajide/utils');
const response = isUrl('APPLE_MUSIC');
console.log(response);
// false
const response = isUrl('https://google.com');
console.log(response);
// true
isHttpUrl
This method is used to check if a string is a valid http URL- url the url to be checked
const { isHttpUrl } = require('@bolajiolajide/utils');
const response = isHttpUrl('ftp://dskjdslds');
console.log(response);
// false
const response = isUrl('https://google.com');
console.log(response);
// true