prantlf / storybook-multilevel-sort

Applies specific sort order to more than two levels of chapters and stories in Storybook.
https://storybook.js.org/addons/storybook-multilevel-sort/
Other
8 stars 4 forks source link

Multilevel Sort fails on Safari 14 - Object.hasOwn is not a function #6

Closed lorenwest closed 1 year ago

lorenwest commented 1 year ago

When running Storybook with storybook-multilevel-sort on a Safari v14 browser, the error at the bottom of this post is shown.

The issue is simple - Safari v14 doesn't have an object.hasOwn() method.

It's pretty easy to workaround by adding the following polyfill before loading storybook-multilevel-sort

('hasOwn' in Object) || (Object.hasOwn = Object.call.bind(Object.hasOwnProperty));

The solution is to use Object.hasOwnProperty() instead of Object.hasOwn() in the following code https://github.com/prantlf/storybook-multilevel-sort/blob/d94e8036246ff4e90a290c7cd4aea03c52273471/lib/index.js#L3

I'd be happy to have the code authors make this change, or I could issue a pull request. The fix doesn't appear to cause any backward compatibility issues.

Here's the full text of the error in case someone lands on this issue from a search. Also attached a screen shot of how this looks on a Safari v14 browser.

Error sorting stories with sort parameter function storySort(story1, story2) {
      return Object(storybook_multilevel_sort__WEBPACK_IMPORTED_MODULE_0__[/* default */ "a"])(order, story1, story2);
    }:

> Object.hasOwn is not a function. (In 'Object.hasOwn(obj, key)', 'Object.hasOwn' is undefined)

Are you using a V7-style sort function in V6 compatibility mode?

More info: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#v7-style-story-sort

Screen Shot 2022-10-27 at 10 34 36 AM

jgornick commented 1 year ago

We are also running into this issue when sorting for in a node/React Native environment.

prantlf commented 1 year ago

Thank you very much for an exhaustive description of the problem! I use Safari myself, but the latest version.

I added a fallback to Object.prototype.hasOwnProperty, according to the description of Object.hasOwn at MDN.

The fix got released as 1.1.1.