mongodb-query-util
Advanced tools
Comparing version 0.0.0-next-514d0059aae41f2aa9cb0bbe6e204dacbac3d73f to 0.0.0-next-516eced55d345dc219ea74b01bd1f493443fc9b6
@@ -19,2 +19,3 @@ "use strict"; | ||
} | ||
// for all others, use native comparisons | ||
return undefined; | ||
@@ -21,0 +22,0 @@ }; |
@@ -0,2 +1,8 @@ | ||
/** | ||
* returns an array of all distinct values in a field (equality or $in) | ||
* | ||
* @param {Any} field the right-hand side of a document field | ||
* @return {Boolean} array of values for this field | ||
*/ | ||
export declare const getDistinctValues: (field?: any) => any; | ||
//# sourceMappingURL=get-distinct-values.d.ts.map |
@@ -5,6 +5,14 @@ "use strict"; | ||
const lodash_1 = require("lodash"); | ||
/** | ||
* returns an array of all distinct values in a field (equality or $in) | ||
* | ||
* @param {Any} field the right-hand side of a document field | ||
* @return {Boolean} array of values for this field | ||
*/ | ||
const getDistinctValues = (field) => { | ||
// field not present, return empty array | ||
if (field === undefined) { | ||
return []; | ||
} | ||
// field is object, could be a $in clause or a primitive value | ||
if ((0, lodash_1.isPlainObject)(field)) { | ||
@@ -15,2 +23,3 @@ if ((0, lodash_1.has)(field, '$in')) { | ||
} | ||
// it is not a $in operator, return single value as array | ||
return [field]; | ||
@@ -17,0 +26,0 @@ }; |
@@ -0,1 +1,8 @@ | ||
/** | ||
* Determines if a field in the query has a distinct value (equality or $in). | ||
* | ||
* @param {Any} field the right-hand side of a document field | ||
* @param {Any} value the value to check | ||
* @return {Boolean} whether or not value is included in field | ||
*/ | ||
export declare const hasDistinctValue: (field: { | ||
@@ -2,0 +9,0 @@ $in: any; |
@@ -6,8 +6,18 @@ "use strict"; | ||
const bson_equal_1 = require("./bson-equal"); | ||
/** | ||
* Determines if a field in the query has a distinct value (equality or $in). | ||
* | ||
* @param {Any} field the right-hand side of a document field | ||
* @param {Any} value the value to check | ||
* @return {Boolean} whether or not value is included in field | ||
*/ | ||
const hasDistinctValue = (field, value) => { | ||
// field not present, add primitive value | ||
if (field === undefined) { | ||
return false; | ||
} | ||
// field is object, could be a $in clause or a primitive value | ||
if ((0, lodash_1.isPlainObject)(field)) { | ||
if ((0, lodash_1.has)(field, '$in')) { | ||
// check if $in array contains the value | ||
const inArray = field.$in; | ||
@@ -19,2 +29,3 @@ return (0, lodash_1.some)(inArray, (other) => { | ||
} | ||
// it is not a $in operator, check value directly | ||
return (0, lodash_1.isEqualWith)(field, value, bson_equal_1.bsonEqual); | ||
@@ -21,0 +32,0 @@ }; |
@@ -0,1 +1,23 @@ | ||
/** | ||
* returns whether a value is fully or partially covered by a range, | ||
* specified with $gt(e)/$lt(e). Ranges can be open ended on either side, | ||
* and can also be single equality queries, e.g. {"field": 16}. | ||
* | ||
* @examples | ||
* inValueRange(15, {value: 15, dx: 0}) => 'yes' | ||
* inValueRange({$gte: 15, $lt: 30}, {value: 20, dx: 5}) => 'yes' | ||
* inValueRange({$gte: 15, $lt: 30}, {value: 15, dx: 5}) => 'yes' | ||
* inValueRange(15, {value: 15, dx: 1}) => 'partial' | ||
* inValueRange({$gt: 15, $lt: 30}, {value: 15, dx: 5}) => 'partial' | ||
* inValueRange({$gte: 15, $lt: 30}, {value: 20, dx: 20}) => 'partial' | ||
* inValueRange({$gte: 15, $lt: 30}, {value: 10, dx: 10}) => 'partial' | ||
* inValueRange({$gte: 15, $lt: 30}, {value: 10, dx: 5}) => 'partial' | ||
* inValueRange({$gt: 15, $lt: 30}, {value: 10, dx: 5}) => 'no' | ||
* inValueRange({$gte: 15, $lt: 30}, {value: 10, dx: 4}) => 'no' | ||
* | ||
* @param {Object|number} field the field value (range or number) | ||
* @param {Object} d object with a `value` and `dx` field if | ||
* the value represents a binned range itself | ||
* @return {String} 'yes', 'partial', 'no' | ||
*/ | ||
export declare const inValueRange: (field: any, d: { | ||
@@ -2,0 +24,0 @@ value: any; |
@@ -6,2 +6,24 @@ "use strict"; | ||
const bson_equal_1 = require("./bson-equal"); | ||
/** | ||
* returns whether a value is fully or partially covered by a range, | ||
* specified with $gt(e)/$lt(e). Ranges can be open ended on either side, | ||
* and can also be single equality queries, e.g. {"field": 16}. | ||
* | ||
* @examples | ||
* inValueRange(15, {value: 15, dx: 0}) => 'yes' | ||
* inValueRange({$gte: 15, $lt: 30}, {value: 20, dx: 5}) => 'yes' | ||
* inValueRange({$gte: 15, $lt: 30}, {value: 15, dx: 5}) => 'yes' | ||
* inValueRange(15, {value: 15, dx: 1}) => 'partial' | ||
* inValueRange({$gt: 15, $lt: 30}, {value: 15, dx: 5}) => 'partial' | ||
* inValueRange({$gte: 15, $lt: 30}, {value: 20, dx: 20}) => 'partial' | ||
* inValueRange({$gte: 15, $lt: 30}, {value: 10, dx: 10}) => 'partial' | ||
* inValueRange({$gte: 15, $lt: 30}, {value: 10, dx: 5}) => 'partial' | ||
* inValueRange({$gt: 15, $lt: 30}, {value: 10, dx: 5}) => 'no' | ||
* inValueRange({$gte: 15, $lt: 30}, {value: 10, dx: 4}) => 'no' | ||
* | ||
* @param {Object|number} field the field value (range or number) | ||
* @param {Object} d object with a `value` and `dx` field if | ||
* the value represents a binned range itself | ||
* @return {String} 'yes', 'partial', 'no' | ||
*/ | ||
const inValueRange = (field, d) => { | ||
@@ -31,2 +53,3 @@ const compOperators = { | ||
if (!(0, lodash_1.isPlainObject)(field)) { | ||
// add an equality condition | ||
conditions.push(function (a) { | ||
@@ -41,2 +64,3 @@ return (0, lodash_1.isEqualWith)(a, field, bson_equal_1.bsonEqual); | ||
if (comparator) { | ||
// add comparison condition(s), right-curried with the value of the query | ||
conditions.push((0, lodash_1.curryRight)(comparator)(value)); | ||
@@ -48,4 +72,32 @@ edgeCase.push(value); | ||
const dx = (0, lodash_1.get)(d, 'dx', null); | ||
// extract bound(s): if a dx value is set, create a 2-element array of | ||
// upper and lower bound. otherwise create a single value array of the | ||
// original bson type (if available) or the extracted value. | ||
// NOTE: this package is a copy of https://github.com/mongodb-js/mongodb-query-util/tree/main. | ||
// Once we converted the code to typescript, eslint started to complain about the fact | ||
// that the sum is not happening always between numbers. | ||
// From tests, it seems expected for d.value to also be a BSON type, and the sum | ||
// here may not make sense. | ||
// Potentially there could be bugs deriving from it, but tests are passing and | ||
// is not easy to tell what is the original intent here, so we suppress the eslint warning. | ||
// | ||
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands | ||
const bounds = dx ? (0, lodash_1.uniq)([d.value, d.value + dx]) : [d.bson || d.value]; | ||
/* | ||
* Logic to determine if the query covers the value (or value range) | ||
* | ||
* if all bounds pass all conditions, the value is fully covered in the range (yes) | ||
* if one of two bounds passes all conditions, the value is partially covered (partial) | ||
* if none of the bounds pass all conditions, the value is not covered (no) | ||
* | ||
* Since the upper bound of a bar represents the exclusive bound | ||
* (i.e. lower <= x < upper) we need to use a little hack to adjust for | ||
* the math. This means that if someone adjusts the query bound manually by | ||
* less than 1 millionth of the value, one of the bars may appear half | ||
* selected instead of not/fully selected. The error is purely visual. | ||
*/ | ||
const results = (0, lodash_1.map)(bounds, function (bound, i) { | ||
// adjust the upper bound slightly as it represents an exclusive bound | ||
// getting this perfectly right would require a lot more code to check for | ||
// all 4 edge cases. | ||
if (i > 0) { | ||
@@ -64,2 +116,3 @@ bound -= 0.00001 * Math.abs(bound) + 0.00001; | ||
} | ||
// check for edge case where range wraps around query on both ends | ||
if ((0, lodash_1.every)(edgeCase, function (val) { | ||
@@ -66,0 +119,0 @@ return val > bounds[0] && val < bounds[bounds.length - 1]; |
@@ -16,3 +16,3 @@ { | ||
"homepage": "https://github.com/mongodb-js/compass", | ||
"version": "0.0.0-next-514d0059aae41f2aa9cb0bbe6e204dacbac3d73f", | ||
"version": "0.0.0-next-516eced55d345dc219ea74b01bd1f493443fc9b6", | ||
"repository": { | ||
@@ -54,6 +54,6 @@ "type": "git", | ||
"devDependencies": { | ||
"@mongodb-js/eslint-config-compass": "0.0.0-next-514d0059aae41f2aa9cb0bbe6e204dacbac3d73f", | ||
"@mongodb-js/mocha-config-compass": "0.0.0-next-514d0059aae41f2aa9cb0bbe6e204dacbac3d73f", | ||
"@mongodb-js/prettier-config-compass": "0.0.0-next-514d0059aae41f2aa9cb0bbe6e204dacbac3d73f", | ||
"@mongodb-js/tsconfig-compass": "0.0.0-next-514d0059aae41f2aa9cb0bbe6e204dacbac3d73f", | ||
"@mongodb-js/eslint-config-compass": "0.0.0-next-516eced55d345dc219ea74b01bd1f493443fc9b6", | ||
"@mongodb-js/mocha-config-compass": "0.0.0-next-516eced55d345dc219ea74b01bd1f493443fc9b6", | ||
"@mongodb-js/prettier-config-compass": "0.0.0-next-516eced55d345dc219ea74b01bd1f493443fc9b6", | ||
"@mongodb-js/tsconfig-compass": "0.0.0-next-516eced55d345dc219ea74b01bd1f493443fc9b6", | ||
"@types/chai": "^4.2.21", | ||
@@ -73,6 +73,6 @@ "@types/mocha": "^9.0.0", | ||
"dependencies": { | ||
"bson": "^6.7.0", | ||
"bson": "^6.8.0", | ||
"lodash": "^4.17.21" | ||
}, | ||
"gitHead": "514d0059aae41f2aa9cb0bbe6e204dacbac3d73f" | ||
"gitHead": "516eced55d345dc219ea74b01bd1f493443fc9b6" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
50944
264
Updatedbson@^6.8.0