Skip to content

Commit

Permalink
closes #40 by extending instanceof method (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbarbounakis authored Nov 26, 2019
1 parent 84fb3a0 commit a8a123c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
9 changes: 5 additions & 4 deletions modules/@themost/query/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var _ = require('lodash');
var query = require('./query');
var QueryExpression = query.QueryExpression;
var QueryField = query.QueryField;
var instanceOf = require('./instance-of').instanceOf;

if (typeof Object.key !== 'function') {
/**
Expand Down Expand Up @@ -721,7 +722,7 @@ SqlFormatter.prototype.formatSelect = function(obj)
if (obj.$ref && obj.$ref[entity]) {
var entityRef = obj.$ref[entity];
//escape entity ref
if (entityRef instanceof QueryExpression) {
if (instanceOf(entityRef, QueryExpression)) {
escapedEntity = "(" + this.format(entityRef) + ") " + $this.escapeName(entity);
}
else {
Expand All @@ -748,7 +749,7 @@ SqlFormatter.prototype.formatSelect = function(obj)
{
//enumerate joins
_.forEach(joins, function(x) {
if (x.$entity instanceof QueryExpression) {
if (instanceOf(x.$entity, QueryExpression)) {
//get on statement (the join comparison)
sql = sql.concat(sprintf(' INNER JOIN (%s)', $this.format(x.$entity)));
//add alias
Expand Down Expand Up @@ -1105,7 +1106,7 @@ SqlFormatter.prototype.format = function(obj, s)
return this.formatFieldEx(field, s);
}
else if (s==='%o') {
if (obj instanceof QueryExpression)
if (instanceOf(obj, QueryExpression))
return this.formatOrder(obj.$order);
return this.formatOrder(obj);
}
Expand All @@ -1116,7 +1117,7 @@ SqlFormatter.prototype.format = function(obj, s)
*/
var query = null;
//cast object to QueryExpression
if (obj instanceof QueryExpression)
if (instanceOf(obj, QueryExpression))
query = obj;
else
query = _.assign(new QueryExpression(), obj);
Expand Down
29 changes: 29 additions & 0 deletions modules/@themost/query/instance-of.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* MOST Web Framework
* A JavaScript Web Framework
* http://themost.io
*
* Copyright (c) 2019, THEMOST LP
*
* Released under the BSD3-Clause license
* Date: 2014-07-16
*/
/**
* @param {*} any
* @param {Function} ctor
* @returns {boolean}
*/
function instanceOf(any, ctor) {
// validate constructor
if (typeof ctor !== 'function') {
return false
}
// validate with instanceof
if (any instanceof ctor) {
return true;
}
return !!(any && any.constructor && any.constructor.name === ctor.name);
}

module.exports.instanceOf = instanceOf;

2 changes: 1 addition & 1 deletion modules/@themost/query/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@themost/query",
"version": "2.3.3",
"version": "2.3.4",
"description": "MOST Web Framework 2.0 - query module",
"main": "index.js",
"peerDependencies": {
Expand Down
1 change: 1 addition & 0 deletions modules/@themost/query/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var Args = require('@themost/common').Args;
var _ = require('lodash');
var Symbol = require('symbol');
var aggregate = Symbol();
var instanceOf = require('./instance-of').instanceOf;
// eslint-disable-next-line no-unused-vars
//noinspection JSUnusedLocalSymbols
require('./natives');
Expand Down

0 comments on commit a8a123c

Please sign in to comment.