-
Version: 2.0.32
-
File: Utiliti.Js
-
Author: Adeniji OluwaFeranmi
-
Created: 27 November, 2022
-
Update: 06 June, 2023
If you have any questions that are beyond the scope of this help file, Please feel free to email via Support Page.
UtilitiJs is a comprehensive JavaScript utility library that provides a collection of helpful functions for common tasks and operations. With UtilitiJs, developers can simplify their coding process and enhance productivity by leveraging the pre-built utility functions available in the library.
UtilitiJs offers a wide range of features and functionalities, including:
-
Type Checking: UtilitiJs includes a typeCheck module that allows developers to perform type checks on various data types such as integers, strings, arrays, objects, functions, booleans, and NaN (not a number). This module provides functions like
isInteger
,isString
,isArray
,isObject
,isFunction
,isBoolean
, andisNaN
to perform type checks easily. -
Value Checking: The valueCheck module in UtilitiJs provides functions to check various properties of numeric values. Developers can use functions like
isPositive
,isNegative
,isZero
,isEven
,isOdd
,isPrime
,isWhole
, andisFractional
to check if a number is positive, negative, zero, even, odd, prime, whole, or fractional. -
String Manipulation: UtilitiJs offers a string module that includes functions to manipulate and transform strings. Developers can utilize functions like
capitalize
,reverse
,truncate
,startsWith
,endsWith
,contains
,replaceAll
, and more to perform common string operations efficiently. -
Array Manipulation: The array module in UtilitiJs provides functions for working with arrays. Developers can find functions like
flatten
,unique
,shuffle
,chunk
,sortBy
,unique
,countOccurrences
, and many others to simplify array manipulations and transformations. -
Mathematical Operations: UtilitiJs includes a math module with functions for performing various mathematical operations. Developers can utilize functions like
clamp
,lerp
,toDegrees
,toRadians
,roundTo
,randomInt
,sum
,average
, and more to perform common mathematical calculations. -
Date and Time: UtilitiJs offers a date module with functions for working with dates and time. Developers can use functions like
formatDate
,isLeapYear
,getDaysInMonth
,addDays
,subtractDays
,compareDates
,isPastDate
, and more to handle date and time-related tasks effectively.
These are just a few highlights of the extensive set of features provided by the UtilitiJs library. By leveraging UtilitiJs, developers can streamline their development process, reduce boilerplate code, and enhance the efficiency of their JavaScript projects.
UtilitiJs can be easily installed via popular package managers like npm or yarn. Simply run the following command in your project directory:
npm install utiliti-js
or
yarn add utiliti-js
Once installed, you can import the desired modules and start using the utility functions in your JavaScript or TypeScript code.
Here's an example of how you can use UtilitiJs in your code:
import { typeCheck, valueCheck, string, array, math, Core } from 'utiliti-js';
const date = new Core.DateFilter();
console.log(typeCheck.isInteger(42)); // Output: true
console.log(valueCheck.isPositive(5)); // Output: true
console.log(string.capitalize('utilitijs')); // Output: Utilitijs
console.log(array.flatten([1, [2, [3, [4]]]])); // Output: [1, 2, 3, 4]
console.log(math.roundTo(3.14159, 2)); // Output
: 3.14
console.log(date.formatDate(new Date(), 'DD/MM/YYYY')); // Output: 05/06/2023
UtilitiJs provides an intuitive and consistent API, making it easy to integrate into your projects and accelerate your development process. Refer to the documentation for detailed information on all the available functions and their usage.
The Core
module contains functions for making HTTP
requests. It defines a
Core
object with methods for performing different operations like managing
state, handling data etc. It also defines a Error()
function for
creating custom error objects that have a getInfo()
method for retrieving
information about the error.
The UtilitiJs Library is intended to be used as a utility library in a JavaScript application. It also defined a Operators
module that allows you to perform logical operations, check types, analyse data and do complex calculations.
Core
is an Object with loads of utility functions The first function, Http,
contains methods for making different types of HTTP requests: get, post, put,
and delete. The DataFilter function takes in a filter function and an array of
data and returns an array of data that has been filtered according to the filter
function. The DateFilter function contains functions for formatting dates as
text or according to a specified format. Finally, the Store class is a basic
implementation of a redux store, which is a state management tool for managing
application state in JavaScript applications.
The Operators module is a set of JavaScript functions that defines a number of
different logical, comparison, and value checking operations. It also includes
some functions for checking the type of a value, as well as some statistical
functions for calculating the sum, mean, median, and mode of an array of
numbers. Some examples of the operations defined in this function are and
,
or
, not
, xor
, nand
, nor
, implies
, iff
, equals
, greaterThan
,
lessThan
, and isPositive
, isNaN
, parseInt
, parseFloat
. These functions can be used to perform logical
operations on values and compare them in different ways.
The Data Filter class represents a data filter that can be used to filter an array of data based on a provided filter function. The class provides a filter
method to apply the filter function to an array of data.
To use the Data Filter class, you can create an instance and provide a filter function:
const DataFilter = new DataFilter((item) => {
// Filter logic here
return item.age > 18;
});
The filter
method can be used to apply the filter function to an array of data:
filter(data: T[]): T[]
Example:
const data = [
{ name: 'John', age: 25 },
{ name: 'Jane', age: 16 },
{ name: 'Bob', age: 30 },
];
const filteredData = DataFilter.filter(data);
console.log(filteredData);
// Output: [{ name: 'John', age: 25 }, { name: 'Bob', age: 30 }]
The Data Filter class throws a Error
if the filter function is not a valid function. Make sure to handle this error appropriately in your application.
Example Error Handling:
try {
const filteredData = DataFilter.filter(data);
} catch (error) {
if (error instanceof Error) {
// Handle Error
} else {
// Handle other errors
}
}
The Data Filter class provides a convenient way to filter an array of data based on a filter function. Use this class to simplify your data filtering operations and enhance the flexibility of your application.
The HTTP Client module provides a simple and flexible way to make HTTP requests in JavaScript/TypeScript. It supports various HTTP methods like GET, POST, PUT, PATCH, and DELETE. The module also allows you to register interceptors for request/response handling.
To use the HTTP Client module in your TypeScript project, you can import the module and create an instance of the Http
class.
import { Http } from 'utiliti-js';
const http = new Http();
The Http
class provides methods for making different types of HTTP requests: get
, post
, put
, patch
, and delete
. These methods return a promise that resolves to the response from the server.
http.get(url: string, headers?: Object): Promise<Response>
Example:
http.get('https://api.example.com/users', { Authorization: 'Bearer token' })
.then((response) => {
// Handle the response
})
.catch((error) => {
// Handle the error
});
http.post(url: string, data: any, headers?: Object): Promise<Response>
Example:
const user = {
name: 'John Doe',
email: 'john.doe@example.com',
};
http.post('https://api.example.com/users', user, { Authorization: 'Bearer token' })
.then((response) => {
// Handle the response
})
.catch((error) => {
// Handle the error
});
http.put(url: string, data: any, headers?: Object): Promise<Response>
Example:
const updatedUser = {
name: 'John Smith',
email: 'john.smith@example.com',
};
http.put('https://api.example.com/users/123', updatedUser, { Authorization: 'Bearer token' })
.then((response) => {
// Handle the response
})
.catch((error) => {
// Handle the error
});
http.patch(url: string, data: any, headers?: Object): Promise<Response>
Example:
const updatedFields = {
name: 'John Smith',
};
http.patch('https://api.example.com/users/123', updatedFields, { Authorization: 'Bearer token' })
.then((response) => {
// Handle the response
})
.catch((error) => {
// Handle the error
});
http.delete(url: string, headers?: Object): Promise<Response>
Example:
http.delete('https://api.example.com/users/123', { Authorization: 'Bearer token' })
.then((response) => {
// Handle the response
})
.catch((error) => {
// Handle the error
});
The Http
class provides a retry feature that allows for automatic retries when an HTTP request fails. This feature is helpful for handling transient errors and improving the robustness of HTTP communication.
The retry feature in the Http
class is configurable with the following options:
retryDelay
(number, default:1000
): The interval between a failed request and the next retry attempt in milliseconds. This value determines how long to wait before retrying the request after a failure.retryAttempt
(number, default:3
): The number of times a request should be retried if it fails. This value determines the maximum number of retry attempts for each failed request.
By default, the retry delay is set to 1000
milliseconds (1 second), and the retry attempt is set to 3
. You can customize these values when creating an instance of the Http
class, as demonstrated in the example below.
To utilize the retry feature of the Http
class, follow these steps:
- Create an instance of the
Http
class with the desired retry configuration.
Example:
import { Http } from 'utiliti-js';
// Create an instance of the Http class with custom retry settings
const http = new Http({
retryDelay: 2000, // Retry after 2 seconds on failure
retryAttempt: 5, // Retry up to 5 times on failure
});
- Use the HTTP methods provided by the
Http
class (get
,post
,put
,patch
,delete
) to send requests. The retry feature is applied automatically to all requests made through theHttp
class instance.
Example:
// Perform a GET request with automatic retries on failure
http.get('https://api.example.com/data')
.then((response) => {
// Handle the successful response
console.log('Success:', response);
})
.catch((error) => {
// Handle the error, including any retries if applicable
console.error('Error:', error);
});
The Http
class will automatically retry the request if it encounters any network-related failures, such as a server timeout or connection issue. If the maximum number of retry attempts is reached and the request still fails, the final error will be thrown, and the catch block will be triggered with the corresponding error information.
Please note that the retry feature is especially useful for handling transient errors that might be resolved with subsequent retries. However, keep in mind that not all types of errors are eligible for retries, and careful consideration should be given to the specific use case and nature of the failure before enabling automatic retries.
The retry feature works in conjunction with custom interceptors. Interceptors allow you to modify the request and response objects before and after each request. If you have added any interceptors using the addInterceptor
or addScopedInterceptor
methods, they will be applied during each retry attempt as well.
Make sure that any interceptors you add do not interfere with the retry process, as this could lead to unexpected behavior.
The Http
class includes a custom error class named HttpRequestError
, which is used to represent HTTP request-related errors. This custom error class extends the standard Error
class and includes additional properties such as method
, url
, data
, and headers
, providing relevant information about the failed request.
When an HTTP request fails, the Http
class will throw a HttpRequestError
if any of the following conditions are met:
- The provided
method
,url
, orheader
is not of the expected type (string or object, respectively). - One of the interceptors is not a function.
- The maximum number of retry attempts (
retryAttempt
) is exceeded.
You can catch and handle HttpRequestError
instances in your code to handle any potential request-related errors effectively.
- The retry feature should be used judiciously, as excessive retries could cause additional load on servers and result in unexpected behavior for certain requests.
- Ensure that the retry delay and retry attempt values are set appropriately based on the nature of the requests and the server's capacity.
- Implement proper error handling and logging to track the success or failure of retry attempts and understand potential issues.
For more details on the usage of the Http
class and other functionalities, refer to the main documentation or comments within the code.
Interceptors allow you to modify requests or responses globally or for specific requests. You can add interceptors using the useInterceptors
and useScopedInterceptors
methods.
Global interceptors apply to all requests made using the Http
instance.
http.useInterceptors(interceptors: Function[]): void
Example:
function requestInterceptor(request: any, next: Function) {
// Modify the request
// ...
// Call the next interceptor or send the request
return next(request);
}
http.useInterceptors([requestInterceptor]);
Scoped interceptors apply to specific requests based on the method and URL.
http.useScopedInterceptors(interceptor: Function[]): void
Example:
function responseInterceptor(response: any, next: Function) {
// Modify the response
// ...
// Call the next interceptor or return the response
return next(response);
}
http.useScopedInterceptors([responseInterceptor], 'https://api.example.com/users/123', 'GET');
For more details on using interceptors and the Http class, refer to the documentation folder.
The Core.Http module provides methods for making HTTP requests to retrieve, create, update, and delete resources on a server. Please note that the use of Core.Http
has been deprecated. It is recommended to use the Http
module instead.
To use the HTTP module, you can create an instance:
import { Core } from "utiliti-js";
const http = new Core.Http();
The get
method sends a GET request to retrieve data from the server:
get(url: string, header: HeadersInit = {}): Promise<Response>
url
(string): The URL to send the GET request to.header
(object): Optional. The headers to include in the request.
Example:
const response = await http.get("https://api.example.com/data", { Authorization: "Bearer token" });
const data = await response.json();
console.log(data);
The post
method sends a POST request to create a new resource on the server:
post(url: string, data: object, header: HeadersInit = {}): Promise<Response>
url
(string): The URL to send the POST request to.data
(object): The data to include in the request body.header
(object): Optional. The headers to include in the request.
Example:
const data = { name: "John Doe", email: "john@example.com" };
const response = await http.post("https://api.example.com/users", data, { Authorization: "Bearer token" });
const createdUser = await response.json();
console.log(createdUser);
The put
method sends a PUT request to update an existing resource on the server:
put(url: string, data: object, header: HeadersInit = {}): Promise<Response>
url
(string): The URL to send the PUT request to.data
(object): The data to include in the request body.header
(object): Optional. The headers to include in the request.
Example:
const data = { name: "John Doe", email: "john@example.com" };
const response = await http.put("https://api.example.com/users/123", data, { Authorization: "Bearer token" });
const updatedUser = await response.json();
console.log(updatedUser);
The patch
method sends a PATCH request to update a resource on the server:
patch(url: string, data: object, header: HeadersInit = {}): Promise<Response>
url
(string): The URL to send the PATCH request to.data
(object): The data to include in the request body.header
(object): Optional. The headers to include in the request.
Example:
const data = { name: "John Doe", email: "john@example.com" };
const response = await http.patch("https://api.example.com/users/123", data, { Authorization: "Bearer token" });
const updatedUser = await response.json();
console.log(updatedUser);
The delete
method sends a DELETE request to delete a resource on the server:
delete(url: string, header: HeadersInit = {}): Promise<Response>
url
(string): The URL to send the DELETE request to.header
(object): Optional. The headers to include in the request.
Example:
const response = await http.delete("https://api.example
.com/users/123", { Authorization: "Bearer token" });
console.log("Resource deleted");
The use of Core.Http
has been deprecated. It is recommended to use the Http
module instead. Please update your code to use the Http
module for making HTTP requests.
The HTTP module provides convenient methods for making HTTP requests to retrieve, create, update, and delete resources on a server. Use this module to interact with APIs and handle data exchange in your application.
The Date Filter class represents a date filter that provides methods for converting dates to text format and formatting dates based on specified formats. Please note that the DateFilter
function has been renamed to DateFilter
. Use the DateFilter
class for date filtering operations.
To use the Date Filter class, you can create an instance:
const DateFilter = new DateFilter();
The text
method can be used to convert a date to text format:
text(date: Date): string
Example:
const date = new Date();
const formattedDate = DateFilter.text(date);
console.log(formattedDate);
// Output: "June 5, 2023"
The formatDate
method can be used to format a date based on a specified format:
formatDate(date: Date, format: string): string
Supported format specifiers:
yyyy
- Yearmm
- Month (padded with leading zeros)dd
- Day (padded with leading zeros)HH
- Hour (padded with leading zeros)MM
- Minute (padded with leading zeros)SS
- Second (padded with leading zeros)ago
- Time elapsed since the date (e.g., "2 days ago")
Example:
const date = new Date();
const formattedDate = DateFilter.formatDate(date, "yyyy-mm-dd HH:MM:SS");
console.log(formattedDate);
// Output: "2023-06-05 12:30:45"
const date = new Date();
const formattedDate = DateFilter.formatDate(date, "ago");
console.log(formattedDate);
// Output: "xx day(s) or xx hour(s) ago"
The DateFilter
function has been renamed to DateFilter
. Please update your code to use the DateFilter
class instead. The previous function will still work but is deprecated.
The Date Filter class provides convenient methods for converting dates to text format and formatting dates based on specified formats. Use this class to enhance the date manipulation capabilities of your application.
The Store
class represents a store that holds the state and manages state updates. It is now implemented as a class or constructor.
To create a store, instantiate the Store
class:
const store = new Store(reducer, initialState);
reducer
(function): The reducer function for state updates.initialState
(optional): The initial state of the store. Default is an empty object ({}
).
The getState
method returns the current state of the store:
getState(): T
Example:
const currentState = store.getState();
console.log(currentState);
The dispatch
method dispatches an action to update the state:
dispatch(action: A): void
action
(A): The action object representing the state update.
Example:
const action = { type: "INCREMENT" };
store.dispatch(action);
The subscribe
method subscribes a listener function to be called on state changes:
subscribe(listener: Function): Function
listener
(function): The listener function to be called on state changes.
Example:
const listener = (state) => {
console.log("State changed:", state);
};
const unsubscribe = store.subscribe(listener);
// To unsubscribe:
unsubscribe();
The replaceReducer
method replaces the current reducer function with a new one:
replaceReducer(nextReducer: (state: T, action: A) => T): void
nextReducer
(function): The new reducer function.
Example:
const newReducer = (state, action) => {
// Custom reducer logic...
return newState;
};
store.replaceReducer(newReducer);
The getReducer
method returns the current reducer function:
getReducer(): (state: T, action: A) => T
Example:
const currentReducer = store.getReducer();
console.log(currentReducer);
To apply middlewares to the store's dispatch function, you can use the applyMiddleware
function:
applyMiddleware(...middlewares: Function[]): (store: any) => (...args: any[]) => any
middlewares
(functions): The middlewares to apply.
Example:
const enhancedStore = applyMiddleware(middleware1, middleware2)(Store);
const store = enhancedStore(reducer, initialState);
To create a subscriber object that can subscribe to store updates, you can use the createSubscriber
function:
const subscriber = createSubscriber(store);
store
(any): The store object to subscribe to.
The subscriber
object has a subscribe
method that can be used to subscribe a callback function to store updates:
subscriber.subscribe(callback: Function)
callback
(function): The callback function to be called on store updates.
To merge multiple reducers into a single reducer function, you can use the mergeReducers
function:
mergeReducers(reducers: { [key: string]: Function }): (state: any, action: any) => any
reducers
(object): An object containing the individual reducers.
Example:
const reducer = mergeReducers({
reducer1,
reducer2,
});
Please note that the Store
class is now a constructor or class and should be instantiated using the new
keyword. Additionally, the Store
class no longer throws an error if the reducer is not a function but instead throws a Error
. Make sure to handle the Error
appropriately in your code.
The TaskQueue
class is responsible for creating and managing a queue of tasks.
To create a task queue, instantiate the TaskQueue
class:
const taskQueue = new TaskQueue();
The addTask
method adds a new task to the queue:
addTask(task: Function): void
task
(function): The task to add to the queue.
Example:
const task = () => {
// Task logic...
};
taskQueue.addTask(task);
The runTasks
method executes all tasks in the queue:
runTasks(): void
Example:
taskQueue.runTasks();
The clearTasks
method clears the queue:
clearTasks(): void
Example:
taskQueue.clearTasks();
The getTaskCount
method returns the number of tasks in the queue:
getTaskCount(): number
Example:
const count = taskQueue.getTaskCount();
console.log(count);
The runTasks
method throws a Error
if a task is not a function. You should handle this error appropriately in your code.
The generateId
function generates a unique ID of a random length:
generateId(): number
Example:
const id = generateId();
console.log(id);
Please note that the TaskQueue
class is now a class and should be instantiated using the new
keyword.
The UrlParser
class is responsible for parsing and manipulating URLs.
To create an instance of UrlParser
, instantiate the class with a URL:
const urlParser = new UrlParser(url);
url
(string): The URL to parse and manipulate.
The getPath
method returns the path of the URL:
getPath(): string
Example:
const path = urlParser.getPath();
console.log(path); // Expected output: The URL path (e.g., "/products")
The getQueryString
method returns the query string of the URL:
getQueryString(): string
Example:
const queryString = urlParser.getQueryString();
console.log(queryString); // Expected output: The query string (e.g., "?category=electronics&sort=price")
The getQueryParameter
method returns the value of a specific query parameter:
getQueryParameter(param: string): string | null
param
(string): The query parameter to retrieve.
Example:
const value = urlParser.getQueryParameter('category');
console.log(value); // Expected output: The value of the query parameter or null if not found
The buildUrl
method builds a URL from its components:
buildUrl(
protocol: string,
hostname: string,
path: string,
queryParams: { [key: string]: string }
): string
protocol
(string): The URL protocol.hostname
(string): The URL hostname.path
(string): The URL path.queryParams
(object): The query parameters.
Example:
const protocol = 'https';
const hostname = 'example.com';
const path = '/api';
const queryParams = {
key1: 'value1',
key2: 'value2'
};
const builtUrl = urlParser.buildUrl(protocol, hostname, path, queryParams);
console.log(builtUrl); // Expected output: The built URL as a string (e.g., "https://example.com/api?key1=value1&key2=value2")
The Random
class is responsible for generating random numbers or strings.
To use the Random
class, create an instance of it:
const random = new Random();
The number
method generates a random number within a specified range:
number(min: number, max: number): number
min
(number): The minimum value.max
(number): The maximum value.
Example:
const randomNumber = random.number(1, 100);
console.log(randomNumber); // Expected output: The generated random number within the specified range
The string
method generates a random string with a specified length:
string(length: number): string
length
(number): The length of the random string.
Example:
const randomString = random.string(8);
console.log(randomString); // Expected output: The generated random string with the specified length
Please note that the Random
class is now a class and should be instantiated using the new
keyword.
The DataValidator
class is responsible for validating data.
To use the DataValidator
class, create an instance of it:
const validator = new DataValidator();
The inRange
method checks if a value is within a certain range:
inRange(value: number, min: number, max: number): boolean
value
(number): The value to check.min
(number): The minimum value of the range.max
(number): The maximum value of the range.
Example:
const value = 5;
const isInRange = validator.inRange(value, 1, 10);
console.log(isInRange); // Expected output: true if the value is within the range, false otherwise
The matchFormat
method checks if a string matches a certain format:
matchFormat(str: string, regex: RegExp): boolean
str
(string): The string to check.regex
(RegExp): The regular expression to match against.
Example:
const str = "abc123";
const regex = /^[a-z]+$/;
const matchesFormat = validator.matchFormat(str, regex);
console.log(matchesFormat); // Expected output: true if the string matches the format, false otherwise
The hasLength
method checks if a string has a certain length:
hasLength(str: string, length: number): boolean
str
(string): The string to check.length
(number): The required length of the string.
Example:
const str = "Hello";
const requiredLength = 5;
const hasRequiredLength = validator.hasLength(str, requiredLength);
console.log(hasRequiredLength); // Expected output: true if the string has the specified length, false otherwise
The trim
function trims leading and trailing whitespace from a string.
Usage:
const trimmedStr = trim(str);
str
(string): The string to trim.
Example:
const str = " Hello, World! ";
const trimmedStr = trim(str);
console.log(trimmedStr); // Expected output: "Hello, World!" (leading and trailing whitespace removed)
Please note that the DataValidator
class and trim
function should be instantiated or used directly respectively.
The array
module provides utility functions for working with arrays.
To use the array
module, you can import it into your JavaScript code using the following syntax:
import * as arrayUtil from 'utiliti-js/Operators/array';
// Or:
import { array } from 'utiliti-js';
The array
module provides the following functions:
Calculates the sum of all numbers in the given array.
array
: An array of numbers.- Returns: The sum of all numbers in the array.
Example:
import { sum } from 'utiliti-js/Operators/array';
const numbers = [1, 2, 3, 4, 5];
const sumResult = sum(numbers);
console.log(sumResult); // Output: 15
Filters the elements of the given array based on a provided predicate function.
array
: An array to be filtered.predicate
: A function that takes an element as an argument and returnstrue
orfalse
to indicate whether the element should be included in the filtered result.- Returns: A new array containing only the elements that satisfy the predicate.
Example:
import { filter } from 'utiliti-js/Operators/array';
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = filter(numbers, (num) => num % 2 === 0);
console.log(evenNumbers); // Output: [2, 4]
Applies a mapping function to each element of the given array and returns a new array with the results.
array
: An array to be mapped.mapper
: A function that takes an element as an argument and returns the transformed value.- Returns: A new array with the transformed elements.
Example:
import { map } from 'utiliti-js/Operators/array';
const numbers = [1, 2, 3, 4, 5];
const squaredNumbers = map(numbers, (num) => num * num);
console.log(squaredNumbers); // Output: [1, 4, 9, 16, 25]
Reduces the elements of the given array to a single value using a reducer function.
array
: An array to be reduced.reducer
: A function that takes an accumulator and the current element as arguments and returns the updated accumulator value.initialValue
: An optional initial value for the accumulator.- Returns: The final value of the accumulator.
Example:
import { reduce } from 'utiliti-js/Operators/array';
const numbers = [1, 2, 3, 4, 5];
const sum = reduce(numbers, (accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // Output: 15
Flattens a nested array structure into a single-level array.
array
: A nested array to be flattened.- Returns: A new array with all elements flattened.
Example:
import { flatten } from 'utiliti-js/Operators/array';
const nestedArray = [1, [2, [3, [4, 5]]]];
const flattenedArray = flatten(nestedArray);
console.log(flattenedArray); // Output: [1, 2, 3, 4, 5]
These are the available
functions provided by the array
module in the utiliti-js
library. You can import the module as shown above and utilize these functions in your JavaScript code.
The compare
module provides functions for comparing values and performing equality checks.
To use the compare
module, you can import it into your TypeScript code using either of the following syntaxes:
import { compare } from 'utiliti-js';
or
import * as compare from 'utiliti-js/Operators/compare';
The compare
module provides the following functions:
Checks if two values are equal.
a
: The first value.b
: The second value.- Returns:
true
if the values are equal,false
otherwise.
Example:
import { compare } from 'utiliti-js';
const result = compare.equals(5, 5);
console.log(result); // Output: true
Checks if the first value is greater than the second value.
a
: The first value.b
: The second value.- Returns:
true
if the first value is greater than the second value,false
otherwise.
Example:
import { compare } from 'utiliti-js';
const result = compare.greaterThan(10, 5);
console.log(result); // Output: true
Checks if the first value is less than the second value.
a
: The first value.b
: The second value.- Returns:
true
if the first value is less than the second value,false
otherwise.
Example:
import { compare } from 'utiliti-js';
const result = compare.lessThan(5, 10);
console.log(result); // Output: true
Performs a deep equality check between two objects or arrays.
obj1
: The first object or array.obj2
: The second object or array.- Returns:
true
if the objects or arrays are deeply equal,false
otherwise.
Example:
import { compare } from 'utiliti-js';
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };
const result = compare.deepEqual(obj1, obj2);
console.log(result); // Output: true
These are the available functions provided by the compare
module in the utiliti-js
library. You can import the module as shown above and utilize these functions in your TypeScript code.
The logic
module provides utility functions for logical operations.
To use the logic
module, you can import it into your TypeScript code using either of the following syntaxes:
import { logic } from 'utiliti-js/Operators/logic';
or
import * as logic from 'utiliti-js/Operators/logic';
The logic
module provides the following functions:
Returns true
if all the conditions are truthy values, false
otherwise.
...conditions
: The conditions to be evaluated.- Returns:
true
if all conditions are truthy values,false
otherwise.
Example:
import { logic } from 'utiliti-js/Operators/logic';
const result = logic.and(true, 5 > 3, 'Hello');
console.log(result); // Output: true
Returns true
if any of the conditions are truthy values, false
otherwise.
...conditions
: The conditions to be evaluated.- Returns:
true
if any condition is a truthy value,false
otherwise.
Example:
import { logic } from 'utiliti-js/Operators/logic';
const result = logic.or(false, 5 < 3, '');
console.log(result); // Output: true
Returns the boolean negation of the condition.
condition
: The condition to be negated.- Returns: The boolean negation of the condition.
Example:
import { logic } from 'utiliti-js/Operators/logic';
const result = logic.not(true);
console.log(result); // Output: false
Returns true
if an odd number of the conditions are truthy values, false
otherwise.
...conditions
: The conditions to be evaluated.- Returns:
true
if an odd number of conditions are truthy values,false
otherwise.
Example:
import { logic } from 'utiliti-js/Operators/logic';
const result = logic.xor(true, false, true);
console.log(result); // Output: true
Returns true
if any of the conditions are falsy values, false
otherwise.
...conditions
: The conditions to be evaluated.- Returns:
true
if any condition is a falsy value,false
otherwise.
Example:
import { logic } from 'utiliti-js/Operators/logic';
const result = logic.nand(true, false, true);
console.log(result); // Output: false
Returns true
if all of the conditions are falsy values, false
otherwise.
...conditions
: The conditions to be evaluated.- Returns:
true
if all conditions are falsy values,false
otherwise.
Example:
import { logic } from 'utiliti-js/Operators/logic';
const result = logic.nor(false, '', null);
console.log(result); // Output: true
Returns the value of a logical implication (a -> b).
a
: The antecedent of the implication.b
: The consequent of the implication.- Returns: The truth value of the implication.
Example:
import { logic } from 'utiliti-js/Operators/logic';
const result = logic.implies(true, false);
console.log(result); // Output: false
Returns the value of a biconditional implication (a <-> b).
a
: The first proposition of the biconditional.b
: The second proposition of the biconditional.- Returns: The truth value of the biconditional.
Example:
import { logic } from 'utiliti-js/Operators/logic';
const result = logic.iff(true, false);
console.log(result); // Output: false
These are the available functions provided by the logic
module in the utiliti-js
library. You can import the module as shown above and utilize these functions in your code.
The object
module provides utility functions for working with objects.
To use the object
module, you can import it into your TypeScript code using the following import style:
import { object } from 'utiliti-js';
The object
module provides the following functions:
Returns the value at the specified key in the object.
object
: The object to retrieve the value from.key
: The key to retrieve the value for.- Returns: The value at the specified key in the object.
Example:
import { object } from 'utiliti-js';
const obj = { name: 'John', age: 30 };
const value = object.getProperty(obj, 'name');
console.log(value); // Output: John
Sets the value at the specified key in the object.
object
: The object to set the value in.key
: The key to set the value for.value
: The value to set.
Example:
import { object } from 'utiliti-js';
const obj = { name: 'John', age: 30 };
object.setProperty(obj, 'age', 35);
console.log(obj); // Output: { name: 'John', age: 35 }
Returns a boolean indicating whether the object has a property with the specified key.
object
: The object to check for the property.key
: The key to check for.- Returns: Whether the object has the specified key.
Example:
import { object } from 'utiliti-js';
const obj = { name: 'John', age: 30 };
const hasName = object.hasProperty(obj, 'name');
console.log(hasName); // Output: true
const hasEmail = object.hasProperty(obj, 'email');
console.log(hasEmail); // Output: false
Returns an array of the object's own enumerable property names.
object
: The object to get the keys for.- Returns: An array of the object's own enumerable property names.
Example:
import { object } from 'utiliti-js';
const obj = { name: 'John', age: 30 };
const keys = object.getKeys(obj);
console.log(keys); // Output: ['name', 'age']
Returns an array of the object's own enumerable property values.
object
: The object to get the values for.- Returns: An array of the object's own enumerable property values.
Example:
import { object } from 'utiliti-js';
const obj = { name: 'John', age: 30 };
const values = object.getValues(obj);
console.log(values); // Output: ['John', 30]
Returns an array of the object's own enumerable property [key, value] pairs.
object
: The object to get the entries for.- Returns: An array of the object's own enumerable property [key, value] pairs.
Example:
import { object } from 'utiliti-js';
const obj = { name: 'John', age: 30 };
const entries = object.getEntries(obj);
console.log(entries); // Output
: [['name', 'John'], ['age', 30]]
Copies the own enumerable properties of source objects to the target object and returns the target object.
object
: The target object to extend.sources
: The source objects to copy properties from.- Returns: The extended target object.
Example:
import { object } from 'utiliti-js';
const target = { name: 'John' };
const source = { age: 30 };
const extended = object.extendObject(target, source);
console.log(extended); // Output: { name: 'John', age: 30 }
Creates a shallow copy of the object.
object
: The object to clone.- Returns: A shallow copy of the object.
Example:
import { object } from 'utiliti-js';
const obj = { name: 'John', age: 30 };
const clone = object.cloneShallow(obj);
console.log(clone); // Output: { name: 'John', age: 30 }
Creates a deep copy of the object.
object
: The object to clone.- Returns: A deep copy of the object.
Example:
import { object } from 'utiliti-js';
const obj = { name: 'John', age: 30 };
const clone = object.cloneDeep(obj);
console.log(clone); // Output: { name: 'John', age: 30 }
Calls the callback function for each own enumerable property of the object.
object
: The object to iterate over.callback
: The function to call for each property.
The callback
function receives the following parameters:
value
: The value of the current property.key
: The key of the current property.object
: The object being iterated over.
Example:
import { object } from 'utiliti-js';
const obj = { name: 'John', age: 30 };
object.forEachProperty(obj, (value, key) => {
console.log(`${key}: ${value}`);
});
// Output:
// name: John
// age: 30
Calls the callback function for each own enumerable property of the object and returns a new object with the returned values.
object
: The object to iterate over.callback
: The function to call for each property.
The callback
function receives the following parameters:
value
: The value of the current property.key
: The key of the current property.object
: The object being iterated over.
Returns: A new object with the returned values.
Example:
import { object } from 'utiliti-js';
const obj = { name: 'John', age: 30 };
const mapped = object.mapProperties(obj, (value, key) => {
return `${key}: ${value}`;
});
console.log(mapped); // Output: { name: 'name: John', age: 'age: 30' }
filterProperties(object: object, callback: (value: any, key: string, object: object) => boolean): object
Calls the callback function for each own enumerable property of the object and returns
a new object with the properties for which the callback returned a truthy value.
object
: The object to iterate over.callback
: The function to call for each property.
The callback
function receives the following parameters:
value
: The value of the current property.key
: The key of the current property.object
: The object being iterated over.
Returns: A new object with the properties for which the callback returned a truthy value.
Example:
import { object } from 'utiliti-js';
const obj = { name: 'John', age: 30 };
const filtered = object.filterProperties(obj, (value, key) => {
return typeof value === 'string';
});
console.log(filtered); // Output: { name: 'John' }
That covers the functions provided by the object
module. You can now use these functions in your TypeScript code to work with objects more efficiently.
The typeCheck
module provides utility functions to perform type checks on values.
Checks if a value is an integer.
num
: The value to check.- Returns:
true
if the value is an integer,false
otherwise.
Example:
import { typeCheck } from 'utiliti-js';
console.log(typeCheck.isInteger(5)); // Output: true
console.log(typeCheck.isInteger(5.5)); // Output: false
Checks if a value is a string.
val
: The value to check.- Returns:
true
if the value is a string,false
otherwise.
Example:
import { typeCheck } from 'utiliti-js';
console.log(typeCheck.isString('hello')); // Output: true
console.log(typeCheck.isString(123)); // Output: false
Checks if a value is an array.
val
: The value to check.- Returns:
true
if the value is an array,false
otherwise.
Example:
import { typeCheck } from 'utiliti-js';
console.log(typeCheck.isArray([1, 2, 3])); // Output: true
console.log(typeCheck.isArray('hello')); // Output: false
Checks if a value is an object.
val
: The value to check.- Returns:
true
if the value is an object,false
otherwise.
Example:
import { typeCheck } from 'utiliti-js';
console.log(typeCheck.isObject({ name: 'John' })); // Output: true
console.log(typeCheck.isObject('hello')); // Output: false
Checks if a value is a function.
val
: The value to check.- Returns:
true
if the value is a function,false
otherwise.
Example:
import { typeCheck } from 'utiliti-js';
console.log(typeCheck.isFunction(() => {})); // Output: true
console.log(typeCheck.isFunction(123)); // Output: false
Checks if a value is a boolean.
val
: The value to check.- Returns:
true
if the value is a boolean,false
otherwise.
Example:
import { typeCheck } from 'utiliti-js';
console.log(typeCheck.isBoolean(true)); // Output: true
console.log(typeCheck.isBoolean('false')); // Output: false
Checks if a value is NaN (not a number).
value
: The value to check.- Returns:
true
if the value is NaN,false
otherwise.
Example:
import { typeCheck } from 'utiliti-js';
console.log(typeCheck.isNaN(NaN)); // Output: true
console.log(typeCheck.isNaN(5)); // Output: false
Converts a string to an integer.
str
: The string to convert.radix
(optional): The radix used for parsing. Defaults to 10 if not specified.- Returns: The parsed integer value.
**Example:
**
import { typeCheck } from 'utiliti-js';
console.log(typeCheck.parseInt('10')); // Output: 10
console.log(typeCheck.parseInt('101', 2)); // Output: 5
Converts a string to a floating-point number.
str
: The string to convert.- Returns: The parsed floating-point number value.
Example:
import { typeCheck } from 'utiliti-js';
console.log(typeCheck.parseFloat('3.14')); // Output: 3.14
console.log(typeCheck.parseFloat('2.99')); // Output: 2.99
That covers the functions provided by the typeCheck
module. You can now use these functions in your TypeScript code to perform type checks and conversions.
The valueCheck
module provides utility functions to perform checks on numeric values.
Checks if a number is positive.
num
: The number to check.- Returns:
true
if the number is positive,false
otherwise.
Example:
import { valueCheck } from 'utiliti-js';
console.log(valueCheck.isPositive(5)); // Output: true
console.log(valueCheck.isPositive(-5)); // Output: false
Checks if a number is negative.
num
: The number to check.- Returns:
true
if the number is negative,false
otherwise.
Example:
import { valueCheck } from 'utiliti-js';
console.log(valueCheck.isNegative(-5)); // Output: true
console.log(valueCheck.isNegative(5)); // Output: false
Checks if a number is zero.
num
: The number to check.- Returns:
true
if the number is zero,false
otherwise.
Example:
import { valueCheck } from 'utiliti-js';
console.log(valueCheck.isZero(0)); // Output: true
console.log(valueCheck.isZero(5)); // Output: false
Checks if a number is even.
num
: The number to check.- Returns:
true
if the number is even,false
otherwise.
Example:
import { valueCheck } from 'utiliti-js';
console.log(valueCheck.isEven(4)); // Output: true
console.log(valueCheck.isEven(5)); // Output: false
Checks if a number is odd.
num
: The number to check.- Returns:
true
if the number is odd,false
otherwise.
Example:
import { valueCheck } from 'utiliti-js';
console.log(valueCheck.isOdd(3)); // Output: true
console.log(valueCheck.isOdd(4)); // Output: false
Checks if a number is prime.
num
: The number to check.- Returns:
true
if the number is prime,false
otherwise.
Example:
import { valueCheck } from 'utiliti-js';
console.log(valueCheck.isPrime(7)); // Output: true
console.log(valueCheck.isPrime(8)); // Output: false
Checks if a number is a whole number.
num
: The number to check.- Returns:
true
if the number is a whole number,false
otherwise.
Example:
import { valueCheck } from 'utiliti-js';
console.log(valueCheck.isWhole(4)); // Output: true
console.log(valueCheck.isWhole(4.5)); // Output: false
Checks if a number is a fractional number.
num
: The number to check.- Returns:
true
if the number is a fractional number,false
otherwise.
Example:
import { valueCheck } from 'utiliti-js';
console.log(valueCheck.isFractional(4.5)); // Output
: true
console.log(valueCheck.isFractional(4)); // Output: false
That covers the functions provided by the valueCheck
module. You can now use these functions in your TypeScript code to perform checks on numeric values.
This documentation provides an overview of the functions available in the valueCheck
module, their parameters, return types, and examples of usage. You can use this documentation to understand and utilize the functionalities provided by the module.
Additional Documentation
For more comprehensive documentation and details about the functions and features not covered here, please check the documentation folder in the project repository. You can find more in-depth explanations and usage examples for the functions provided in the code.
-
"Utiliti Error: url must be a string" when using the Http function: This error occurs when the url argument passed to the Http function is not a string. Make sure that you are passing a valid string as the url argument.
-
"Utiliti Error: header must be an object" when using the Http function: This error occurs when the header argument passed to the Http function is not an object. Make sure that you are passing a valid object as the header argument.
-
"Utiliti Error: data must be an array" when using the DataFilter function: This error occurs when the data argument passed to the DataFilter function is not an array. Make sure that you are passing a valid array as the data argument
-
"Utiliti Error: the body of the request must be an object" when using the Http function: This error occurs when the data argument passed to the Http function is not an object. Make sure that you are passing a valid object as the data argument.
-
"Utiliti Error: filter must be a function" when using the DataFilter function: This error occurs when the filter argument passed to the DataFilter function is not a function. Make sure that you are passing a valid function as the filter argument.
The library now supports Typescript fully.
The library now provides a wide range of utility functions for mathematical operations, string manipulation, date manipulation, and includes an interface for creating and managing stores.
- TypeScript support: The library has been converted to TypeScript to provide type safety and improved developer experience. TypeScript definitions have been added to enable better code analysis, autocompletion, and error checking.
- Updated build process: The build process now includes a TypeScript compiler step using
ts-loader
in webpack configuration. This ensures that TypeScript files are transpiled to JavaScript during the build process, allowing seamless integration with existing JavaScript projects. - Enhanced type definitions: Type definitions have been added or updated throughout the library to provide more accurate and descriptive types. This helps developers to write more reliable and maintainable code by catching type errors during development.
- Improved error handling: Error handling has been improved throughout the library. Custom error types have been used to provide more informative error messages, helping developers to identify and resolve issues more efficiently.
- Refactored and optimized code: The codebase has been refactored to follow best practices and improve performance. Redundant code has been removed, and optimizations have been made to enhance the overall efficiency of the library.
In this release, we have made significant enhancements to the Store
class, providing improved type safety and ensuring that the type of the action passed to the dispatch
method matches the type of action expected by the reducer function.
The Store
class is now defined as a generic class with two type parameters: T
for the state type and A
for the action type. This allows for better type inference and ensures type consistency throughout the store's methods.
The reducer
parameter in the constructor and the dispatch
method have been updated to use the new action type A
. This enforces that only actions of the specified type can be dispatched to update the state.
Example usage:
interface AppState {
count: number;
}
interface IncrementAction {
type: "INCREMENT";
payload: number;
}
const reducer = (state: AppState, action: IncrementAction): AppState => {
// ...
};
const initialState: AppState = { count: 0 };
const store = new Store<AppState, IncrementAction>(reducer, initialState);
By specifying the action type when creating a new Store
instance, TypeScript can now provide better type checking and inference. It ensures that the reducer function expects and handles the correct action type, reducing the chances of runtime errors caused by incompatible actions.
- Addressed issues reported in previous versions: Various bugs reported in previous versions have been fixed to ensure the library works as expected and provides a stable experience.
- Improved error handling
-
Added mathematical utility functions:
import { maths } from "utiliti-js"
clamp
: Restricts a value within a specified range.lerp
: Performs linear interpolation between two values.toDegrees
: Converts an angle from radians to degrees.toRadians
: Converts an angle from degrees to radians.roundTo
: Rounds a number to a specified number of decimal places.randomInt
: Generates a random integer within a specified range.sum
: Calculates the sum of an array of numbers.average
: Calculates the average of an array of numbers.min
: Finds the minimum value in an array of numbers.max
: Finds the maximum value in an array of numbers.factorial
: Calculates the factorial of a number.fibonacci
: Generates the Fibonacci sequence up to a specified number of terms.isPrime
: Checks if a number is prime.gcd
: Calculates the greatest common divisor of two numbers.lcm
: Calculates the least common multiple of two numbers.isEven
: Checks if a number is even.isOdd
: Checks if a number is odd.
-
Added string manipulation utility functions:
import { strings } from "utiliti-js"
capitalize
: Converts the first character of a string to uppercase.reverse
: Reverses the characters in a string.truncate
: Truncates a string to a specified length.startsWith
: Checks if a string starts with a specified prefix.endsWith
: Checks if a string ends with a specified suffix.contains
: Checks if a string contains a specified substring.replaceAll
: Replaces all occurrences of a substring in a string.countWords
: Counts the number of words in a string.countCharacters
: Counts the number of characters in a string.stripTags
: Removes HTML tags from a string.isValidEmail
: Checks if a string is a valid email address.isValidURL
: Checks if a string is a valid URL.
-
Added date manipulation utility functions to
DateFilter
:isLeapYear
: Checks if a given year is a leap year.getDaysInMonth
: Gets the number of days in a month for a given year.addDays
: Adds a specified number of days to a given date.subtractDays
: Subtracts a specified number of days from a given date.compareDates
: Compares two dates and returns the difference in days.isPastDate
: Checks if a given date is in the past.
-
Added
IStore
interface forStore
class.
Please note that this is a major release that introduces significant changes to the library. It is recommended to thoroughly test the updated library in your project environment before deploying it to production. If you encounter any issues or have any feedback, please don't hesitate to reach out to us.
We hope that these enhancements will improve the overall developer experience and help catch potential issues at compile-time. Happy coding!
- No issue reported since last release.
UtilitiJs is an open-source project, and contributions from the community are to contribute to the library, please visit the UtilitiJs GitHub repository for more information. highly encouraged. If you encounter any issues, have feature requests, or want
UtilitiJs is built and maintained by a passionate developer who aim to provide a reliable and feature-rich utility library to the JavaScript community.
UtilitiJs is released under the MIT License. You are free to use, modify, and distribute the library in your projects, both personal and commercial.
Start leveraging the power of UtilitiJs to simplify your JavaScript development and unlock new possibilities in your projects. Happy coding with UtilitiJs!
Copyright © 2023 Adeniji Oluwaferanmi. All Rights Reserved.