Skip to content

Commit eb66159

Browse files
committed
feat(core basepattern): Allow to specify parser options on the pattern.
1 parent cacb743 commit eb66159

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

src/core/basepattern.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class BasePattern {
1616
static trigger; // A CSS selector to match elements that should trigger the pattern instantiation.
1717
static parser; // Options parser.
1818

19+
// Parser options
20+
parser_group_options = true;
21+
parser_multiple = undefined;
22+
parser_inherit = true;
23+
1924
constructor(el, options = {}) {
2025
// Make static variables available on instance.
2126
this.name = this.constructor.name;
@@ -68,7 +73,14 @@ class BasePattern {
6873

6974
// Create the options object by parsing the element and using the
7075
// optional options as default.
71-
this.options = this.parser?.parse(this.el, options) ?? options;
76+
this.options =
77+
this.parser?.parse(
78+
this.el,
79+
options,
80+
this.parser_multiple,
81+
this.parser_inherit,
82+
this.parser_group_options
83+
) ?? options;
7284

7385
// Store pattern instance on element
7486
this.el[`pattern-${this.name}`] = this;

src/core/basepattern.test.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe("Basepattern class tests", function () {
1515
jest.restoreAllMocks();
1616
});
1717

18-
it("1 - Trigger, name and parser are statically available on the class.", async function () {
18+
it("1.1 - Trigger, name and parser are statically available on the class.", async function () {
1919
class Pat extends BasePattern {
2020
static name = "example";
2121
static trigger = ".example";
@@ -37,6 +37,57 @@ describe("Basepattern class tests", function () {
3737
expect(typeof pat.parser.parse).toBe("function");
3838
});
3939

40+
it("1.2 - Options are created with grouping per default.", async function () {
41+
const Parser = (await import("./parser")).default;
42+
43+
const parser = new Parser("example");
44+
parser.addArgument("a", 1);
45+
parser.addArgument("camel-b", 2);
46+
parser.addArgument("test-a", 3);
47+
parser.addArgument("test-b", 4);
48+
49+
class Pat extends BasePattern {
50+
static name = "example";
51+
static trigger = ".example";
52+
static parser = parser;
53+
}
54+
55+
const el = document.createElement("div");
56+
const pat = new Pat(el);
57+
await utils.timeout(1);
58+
59+
expect(pat.options.a).toBe(1);
60+
expect(pat.options.camelB).toBe(2);
61+
expect(pat.options.test.a).toBe(3);
62+
expect(pat.options.test.b).toBe(4);
63+
});
64+
65+
it("1.3 - Option grouping can be turned off.", async function () {
66+
const Parser = (await import("./parser")).default;
67+
68+
const parser = new Parser("example");
69+
parser.addArgument("a", 1);
70+
parser.addArgument("camel-b", 2);
71+
parser.addArgument("test-a", 3);
72+
parser.addArgument("test-b", 4);
73+
74+
class Pat extends BasePattern {
75+
static name = "example";
76+
static trigger = ".example";
77+
static parser = parser;
78+
79+
parser_group_options = false;
80+
}
81+
82+
const el = document.createElement("div");
83+
const pat = new Pat(el);
84+
await utils.timeout(1);
85+
86+
expect(pat.options.a).toBe(1);
87+
expect(pat.options["camel-b"]).toBe(2);
88+
expect(pat.options["test-a"]).toBe(3);
89+
expect(pat.options["test-b"]).toBe(4);
90+
});
4091
it("2 - Base pattern is class based and does inheritance, polymorphism, encapsulation, ... pt1", async function () {
4192
class Pat1 extends BasePattern {
4293
some = "thing";

0 commit comments

Comments
 (0)