@dongskyler/helpers.js

A collection of helper functions in JavaScript and TypeScript

Usage no npm install needed!

<script type="module">
  import dongskylerHelpersJs from 'https://cdn.skypack.dev/@dongskyler/helpers.js';
</script>

README

@dongskyler/helpers.js

Homepage: https://github.com/dongskyler/helpers.js

npm version Node.js CI codecov CodeFactor DeepScan grade License: MIT

GitHub issues GitHub issues-closed GitHub pull-requests GitHub pull-requests closed


Table of contents


Description

This is a collections of JavaScript and TypeScript helper functions.


Functions

inputOutput

  • listFilesInDirSync()

    listFilesInDirSync(dir[, ext, removeExt])

    • Parameters
      • dir: string (Positional, required)
        • Path to directory.
      • ext: string | null (Positional, optional)
        • Filter by file extension.
      • removeExt: boolean (Positional, optional)
        • Remove the file extension from the filename string.
    • Return value: string[] | null
      • An array of filenames in a directory.
  • loadFile()

  • readPipeWrite()

  • writeToFile()

others

  • runFuncsWithArgs()

    runFuncsWithArgs(funcs[, args, spread])

    • Run an array of functions with the same set of argument(s)
    • Parameters
      • funcs: ((...args: any | any[] | null) => any)[] (Positional, required)
        • An array of functions.
      • args: any | any[] | null = null (Positional, optional)
        • An array of arguments. Each element of the array will be presented to each function.
      • spread: boolean (Positional, optional)
        • If args is an array of arrays, you can choose to expand each sub-array with the spread operator.
        • Default is false.
    • Return value: any[]
      • An array of return values from functions
  • valOfKeysAndIndices()

    valOfKeysAndIndices(obj[, key0[, key1[, ...keyN]]])

    • Returns the value in an array or an object (or you call it JSON, a hash map or a dictionary) given the series of indices and/or keys pointing to the value.
    • Parameters
      • obj: any (Positional, required)
        • An array, an object (or you call it JSON, a hash map or a dictionary) or a combination.
        • The first index or key pointing to the value is required.
      • keyN: string | number | null (Positional, optional)
        • An arbitrary number of indices and/or keys pointing to the value.
        • For example, if x = {name: 'Tom'}, to fetch value 'Tom', use valOfKeysAndIndicies(x, 'name'). If y = { key0: [{ key1: [3.142, 6.626,] },] }, to fetch 6.626, use valOfKeysAndIndicies(y, 'key0', 0, 'key', 1).
    • Return value: any
      • The value from the given indices/keys.

sort

  • comparatorLexic()

    comparatorLexic([{key = [], ignoreCase = false, descending = false})

    • Compare function for comparing lexicographic order.
    • Parameters
      • ignoreCase: boolean (Named, optional)
        • Ignore the case of the given string.
        • Default is false.
      • descending: boolean (Named, optional)
        • Ascending or descending order.
        • Default is false, that is, ascending order.
      • keyN: string | number | null (Positional, optional)
        • An arbitrary number of indices and/or keys pointing to the value for comparison.
        • For example, if x = {name: 'Tom'}, use comparatorLexic('name') to fetch value 'Tom' for comparison. If x = { key0: [{ key1: [Earth, Mars,] },] }, to fetch Mars for comparison, use comparatorLexic('key0', 0, 'key', 1).
        • See keyN in valOfKeysAndIndices for more details.
        • Default is null.
    • Return value: function
      • Function (x: any, y: any) => number
        • Return value: number
          • -1 if a should placed before b.
          • 0 if a and b are equal.
          • 1 if a should be placed after b.
  • comparatorNumeric()

    comparatorNumeric([{key = [], ignoreSign = false, descending = false})

    • Compare function for comparing numerical order.
    • Parameters
      • keyN: string | number | null (Positional, optional)
        • An arbitrary number of indices and/or keys pointing to the value for comparison.
        • If x = { key0: [{ key1: [3.142, 6.626,] },] }, to fetch 6.626 for comparison, use comparatorLexic('key0', 0, 'key', 1).
        • See keyN in valOfKeysAndIndices for more details.
        • Default is null.
      • ignoreSign: boolean (Named, optional)
        • Ignore the sign of the number, i.e. use the absolute value for comparison.
        • Default is false.
      • descending: boolean (Named, optional)
        • Ascending or descending order.
        • Default is false, that is, ascending order.
    • Return value: function
      • Function (a: any, b: any) => number
        • Return value: number
          • -1 if a should placed before b.
          • 0 if a and b are equal.
          • 1 if a should be placed after b.
  • bubbleSort()

    bubbleSort(arr[, compareFunction, k])

    • Bubble sort algorithm. Stable.
    • Parameters
      • arr: any[] (Positional, required)
        • An array to be sorted.
      • compareFunction: (a: any, b: any) => number (Positional, optional)
      • k: number (Positional, optional)
        • Partially sort the array to get the first k sorted elements
    • Return value: any[]
      • A sorted array.
  • mergeSort()

    mergeSort(arr[, compareFunction])

    • Merge sort algorithm. Stable.
    • Parameters
      • arr: any[] (Positional, required)
        • An array to be sorted.
      • compareFunction: (a: any, b: any) => number (Positional, optional)
    • Return value: any[]
      • A sorted array.
  • partialSort()

    partialSort(arr[, compareFunction, k])

    • Partially sort the array to get the first k sorted elements, given the compare function.