Skip to content

Commit

Permalink
refactor(utils): replace var with const/let, use strict equal
Browse files Browse the repository at this point in the history
  • Loading branch information
StephaneBour committed Jan 15, 2024
1 parent 6337ed6 commit 641bfe9
Showing 1 changed file with 55 additions and 57 deletions.
112 changes: 55 additions & 57 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
(function () {

var global = window;
const global = window;
if (!global.sense)
global.sense = {};

var ns = {};
const ns = {};
global.sense.utils = ns;

var sense = global.sense;
const sense = global.sense;

ROW_PARSE_MODE = {
let ROW_PARSE_MODE = {
REQUEST_START: 2,
IN_REQUEST: 4,
MULTI_DOC_CUR_DOC_END: 8,
REQUEST_END: 16,
BETWEEN_REQUESTS: 32
};

getRowParseMode = function (row, editor) {
let getRowParseMode = function (row, editor) {
editor = editor || sense.editor;
if (row == null || typeof row == "undefined") row = editor.getCursorPosition().row;

var session = editor.getSession();
const session = editor.getSession();
if (row >= session.getLength()) return ROW_PARSE_MODE.BETWEEN_REQUESTS;
var mode = (session.getState(row) || {}).name;
const mode = (session.getState(row) || {}).name;
if (!mode)
return ROW_PARSE_MODE.BETWEEN_REQUESTS; // shouldn't really happen


if (mode != "start") return ROW_PARSE_MODE.IN_REQUEST;
var line = (session.getLine(row) || "").trim();
if (mode !== "start") return ROW_PARSE_MODE.IN_REQUEST;
let line = (session.getLine(row) || "").trim();
if (!line) return ROW_PARSE_MODE.BETWEEN_REQUESTS; // empty line waiting for a new req to start

if (line.indexOf("}", line.length - 1) >= 0) {
// check for a multi doc request (must start a new json doc immediately after this one end.
// check for a multi doc request (must start a new json doc immediately after this one end).
row++;
if (row < session.getLength()) {
line = (session.getLine(row) || "").trim();
if (line.indexOf("{") == 0) { // next line is another doc in a multi doc
if (line.indexOf("{") === 0) { // next line is another doc in a multi doc
return ROW_PARSE_MODE.MULTI_DOC_CUR_DOC_END | ROW_PARSE_MODE.IN_REQUEST;
}

Expand All @@ -51,15 +50,15 @@
return ROW_PARSE_MODE.REQUEST_START | ROW_PARSE_MODE.REQUEST_END;
}
line = (session.getLine(row) || "").trim();
if (line.indexOf("{") != 0) { // next line is another request
if (line.indexOf("{") !== 0) { // next line is another request
return ROW_PARSE_MODE.REQUEST_START | ROW_PARSE_MODE.REQUEST_END;
}

return ROW_PARSE_MODE.REQUEST_START;
};

function rowPredicate(row, editor, value) {
var mode = getRowParseMode(row, editor);
const mode = getRowParseMode(row, editor);
return (mode & value) > 0;
}

Expand Down Expand Up @@ -91,7 +90,7 @@

ns.iterForCurrentLoc = function (editor) {
editor = editor || sense.editor;
var pos = editor.getCursorPosition();
const pos = editor.getCursorPosition();
return ns.iterForPosition(pos.row, pos.column, editor);
};

Expand All @@ -101,24 +100,24 @@
};

ns.isEmptyToken = function (tokenOrTokenIter) {
var token = tokenOrTokenIter && tokenOrTokenIter.getCurrentToken ? tokenOrTokenIter.getCurrentToken() : tokenOrTokenIter;
return !token || token.type == "whitespace"
const token = tokenOrTokenIter && tokenOrTokenIter.getCurrentToken ? tokenOrTokenIter.getCurrentToken() : tokenOrTokenIter;
return !token || token.type === "whitespace"
};

ns.isUrlOrMethodToken = function (tokenOrTokenIter) {
var t = tokenOrTokenIter.getCurrentToken ? tokenOrTokenIter.getCurrentToken() : tokenOrTokenIter;
return t && t.type && (t.type == "method" || t.type.indexOf("url") == 0);
const t = tokenOrTokenIter.getCurrentToken ? tokenOrTokenIter.getCurrentToken() : tokenOrTokenIter;
return t && t.type && (t.type === "method" || t.type.indexOf("url") === 0);
};


ns.nextNonEmptyToken = function (tokenIter) {
var t = tokenIter.stepForward();
let t = tokenIter.stepForward();
while (t && ns.isEmptyToken(t)) t = tokenIter.stepForward();
return t;
};

ns.prevNonEmptyToken = function (tokenIter) {
var t = tokenIter.stepBackward();
let t = tokenIter.stepBackward();
// empty rows return null token.
while ((t || tokenIter.getCurrentTokenRow() > 0) && ns.isEmptyToken(t)) t = tokenIter.stepBackward();
return t;
Expand All @@ -127,7 +126,7 @@
ns.prevRequestStart = function (pos, editor) {
editor = editor || sense.editor;
pos = pos || editor.getCursorPosition();
var curRow = pos.row;
let curRow = pos.row;
while (curRow > 0 && !ns.isStartRequestRow(curRow, editor)) curRow--;

return { row: curRow, column: 0};
Expand All @@ -136,36 +135,36 @@
ns.nextRequestEnd = function (pos, editor) {
editor = editor || sense.editor;
pos = pos || editor.getCursorPosition();
var session = editor.getSession();
var curRow = pos.row;
var maxLines = session.getLength();
const session = editor.getSession();
let curRow = pos.row;
const maxLines = session.getLength();
for (; curRow < maxLines - 1; curRow++) {
var curRowMode = getRowParseMode(curRow, editor);
const curRowMode = getRowParseMode(curRow, editor);
if ((curRowMode & ROW_PARSE_MODE.REQUEST_END) > 0) break;
if (curRow != pos.row && (curRowMode & ROW_PARSE_MODE.REQUEST_START) > 0) break;
if (curRow !== pos.row && (curRowMode & ROW_PARSE_MODE.REQUEST_START) > 0) break;
}

var column = (session.getLine(curRow) || "").length;
const column = (session.getLine(curRow) || "").length;

return { row: curRow, column: column};
};

ns.nextDataDocEnd = function (pos, editor) {
editor = editor || sense.editor;
pos = pos || editor.getCursorPosition();
var session = editor.getSession();
var curRow = pos.row;
var maxLines = session.getLength();
const session = editor.getSession();
let curRow = pos.row;
const maxLines = session.getLength();
for (; curRow < maxLines - 1; curRow++) {
var curRowMode = getRowParseMode(curRow, editor);
const curRowMode = getRowParseMode(curRow, editor);
if ((curRowMode & ROW_PARSE_MODE.REQUEST_END) > 0) {
break;
}
if ((curRowMode & ROW_PARSE_MODE.MULTI_DOC_CUR_DOC_END) > 0) break;
if (curRow != pos.row && (curRowMode & ROW_PARSE_MODE.REQUEST_START) > 0) break;
if (curRow !== pos.row && (curRowMode & ROW_PARSE_MODE.REQUEST_START) > 0) break;
}

var column = (session.getLine(curRow) || "").length;
const column = (session.getLine(curRow) || "").length;

return { row: curRow, column: column };
};
Expand All @@ -174,8 +173,8 @@
ns.getCurrentRequestRange = function (editor) {
if (ns.isInBetweenRequestsRow(null, editor)) return null;

var reqStart = ns.prevRequestStart(null, editor);
var reqEnd = ns.nextRequestEnd(reqStart, editor);
const reqStart = ns.prevRequestStart(null, editor);
const reqEnd = ns.nextRequestEnd(reqStart, editor);
return new (ace.require("ace/range").Range)(
reqStart.row, reqStart.column,
reqEnd.row, reqEnd.column
Expand All @@ -187,38 +186,38 @@

if (ns.isInBetweenRequestsRow(null, editor)) return null;

var request = {
const request = {
method: "",
data: [],
url: null
};

var currentReqRange = ns.getCurrentRequestRange(editor);
const currentReqRange = ns.getCurrentRequestRange(editor);

var pos = currentReqRange.start;
var tokenIter = ns.iterForPosition(pos.row, pos.column, editor);
var t = tokenIter.getCurrentToken();
const pos = currentReqRange.start;
const tokenIter = ns.iterForPosition(pos.row, pos.column, editor);
let t = tokenIter.getCurrentToken();
request.method = t.value;
t = ns.nextNonEmptyToken(tokenIter);
if (!t || t.type == "method") return null;
if (!t || t.type === "method") return null;
request.url = "";
while (t && t.type && t.type.indexOf("url") == 0) {
while (t && t.type && t.type.indexOf("url") === 0) {
request.url += t.value;
t = tokenIter.stepForward();
}

var bodyStartRow = (t ? 0 : 1) + tokenIter.getCurrentTokenRow(); // artificially increase end of docs.
var bodyStartColumn = 0;
let bodyStartRow = (t ? 0 : 1) + tokenIter.getCurrentTokenRow();
let bodyStartColumn = 0;
while (bodyStartRow < currentReqRange.end.row ||
(bodyStartRow == currentReqRange.end.row &&
(bodyStartRow === currentReqRange.end.row &&
bodyStartColumn < currentReqRange.end.column
)) {
dataEndPos = ns.nextDataDocEnd({ row: bodyStartRow, column: bodyStartColumn}, editor);
var bodyRange = new (ace.require("ace/range").Range)(
bodyStartRow, bodyStartColumn,
dataEndPos.row, dataEndPos.column
const bodyRange = new (ace.require("ace/range").Range)(
bodyStartRow, bodyStartColumn,
dataEndPos.row, dataEndPos.column
);
var data = editor.getSession().getTextRange(bodyRange);
const data = editor.getSession().getTextRange(bodyRange);
request.data.push(data.trim());
bodyStartRow = dataEndPos.row + 1;
bodyStartColumn = 0;
Expand All @@ -227,7 +226,7 @@
};

ns.textFromRequest = function (request) {
var data = request.data;
let data = request.data;
if (typeof data != "string") {
data = data.join("\n");
}
Expand All @@ -236,7 +235,7 @@

ns.replaceCurrentRequest = function (newRequest, curRequestRange) {
if (!curRequestRange) curRequestRange = ns.getCurrentRequestRange();
var text = ns.textFromRequest(newRequest);
const text = ns.textFromRequest(newRequest);
if (curRequestRange) {
sense.editor.getSession().replace(curRequestRange, text);
}
Expand All @@ -248,8 +247,8 @@

ns.getUrlParam = function (name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
const regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
};

Expand All @@ -260,9 +259,9 @@

ns.updateEditorAndCallWhenUpdated = function (data, editor, callback) {
editor = editor || sense.editor;
var session = editor.getSession();
const session = editor.getSession();

function mycallback() {
function mycallback() {
session.removeListener(mycallback);
if (session.bgTokenizer.running) {
setTimeout(mycallback, 50); // wait
Expand All @@ -274,5 +273,4 @@
session.on('tokenizerUpdate', mycallback);
session.setValue(data);
}

})();

0 comments on commit 641bfe9

Please sign in to comment.