Skip to content

Commit

Permalink
Build
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpainsi committed Jun 27, 2023
1 parent 8f937e4 commit 340ac26
Show file tree
Hide file tree
Showing 15 changed files with 193 additions and 23 deletions.
18 changes: 14 additions & 4 deletions dist/cjs/src/filtering.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FilterData = exports.Filtering = void 0;
const schema_1 = require("./schema");
const result_1 = require("./result");
const utils_1 = require("./utils");
class Filtering {
Expand Down Expand Up @@ -75,11 +76,20 @@ class FilterData {
return this.#checkedFilters;
}
checkFilter(groupName, filterName) {
if (this.#disabledGroups.has(groupName)) {
return;
if (groupName instanceof schema_1.Filter) {
if (this.#disabledGroups.has(groupName.group.name)) {
return;
}
const filters = this.#getFiltersFromGroup(groupName.group.name);
filters.add(groupName.name);
}
else if (typeof groupName === 'string' && typeof filterName === 'string') {
if (this.#disabledGroups.has(groupName)) {
return;
}
const filters = this.#getFiltersFromGroup(groupName);
filters.add(filterName);
}
const filters = this.#getFiltersFromGroup(groupName);
filters.add(filterName);
}
#getFiltersFromGroup(groupName) {
if (!this.#checkedFilters.has(groupName)) {
Expand Down
39 changes: 39 additions & 0 deletions dist/cjs/src/filtering.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const test_data_1 = require("../test/test-data");
const globals_1 = require("@jest/globals");
const test_utils_1 = require("../test/test-utils");
const simple_1 = require("../test/data/simple");
const medium_1 = require("../test/data/medium");
const schema_1 = require("./schema");
const filtering_1 = require("./filtering");
const parser_1 = require("./parser");
(0, globals_1.describe)('Test Tiltering', function () {
const testData = [{
name: 'Select one filter in one group',
Expand Down Expand Up @@ -137,4 +141,39 @@ const medium_1 = require("../test/data/medium");
const schema = (0, test_utils_1.jsToSchema)(testData.schema);
(0, test_utils_1.testFiltering)(schema, testData);
});
(0, globals_1.test)('FilterData checkFilter with object', () => {
const filterData = new filtering_1.FilterData();
const group = new schema_1.Group('group');
const filter = new schema_1.Filter('filter');
group.addFilter(filter);
filterData.checkFilter(filter);
expect(filterData.checkedFilters).toEqual(new Map([
['group', new Set(['filter'])],
]));
});
(0, globals_1.test)('FilterData checkFilter with string group and filter', () => {
const filterData = new filtering_1.FilterData();
filterData.checkFilter('group', 'filter');
expect(filterData.checkedFilters).toEqual(new Map([
['group', new Set(['filter'])],
]));
});
(0, globals_1.test)('Filtering with unavailable filter no checked', () => {
const schema = new parser_1.Parser().parseSchemaFromHtml((0, test_utils_1.jsxToHtml)((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: "filtering-group", "data-group-name": "available", children: (0, jsx_runtime_1.jsx)("div", { className: "filtering-filter", "data-filter-name": "true", children: "Available?" }) }), (0, jsx_runtime_1.jsx)("div", { id: "item-1", className: "filtering-item", "data-filter-available": "true", children: "1" }), (0, jsx_runtime_1.jsx)("div", { id: "item-2", className: "filtering-item", "data-filter-available": "", children: "2" })] })));
const filtering = new filtering_1.Filtering(schema);
let result = filtering.filter(new filtering_1.FilterData());
expect(result.filteredItems.map((item) => item.data.element.id)).toEqual(['item-1', 'item-2']);
});
(0, globals_1.test)('Filtering with missing filter no checked', () => {
const filtering = new filtering_1.Filtering(new parser_1.Parser().parseSchemaFromHtml((0, test_utils_1.jsxToHtml)((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: "filtering-group", "data-group-name": "available", children: (0, jsx_runtime_1.jsx)("div", { className: "filtering-filter", "data-filter-name": "true", children: "Available?" }) }), (0, jsx_runtime_1.jsx)("div", { id: "item-1", className: "filtering-item", "data-filter-available": "true", children: "1" }), (0, jsx_runtime_1.jsx)("div", { id: "item-2", className: "filtering-item", children: "2" })] }))));
let result = filtering.filter(new filtering_1.FilterData());
expect(result.filteredItems.map((item) => item.data.element.id)).toEqual(['item-1', 'item-2']);
});
(0, globals_1.test)('Filtering with unavailable filter checked', () => {
const filtering = new filtering_1.Filtering(new parser_1.Parser().parseSchemaFromHtml((0, test_utils_1.jsxToHtml)((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: "filtering-group", "data-group-name": "available", children: (0, jsx_runtime_1.jsx)("div", { className: "filtering-filter", "data-filter-name": "true", children: "Available?" }) }), (0, jsx_runtime_1.jsx)("div", { id: "item-1", className: "filtering-item", "data-filter-available": "true", children: "1" }), (0, jsx_runtime_1.jsx)("div", { id: "item-2", className: "filtering-item", "data-filter-available": "", children: "2" })] }))));
let result = filtering.filter((0, test_utils_1.createFilterData)({
'available': ['true']
}));
expect(result.filteredItems.map((item) => item.data.element.id)).toEqual(['item-1']);
});
});
20 changes: 20 additions & 0 deletions dist/cjs/src/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ exports.FilterResult = exports.GroupResult = exports.Result = void 0;
class Result {
#schema;
#groups = new Map();
#filteredItems = new Set();
#allItems = new Set();
constructor(schema) {
this.#schema = schema;
this.#initialize();
Expand Down Expand Up @@ -40,9 +42,13 @@ class Result {
result.add(item);
}
}
for (const item of this.#filteredItems) {
result.add(item);
}
return [...result];
}
addFilteredItem(item) {
this.#filteredItems.add(item);
for (const groupName of item.getGroupNames()) {
const groupResult = this.#groups.get(groupName);
groupResult.addFilteredItem(item);
Expand All @@ -55,9 +61,13 @@ class Result {
result.add(item);
}
}
for (const item of this.#allItems) {
result.add(item);
}
return [...result];
}
addAllItem(item) {
this.#allItems.add(item);
for (const groupName of item.getGroupNames()) {
const groupResult = this.#groups.get(groupName);
groupResult.addAllItem(item);
Expand All @@ -67,6 +77,8 @@ class Result {
exports.Result = Result;
class GroupResult {
#schemaGroup;
#filteredItems = new Set();
#allItems = new Set();
#filters = new Map();
constructor(schemaGroup) {
this.#schemaGroup = schemaGroup;
Expand All @@ -90,9 +102,13 @@ class GroupResult {
result.add(item);
}
}
for (const item of this.#filteredItems) {
result.add(item);
}
return [...result];
}
addFilteredItem(item) {
this.#filteredItems.add(item);
for (const filterName of item.getFilterNames(this.schemaGroup.name)) {
this.#filters.get(filterName)?.addFilteredItem(item);
}
Expand All @@ -104,9 +120,13 @@ class GroupResult {
result.add(item);
}
}
for (const item of this.#allItems) {
result.add(item);
}
return [...result];
}
addAllItem(item) {
this.#allItems.add(item);
for (const filterName of item.getFilterNames(this.schemaGroup.name)) {
this.#filters.get(filterName)?.addAllItem(item);
}
Expand Down
12 changes: 9 additions & 3 deletions dist/cjs/src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,14 @@ class Item {
return new Set(this.#groups.keys());
}
addFilter(groupName, filterName) {
const filters = this.#getFiltersFromGroup(groupName);
filters.add(filterName);
if (groupName instanceof Filter) {
const filters = this.#getFiltersFromGroup(groupName.group.name);
filters.add(groupName.name);
}
else if (typeof groupName === 'string' && typeof filterName === 'string') {
const filters = this.#getFiltersFromGroup(groupName);
filters.add(filterName);
}
}
#getFiltersFromGroup(groupName) {
if (!this.#groups.has(groupName)) {
Expand All @@ -122,7 +128,7 @@ class Item {
}
getFilterNames(groupName) {
if (this.#groups.has(groupName)) {
return new Set(this.#groups.get(groupName));
return this.#groups.get(groupName);
}
else {
return new Set();
Expand Down
11 changes: 10 additions & 1 deletion dist/cjs/src/schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,23 @@ const filtering_1 = require("./filtering");
group.addFilter(filter1);
(0, globals_1.expect)(() => group.addFilter(filter2)).toThrow();
});
(0, globals_1.test)('Item.addFilter', () => {
(0, globals_1.test)('Item.addFilter with string group and filter', () => {
const item = new schema_1.Item({ name: 'item' });
item.addFilter('group-1', 'filter-1');
item.addFilter('group-2', 'filter-2');
(0, globals_1.expect)([...item.getGroupNames()].sort()).toEqual(['group-1', 'group-2'].sort());
(0, globals_1.expect)([...item.getFilterNames('group-1')]).toEqual(['filter-1']);
(0, globals_1.expect)([...item.getFilterNames('group-2')]).toEqual(['filter-2']);
});
(0, globals_1.test)('Item.addFilter with Filter object', () => {
const item = new schema_1.Item({ name: 'item' });
const group = new schema_1.Group('group');
const filter = new schema_1.Filter('filter');
group.addFilter(filter);
item.addFilter(filter);
(0, globals_1.expect)([...item.getGroupNames()].sort()).toEqual(['group'].sort());
(0, globals_1.expect)([...item.getFilterNames('group')]).toEqual(['filter']);
});
(0, globals_1.test)('FilterData.checkFilter', () => {
const filterData = new filtering_1.FilterData();
filterData.checkFilter('group', 'filter');
Expand Down
18 changes: 14 additions & 4 deletions dist/esm/src/filtering.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Filter } from './schema';
import { Result } from './result';
import { findOne } from './utils';
export class Filtering {
Expand Down Expand Up @@ -71,11 +72,20 @@ export class FilterData {
return this.#checkedFilters;
}
checkFilter(groupName, filterName) {
if (this.#disabledGroups.has(groupName)) {
return;
if (groupName instanceof Filter) {
if (this.#disabledGroups.has(groupName.group.name)) {
return;
}
const filters = this.#getFiltersFromGroup(groupName.group.name);
filters.add(groupName.name);
}
else if (typeof groupName === 'string' && typeof filterName === 'string') {
if (this.#disabledGroups.has(groupName)) {
return;
}
const filters = this.#getFiltersFromGroup(groupName);
filters.add(filterName);
}
const filters = this.#getFiltersFromGroup(groupName);
filters.add(filterName);
}
#getFiltersFromGroup(groupName) {
if (!this.#checkedFilters.has(groupName)) {
Expand Down
41 changes: 40 additions & 1 deletion dist/esm/src/filtering.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { blue, expensive, large, medium, red, small } from '../test/test-data';
import { describe, test } from '@jest/globals';
import { jsToSchema, testFiltering } from '../test/test-utils';
import { createFilterData, jsToSchema, jsxToHtml, testFiltering } from '../test/test-utils';
import { simpleTestSchema } from '../test/data/simple';
import { mediumTestSchema } from '../test/data/medium';
import { Filter, Group } from './schema';
import { FilterData, Filtering } from './filtering';
import { Parser } from './parser';
describe('Test Tiltering', function () {
const testData = [{
name: 'Select one filter in one group',
Expand Down Expand Up @@ -135,4 +139,39 @@ describe('Test Tiltering', function () {
const schema = jsToSchema(testData.schema);
testFiltering(schema, testData);
});
test('FilterData checkFilter with object', () => {
const filterData = new FilterData();
const group = new Group('group');
const filter = new Filter('filter');
group.addFilter(filter);
filterData.checkFilter(filter);
expect(filterData.checkedFilters).toEqual(new Map([
['group', new Set(['filter'])],
]));
});
test('FilterData checkFilter with string group and filter', () => {
const filterData = new FilterData();
filterData.checkFilter('group', 'filter');
expect(filterData.checkedFilters).toEqual(new Map([
['group', new Set(['filter'])],
]));
});
test('Filtering with unavailable filter no checked', () => {
const schema = new Parser().parseSchemaFromHtml(jsxToHtml(_jsxs(_Fragment, { children: [_jsx("div", { className: "filtering-group", "data-group-name": "available", children: _jsx("div", { className: "filtering-filter", "data-filter-name": "true", children: "Available?" }) }), _jsx("div", { id: "item-1", className: "filtering-item", "data-filter-available": "true", children: "1" }), _jsx("div", { id: "item-2", className: "filtering-item", "data-filter-available": "", children: "2" })] })));
const filtering = new Filtering(schema);
let result = filtering.filter(new FilterData());
expect(result.filteredItems.map((item) => item.data.element.id)).toEqual(['item-1', 'item-2']);
});
test('Filtering with missing filter no checked', () => {
const filtering = new Filtering(new Parser().parseSchemaFromHtml(jsxToHtml(_jsxs(_Fragment, { children: [_jsx("div", { className: "filtering-group", "data-group-name": "available", children: _jsx("div", { className: "filtering-filter", "data-filter-name": "true", children: "Available?" }) }), _jsx("div", { id: "item-1", className: "filtering-item", "data-filter-available": "true", children: "1" }), _jsx("div", { id: "item-2", className: "filtering-item", children: "2" })] }))));
let result = filtering.filter(new FilterData());
expect(result.filteredItems.map((item) => item.data.element.id)).toEqual(['item-1', 'item-2']);
});
test('Filtering with unavailable filter checked', () => {
const filtering = new Filtering(new Parser().parseSchemaFromHtml(jsxToHtml(_jsxs(_Fragment, { children: [_jsx("div", { className: "filtering-group", "data-group-name": "available", children: _jsx("div", { className: "filtering-filter", "data-filter-name": "true", children: "Available?" }) }), _jsx("div", { id: "item-1", className: "filtering-item", "data-filter-available": "true", children: "1" }), _jsx("div", { id: "item-2", className: "filtering-item", "data-filter-available": "", children: "2" })] }))));
let result = filtering.filter(createFilterData({
'available': ['true']
}));
expect(result.filteredItems.map((item) => item.data.element.id)).toEqual(['item-1']);
});
});
20 changes: 20 additions & 0 deletions dist/esm/src/result.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export class Result {
#schema;
#groups = new Map();
#filteredItems = new Set();
#allItems = new Set();
constructor(schema) {
this.#schema = schema;
this.#initialize();
Expand Down Expand Up @@ -37,9 +39,13 @@ export class Result {
result.add(item);
}
}
for (const item of this.#filteredItems) {
result.add(item);
}
return [...result];
}
addFilteredItem(item) {
this.#filteredItems.add(item);
for (const groupName of item.getGroupNames()) {
const groupResult = this.#groups.get(groupName);
groupResult.addFilteredItem(item);
Expand All @@ -52,9 +58,13 @@ export class Result {
result.add(item);
}
}
for (const item of this.#allItems) {
result.add(item);
}
return [...result];
}
addAllItem(item) {
this.#allItems.add(item);
for (const groupName of item.getGroupNames()) {
const groupResult = this.#groups.get(groupName);
groupResult.addAllItem(item);
Expand All @@ -63,6 +73,8 @@ export class Result {
}
export class GroupResult {
#schemaGroup;
#filteredItems = new Set();
#allItems = new Set();
#filters = new Map();
constructor(schemaGroup) {
this.#schemaGroup = schemaGroup;
Expand All @@ -86,9 +98,13 @@ export class GroupResult {
result.add(item);
}
}
for (const item of this.#filteredItems) {
result.add(item);
}
return [...result];
}
addFilteredItem(item) {
this.#filteredItems.add(item);
for (const filterName of item.getFilterNames(this.schemaGroup.name)) {
this.#filters.get(filterName)?.addFilteredItem(item);
}
Expand All @@ -100,9 +116,13 @@ export class GroupResult {
result.add(item);
}
}
for (const item of this.#allItems) {
result.add(item);
}
return [...result];
}
addAllItem(item) {
this.#allItems.add(item);
for (const filterName of item.getFilterNames(this.schemaGroup.name)) {
this.#filters.get(filterName)?.addAllItem(item);
}
Expand Down
12 changes: 9 additions & 3 deletions dist/esm/src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,14 @@ export class Item {
return new Set(this.#groups.keys());
}
addFilter(groupName, filterName) {
const filters = this.#getFiltersFromGroup(groupName);
filters.add(filterName);
if (groupName instanceof Filter) {
const filters = this.#getFiltersFromGroup(groupName.group.name);
filters.add(groupName.name);
}
else if (typeof groupName === 'string' && typeof filterName === 'string') {
const filters = this.#getFiltersFromGroup(groupName);
filters.add(filterName);
}
}
#getFiltersFromGroup(groupName) {
if (!this.#groups.has(groupName)) {
Expand All @@ -116,7 +122,7 @@ export class Item {
}
getFilterNames(groupName) {
if (this.#groups.has(groupName)) {
return new Set(this.#groups.get(groupName));
return this.#groups.get(groupName);
}
else {
return new Set();
Expand Down
Loading

0 comments on commit 340ac26

Please sign in to comment.