JavaScript Code Assistant
Languages / 简体中文
To format minified source code and offer the position in generated code if corresponding position in source code is provided.
const codon = require('codon');
let sourceCode = '// Minified JavaScript Source Code';
// -- return transformed position directly --
let output_1 = codon.format({
code: sourceCode,
position: { line: 1, column: 10 },
});
// RETURN: { code, position }
// -- to do poisition transformation by yourself --
let output_2 = codon.format({
code: sourceCode,
position: true,
});
// RETURN: { code, positionFor }
let transformedPosition = output_2.positionFor({ line: 1, column: 10 });
// RETURN: { line, column }
If you wanna compress source code other than formatting it, use codon.compress()
instead.
The only parameter is an object with following attributes:
-
string | Buffer code
Source code. -
boolean | Object | [Object] position OPTIONAL
As object (array), it represents the position in source code. As boolean and valuedtrue
, there SHOULD be an attribute namedpositionFor
which is a function in charge of transforming position(s) in source code to position(s) in generated code.
E.g.
-
{ string code } codon.format({ string code })
To generate formatted code. -
{ string code, object position } codon.format({ string code, object position })
To generate formatted code, and a transformed position object. -
{ string code, Array position } codon.format({ string code, Array position })
To generate formatted code, and a group of transformed position objects. -
{ string code, function positionFor } codon.format({ string code, boolean position })
To generate formatted code, and a transform method.
Similar to __codon.compress(), but to beautify source code other than minifying it.