Skip to content

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

npm install @dnext-angular/utilities

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

  1. typeOf
  2. Returns the type of the given variable.
  3. Params:
    • obj - The variable to check.
  4. Returns: string (variable type)
  5. Example:

    typeOf([]); // 'array'
    typeOf({}); // 'object'
    typeOf(''); // 'string'
    typeOf(new Date()); // 'date'
    typeOf(1); // 'number'
    typeOf(function(){}); // 'function'
    typeOf(/test/i); // 'regexp'
    typeOf(true); // 'boolean'
    typeOf(null); // 'null'
    typeOf(); // 'undefined'
    typeOf(Symbol()); // 'symbol'
    

  6. sleep

  7. Delays the execution of an asynchronous function.
  8. Params:
    • ms - The delay time in milliseconds.
  9. Example:

    async function sleepyWork() {
        console.log("I'm going to sleep for 1 second.");
        await sleep(1000);
        console.log('I woke up after 1 second.');
    }
    

  10. strictEqual

  11. Checks if two values are strictly equal.
  12. Params:
    • a - The first value.
    • b - The second value.
  13. Returns: boolean

  14. shallowEqual

  15. Checks if two objects are shallowly equal.
  16. Params:
    • a - The first object.
    • b - The second object.
  17. Returns: boolean

  18. is

  19. Checks if a value is of a particular type.
  20. Params:
    • type - The type to check against.
    • val - The value to check.
  21. Returns: boolean
  22. Example:

    is(Array, [1]); // true
    is(Date, new Date()); // true
    is(Map, new Map()); // true
    

  23. isString

  24. Checks if a value is a string.
  25. Params:
    • val - The value to check.
  26. Returns: boolean

  27. isNumber

  28. Checks if a value is a number.
  29. Params:
    • val - The value to check.
  30. Returns: boolean

  31. isBoolean

  32. Checks if a value is a boolean.
  33. Params:
    • val - The value to check.
  34. Returns: boolean

  35. isArray

  36. Checks if a value is an array.
  37. Params:
    • val - The value to check.
  38. Returns: boolean

  39. isObject

    • Checks if a value is an object.
    • Params:
    • val - The value to check.
    • Returns: boolean
  40. isFunction

    • Checks if a value is a function.
    • Params:
    • val - The value to check.
    • Returns: boolean
  41. isNull

    • Checks if a value is null.
    • Params:
    • val - The value to check.
    • Returns: boolean
  42. isUndefined

    • Checks if a value is undefined.
    • Params:
    • val - The value to check.
    • Returns: boolean
  43. isRegExp

    • Checks if a value is a regular expression.
    • Params:
    • val - The value to check.
    • Returns: boolean
  44. isDate

    • Checks if a value is a date.
    • Params:
    • val - The value to check.
    • Returns: boolean
  45. isPromise

    • Checks if a value is a promise.
    • Params:
    • val - The value to check.
    • Returns: boolean
  46. isSet

    • Checks if a value is a set.
    • Params:
    • val - The value to check.
    • Returns: boolean
  47. isMap

    • Checks if a value is a map.
    • Params:
    • val - The value to check.
    • Returns: boolean
  48. isWeakSet

    • Checks if a value is a weak set.
    • Params:
    • val - The value to check.
    • Returns: boolean
  49. isWeakMap

    • Checks if a value is a weak map.
    • Params:
    • val - The value to check.
    • Returns: boolean
  50. isSymbol

    • Checks if a value is a symbol.
    • Params:
    • val - The value to check.
    • Returns: boolean
  51. isBigInt

    • Checks if a value is a BigInt.
    • Params:
    • val - The value to check.
    • Returns: boolean
  52. isNil

    • Checks whether a value is null or undefined.
    • Params:
    • val - The value to check.
    • Returns: boolean
    • Example:
      isNil(null); // true
      isNil(undefined); // true
      
  53. isNumeric

    • Checks if a value is a number.
    • Params:
    • input - The value to check.
    • Returns: boolean
  54. isPlainObject

    • Checks if a value is an object created by the Object constructor.
    • Params:
    • val - The value to check.
    • Returns: boolean
    • Example:
      isPlainObject({ a: 1 }); // true
      isPlainObject(new Map()); // false
      
  55. nodeListToArray

    • Converts a NodeList to an array.
    • Params:
    • nodeList - The NodeList to convert.
    • Returns: Node[]
    • Example:
      nodeListToArray(document.childNodes); // [ <!DOCTYPE html>, html ]
      
  56. randomColor

    • Returns a random color in hex format.
    • Returns: string - A random hex color
  57. randomHexColorCode

    • Generates a random hexadecimal color code.
    • Returns: string - A random hex color code
    • Example:
      randomHexColorCode(); // "#e34155"
      
  58. 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:
      randomIntArrayInRange(12, 35, 10); // [ 34, 14, 27, 17, 30, 27, 20, 26, 21, 14 ]
      
  59. toCurrency

    • Formats a number as currency.
    • Params:
    • n - The number to format.
    • curr - The currency code.
    • LanguageFormat - The locale format (optional).
    • Returns: string
    • Example:
      toCurrency(123456.789, 'EUR'); // €123,456.79
      toCurrency(123456.789, 'USD', 'en-us'); // $123,456.79
      
  60. union

    • Finds the union of two arrays.
    • Params:
    • a - The first array.
    • b - The second array.
    • Returns: any[]
    • Example:
      union([1, 2, 3], [4, 3, 2]); // [1, 2, 3, 4]
      
  61. maxN

    • Returns the n largest elements from a list.
    • Params:
    • arr - The array.
    • n - The number of elements (default is 1).
    • Returns: any[]
    • Example:
      maxN([1, 2, 3]); // [3]
      maxN([1, 2, 3], 2); // [3, 2]
      
  62. minN

    • Returns the n smallest elements from a list.
    • Params:
    • arr - The array.
    • n - The number of elements (default is 1).
    • Returns: any[]
    • Example:
      minN([1, 2, 3]); // [1]
      minN([1, 2, 3], 2); // [1, 2]
      
  63. formatter

    • Formats a date and time.
    • Returns: string
    • Example:
      const date = new Date();
      formatter.format(date); // 'Thursday, May 18, 2023 at 7:59 PM'
      
  64. getDaysDiffBetweenDates

    • Finds the difference in days between two dates.
    • Params:
    • dateInitial - The initial date.
    • dateFinal - The final date.
    • Returns: number
    • Example:
      getDaysDiffBetweenDates(new Date('2019-01-13'), new Date('2019-01-15')); // 2
      
  65. getColonTimeFromDate

    • Gets the time from a Date object as a string.
    • Params:
    • date - The Date object.
    • Returns: string
    • Example:
      getColonTimeFromDate(new Date()); // "08:38:00"
      
  66. isAfterDate

    • Checks if a date is after another date.
    • Params:
    • dateA - The first date.
    • dateB - The second date.
    • Returns: boolean
    • Example:
      isAfterDate(new Date(2010, 10, 21), new Date(2010, 10, 20)); // true
      
  67. isBeforeDate

    • Checks if a date is before another date.
    • Params:
    • dateA - The first date.
    • dateB - The second date.
    • Returns: boolean
    • Example:
      isBeforeDate(new Date(2010, 10, 20), new Date(2010, 10, 21)); // true
      
  68. isSameDate

    • Checks if two dates are equal.
    • Params:
    • dateA - The first date.
    • dateB - The second date.
    • Returns: boolean
    • Example:
      isSameDate(new Date(2010, 10, 20), new Date(2010, 10, 20)); // true
      
  69. maxDate

    • Gets the latest date.
    • Params:
    • dates - The dates to compare.
    • Returns: Date
    • Example:
      const array = [
          new Date(2017, 4, 13),
          new Date(2018, 2, 12),
          new Date(2016, 0, 10),
          new Date(2016, 0, 9)
      ];
      maxDate(array); // 2018-03-11T22:00:00.000Z
      
  70. minDate

    • Gets the earliest date.
    • Params:
    • dates - The dates to compare.
    • Returns: Date
    • Example:
      const array = [
          new Date(2017, 4, 13),
          new Date(2018, 2, 12),
          new Date(2016, 0, 10),
          new Date(2016, 0, 9)
      ];
      minDate(array); // 2016-01-08T22:00:00.000Z
      
  71. tomorrow

    • Gets a string representation of tomorrow’s date.
    • Returns: string
    • Example:
      tomorrow(); // 2019-09-08 (if current date is 2018-09-08)
      

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

  1. getEnumKeyValueAsMap
  2. Converts an enum to a Map object with keys and values from the enum.
  3. Params:

    • value - The enum to convert.
  4. prepareEnumKeyValueForScreen

  5. Converts an enum to an array of objects with value and viewValue properties, suitable for display in a UI.
  6. Params:

    • value - The enum to convert.
  7. prepareEnumKeyValueForScreenByEnum

  8. Converts an array of enum keys and an enum class to an array of objects with value and viewValue properties.
  9. Params:

    • enumKeys - Array of enum keys.
    • enumClass - The enum class.
  10. getEnumKeyByValue

  11. Retrieves the key from an enum based on the given value.
  12. Example:
    enum StringEnum {
      ONE = "one",
      TWO = "two",
      THREE = "three"
    }
    getEnumKeyByValue(StringEnum, 'one') => 'ONE'
    
  13. Params:

    • source - The enum source.
    • value - The value to find the key for.
  14. parseEnum

  15. Converts an enum to an object.
  16. Params:

    • _enum - The enum to convert.
  17. getEnumNamesAndValues

  18. Returns the names and values of an enum as an array of objects.
  19. Example:
    enum StringEnum {
      ONE,
      TWO
    }
    getEnumNamesAndValues(StringEnum) => [{name: 'ONE', value: 0}, {name: 'TWO', value: 1}]
    
  20. Params:

    • _enum - The enum to process.
  21. getEnumNames

  22. Returns the keys (names) of an enum as an array.
  23. Example:
    enum StringEnum {
      ONE = "one",
      TWO = "two"
    }
    getEnumNames(StringEnum) => ['ONE', 'TWO']
    
  24. Params:

    • _enum - The enum to process.
  25. getEnumValues

  26. Returns the values of an enum as an array.
  27. Example:
    enum StringEnum {
      ONE = "one",
      TWO = "two"
    }
    getEnumValues(StringEnum) => ['one', 'two']
    
  28. Params:

    • _enum - The enum to process.
  29. isEnumValue

  30. Checks if the given value exists in the enum object.
  31. Params:
    • enumObj - The enum object to check.
    • value - The value to check for.