Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

198
Visualizações
CommonJS to ES6 migration exports. to export

I'm migrating a Nativescript project from version 6.8 to 8.1. This involves converting modules from CommonJS to ES6, which is mostly just converting function exports, so that

exports.foo = function(args) { ... }

becomes

export function foo(args) { ... }

and if you invoke the function from within the module, exports.foo() becomes just foo().

In addition to my own code I'm finding I'm having to migrate some of the plugins I use as newer versions aren't available. So far so good, except for this block of code:

/**
 * List of outout formats.
 */
(function (OutputFormat) {
    /**
     * PNG
     */
    OutputFormat[OutputFormat["PNG"] = 1] = "PNG";
    /**
     * JPEG
     */
    OutputFormat[OutputFormat["JPEG"] = 2] = "JPEG";
})(exports.OutputFormat || (exports.OutputFormat = {}));
var OutputFormat = exports.OutputFormat;

I'm having a hard time following what this does, much less converting it to ES6 syntax. For context, here's the type definition:

export declare enum OutputFormat {
    /**
     * PNG
     */
    PNG = 1,
    /**
     * JPEG
     */
    JPEG = 2,
} 

I'd welcome any suggestions on how to convert this.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First look at what it's invoked with:

(exports.OutputFormat || (exports.OutputFormat = {}));
  • If exports.OutputFormat is truthy, it'll be the parameter
  • Otherwise, the following expression will be the parameter: exports.OutputFormat = {}, which will:
    • create an empty object
    • assign that empty object to exports.OutputFormat
    • evaluate to that empty object

Unless OutputFormat is referenced elsewhere in this module, which seems unlikely, you can turn it into ES6 module syntax with:

export const OutputFormat = {
  PNG: 1,
  1: "PNG",
  JPEG: 2,
  2: "JPEG",
};

While you can also export an empty object and then run

OutputFormat[OutputFormat["PNG"] = 1] = "PNG";
OutputFormat[OutputFormat["JPEG"] = 2] = "JPEG";

, those lines of code are much more confusing than they need to be, so I'd refactor them to the above.

(or you could iterate over an array of [["PNG", 1], ["JPEG", 2]] and assign)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda