Comparing version 1.5.0 to 1.5.1
35
index.js
@@ -8,19 +8,7 @@ /*jshint browserify: true */ | ||
QB = {}, | ||
bind, | ||
toArray, | ||
warn; | ||
toArray = Function.prototype.call.bind(Array.prototype.slice); | ||
bind = (typeof Function.prototype.bind === 'function' ? ( | ||
Function.prototype.call.bind(Function.prototype.bind) | ||
) : ( | ||
function (fn, self) { | ||
return function () { | ||
return fn.apply(self, arguments); | ||
}; | ||
} | ||
)); | ||
toArray = bind(Function.prototype.call, Array.prototype.slice); | ||
warn = (function () { | ||
@@ -38,7 +26,6 @@ if (typeof console !== 'undefined') { | ||
for (var key in types._PartialStatement.prototype) { | ||
if (types._PartialStatement.prototype.hasOwnProperty(key) && key !== 'constructor') { | ||
QB[key] = bind(types._PartialStatement.prototype[key], null); | ||
} | ||
} | ||
Object.keys(types._PartialStatement.prototype).forEach(function (key) { | ||
if (key === 'constructor') return; | ||
QB[key] = types._PartialStatement.prototype[key].bind(null); | ||
}); | ||
@@ -117,11 +104,9 @@ QB.bool = function (value) {return new types.BooleanLiteral(value);}; | ||
for (var key in assumptions.builtins) { | ||
if (assumptions.builtins.hasOwnProperty(key)) { | ||
QB[key] = QB.fn(key, assumptions.builtins[key]); | ||
if (assumptions.deprecatedBuiltins.indexOf(key) !== -1) { | ||
QB[key] = deprecateAqlFunction(QB[key], key); | ||
} | ||
Object.keys(assumptions.builtins).forEach(function (key) { | ||
QB[key] = QB.fn(key, assumptions.builtins[key]); | ||
if (assumptions.deprecatedBuiltins.indexOf(key) !== -1) { | ||
QB[key] = deprecateAqlFunction(QB[key], key); | ||
} | ||
} | ||
}); | ||
module.exports = QB; |
{ | ||
"name": "aqb", | ||
"version": "1.5.0", | ||
"version": "1.5.1", | ||
"description": "ArangoDB AQL query builder.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,3 +7,3 @@ # ArangoDB Query Builder | ||
![NPM status](https://nodei.co/npm/aqb.png?downloads=true&stars=true) | ||
[![NPM status](https://nodei.co/npm/aqb.png?downloads=true&stars=true)](https://npmjs.org/package/aqb) | ||
@@ -10,0 +10,0 @@ [![Build status](https://img.shields.io/travis/arangodb/aqbjs.svg)](https://travis-ci.org/arangodb/aqbjs) [![Coverage Status](https://img.shields.io/coveralls/arangodb/aqbjs.svg)](https://coveralls.io/r/arangodb/aqbjs?branch=master) [![Codacy rating](https://img.shields.io/codacy/1d5a1be201024e0caaf0aa6bc990231e.svg)](https://www.codacy.com/public/me_4/aqbjs) |
232
types.js
/*jshint browserify: true */ | ||
'use strict'; | ||
var AqlError = require('./errors').AqlError, | ||
keywords = require('./assumptions').keywords; | ||
var AqlError = require('./errors').AqlError; | ||
var keywords = require('./assumptions').keywords; | ||
@@ -17,4 +17,49 @@ function wrapAQL(expr) { | ||
function isValidNumber(number) { | ||
return ( | ||
number === number && | ||
number !== Infinity && | ||
number !== -Infinity | ||
); | ||
} | ||
function castNumber(number) { | ||
if (Math.floor(number) === number) { | ||
return new IntegerLiteral(number); | ||
} | ||
return new NumberLiteral(number); | ||
} | ||
function castBoolean(bool) { | ||
return new BooleanLiteral(bool); | ||
} | ||
function castString(str) { | ||
if (str.match(NumberLiteral.re)) { | ||
return autoCastToken(Number(str)); | ||
} | ||
if (str.charAt(0) === '"') { | ||
return new StringLiteral(JSON.parse(str)); | ||
} | ||
var match = str.match(RangeExpression.re); | ||
if (match) { | ||
return new RangeExpression(Number(match[1]), Number(match[2])); | ||
} | ||
if (str.match(Identifier.re)) { | ||
return new Identifier(str); | ||
} | ||
return new SimpleReference(str); | ||
} | ||
function castObject(obj) { | ||
if (obj.constructor && obj.constructor.name === 'ArangoCollection') { | ||
return new Identifier(obj.name()); | ||
} | ||
if (Array.isArray(obj)) { | ||
return new ListLiteral(obj); | ||
} | ||
return new ObjectLiteral(obj); | ||
} | ||
function autoCastToken(token) { | ||
var match; | ||
if (token === null || token === undefined) { | ||
@@ -26,38 +71,12 @@ return new NullLiteral(); | ||
} | ||
if (typeof token === 'number' && token === token && token !== Infinity && token !== -Infinity) { | ||
if (Math.floor(token) === token) { | ||
return new IntegerLiteral(token); | ||
} | ||
return new NumberLiteral(token); | ||
var type = typeof token; | ||
if (Object.keys(autoCastToken).indexOf(type) === -1) { | ||
throw new AqlError('Invalid AQL value: (' + type + ') ' + token); | ||
} | ||
if (typeof token === 'boolean') { | ||
return new BooleanLiteral(token); | ||
} | ||
if (typeof token === 'string') { | ||
if (token.match(/^[-+]?[0-9]+(\.[0-9]+)?$/)) { | ||
return autoCastToken(Number(token)); | ||
} | ||
if (token.charAt(0) === '"') { | ||
return new StringLiteral(JSON.parse(token)); | ||
} | ||
match = token.match(/^([0-9]+)\.\.([0-9]+)$/); | ||
if (match) { | ||
return new RangeExpression(Number(match[1]), Number(match[2])); | ||
} | ||
if (token.match(Identifier.re)) { | ||
return new Identifier(token); | ||
} | ||
return new SimpleReference(token); | ||
} | ||
if (typeof token === 'object') { | ||
if (token.constructor && token.constructor.name === 'ArangoCollection') { | ||
return new Identifier(token.name()); | ||
} | ||
if (Object.prototype.toString.call(token) === '[object Array]') { | ||
return new ListLiteral(token); | ||
} | ||
return new ObjectLiteral(token); | ||
} | ||
throw new AqlError('Invalid AQL value: (' + (typeof token) + ') ' + token); | ||
return autoCastToken[type](token); | ||
} | ||
autoCastToken.number = castNumber; | ||
autoCastToken.boolean = castBoolean; | ||
autoCastToken.string = castString; | ||
autoCastToken.object = castObject; | ||
@@ -102,6 +121,7 @@ function Expression() {} | ||
this.value = Number(value); | ||
if (this.value !== this.value || this.value === Infinity) { | ||
if (!isValidNumber(this.value)) { | ||
throw new AqlError('Expected value to be a finite number: ' + value); | ||
} | ||
} | ||
NumberLiteral.re = /^[-+]?[0-9]+(\.[0-9]+)?$/; | ||
NumberLiteral.prototype = new Expression(); | ||
@@ -117,3 +137,3 @@ NumberLiteral.prototype.constructor = NumberLiteral; | ||
this.value = Number(value); | ||
if (this.value !== this.value || this.value === Infinity || Math.floor(this.value) !== this.value) { | ||
if (!isValidNumber(this.value) || Math.floor(this.value) !== this.value) { | ||
throw new AqlError('Expected value to be a finite integer: ' + value); | ||
@@ -137,9 +157,6 @@ } | ||
if (value && value instanceof ListLiteral) {value = value.value;} | ||
if (!value || Object.prototype.toString.call(value) !== '[object Array]') { | ||
if (!value || !Array.isArray(value)) { | ||
throw new AqlError('Expected value to be an array: ' + value); | ||
} | ||
this.value = []; | ||
for (var i = 0; i < value.length; i++) { | ||
this.value[i] = autoCastToken(value[i]); | ||
} | ||
this.value = value.map(autoCastToken); | ||
} | ||
@@ -149,6 +166,3 @@ ListLiteral.prototype = new Expression(); | ||
ListLiteral.prototype.toAQL = function () { | ||
var value = [], i; | ||
for (i = 0; i < this.value.length; i++) { | ||
value.push(wrapAQL(this.value[i])); | ||
} | ||
var value = this.value.map(wrapAQL); | ||
return '[' + value.join(', ') + ']'; | ||
@@ -163,7 +177,6 @@ }; | ||
this.value = {}; | ||
for (var key in value) { | ||
if (value.hasOwnProperty(key)) { | ||
this.value[key] = autoCastToken(value[key]); | ||
} | ||
} | ||
var self = this; | ||
Object.keys(value).forEach(function (key) { | ||
self.value[key] = autoCastToken(value[key]); | ||
}); | ||
} | ||
@@ -173,12 +186,10 @@ ObjectLiteral.prototype = new Expression(); | ||
ObjectLiteral.prototype.toAQL = function () { | ||
var items = [], key; | ||
for (key in this.value) { | ||
if (this.value.hasOwnProperty(key)) { | ||
if (key.match(Identifier.re) || key === String(Number(key))) { | ||
items.push(key + ': ' + wrapAQL(this.value[key])); | ||
} else { | ||
items.push(JSON.stringify(key) + ': ' + wrapAQL(this.value[key])); | ||
} | ||
var value = this.value; | ||
var items = Object.keys(value).map(function (key) { | ||
if (key.match(Identifier.re) || key === String(Number(key))) { | ||
return key + ': ' + wrapAQL(value[key]); | ||
} else { | ||
return JSON.stringify(key) + ': ' + wrapAQL(value[key]); | ||
} | ||
} | ||
}); | ||
return '{' + items.join(', ') + '}'; | ||
@@ -191,2 +202,3 @@ }; | ||
} | ||
RangeExpression.re = /^([0-9]+)\.\.([0-9]+)$/; | ||
RangeExpression.prototype = new Expression(); | ||
@@ -260,10 +272,6 @@ RangeExpression.prototype.constructor = RangeExpression; | ||
SimpleReference.prototype.toAQL = function () { | ||
var value = String(this.value), | ||
tokens = value.split('.'), | ||
i; | ||
for (i = 0; i < tokens.length; i++) { | ||
if (keywords.indexOf(tokens[i]) !== -1) { | ||
tokens[i] = '`' + tokens[i] + '`'; | ||
} | ||
} | ||
var value = String(this.value); | ||
var tokens = value.split('.').map(function (token) { | ||
return keywords.indexOf(token) === -1 ? token : '`' + token + '`'; | ||
}); | ||
return tokens.join('.'); | ||
@@ -329,6 +337,3 @@ }; | ||
this.operator = operator; | ||
this.values = []; | ||
for (var i = 0; i < values.length; i++) { | ||
this.values.push(autoCastToken(values[i])); | ||
} | ||
this.values = values.map(autoCastToken); | ||
} | ||
@@ -338,6 +343,3 @@ NAryOperation.prototype = new Operation(); | ||
NAryOperation.prototype.toAQL = function () { | ||
var values = [], i; | ||
for (i = 0; i < this.values.length; i++) { | ||
values.push(wrapAQL(this.values[i])); | ||
} | ||
var values = this.values.map(wrapAQL); | ||
return values.join(' ' + this.operator + ' '); | ||
@@ -353,12 +355,7 @@ }; | ||
} | ||
if (args && Object.prototype.toString.call(args) !== '[object Array]') { | ||
if (args && !Array.isArray(args)) { | ||
throw new AqlError('Expected arguments to be an array: ' + args); | ||
} | ||
this.functionName = functionName; | ||
this.args = []; | ||
if (args) { | ||
for (var i = 0; i < args.length; i++) { | ||
this.args[i] = autoCastToken(args[i]); | ||
} | ||
} | ||
this.args = args ? args.map(autoCastToken) : []; | ||
} | ||
@@ -369,6 +366,3 @@ FunctionCall.re = /^[_a-z][_0-9a-z]*(::[_a-z][_0-9a-z]*)*$/i; | ||
FunctionCall.prototype.toAQL = function () { | ||
var args = [], i; | ||
for (i = 0; i < this.args.length; i++) { | ||
args.push(wrapAQL(this.args[i])); | ||
} | ||
var args = this.args.map(wrapAQL); | ||
return this.functionName + '(' + args.join(', ') + ')'; | ||
@@ -455,18 +449,17 @@ }; | ||
this.dfns = []; | ||
var self = this; | ||
if (!dfns || typeof dfns !== 'object') { | ||
throw new AqlError('Expected definitions to be an object'); | ||
} | ||
if (Object.prototype.toString.call(dfns) === '[object Array]') { | ||
for (var i = 0; i < dfns.length; i++) { | ||
if (Object.prototype.toString.call(dfns[i]) !== '[object Array]' || dfns[i].length !== 2) { | ||
if (Array.isArray(dfns)) { | ||
dfns.forEach(function (dfn, i) { | ||
if (!Array.isArray(dfn) || dfn.length !== 2) { | ||
throw new AqlError('Expected definitions[' + i + '] to be a tuple'); | ||
} | ||
this.dfns.push([new Identifier(dfns[i][0]), autoCastToken(dfns[i][1])]); | ||
} | ||
self.dfns.push([new Identifier(dfn[0]), autoCastToken(dfn[1])]); | ||
}); | ||
} else { | ||
for (var key in dfns) { | ||
if (dfns.hasOwnProperty(key)) { | ||
this.dfns.push([new Identifier(key), autoCastToken(dfns[key])]); | ||
} | ||
} | ||
Object.keys(dfns).forEach(function (key) { | ||
self.dfns.push([new Identifier(key), autoCastToken(dfns[key])]); | ||
}); | ||
} | ||
@@ -478,7 +471,5 @@ if (this.dfns.length === 0) { | ||
Definitions.prototype.toAQL = function () { | ||
var dfns = []; | ||
for (var i = 0; i < this.dfns.length; i++) { | ||
dfns.push(this.dfns[i][0].toAQL() + ' = ' + wrapAQL(this.dfns[i][1])); | ||
} | ||
return dfns.join(', '); | ||
return this.dfns.map(function (dfn) { | ||
return dfn[0].toAQL() + ' = ' + wrapAQL(dfn[1]); | ||
}).join(', '); | ||
}; | ||
@@ -559,3 +550,3 @@ | ||
function SortExpression(prev, args) { | ||
if (!args || Object.prototype.toString.call(args) !== '[object Array]') { | ||
if (!args || !Array.isArray(args)) { | ||
throw new AqlError('Expected sort list to be an array: ' + args); | ||
@@ -568,20 +559,19 @@ } | ||
this.args = []; | ||
var allowKeyword = false, i, value; | ||
for (i = 0; i < args.length; i++) { | ||
value = args[i]; | ||
if (!allowKeyword && value) { | ||
if (value instanceof Keyword || ( | ||
typeof value === 'string' && SortExpression.keywords.indexOf(value.toUpperCase()) !== -1 | ||
var allowKeyword = false; | ||
this.args = args.map(function (arg, i) { | ||
if (!allowKeyword && arg) { | ||
if (arg instanceof Keyword || ( | ||
typeof arg === 'string' && SortExpression.keywords.indexOf(arg.toUpperCase()) !== -1 | ||
)) { | ||
throw new AqlError('Unexpected keyword ' + value.toString() + ' at offset ' + i); | ||
throw new AqlError('Unexpected keyword ' + arg.toString() + ' at offset ' + i); | ||
} | ||
} | ||
if (typeof value === 'string' && SortExpression.keywords.indexOf(value.toUpperCase()) !== -1) { | ||
this.args[i] = new Keyword(value); | ||
if (typeof arg === 'string' && SortExpression.keywords.indexOf(arg.toUpperCase()) !== -1) { | ||
allowKeyword = false; | ||
return new Keyword(arg); | ||
} else { | ||
this.args[i] = autoCastToken(value); | ||
allowKeyword = true; | ||
return autoCastToken(arg); | ||
} | ||
} | ||
}); | ||
} | ||
@@ -593,9 +583,9 @@ SortExpression.keywords = ['ASC', 'DESC']; | ||
var args = [], j = 0; | ||
for (var i = 0; i < this.args.length; i++) { | ||
if (this.args[i] instanceof Keyword) { | ||
args[j] += ' ' + this.args[i].toAQL(); | ||
this.args.forEach(function (arg, i) { | ||
if (arg instanceof Keyword) { | ||
args[j] += ' ' + arg.toAQL(); | ||
} else { | ||
j = args.push(wrapAQL(this.args[i])) - 1; | ||
j = args.push(wrapAQL(arg)) - 1; | ||
} | ||
} | ||
}); | ||
return ( | ||
@@ -602,0 +592,0 @@ (this.prev ? this.prev.toAQL() + ' ' : '') + |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
61716
953