Skip to content

Commit

Permalink
export sort, update sort, code styling
Browse files Browse the repository at this point in the history
  • Loading branch information
m1ga committed Jan 12, 2023
1 parent d0f352e commit 7d1d112
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 36 deletions.
18 changes: 5 additions & 13 deletions lib/jsanalyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,14 @@ const path = require('path');
const SOURCE_MAPPING_URL_REGEXP = /\/\/#[ \t]+sourceMappingURL=([^\s'"`]+?)[ \t]*$/mg;
const __ = appc.i18n(__dirname).__;

function sortObject (o) {
var sorted = {},
key,
a = [];

for (key in o) {
a.push(key);
}

a.sort();

for (key = 0; key < a.length; key++) {
sorted[a[key]] = o[a[key]];
function sortObject(o) {
const sorted = {};
for (const key of Object.keys(o).sort()) {
sorted[key] = o[key];
}
return sorted;
}
exports.sortObject = sortObject;

/**
* Returns an object with the Titanium API usage statistics.
Expand Down
30 changes: 7 additions & 23 deletions tests/jsanalyze_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,7 @@ const path = require('path');
const fs = require('fs-extra');
const should = require('should'); // eslint-disable-line no-unused-vars
const jsanalyze = require('../lib/jsanalyze');

function sortObject (o) {
var sorted = {},
key,
a = [];

for (key in o) {
a.push(key);
}

a.sort();

for (key = 0; key < a.length; key++) {
sorted[a[key]] = o[a[key]];
}
return sorted;
}
const sortObject = require('../lib/jsanalyze').sortObject;

describe('jsanalyze', function () {
describe('#analyzeJs()', function () {
Expand Down Expand Up @@ -85,7 +69,7 @@ describe('jsanalyze', function () {
it('generates source maps inline into generated js file', function () {
const inputJSFile = path.join(__dirname, 'resources/input.js');
const contents = fs.readFileSync(inputJSFile, 'utf-8');
var expectedSourceMap = fs.readJSONSync(inputJSFile + '.map');
let expectedSourceMap = fs.readJSONSync(`${inputJSFile}.map`);

expectedSourceMap.sourceRoot = path.dirname(inputJSFile);
expectedSourceMap = sortObject(expectedSourceMap);
Expand All @@ -103,7 +87,7 @@ describe('jsanalyze', function () {
it('generates source maps inline into generated js file and removes sourcesContent for android platform', function () {
const inputJSFile = path.join(__dirname, 'resources/input.js');
const contents = fs.readFileSync(inputJSFile, 'utf-8');
var expectedSourceMap = fs.readJSONSync(inputJSFile + '.map');
let expectedSourceMap = fs.readJSONSync(`${inputJSFile}.map`);
expectedSourceMap.sourceRoot = path.dirname(inputJSFile);
delete expectedSourceMap.sourcesContent;
expectedSourceMap = sortObject(expectedSourceMap);
Expand All @@ -127,7 +111,7 @@ describe('jsanalyze', function () {
sourceMap: true,
filename: 'intermediate.js'
});
var expectedSourceMap = fs.readJSONSync(path.join(__dirname, 'resources/intermediate.js.map'));
let expectedSourceMap = fs.readJSONSync(path.join(__dirname, 'resources/intermediate.js.map'));
expectedSourceMap.sourceRoot = path.dirname(inputJSFile); // passes along the original source file via sources/sourceRoot
expectedSourceMap = sortObject(expectedSourceMap);
const expectedBase64Map = Buffer.from(JSON.stringify(expectedSourceMap)).toString('base64');
Expand All @@ -143,7 +127,7 @@ describe('jsanalyze', function () {
sourceMap: true,
filename: path.join(__dirname, 'resources/intermediate.js')
});
var expectedSourceMap = fs.readJSONSync(path.join(__dirname, 'resources/intermediate.js.map'));
let expectedSourceMap = fs.readJSONSync(path.join(__dirname, 'resources/intermediate.js.map'));
expectedSourceMap.sourceRoot = path.dirname(originalSourceFile); // passes along the original source file via sources/sourceRoot
expectedSourceMap = sortObject(expectedSourceMap);
const expectedBase64Map = Buffer.from(JSON.stringify(expectedSourceMap)).toString('base64');
Expand All @@ -155,7 +139,7 @@ describe('jsanalyze', function () {
// only difference here is that there's an extra newline to deal with versus the "base" test case
const inputJSFile = path.join(__dirname, 'resources/input.nonexistent.sourcemapfile.js');
const contents = fs.readFileSync(inputJSFile, 'utf-8');
var expectedSourceMap = fs.readJSONSync(path.join(__dirname, 'resources/input.nonexistent.sourcemapfile.js.map'));
let expectedSourceMap = fs.readJSONSync(path.join(__dirname, 'resources/input.nonexistent.sourcemapfile.js.map'));
expectedSourceMap.sourceRoot = path.dirname(inputJSFile);
expectedSourceMap = sortObject(expectedSourceMap);
const results = jsanalyze.analyzeJs(contents,
Expand Down Expand Up @@ -188,7 +172,7 @@ describe('jsanalyze', function () {
it('should transform contents', function () {
const inputJSFile = path.join(__dirname, 'resources/input.js');

var expectedSourceMap = fs.readJSONSync(inputJSFile + '.map');
let expectedSourceMap = fs.readJSONSync(`${inputJSFile}.map`);
expectedSourceMap.sourceRoot = path.dirname(inputJSFile);
expectedSourceMap = sortObject(expectedSourceMap);
const results = jsanalyze.analyzeJsFile(inputJSFile,
Expand Down

0 comments on commit 7d1d112

Please sign in to comment.