diff --git a/package-lock.json b/package-lock.json index c392774..8de5ce8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "rsql-query-builder-mongodb", - "version": "1.1.2", + "version": "1.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "rsql-query-builder-mongodb", - "version": "1.1.2", + "version": "1.2.0", "license": "MIT", "dependencies": { "rsql-query-builder": "^1.0.4" @@ -1905,9 +1905,9 @@ } }, "node_modules/rsql-query-builder": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/rsql-query-builder/-/rsql-query-builder-1.2.0.tgz", - "integrity": "sha512-2otMXgsWoLTOj1RK13OVp68szF0gJ8/ruSGOKuUg2EOJCtdW+PDnj7UQmkjX/OadEx9w5b3fIZiCU4jPJfQktw==", + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/rsql-query-builder/-/rsql-query-builder-1.3.0.tgz", + "integrity": "sha512-iGCIDtUP4mMj2sdlPPl7hWj9Te1DquNSR/FxpZCrjIDjpBnq6E9wkBa3LWOO9abpsdkVfm3KvM/KvwXI7TmC1Q==", "license": "MIT" }, "node_modules/semver": { diff --git a/package.json b/package.json index 0571c57..4a3d8d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rsql-query-builder-mongodb", - "version": "1.2.0", + "version": "1.3.0", "description": "Library for building RSQL query strings for MongoDB.", "main": "dist/index.js", "module": "dist/index.mjs", diff --git a/src/RSQLBuilderMongoDB.test.ts b/src/RSQLBuilderMongoDB.test.ts index aca4604..cab0bd0 100644 --- a/src/RSQLBuilderMongoDB.test.ts +++ b/src/RSQLBuilderMongoDB.test.ts @@ -1,22 +1,66 @@ import { describe, it, expect } from 'vitest'; -import { RSQLBuilderMongoDB } from './'; +import { MongoRegexOptions, RSQLBuilderMongoDB } from './'; describe('RSQLBuilderMongoDB', () => { it("Test operator REGEX ('=regex=')", () => { - expect(new RSQLBuilderMongoDB().regex('name', 'Filip').toString()).toBe('name=regex="Filip"'); + expect(new RSQLBuilderMongoDB().regex('name', 'Fil*').toString()).toBe('name=regex="Fil*"'); + expect(new RSQLBuilderMongoDB().regex('name', 'Fil*', 'si').toString()).toBe('name=regex="Fil*"=si'); + expect( + new RSQLBuilderMongoDB() + .regex('name', 'Fil*', [ + MongoRegexOptions.DOTALL, + MongoRegexOptions.CASE_INSENSITIVE, + MongoRegexOptions.MULTILINE, + MongoRegexOptions.EXTENDED + ]) + .toString() + ).toBe('name=regex="Fil*"=simx'); }); it("Test operator NOT REGEX ('=notregex=')", () => { expect(new RSQLBuilderMongoDB().notRegex('name', 'Filip').toString()).toBe('name=notregex="Filip"'); + expect(new RSQLBuilderMongoDB().notRegex('name', 'Fil*', 'si').toString()).toBe('name=notregex="Fil*"=si'); + expect( + new RSQLBuilderMongoDB() + .notRegex('name', 'Fil*', [ + MongoRegexOptions.DOTALL, + MongoRegexOptions.CASE_INSENSITIVE, + MongoRegexOptions.MULTILINE, + MongoRegexOptions.EXTENDED + ]) + .toString() + ).toBe('name=notregex="Fil*"=simx'); }); it("Test operator LIKE ('=regex=')", () => { expect(new RSQLBuilderMongoDB().like('name', 'Fil*').toString()).toBe('name=regex="Fil*"'); + expect(new RSQLBuilderMongoDB().like('name', 'Fil*', 'si').toString()).toBe('name=regex="Fil*"=si'); + expect( + new RSQLBuilderMongoDB() + .like('name', 'Fil*', [ + MongoRegexOptions.DOTALL, + MongoRegexOptions.CASE_INSENSITIVE, + MongoRegexOptions.MULTILINE, + MongoRegexOptions.EXTENDED + ]) + .toString() + ).toBe('name=regex="Fil*"=simx'); }); it("Test operator LIKE ('=notregex=')", () => { expect(new RSQLBuilderMongoDB().notLike('name', 'Fil*').toString()).toBe('name=notregex="Fil*"'); + expect(new RSQLBuilderMongoDB().notLike('name', 'Fil*', 'si').toString()).toBe('name=notregex="Fil*"=si'); + expect( + new RSQLBuilderMongoDB() + .notLike('name', 'Fil*', [ + MongoRegexOptions.DOTALL, + MongoRegexOptions.CASE_INSENSITIVE, + MongoRegexOptions.MULTILINE, + MongoRegexOptions.EXTENDED + ]) + .toString() + ).toBe('name=notregex="Fil*"=simx'); }); it("Test operator EXISTS ('=exists=')", () => { diff --git a/src/RSQLBuilderMongoDB.ts b/src/RSQLBuilderMongoDB.ts index 2ba7cf4..5fe138e 100644 --- a/src/RSQLBuilderMongoDB.ts +++ b/src/RSQLBuilderMongoDB.ts @@ -4,6 +4,13 @@ import { RSQLBuilderBase, RSQLBuilderOptions } from 'rsql-query-builder'; type ComparisonOperator = 'regex' | 'notRegex' | 'exists'; +export enum MongoRegexOptions { + CASE_INSENSITIVE = 'i', // Ignore case + MULTILINE = 'm', // Multiline mode + DOTALL = 's', // Dot matches newline + EXTENDED = 'x' // Ignore whitespace and allow comments +} + /** RSQL Query Builder for MongoDB. * * @template Selector - The type of the selector. It is used to define the selector names and is a list of strings. @@ -28,8 +35,16 @@ class RSQLBuilderMongoDB extends RSQLBuilderBase { - super.addComparison(selector, 'regex', regex); + public regex( + selector: TSelector, + regex: string, + options?: string | Array + ): RSQLBuilderMongoDB { + if (Array.isArray(options)) { + options = options.join(''); + } + + super.addComparison(selector, 'regex', regex, options); return this; } @@ -42,8 +57,16 @@ class RSQLBuilderMongoDB extends RSQLBuilderBase { - super.addComparison(selector, 'notRegex', regex); + public notRegex( + selector: TSelector, + regex: string, + options?: string | Array + ): RSQLBuilderMongoDB { + if (Array.isArray(options)) { + options = options.join(''); + } + + super.addComparison(selector, 'notRegex', regex, options); return this; } @@ -56,7 +79,15 @@ class RSQLBuilderMongoDB extends RSQLBuilderBase { + public like( + selector: TSelector, + regex: string, + options?: string | Array + ): RSQLBuilderMongoDB { + if (Array.isArray(options)) { + options = options.join(''); + } + return this.regex(selector, regex, options); } @@ -69,7 +100,15 @@ class RSQLBuilderMongoDB extends RSQLBuilderBase { + public notLike( + selector: TSelector, + regex: string, + options?: string | Array + ): RSQLBuilderMongoDB { + if (Array.isArray(options)) { + options = options.join(''); + } + return this.notRegex(selector, regex, options); } diff --git a/src/index.ts b/src/index.ts index 7f44029..230fb6f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1 @@ -export {default as RSQLBuilderMongoDB} from './RSQLBuilderMongoDB'; \ No newline at end of file +export { default as RSQLBuilderMongoDB, MongoRegexOptions } from './RSQLBuilderMongoDB';