Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

quri

Package Overview
Dependencies
Maintainers
2
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

quri - npm Package Compare versions

Comparing version
1.0.1
to
1.0.2
+331
dist/Quri.js
'use strict';
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
Object.defineProperty(exports, "__esModule", {
value: true
});
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Enum for the possible operator string values.
*
* @readonly
* @enum {string}
*/
var operators = exports.operators = ['=', '==', '===', 'eq', '!=', '!==', 'neq', '>', 'gt', '>=', 'gte', '<', 'lt', '<=', 'lte', 'in', 'not_in', 'nin', 'like', 'between'];
/**
* Enum for the logical conjunctions used to join expressions.
*
* @readonly
* @enum {string}
*/
var conjunctions = exports.conjunctions = ['and', 'or'];
var defaultConjunction = exports.defaultConjunction = 'and';
/**
* A class to wrap a QURI string creation.
*
* @class Quri
*/
var Quri = function () {
/**
* Quri constructor
*
* @param {conjunctions} conjunction - Sets the logical conjunction (and|or)
* for all expressions at this level.
*/
function Quri() {
var conjunction = arguments.length <= 0 || arguments[0] === undefined ? defaultConjunction : arguments[0];
_classCallCheck(this, Quri);
this._conjunction = conjunction;
this._criteria = [];
}
/**
* Return the logical conjunction (and|or) for this Quri instance.
*
* @returns {conjunctions}
*/
_createClass(Quri, [{
key: 'appendExpression',
/**
* Appends a new expression.
*
* @example appendExpression('customer_name', 'like', 'Greg%')
*
* @param {string} fieldName - Field name.
* @param {operators} operator - The operator string.
* @param {string|number|Array} value - Value(s) that match the operator.
* The only operators that allow array values are in|nin.
* @returns {Quri}
*/
value: function appendExpression(fieldName, operator, value) {
this._criteria.push({
fieldName: fieldName,
operator: operator,
value: value
});
return this;
}
/**
* Appends a criteria object to the current criteria array.
*
* @param {Quri|object|string} criteria - A Quri instance, or any object
* with a toString method.
* @returns {Quri}
*/
}, {
key: 'appendCriteria',
value: function appendCriteria(criteria) {
this._criteria.push({
criteria: criteria
});
return this;
}
/**
* Alias for appendCriteria.
*
* @param {Quri} quri - A Quri instance.
* @returns {Quri}
*/
}, {
key: 'appendQuri',
value: function appendQuri(quri) {
return this.appendCriteria(quri);
}
/**
* Returns the formatted QURI string.
*
* @returns {string}
*/
}, {
key: 'toString',
value: function toString() {
var criteriaMap = this._criteria.map(function (item) {
if (item.criteria != null) {
return '(' + item.criteria.toString() + ')';
}
var fieldNameString = JSON.stringify(item.fieldName);
var operatorString = Quri.operatorToString(item.operator);
var valueString = JSON.stringify(item.value);
if (valueString.charAt(0) === '[') {
// If it's an array we need to remove the [ ] from the outside.
valueString = valueString.substring(1, valueString.length - 1);
}
return fieldNameString + '.' + operatorString + '(' + valueString + ')';
});
return criteriaMap.join(this._conjunction === 'and' ? ',' : '|');
}
/**
* Exports the criteria as a plain JS object.
*
* @returns {object}
*/
}, {
key: 'toJS',
value: function toJS() {
var object = { criteria: [] };
if (this._conjunction !== defaultConjunction) {
object.conjunction = this._conjunction;
}
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = this._criteria[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var item = _step.value;
if (item.criteria != null) {
object.criteria.push(item.criteria.toJS());
} else {
var fieldName = item.fieldName;
var operator = item.operator;
var value = item.value;
object.criteria.push([fieldName, operator, value]);
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return object;
}
/**
* Imports the criteria from a plain JS object
*
* @param {object} object - The input object
* @returns {Quri}
*/
}, {
key: 'conjunction',
get: function get() {
return this._conjunction;
}
/**
* Change the logical conjunction (and|or) for all expressions.
*
* @param {conjunctions} conjunction - Sets the logical conjunction (and|or)
* for all expressions at this level.
*/
,
set: function set() {
var conjunction = arguments.length <= 0 || arguments[0] === undefined ? defaultConjunction : arguments[0];
this._conjunction = conjunction;
}
}], [{
key: 'fromJS',
value: function fromJS(object) {
var quri = new Quri(object.conjunction);
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = object.criteria[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var item = _step2.value;
if (item.criteria) {
// Assume objects with a criteria property
// should become Quri instances.
var innerQuri = Quri.fromJS(item);
quri.appendQuri(innerQuri);
} else if (item.length === 3) {
// Assume an iterable with 3 items is an expression.
var _item = _slicedToArray(item, 3);
var fieldName = _item[0];
var operator = _item[1];
var value = _item[2];
quri.appendExpression(fieldName, operator, value);
} else {
// Assume anything else is a criteria object.
quri.appendCriteria(item);
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
return quri;
}
/**
* Takes the operator string from the user and returns the corresponding
* string in the QURI specificiation.
*
* @example operatorToString('=='); // 'eq'
*
* @param {operators} operator - A valid operator string.
* @returns {string}
* @see operators
*/
}, {
key: 'operatorToString',
value: function operatorToString(operator) {
switch (operator) {
case '=':
case '==':
case '===':
case 'eq':
return 'eq';
case '!=':
case '!==':
case 'neq':
return 'neq';
case '>':
case 'gt':
return 'gt';
case '>=':
case 'gte':
return 'gte';
case '<':
case 'lt':
return 'lt';
case '<=':
case 'lte':
return 'lte';
case 'in':
return 'in';
case 'not_in':
case 'nin':
return 'nin';
case 'like':
return 'like';
case 'between':
return 'between';
default:
throw new Error('Unsupported operator \'' + operator + '\'');
}
}
}]);
return Quri;
}();
exports.default = Quri;
+4
-132
'use strict';
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
Object.defineProperty(exports, "__esModule", {

@@ -9,134 +7,8 @@ value: true

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var _Quri = require('./Quri');
/**
* @class Criteria
*
* A class to wrap a QURI string creation
*/
var _Quri2 = _interopRequireDefault(_Quri);
var Criteria = (function () {
/**
* Criteria constructor
*
* @param andOr Sets the and|or for all expressions at this level
*/
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function Criteria() {
var andOr = arguments.length <= 0 || arguments[0] === undefined ? 'and' : arguments[0];
_classCallCheck(this, Criteria);
this.andOr = andOr;
this.criteria = [];
}
/**
* Appends a new expression.
*
* @example appendExpression('customer_name', 'like', 'Greg%')
*
* @param {string} fieldName Field name
* @param {string} operator String '==', '!=', '>', '>=', '<', '<=', 'in', 'not_in', 'like', 'between'
* @param {string,number,Array} value Value(s) that match the operator.
* The only operators that allow array values are in|out|nin
*/
_createClass(Criteria, [{
key: 'appendExpression',
value: function appendExpression(fieldName, operator, value) {
this.criteria.push({
fieldName: fieldName,
operator: operator,
value: value
});
}
/**
* Appends a criteria object to the current criteria array.
* The criteria object may be a new Criteria class or any object with a toString method.
*
* @param {Criteria} criteria
*/
}, {
key: 'appendCriteria',
value: function appendCriteria(criteria) {
this.criteria.push({
criteria: criteria
});
}
/**
* Returns the formatted QURI string
*
* @returns {string}
*/
}, {
key: 'toString',
value: function toString() {
var criteriaMap = this.criteria.map(function (expression) {
if (expression.hasOwnProperty('criteria')) {
return '(' + expression.criteria.toString() + ')';
}
var operator = Criteria.operatorToString(expression.operator);
var valueStr = JSON.stringify(expression.value);
if (valueStr.charAt(0) == '[') {
// If it's an array we need to remove the [ ] from the outside.
valueStr = valueStr.substring(1, valueStr.length - 1);
}
return JSON.stringify(expression.fieldName) + '.' + operator + '(' + valueStr + ')';
});
return criteriaMap.join(this.andOr == 'and' ? ',' : '|');
}
/**
* Takes the operator string from the user and returns the corresponding string in the QURI specificiation.
*
* @example operatorToString('=='); // 'eq'
*
* @param {string} operatorStr
* @returns {string}
*/
}], [{
key: 'operatorToString',
value: function operatorToString(operatorStr) {
switch (operatorStr) {
case '=':
case '==':
case 'eq':
return 'eq';
case '!=':
case 'neq':
return 'neq';
case '>':
case 'gt':
return 'gt';
case '>=':
case 'gte':
return 'gte';
case '<':
case 'lt':
return 'lt';
case '<=':
case 'lte':
return 'lte';
case 'in':
return 'in';
case 'not_in':
case 'nin':
return 'nin';
case 'like':
return 'like';
case 'between':
return 'between';
}
}
}]);
return Criteria;
})();
exports.default = Criteria;
exports.default = _Quri2.default;
{
"name": "quri",
"version": "1.0.1",
"version": "1.0.2",
"description": "JS wrapper for the QURI string specification",

@@ -53,2 +53,3 @@ "main": "dist/index.js",

"eslint": "*",
"eslint-config-airbnb": "^4.0.0",
"isparta": "*",

@@ -55,0 +56,0 @@ "rimraf": "*",

+11
-11

@@ -14,3 +14,3 @@ # quri-js

```sh
npm i -D quri
npm install quri
```

@@ -27,7 +27,7 @@

```js
import Criteria from "../src/index.js";
import quri from "quri";
let criteria = new Criteria();
let quri = new quri();
criteria.toString(); // "field_1".eq("my value")
quri.toString(); // "field_1".eq("my value")
```

@@ -38,12 +38,12 @@

```js
let criteria = new Criteria();
criteria.appendExpression('field_1', '==', 'outer');
let quri = new quri();
quri.appendExpression('field_1', '==', 'outer');
let nestedCriteria = new Criteria('or');
nestedCriteria.appendExpression('field_2', 'like', 'nested%');
nestedCriteria.appendExpression('field_3', 'in', [1,2,3,4]);
let nestedQuri = new quri('or');
nestedQuri.appendExpression('field_2', 'like', 'nested%');
nestedQuri.appendExpression('field_3', 'in', [1,2,3,4]);
criteria.appendCriteria(nestedCriteria);
quri.appendCriteria(nestedQuri);
criteria.toString();
quri.toString();
// "field_1".eq("outer"),("field_2".eq("nested%")|"field_3".in(1,2,3,4))

@@ -50,0 +50,0 @@ ```