Utilities¶
This library was generated with Angular CLI version 18.0.1
Packages Details¶
| Utility Name | Version | Description |
|---|---|---|
| Csv Parser | 1.0.0 | CSV Parser |
| Enum Helpers | 1.0.0 | Enum Helpers |
| Object Helpers | 1.0.0 | Object Helpers |
| RFC-6902 JSON Patch | 1.0.0 | RFC-6902 JSON Patch |
| RFC-7396 JSON Merge Patch | 1.0.0 | RFC-7396 JSON Merge Patch |
| Link Header Parser | 1.0.0 | Link Header Parser |
How to install¶
How to use¶
CSVParser Usage¶
To use the CsvParser class, instantiate it and call the parse method with a CSV file and an optional configuration.
Example¶
import { CsvParser } from './csvParser';
import { CSVParserConfig } from './csvParserConfig';
import { CSVParserError } from './csvParserError';
// Instantiate the CsvParser
const csvParser = new CsvParser();
// Create a CSV file (for example purposes, usually this comes from an input element)
const csvFile = new File(["name,age\nJohn,30\nDoe,25"], "test.csv", { type: "text/csv" });
// Optional configuration
const config: CSVParserConfig = {
header: true,
delimiter: ","
};
// Parse the CSV file
csvParser.parse(csvFile, config).subscribe({
next: (result) => {
if (result instanceof CSVParserError) {
console.error('CSV Parsing Error:', result.message);
} else {
console.log('Parsed CSV Data:', result);
}
},
error: (error) => {
console.error('Error:', error);
},
complete: () => {
console.log('CSV parsing completed.');
}
});
LinkHeaderParser Usage¶
To use the LinkHeaderParser, instantiate the class and call the parse method with a link header string.
Example¶
const parser = new LinkHeaderParser();
const pagination = parser.parse('<https://api.example.com/page1>; rel="first", <https://api.example.com/page2>; rel="next"');
console.log(pagination);
JSON Patch (RFC 6902)¶
This module provides functions for applying and generating JSON patches following the RFC 6902 specification.
Functions¶
applyPatch¶
Applies a JSON Patch (application/json-patch+json type) to an object. The patch must be an array of operations.
import { applyPatch } from './path/to/module';
import { Operation } from './path/to/module';
const object = {a: 'b'};
const patch: Operation[] = [
{ op: 'replace', path: '/a', value: 'c' }
];
const result = applyPatch(object, patch);
console.log(result); // [null]
JSON Merge Patch (RFC 7396)¶
This module provides a function for generating patch objects in the JSON merge patch format as defined by RFC 7396. The function jsonPatch can be used to create patches that update JSON documents.
How to Use¶
import { jsonPatch } from './path/to/module'; // Adjust the import path as necessary
const target = {a: 'b'};
const patchItem = {a: 'c'};
const patch = jsonPatch(target, patchItem);
console.log(patch); // {a: 'c'}
const target1 = {a: 'b', b: 'c'};
const patchItem1 = {b: 'c'};
const patch1 = jsonPatch(target1, patchItem1);
console.log(patch1); // {a: null}
Utility Functions¶
This module provides various utility functions for JavaScript, including type checking, date manipulation, and more.
Functions¶
- typeOf
- Returns the type of the given variable.
- Params:
obj- The variable to check.
- Returns:
string(variable type) -
Example:
-
sleep
- Delays the execution of an asynchronous function.
- Params:
ms- The delay time in milliseconds.
-
Example:
-
strictEqual
- Checks if two values are strictly equal.
- Params:
a- The first value.b- The second value.
-
Returns:
boolean -
shallowEqual
- Checks if two objects are shallowly equal.
- Params:
a- The first object.b- The second object.
-
Returns:
boolean -
is
- Checks if a value is of a particular type.
- Params:
type- The type to check against.val- The value to check.
- Returns:
boolean -
Example:
-
isString
- Checks if a value is a string.
- Params:
val- The value to check.
-
Returns:
boolean -
isNumber
- Checks if a value is a number.
- Params:
val- The value to check.
-
Returns:
boolean -
isBoolean
- Checks if a value is a boolean.
- Params:
val- The value to check.
-
Returns:
boolean -
isArray
- Checks if a value is an array.
- Params:
val- The value to check.
-
Returns:
boolean -
isObject
- Checks if a value is an object.
- Params:
val- The value to check.- Returns:
boolean
-
isFunction
- Checks if a value is a function.
- Params:
val- The value to check.- Returns:
boolean
-
isNull
- Checks if a value is null.
- Params:
val- The value to check.- Returns:
boolean
-
isUndefined
- Checks if a value is undefined.
- Params:
val- The value to check.- Returns:
boolean
-
isRegExp
- Checks if a value is a regular expression.
- Params:
val- The value to check.- Returns:
boolean
-
isDate
- Checks if a value is a date.
- Params:
val- The value to check.- Returns:
boolean
-
isPromise
- Checks if a value is a promise.
- Params:
val- The value to check.- Returns:
boolean
-
isSet
- Checks if a value is a set.
- Params:
val- The value to check.- Returns:
boolean
-
isMap
- Checks if a value is a map.
- Params:
val- The value to check.- Returns:
boolean
-
isWeakSet
- Checks if a value is a weak set.
- Params:
val- The value to check.- Returns:
boolean
-
isWeakMap
- Checks if a value is a weak map.
- Params:
val- The value to check.- Returns:
boolean
-
isSymbol
- Checks if a value is a symbol.
- Params:
val- The value to check.- Returns:
boolean
-
isBigInt
- Checks if a value is a BigInt.
- Params:
val- The value to check.- Returns:
boolean
-
isNil
- Checks whether a value is null or undefined.
- Params:
val- The value to check.- Returns:
boolean - Example:
-
isNumeric
- Checks if a value is a number.
- Params:
input- The value to check.- Returns:
boolean
-
isPlainObject
- Checks if a value is an object created by the Object constructor.
- Params:
val- The value to check.- Returns:
boolean - Example:
-
nodeListToArray
- Converts a NodeList to an array.
- Params:
nodeList- The NodeList to convert.- Returns:
Node[] - Example:
-
randomColor
- Returns a random color in hex format.
- Returns:
string- A random hex color
-
randomHexColorCode
- Generates a random hexadecimal color code.
- Returns:
string- A random hex color code - Example:
-
randomIntArrayInRange
- Generates an array with n random integers in a specified range.
- Params:
min- The minimum value.max- The maximum value.n- The number of random integers (default is 1).- Returns:
number[] - Example:
-
toCurrency
- Formats a number as currency.
- Params:
n- The number to format.curr- The currency code.LanguageFormat- The locale format (optional).- Returns:
string - Example:
-
union
- Finds the union of two arrays.
- Params:
a- The first array.b- The second array.- Returns:
any[] - Example:
-
maxN
- Returns the n largest elements from a list.
- Params:
arr- The array.n- The number of elements (default is 1).- Returns:
any[] - Example:
-
minN
- Returns the n smallest elements from a list.
- Params:
arr- The array.n- The number of elements (default is 1).- Returns:
any[] - Example:
-
formatter
- Formats a date and time.
- Returns:
string - Example:
-
getDaysDiffBetweenDates
- Finds the difference in days between two dates.
- Params:
dateInitial- The initial date.dateFinal- The final date.- Returns:
number - Example:
-
getColonTimeFromDate
- Gets the time from a Date object as a string.
- Params:
date- The Date object.- Returns:
string - Example:
-
isAfterDate
- Checks if a date is after another date.
- Params:
dateA- The first date.dateB- The second date.- Returns:
boolean - Example:
-
isBeforeDate
- Checks if a date is before another date.
- Params:
dateA- The first date.dateB- The second date.- Returns:
boolean - Example:
-
isSameDate
- Checks if two dates are equal.
- Params:
dateA- The first date.dateB- The second date.- Returns:
boolean - Example:
-
maxDate
- Gets the latest date.
- Params:
dates- The dates to compare.- Returns:
Date - Example:
-
minDate
- Gets the earliest date.
- Params:
dates- The dates to compare.- Returns:
Date - Example:
-
tomorrow
- Gets a string representation of tomorrow’s date.
- Returns:
string - Example:
Enum Utility Functions¶
This module provides utility functions for working with TypeScript enums, including methods for converting enums to various formats and extracting keys/values.
Functions¶
- getEnumKeyValueAsMap
- Converts an enum to a
Mapobject with keys and values from the enum. -
Params:
value- The enum to convert.
-
prepareEnumKeyValueForScreen
- Converts an enum to an array of objects with
valueandviewValueproperties, suitable for display in a UI. -
Params:
value- The enum to convert.
-
prepareEnumKeyValueForScreenByEnum
- Converts an array of enum keys and an enum class to an array of objects with
valueandviewValueproperties. -
Params:
enumKeys- Array of enum keys.enumClass- The enum class.
-
getEnumKeyByValue
- Retrieves the key from an enum based on the given value.
- Example:
-
Params:
source- The enum source.value- The value to find the key for.
-
parseEnum
- Converts an enum to an object.
-
Params:
_enum- The enum to convert.
-
getEnumNamesAndValues
- Returns the names and values of an enum as an array of objects.
- Example:
-
Params:
_enum- The enum to process.
-
getEnumNames
- Returns the keys (names) of an enum as an array.
- Example:
-
Params:
_enum- The enum to process.
-
getEnumValues
- Returns the values of an enum as an array.
- Example:
-
Params:
_enum- The enum to process.
-
isEnumValue
- Checks if the given value exists in the enum object.
- Params:
enumObj- The enum object to check.value- The value to check for.