Skip to content

Commit f9548b0

Browse files
authored
Merge pull request #1093 from Patternslib/basepattern-static-parser
feat(core basepattern): Provide the parser as static attribute.
2 parents a021f94 + 49db677 commit f9548b0

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

src/core/basepattern.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ const log = logging.getLogger("Patternslib Base");
1313
class BasePattern {
1414
static name; // name of pattern used in Registry.
1515
static trigger; // A CSS selector to match elements that should trigger the pattern instantiation.
16-
parser; // Options parser.
16+
static parser; // Options parser.
1717

1818
constructor(el, options = {}) {
19-
// Make static ``name`` and ``trigger`` available on instance.
19+
// Make static variables available on instance.
2020
this.name = this.constructor.name;
2121
this.trigger = this.constructor.trigger;
22+
this.parser = this.constructor.parser;
2223

2324
if (!el) {
2425
log.warn(`No element given to pattern ${this.name}.`);
@@ -49,7 +50,7 @@ class BasePattern {
4950
}
5051

5152
// Create the options object by parsing the element and using the
52-
// optional optios as default.
53+
// optional options as default.
5354
this.options = this.parser?.parse(this.el, options) ?? options;
5455

5556
// Store pattern instance on element

src/core/basepattern.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Also see: https://github.com/Patternslib/pat-PATTERN_TEMPLATE
2121
class Pattern extends BasePattern {
2222
static name = "test-pattern";
2323
static trigger = ".pat-test-pattern";
24-
parser = parser;
24+
static parser = parser;
2525

2626
async init() {
2727
import("./test-pattern.scss");

src/core/basepattern.test.js

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

18-
it("1 - Trigger and name are statically available on the class.", async function () {
18+
it("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";
22+
static parser = { parse: () => {} }; // dummy parser
2223
}
2324

2425
// trigger and name are static and available on the class itself
25-
expect(Pat.trigger).toBe(".example");
2626
expect(Pat.name).toBe("example");
27+
expect(Pat.trigger).toBe(".example");
28+
expect(typeof Pat.parser.parse).toEqual("function");
2729

2830
const el = document.createElement("div");
2931

@@ -32,6 +34,7 @@ describe("Basepattern class tests", function () {
3234

3335
expect(pat.name).toEqual("example");
3436
expect(pat.trigger).toEqual(".example");
37+
expect(typeof pat.parser.parse).toEqual("function");
3538
});
3639

3740
it("2 - Base pattern is class based and does inheritance, polymorphism, encapsulation, ... pt1", async function () {

0 commit comments

Comments
 (0)