postkit-filter-sort
v1.0.0
Published
A utility library for filtering and sorting posts
Readme
Package name
postkit-filter-sort
Purpose
A utility library for filtering and sorting posts by status, tag, date, and title.
Exports
filterByStatus
- Input:
posts: Post[],status: PostStatus - Output:
Post[] - Description: Returns a new list of Posts that's filtered by Status
filterByTag
- Input:
posts: Post[],tag:string - Output:
Post[] - Description: Returns a new list of Posts that's filtered by Tag
sortByDate
- Input:
posts: Post[],direction?: 'asc' | 'desc' - Output:
Post[] - Description: Returns a new list of Posts sorted by date. Defaults to
'desc'(newest first) if no direction is provided.
sortByTitle
- Input:
posts: Post[],direction?: 'asc' | 'desc' - Output:
Post[] - Description: Returns a new list of Posts sorted by Title. Defaults to
'asc'(A → Z) if no direction is provided.
Example Usage
import { filterByStatus, filterByTag, sortByDate, sortByTitle } from 'postkit-filter-sort'
// a simple list of posts
const posts = [post1, post2, post3]
// filter by status
const drafts = filterByStatus(posts, 'draft')
// filter by tag
const tagged = filterByTag(posts, 'typescript')
// sort by title (A → Z by default)
const byTitle = sortByTitle(posts)
// sort by title Z → A
const byTitleDesc = sortByTitle(posts, 'desc')
// sort by date (newest first by default)
const byDate = sortByDate(posts)
// sort by date oldest first
const byDateAsc = sortByDate(posts, 'asc')Edge Cases
Empty array: If
postsis empty, all functions return[]Case sensitivity (
filterByTag): Tags are normalized to lowercase before matching.'Popular'will match posts tagged'popular'Tie breaking (
sortByDate,sortByTitle): If two posts share the same date or title, they are sorted byidas a tiebreakerCombining filter and sort: Functions can be chained — the caller controls the order:
const result = sortByDate(filterByStatus(posts, "draft"));Design Notes
- Decided to return a new array instead of mutating the current array for filter and sort for performance issue. If the user wants to access the original array, then there's multiple API calls that needs to happen instead of just referring to the original array
- Chose
asc | descas direction instead of boolean. It would be difficult for users to ascertain whethertrueis for ascending or descending order. Avoiding "magic booleans" - Direction is optional with a default because requiring it every time
would be inconvenient. Most users of a publishing app want to see the
newest posts first, so
sortByDatedefaults to'desc'andsortByTitledefaults to'asc'(A → Z) for natural alphabetical reading.
