Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions spec/disposable-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ describe "Disposable", ->

disposable.dispose()
expect(disposalAction.callCount).toBe 1

describe ".isDisposable(object)", ->
it "returns true if the object implements the ::dispose function", ->
expect(Disposable.isDisposable(new Disposable(->))).toBe(true)
expect(Disposable.isDisposable({dispose: ->})).toBe(true)

expect(Disposable.isDisposable(null)).toBe(false)
expect(Disposable.isDisposable(undefined)).toBe(false)
expect(Disposable.isDisposable({foo: ->})).toBe(false)
expect(Disposable.isDisposable({dispose: 1})).toBe(false)
9 changes: 9 additions & 0 deletions src/disposable.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ module.exports =
class Disposable
disposed: false

# Public: Ensure that an `object` correctly implements the `Disposable`
# contract.
#
# * `object` Any object you want to perform the check against.
#
# Returns a {Boolean} indicating whether `object` is a valid `Disposable`.
@isDisposable: (object) ->
typeof object?.dispose is "function"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is the existential operator needed here? It might be useful to blow up if you're accidentally passing in a null or undefined object

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Generally, I could go either way, but I think it's good to imitate Array.isArray, Number.isNaN et al in this case. They return false when passed null or undefined.


###
Section: Construction and Destruction
###
Expand Down