Skip to content

Commit

Permalink
Merge pull request #6 from bert-w/cleanup-deps
Browse files Browse the repository at this point in the history
remove lodash + move commander to dev + default to non-console
  • Loading branch information
bert-w authored Feb 28, 2024
2 parents 9de8381 + a17b03e commit 2af37e8
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ import { Sqomplexity } from 'sqomplexity';
];

// Construct SQompLexity (passing `score` only outputs the complexity score):
const command = (new Sqomplexity({ score: true }, null, false));
const command = new Sqomplexity({ score: true });

console.log(await command.run(queries));

Expand Down
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ program
.option('-p, --pretty-print', 'output JSON with indentation and newlines', false)
.action(async(queries, options) => {
try {
await (new Sqomplexity(options, process.cwd())).run(queries);
await (new Sqomplexity(options, process.cwd(), true)).run(queries);
} catch (e) {
program.addHelpText('after', '\n' + e.stack);
program.help();
Expand Down
2 changes: 1 addition & 1 deletion examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Sqomplexity } from 'sqomplexity';
];

// Construct SQompLexity (passing `score` only outputs the complexity score):
const command = (new Sqomplexity({ score: true }, null, false));
const command = new Sqomplexity({ score: true });

console.log(await command.run(queries));

Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
},
"devDependencies": {
"babel-loader": "^9.1.2",
"commander": "^11.0.0",
"cross-env": "^7.0.3",
"eslint": "^8.30.0",
"eslint-config-standard": "^17.0.0",
Expand All @@ -47,10 +48,6 @@
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4"
},
"dependencies": {
"commander": "^11.0.0",
"lodash.get": "^4.4.2"
},
"engines": {
"node": ">=16"
}
Expand Down
4 changes: 1 addition & 3 deletions src/calculator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import _ from 'lodash.get';

/**
* Calculate a complexity score based on an AST of the SQL query and meta properties.
*/
Expand Down Expand Up @@ -294,7 +292,7 @@ export class Calculator {
*/
_calculateHaving(ast) {
let score = 0;
if (_(ast, 'having.type') === 'binary_expr') {
if (ast?.having?.type === 'binary_expr') {
score += this._expression(ast.having, 'having');
}

Expand Down
4 changes: 2 additions & 2 deletions src/sqomplexity.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export class Sqomplexity {
* @param {boolean} [options.all]
* @param {boolean} [options.prettyPrint]
* @param {string|null} cwd Used for determining the correct path when using a file path for the weights.
* @param {boolean} console Pass false to return the values instead of outputting them.
* @param {boolean} console Pass true to echo the values instead of returning them.
*/
constructor(options = {}, cwd = null, console = true) {
constructor(options = {}, cwd = null, console = false) {
this.options = options || {};
this.cwd = cwd;
this.console = console;
Expand Down
22 changes: 15 additions & 7 deletions tests/sqomplexity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ process.chdir(__dirname);

describe('src/sqomplexity.js', function() {
it('throws on passing no queries', async function() {
const sqomplexity = new Sqomplexity({}, null, false);
const sqomplexity = new Sqomplexity();

await expect(sqomplexity.run([])).rejects.toThrow(Error);
});

it('accepts a single query', async function() {
const sqomplexity = new Sqomplexity({}, null, false);
const sqomplexity = new Sqomplexity();

const result = await sqomplexity.run([
'SELECT * FROM users'
Expand All @@ -26,7 +26,7 @@ describe('src/sqomplexity.js', function() {
});

it('accepts multiple queries', async function() {
const sqomplexity = new Sqomplexity({}, null, false);
const sqomplexity = new Sqomplexity();

const result = (await sqomplexity.run([
'SELECT * FROM users',
Expand All @@ -40,7 +40,7 @@ describe('src/sqomplexity.js', function() {
it('accepts base64-encoded queries', async function() {
const sqomplexity = new Sqomplexity({
base64: true
}, null, false);
});

expect(
(await sqomplexity.run([btoa('SELECT * FROM users')]))[0].complexity
Expand All @@ -50,7 +50,7 @@ describe('src/sqomplexity.js', function() {
it('outputs score only', async function() {
const sqomplexity = new Sqomplexity({
score: true
}, null, false);
});

expect(
(await sqomplexity.run(['SELECT * FROM users']))[0]
Expand All @@ -60,11 +60,19 @@ describe('src/sqomplexity.js', function() {
it('can print to console', async function() {
const sqomplexity = new Sqomplexity({
score: true
});
}, null, true);

let result;

console.log = function(log) {
result = log;
};

expect(
(await sqomplexity.run(['SELECT * FROM users']))
).toBeUndefined();

expect(result).toBeDefined();
});

it('accepts custom weights', async function() {
Expand Down Expand Up @@ -104,7 +112,7 @@ describe('src/sqomplexity.js', function() {
variety: 1
}
}
}, null, false);
});

expect(
(await sqomplexity.run(['SELECT * FROM users']))[0]
Expand Down

0 comments on commit 2af37e8

Please sign in to comment.