Fluture icon indicating copy to clipboard operation
Fluture copied to clipboard

Inconsistency between importing as esm contra cjs

Open dotnetCarpenter opened this issue 5 months ago • 0 comments

This is very minor but does not look like it is documented in the README (or I missed it).

I am in the process of changing a code base from cjs require to esm import and fell in the trap that I thought I could replace

const F = require ("fluture")
// with 
import F from "fluture"

In the former case, F would have F.Future etc. but when importing as esm, F = Future.

The work-around is to use:

import * as F from 'fluture'

It would be nice, if it's was import F from... and import { Future } from .... Perhaps this is deliberate but then it should be written in the README ;)

I did play around with index.js a little to see if I could mimic the behaviour of index.cjs but it quickly got ugly. So I'm all for documenting the difference between the two "import" methods. Basically, add a little bit more to https://github.com/fluture-js/Fluture?tab=readme-ov-file#ecmascript-module.

My issue with single name imports, is that I have to do this little ceremony, in order to get Sanctuary to understand the Fluture types, before I can use it anywhere else in my code.

import sanctuary    from 'sanctuary'
import $            from 'sanctuary-def'
import { env }      from 'fluture-sanctuary-types'
import * as F       from 'fluture'

const Future = F.Future
const checkTypes = process.env.NODE_ENV === 'development'
F.debugMode (checkTypes)

//    createEnum :: String -⁠> Array Any -⁠> Type
const createEnum = name => (
  $.EnumType (name)
             ('https://www.firefund.net/')
)

//    $DateIso :: NullaryType
const $DateIso = (
  $.NullaryType ('DateIso')
                ('https://www.firefund.net/')
                ([$.String])
                (x => /^\d{4}-\d{2}-\d{2}$/.test (x))
)

const $IntlNumberFormat = (
  $.NullaryType
    ('Intl.NumberFormat')
    ('https://devdocs.io/javascript/global_objects/intl/numberformat')
    ([])
    (x => Object.prototype.toString.call (x) === '[object Intl.NumberFormat]'));

const S = sanctuary.create ({
  checkTypes,
  env: [
    ...sanctuary.env,
    ...env,
    $DateIso,
    $IntlNumberFormat,
  ]
})

export {
  S,
  $,
  F,
  Future,
  createEnum,
  $DateIso,
}

Perhaps there is a better way?

dotnetCarpenter avatar Sep 16 '24 23:09 dotnetCarpenter