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

Inconsistency between importing as esm contra cjs #611

Open
dotnetCarpenter opened this issue Sep 16, 2024 · 0 comments
Open

Inconsistency between importing as esm contra cjs #611

dotnetCarpenter opened this issue Sep 16, 2024 · 0 comments

Comments

@dotnetCarpenter
Copy link

dotnetCarpenter commented Sep 16, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant