Skip to content

Instantly share code, notes, and snippets.

@jeremyswann
Last active July 31, 2019 06:04
Show Gist options
  • Select an option

  • Save jeremyswann/9a3447f21d44cc6b1c5579eb61d20a9e to your computer and use it in GitHub Desktop.

Select an option

Save jeremyswann/9a3447f21d44cc6b1c5579eb61d20a9e to your computer and use it in GitHub Desktop.
Utility Functions

Utility Functions

Collection of reusable typed functions

//@ts-check
/**
* *Filter Array
* Filter an array of objects by a specific key
* @param {Array} array
* @param {String} filterKey
* @returns {Promise<String>} value of object
*/
async function filterArray(array, filterKey) {
/** Array.prototype.filter() */
const result = await array.filter(object => object.key === filterKey)
/** Destruct resulting array */
const [{ value }] = await result
return value
}
//@ts-check
/**
* *Format Date
* Format a datetime string to en-AU locale date
* @param {String} datetime
* @returns {Promise<String>} presentation date DD/MM/YYYY
*/
async function formatDate(datetime) {
const date = await new Date(datetime)
const day = await date.getDate()
const month = (await date.getMonth()) + 1
const year = await date.getFullYear()
return day + '/' + month + '/' + year
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment