Skip to content

Commit

Permalink
feat(utils): add trimArray function
Browse files Browse the repository at this point in the history
Trims an array to a given maximum length and appends a message indicating how many more elements are not shown.
  • Loading branch information
dankerow committed Aug 22, 2024
1 parent 777cb2f commit 451268a
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/utils/trimArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Trims an array to a given maximum length and appends a message indicating how many more elements are not shown.
*
* @param {Array} array - The array to be trimmed.
* @param {number} [maxLength=10] - The maximum length of the trimmed array.
* @return {Array} - The trimmed array with a message indicating how many more elements are not shown.
*/
export default (array: any[], maxLength: number = 10): any[] => {
if (array.length > maxLength) {
const length = array.length - maxLength
array = array.slice(0, maxLength)
array.push(`and ${length} more...`)
}

return array
}

0 comments on commit 451268a

Please sign in to comment.