sanctuary-js / sanctuary-def

Run-time type system for JavaScript
MIT License
294 stars 23 forks source link

HtmlElement is unrecognized #309

Closed dotnetCarpenter closed 2 years ago

dotnetCarpenter commented 2 years ago

I believe I have found a bug in how $.HtmlElement is defined.

<section id="HtmlElement"></section>
import $ from 'sanctuary-def'

const HtmlElement = document.querySelector ('#HtmlElement')

const testCase = $.test ([])
                        ($.HtmlElement)
                        (HtmlElement)

const sanityCheck = $.test ([])
                           ($.Boolean)
                           (true)

/*
  The Console should display true since a <section> is an HTMLElement.
  All test-cases should be true
 */

console.debug ('testCase', testCase)       // false
console.debug ('sanityCheck', sanityCheck) // true
console.debug ('check', /^\[object HTML.+Element\]$/.test (Object.toString.call (HtmlElement)))

Live test-case: https://playcode.io/849895/ Offending line: https://github.com/sanctuary-js/sanctuary-def/blob/master/index.js#L719

This means that currently one can not use an HtmlElement as an argument.

please ignore the silliness of this example - I realize that HtmlElement is not a StrMap

const textContent = S.flip (S.insert ('textContent'))

textContent (HtmlElement) ('text content to inserted')
Uncaught TypeError: Unrecognized value

flip :: Functor f => f (a -> b) -> a -> f b
                                   ^
                                   1

1)  <object HTMLElement> :: (no types)

The value at position 1 is not a member of any type in the environment.

The environment contains the following types:

  - Function
  - Arguments
  - Array c
  - Array2 c d
  - Boolean
  - Buffer
  - Date
  - Descending c
  - Either c d
  - Error
  - (c -> d)
  - HtmlElement
  - Identity c
  - JsMap c d
  - JsSet c
  - Maybe c
  - Module
  - Null
  - Number
  - Object
  - Pair c d
  - RegExp
  - StrMap c
  - String
  - Symbol
  - Type
  - TypeClass
  - Undefined
  - Future c d
  - ConcurrentFuture c d
  - Event
    invalidValue index.js:2666
    satisfactoryTypes index.js:1351
    assertRight index.js:2731
    wrapNext index.js:2823
dotnetCarpenter commented 2 years ago

A fix could be to change line 718-720 to

function(x) {
    return (x && x.toString && /^\[object HTML.*Element\]$/.test (x.toString ()) || false);
}