Skip to content

Commit

Permalink
feat($type): Use 'regexp' as type name when strict mode is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
kofrasa committed Dec 4, 2024
1 parent 3632b80 commit ecfff7b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
- Export default update function and factory in module index.
- Improve performance of `$bucketAuto` by removing excessive hashing.
- Simplify API for adding operators in Context class.
- Deprecate `updateObject(..)` in favour of `update(..)`.
- Renamed `updateObject(..)` to `update(..)`.
- Remove optimistic string representation on custom type without explicit `toString()` method.
- Expression operator `$type` returns `"regexp"` instead of `"regex"` when `useStrictMode`=`false`.

**Fixes**

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ Query and aggregation operations can be configured with options to enabled diffe
| idKey | The key that is used to lookup the ID value of a document. | "\_id" | |
| collation | [Collation](http://kofrasa.github.io/mingo/interfaces/core.CollationSpec.html) specification for string sorting operations. | _none_ | See [Intl.Collator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator) |
| processingMode | Determines copy rules for inputs and outputs. | [CLONE_OFF](http://kofrasa.github.io/mingo/enums/core.ProcessingMode.html) | Turn off cloning and modifies the input collection as needed. <br>This option will also return output objects with shared paths in their graph when specific operators are used. Provides the greatest speedup by minizing cloning. When using the aggregation pipeline, you can use the `$out` operator to collect immutable intermediate results. |
| useStrictMode | Enforces strict MongoDB compatibilty. | true | When disabled, behaviour changes as follows. <ul><li>`$elemMatch` returns all matching nested documents instead of only the first.</li><li>Empty string `""` is coerced to false during boolean checking in supported operators which is consistent with Javascript semantics.</li><li>`$type` returns JS native type names `"undefined"`, `"boolean"`, and `"number"` instead of `"missing"`,`"bool"`, and `"int","long","double"`.</li><ul> |
| useStrictMode | Enforces strict MongoDB compatibilty. | true | When disabled, behaviour changes as follows. <ul><li>`$elemMatch` returns all matching nested documents instead of only the first.</li><li>Empty string `""` is coerced to false during boolean checking in supported operators which is consistent with Javascript semantics.</li><li>`$type` returns JS native type names as follows. <ul><li>`"missing"` -> `"undefined"`</li><li>`"bool"` -> `"boolean"`</li><li> `"int"\|"long"\|"double"` -> `"number"`</li><li>`"regex"` -> `"regexp"`</li></ul><ul> |
| scriptEnabled | Enable or disable using custom script execution. | true | When disabled, operators that execute custom code are disallowed such as; `$where`, `$accumulator`, and `$function`. |
| hashFunction | Custom hash function to replace the default based on "Effective Java" hashCode. | _default_ | Expects function `(value: unknown) => number`. |
| collectionResolver | Function to resolve strings to arrays for use with operators that reference other collections such as; `$lookup`, `$out` and `$merge`. | _none_ | Expects function `(name: string) => AnyObject[]` |
Expand Down
3 changes: 2 additions & 1 deletion src/operators/expression/type/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { computeValue, ExpressionOperator, Options } from "../../../core";
import { Any, AnyObject } from "../../../types";
import { isNumber, MAX_INT, MIN_INT, typeOf } from "../../../util";
import { isNumber, isRegExp, MAX_INT, MIN_INT, typeOf } from "../../../util";

export const $type: ExpressionOperator = (
obj: AnyObject,
Expand All @@ -19,6 +19,7 @@ export const $type: ExpressionOperator = (
if (v % 1 != 0) return "double";
return v >= MIN_INT && v <= MAX_INT ? "int" : "long";
}
if (isRegExp(v)) return "regex";
}
return typeOf(v);
};

0 comments on commit ecfff7b

Please sign in to comment.