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.
First look at what it's invoked with:
(exports.OutputFormat || (exports.OutputFormat = {}));
exports.OutputFormat
is truthy, it'll be the parameterexports.OutputFormat = {}
, which will:
exports.OutputFormat
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.