Skip to content

Commit

Permalink
Use startsWith() and endsWith() methods from JavaScript's built-i…
Browse files Browse the repository at this point in the history
…n String prototype
  • Loading branch information
kysrpex committed Oct 21, 2024
1 parent b2bf83c commit 80183d2
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 19 deletions.
3 changes: 1 addition & 2 deletions lib/createdb.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env node
const args = require("commander");
const packageInfo = require("../package");
const { startsWith } = require("./mapper");
var postgresClient = require("pg-native");
var sqlite3 = require("sqlite3");

Expand Down Expand Up @@ -55,7 +54,7 @@ const createDbPostgres = function (sessions) {
const main = function (argv_) {
const argv = argv_ || process.argv;
args.parse(argv);
if (startsWith(args.sessions, "postgresql://")) {
if (args.sessions.startsWith("postgresql://")) {
createDbPostgres(args.sessions);
} else {
createDbSqlite(args.sessions);
Expand Down
19 changes: 2 additions & 17 deletions lib/mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,6 @@ var sqlite3 = require("sqlite3");
var postgresClient = require("pg-native");
var watchFile = require("node-watch");

var startsWith = function (subjectString, searchString) {
var reversedSubjectString = subjectString.split('').reverse().join('');
var reversedSearchString = searchString.split('').reverse().join('');
return endsWith(reversedSubjectString, reversedSearchString);
}

var endsWith = function (subjectString, searchString) {
var position = subjectString.length;
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};

var updateFromJson = function (path, map) {
var content = fs.readFileSync(path, "utf8");
var keyToSession = JSON.parse(content);
Expand Down Expand Up @@ -155,12 +142,12 @@ var mapFor = function (path, pollingInterval) {
var map = {};
var loadMap;
var watch;
if (endsWith(path, ".sqlite")) {
if (path.endsWith(".sqlite")) {
loadMap = function () {
updateFromSqlite(path, map);
};
watch = watchFile;
} else if (startsWith(path, "postgresql://")) {
} else if (path.startsWith("postgresql://")) {
loadMap = function () {
updateFromPostgres(path, map);
};
Expand All @@ -179,6 +166,4 @@ var mapFor = function (path, pollingInterval) {
return map;
};

exports.endsWith = endsWith;
exports.mapFor = mapFor;
exports.startsWith = startsWith;

0 comments on commit 80183d2

Please sign in to comment.