Creating Your Own ELM Module Using Moment.js

I played with Elm a couple of times, and I wanted to make the Moment.JS port, as I saw the lack of libraries in what I wanted, but Moment has everything I need.

The fact is that I always encounter the same error. I have Moment.JS in my Native folder (it's called MomentJS.js) and another Moment.js file (my cover). The problem is that when I call the moment in Moment.js, I get an error message indicating that the moment is not defined.

I tried to import MomentJS.js into an elm file, as well as before and / or after Moment.js. I also tried copying all the JS into Moment.js and adding my wrapper at the end of it. None of this happened. Do you know what I can do? I searched for similar repositories on the Internet, but I never saw a module with a shell and another JS file only for the native library.

This is my Moment.js code:

var _user$project$Native_Moment = (function() { var moment = require('moment'); var format = function ( format, date ) { return moment().format(); } return { format: format }; })(); 

and my Moment.elm code:

 module Moment exposing (format) {-| A module desc @docs format -} import Native.MomentJS import Native.Moment {-| Call the default `Moment.js` format method -} format : String -> String -> String format fm dt = Native.Moment.format fm dt 

The last thing I tried was to download Moment from npm, copy its folder from the node_modules folder to my Native folder and do moment = require('moment') , but I got TypeError: fun(...) is not a function .

Any suggestions?

+5
source share
2 answers

After some digging, what you want to do is probably yes! :) However, this will not be a simple copy. Look at the source that you sent here , for this you need to compare each function with the functions of the native elm, I would recommend starting with this conversion.

First, get a simple world of greeting. Example example of an example, see here

Secondly, add some of the simpler functions from moment.js one by one, I recommend starting from the moment \ src \ lib \ format \ format.js

Finally, I know that this is not what you want to hear, but if you really want to write javascript in elms, maybe Elm is not what you are looking for? I really can't imagine transforming an entire library like Moment.js would be easier than creating my own Elm library inspired by Moment.js

Anyway, good luck! This seems like a fun challenge anyway :)

+5
source

There are two supported ways for Elm and JavaScript to interact with each other: ports and flags. Both of them are asynchronous and will be inconvenient for your needs.

https://guide.elm-lang.org/interop/javascript.html

Do you have to write your own code? The knitting maker says no .

Thus, the best way forward is to use one of the existing time / date libraries or write what you need.

+2
source

Source: https://habr.com/ru/post/1275199/


All Articles