Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: define Buffer #294

Merged
merged 1 commit into from
Jul 25, 2020
Merged

types: define Buffer #294

merged 1 commit into from
Jul 25, 2020

Conversation

davidchambers
Copy link
Member

This was recently requested by @jceb in sanctuary-js/sanctuary#575.

Comment on lines +624 to +628
(function(x) {
return typeof Buffer !== 'undefined' &&
// eslint-disable-next-line no-undef
Buffer.isBuffer (x);
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following implementation is more elegant but less conducive to maintaining 100% branch coverage:

(typeof Buffer === 'undefined' ? K (false) : Buffer.isBuffer)

test/index.js Outdated
Comment on lines 1565 to 1573
(Buffer => {
delete global.Buffer;
try {
eq (isBuffer (null)) (false);
eq (isBuffer (Buffer.from ([1, 2, 3]))) (false);
} finally {
global.Buffer = Buffer;
}
}) (Buffer);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete global.Buffer removes our ability to reference Buffer implicitly, so we must retain an explicit reference. (Buffer => …) (Buffer) strikes me as more elegant than const Buffer_ = Buffer; ….

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about const Buffer = global.Buffer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I did not consider { const Buffer = global.Buffer; … }.

@Avaq
Copy link
Member

Avaq commented Jul 19, 2020

The code and approach look good to me. However, I would've probably separated any environment-specific types out into their own packages. I could imagine sanctuary-html, sanctuary-node, (and sanctuary-deno?) packages that each provide envs and utilities for their corresponding environments. Sanctuary-def's env would then only contain JavaScript's built-in types.

@davidchambers
Copy link
Member Author

Providing separate packages for various environments is certainly an option worth considering. :)

Copy link
Member

@Avaq Avaq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good to me. I'll leave it up to you to consider the separate packages idea. I guess we can always move to it in the future if maintaining node types in sanctuary-def becomes unwieldy or problematic for browsers.

@davidchambers davidchambers merged commit 453d771 into master Jul 25, 2020
@davidchambers davidchambers deleted the davidchambers/buffer branch July 25, 2020 08:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants