mongodb-query-util
Advanced tools
Comparing version 0.0.0-next-411e5d6bd1f9dd27c41a57a6e146cbd8a598a426 to 0.0.0-next-42294867749aa34faf6b8e7c48d946d3f3cc2b5e
@@ -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-411e5d6bd1f9dd27c41a57a6e146cbd8a598a426", | ||
"version": "0.0.0-next-42294867749aa34faf6b8e7c48d946d3f3cc2b5e", | ||
"repository": { | ||
@@ -44,3 +44,3 @@ "type": "git", | ||
"lint": "npm run eslint . && npm run prettier -- --check .", | ||
"depcheck": "depcheck", | ||
"depcheck": "compass-scripts check-peer-deps && depcheck", | ||
"check": "npm run typecheck && npm run lint && npm run depcheck", | ||
@@ -55,10 +55,9 @@ "check-ci": "npm run check", | ||
"devDependencies": { | ||
"@mongodb-js/eslint-config-compass": "0.0.0-next-411e5d6bd1f9dd27c41a57a6e146cbd8a598a426", | ||
"@mongodb-js/mocha-config-compass": "0.0.0-next-411e5d6bd1f9dd27c41a57a6e146cbd8a598a426", | ||
"@mongodb-js/prettier-config-compass": "0.0.0-next-411e5d6bd1f9dd27c41a57a6e146cbd8a598a426", | ||
"@mongodb-js/tsconfig-compass": "0.0.0-next-411e5d6bd1f9dd27c41a57a6e146cbd8a598a426", | ||
"@mongodb-js/eslint-config-compass": "0.0.0-next-42294867749aa34faf6b8e7c48d946d3f3cc2b5e", | ||
"@mongodb-js/mocha-config-compass": "0.0.0-next-42294867749aa34faf6b8e7c48d946d3f3cc2b5e", | ||
"@mongodb-js/prettier-config-compass": "0.0.0-next-42294867749aa34faf6b8e7c48d946d3f3cc2b5e", | ||
"@mongodb-js/tsconfig-compass": "0.0.0-next-42294867749aa34faf6b8e7c48d946d3f3cc2b5e", | ||
"@types/chai": "^4.2.21", | ||
"@types/mocha": "^9.0.0", | ||
"@types/sinon-chai": "^3.2.5", | ||
"bson": "^6.2.0", | ||
"chai": "^4.3.6", | ||
@@ -75,5 +74,6 @@ "depcheck": "^1.4.1", | ||
"dependencies": { | ||
"bson": "^6.7.0", | ||
"lodash": "^4.17.21" | ||
}, | ||
"gitHead": "411e5d6bd1f9dd27c41a57a6e146cbd8a598a426" | ||
"gitHead": "42294867749aa34faf6b8e7c48d946d3f3cc2b5e" | ||
} |
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
16
264
2
+ Addedbson@^6.7.0
+ Addedbson@6.10.0(transitive)