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

import momentjs library #241

Open
DanieleRosada opened this issue Jan 12, 2023 · 3 comments
Open

import momentjs library #241

DanieleRosada opened this issue Jan 12, 2023 · 3 comments
Labels

Comments

@DanieleRosada
Copy link

I'm trying to import momentjs library. When I use moment().format(), the library pass this to next function, but this is the object of JS-Interpreter and not momentjs. This is my test:

import moment from 'moment';
let myInterpreter = new Interpreter(condition, (interpreter: any, globalObject: any) => {});
myInterpreter.setValueToScope('moment', myInterpreter.nativeToPseudo(moment));
myInterpreter.run();

Momentjs code generating problem:

function format(inputString) {
        if (!inputString) {
            inputString = this.isUtc()
                ? hooks.defaultFormatUtc
                : hooks.defaultFormat;
        }
        var output = formatMoment(this, inputString); // HERE is the problem
        return this.localeData().postformat(output);
}
function formatMoment(m, format) {
        if (!m.isValid()) {
            return m.localeData().invalidDate();
        }

        format = expandFormat(format, m.localeData());
        formatFunctions[format] =
            formatFunctions[format] || makeFormatFunction(format);

        return formatFunctions[format](m);
    }    
@cpcallen
Copy link
Collaborator

Can you restructure this as a complete example that runs (but, presumably, generates an error)? Specifically, in "this is my test", you don't specify a value for condition.

@DanieleRosada
Copy link
Author

Thank you for the fast answer. Snippet code here: https://stackblitz.com/edit/typescript-cro98o?file=index.ts,package.json

@semireg
Copy link

semireg commented May 10, 2024

I spent a few minutes trying to mimic the moment API using the moment constructor, but gave up and created this working one-shot wrapper.

      const momentFormatWrapper = (dateInput: string, format: string) => {
        return moment(dateInput).format(format);
      };
      interpreter.setProperty(globalObject, 'momentFormat', interpreter.createNativeFunction(momentFormatWrapper));

You may then use within the interpreter e.g. momentFormat('6 Mar 2017', 'MM/DD/YY');

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

No branches or pull requests

3 participants