Skip to content

Commit

Permalink
Merge branch 'master' into expand
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaspoignant authored Apr 19, 2024
2 parents eb8c2b6 + 23ad57f commit 4674189
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scim2-parse-filter",
"version": "0.2.9",
"version": "0.2.10",
"description": "This is a fork https://www.npmjs.com/package/scim2-filter version 0.2.0 with bug correction.",
"main": "lib/src/index.js",
"directories": {
Expand Down
4 changes: 2 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ export type Token = {
export function tokenizer(f: string): Token[] {
const ret: Token[] = [];
let rest = f;
const patterns = /^(?:(\s+)|(-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?(?![-\w._:\/\)\s]))|("(?:[^"]|\\.|\n)*")|([[()]|]\.?)|(\w[-\w._:\/%]*))/;
const patterns = /^(?:(\s+)|(-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?(?![-\w._:\/\)\s]))|("(?:[^"\\]|\\.|\n)*")|([[()]|]\.?)|(\w[-\w._:\/%]*))/;
let n;
while ((n = patterns.exec(rest))) {
if (n[1] || n[0].length === 0) {
//
} else if (n[2]) {
ret.push({ literal: n[2], type: "Number" });
} else if (n[3]) {
const literal = n[3].replace(/\\/g, '\\\\');
const literal = n[3].replace(/\\(?!")/g, '\\\\');
ret.push({ literal, type: "Quoted" });
} else if (n[4]) {
ret.push({ literal: n[4], type: "Bracket" });
Expand Down
51 changes: 30 additions & 21 deletions test/tokenizer.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import "mocha";
import { tokenizer, Token } from "../src/parser";
import { EOT } from './test_util';
import { EOT } from "./test_util";
import chai = require("chai");
const assert = chai.assert;

describe("tokenizer", () => {
const tok = (literal: string, type: string) => ({ literal, type } as Token);
const tok = (literal: string, type: string) => ({ literal, type }) as Token;

it("eot", () => {
assert.deepEqual(tokenizer(""), [EOT]);
Expand All @@ -14,46 +14,55 @@ describe("tokenizer", () => {
it("false", () => {
assert.deepEqual(tokenizer("false"), [
{ literal: "false", type: "Word" },
EOT
EOT,
]);
});

it("userName is AttrPath", () => {
assert.deepEqual(tokenizer("userName"), [
{ literal: "userName", type: "Word" },
EOT
EOT,
]);
});

it("userName eq -12", () => {
assert.deepEqual(
[tok("userName", "Word"), tok("eq", "Word"), tok("-12", "Number"), EOT],
tokenizer("userName eq -12")
tokenizer("userName eq -12"),
);
});

it("0Field1 eq -12", () => {
assert.deepEqual(
[tok("0Field1", "Word"), tok("eq", "Word"), tok("-12", "Number"), EOT],
tokenizer("0Field1 eq -12")
tokenizer("0Field1 eq -12"),
);
});

it("sub-attribute after ValPath", () => {
assert.deepEqual(
tokenizer('emails[type eq "work"].value eq "[email protected]"'),
[
tok("emails", "Word"),
tok("[", "Bracket"),
tok("type", "Word"),
tok("eq", "Word"),
tok("\"work\"", "Quoted"),
tok("].", "Bracket"),
tok("value", "Word"),
tok("eq", "Word"),
tok("\"[email protected]\"", "Quoted"),
EOT,
]
)
})
tokenizer('emails[type eq "work"].value eq "[email protected]"'),
[
tok("emails", "Word"),
tok("[", "Bracket"),
tok("type", "Word"),
tok("eq", "Word"),
tok('"work"', "Quoted"),
tok("].", "Bracket"),
tok("value", "Word"),
tok("eq", "Word"),
tok('"[email protected]"', "Quoted"),
EOT,
],
);
});

it("support of quoted values", () => {
assert.deepEqual(tokenizer('displayName eq "Alice \\"and\\" Bob"'), [
tok("displayName", "Word"),
tok("eq", "Word"),
tok('"Alice \\"and\\" Bob"', "Quoted"),
EOT,
]);
});
});

0 comments on commit 4674189

Please sign in to comment.