kentcdodds / match-sorter

Simple, expected, and deterministic best-match sorting of an array in JavaScript
https://npm.im/match-sorter
MIT License
3.73k stars 129 forks source link

Consider the usage of `hasOwn` #150

Closed sjaq closed 8 months ago

sjaq commented 8 months ago

Relevant code or config

  if (typeof key === 'function') {
    value = key(item)
  } else if (item == null) {
    value = null
  } else if (Object.hasOwn(item, key)) {
    value = (item as IndexableByString)[key]
  } else if (key.includes('.')) {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-call
    return getNestedValues<ItemType>(key, item)
  } else {
    value = null
  }

What you did: Used matchSorter in my project and tested it in Safari 13.1 What happened: TypeError: Object.hasOwn is not a function

Screenshot 2024-02-05 at 16 45 32

Problem description: For our music industry specific project we have to support Safari 13.1 for a little longer as a lot of music producers are stuck at older macOS versions. match-sorter relies on the Object.hasOwn function that is not available in an older browser like Safari 13.

Suggested solution: Switch back to using the previous hasOwnProperty setup.

I understand using this newer API simplifies the code, but it does introduce a need for us to add polyfills for older browsers or consider our usage of match-sorter. In this case specifically the change is so small I wanted to raise an issue as I totally get you have to draw the line somewhere regarding browser support. Just wanted to make sure the change was intentional as I didn't see any reference of a breaking change in the commit history or release log and it seems to have been introduced in an unrelated update #148

kentcdodds commented 8 months ago

I'm happy to accept a PR to switch this to hasOwnProperty :)