Skip to content

Commit

Permalink
fix(core): DataView inlineFilters should allow ES6 arrow functions (#…
Browse files Browse the repository at this point in the history
…1304)

- the sample code below was not working prior to this PR
```ts
dataView = new SlickDataView({ inlineFilters: true });
dataView.setFilter((item, args) => item["percentComplete"] > args.percentCompleteThreshold);
```
  • Loading branch information
ghiscoding authored Jan 2, 2024
1 parent 59ebaa6 commit 25b9a10
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 49 deletions.
8 changes: 3 additions & 5 deletions packages/common/src/core/__tests__/slickDataView.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1471,9 +1471,7 @@ describe('SlickDatView core file', () => {

it('should be able to set a filter as CSP Safe and extra filter arguments and expect items to be filtered', () => {
const searchString = 'Ob'; // we'll provide "searchString" as filter args
function myFilter(item, args) {
return item.name.toLowerCase().includes(args.searchString?.toLowerCase());
}
const myFilter = (item, args) => item.name.toLowerCase().includes(args.searchString?.toLowerCase());
const items = [{ id: 1, name: 'Bob', age: 33 }, { id: 0, name: 'Hobby', age: 44 }, { id: 4, name: 'John', age: 20 }, { id: 3, name: 'Jane', age: 24 }];

dv = new SlickDataView({ inlineFilters: true, useCSPSafeFilter: true });
Expand Down Expand Up @@ -1568,7 +1566,7 @@ describe('SlickDatView core file', () => {
dv.refresh();

// change filter without changing pagination & expect pageNum to be recalculated
dv.setFilter(function (item) { return item.id >= 10 });
dv.setFilter((item) => item.id >= 10);
expect(onPagingInfoSpy).toHaveBeenCalledWith({ dataView: dv, pageNum: 0, pageSize: 2, totalPages: 1, totalRows: 1 }, null, dv);
expect(onRowCountChangeSpy).toHaveBeenCalledWith({ dataView: dv, previous: 2, current: 1, itemCount: 5, callingOnRowsChanged: true }, null, dv);
expect(onRowsChangeSpy).toHaveBeenCalledWith({ dataView: dv, rows: [0, 1], itemCount: 5, calledOnRowCountChanged: true }, null, dv);
Expand Down Expand Up @@ -1622,7 +1620,7 @@ describe('SlickDatView core file', () => {
// change filter without changing pagination will result in 2 changes but only 1 defined as changed because we ignore diffs from 0-1
dv.setRefreshHints({ ignoreDiffsBefore: 1, ignoreDiffsAfter: 3 });
items[0].id = 8;
dv.setFilter(function (item) { return item.id >= 0 || item.name.includes('a') });
dv.setFilter((item) => item.id >= 0 || item.name.includes('a'));
expect(onPagingInfoSpy).toHaveBeenCalledWith({ dataView: dv, pageNum: 0, pageSize: 2, totalPages: 3, totalRows: 5 }, null, dv);
expect(onRowCountChangeSpy).toHaveBeenCalledWith({ dataView: dv, previous: 2, current: 1, itemCount: 5, callingOnRowsChanged: true }, null, dv);
expect(onRowsChangeSpy).toHaveBeenCalledWith({ dataView: dv, rows: [1], itemCount: 5, calledOnRowCountChanged: true }, null, dv);
Expand Down
18 changes: 3 additions & 15 deletions packages/common/src/core/slickDataview.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-new-func */
/* eslint-disable no-bitwise */
import { extend, isDefined } from '@slickgrid-universal/utils';
import { type AnyFunction, extend, getFunctionDetails, isDefined } from '@slickgrid-universal/utils';

import { SlickGroupItemMetadataProvider } from '../extensions/slickGroupItemMetadataProvider';
import type {
Expand Down Expand Up @@ -52,7 +52,6 @@ export type FilterWithCspCachingFn<T> = (item: T[], args: any, filterCache: any[
export type DataIdType = number | string;
export type SlickDataItem = SlickNonDataItem | SlickGroup | SlickGroupTotals | any;
export type GroupGetterFn = (val: any) => string | number;
export type AnyFunction = (...args: any[]) => any;

/**
* A sample Model implementation.
Expand Down Expand Up @@ -1007,17 +1006,6 @@ export class SlickDataView<TData extends SlickDataItem = any> implements CustomD
return groupedRows;
}

protected getFunctionInfo(fn: AnyFunction) {
const fnStr = fn.toString();
const usingEs5 = fnStr.indexOf('function') >= 0; // with ES6, the word function is not present
const fnRegex = usingEs5 ? /^function[^(]*\(([^)]*)\)\s*{([\s\S]*)}$/ : /^[^(]*\(([^)]*)\)\s*{([\s\S]*)}$/;
const matches = fn.toString().match(fnRegex) || [];
return {
params: matches[1].split(','),
body: matches[2]
};
}

protected compileAccumulatorLoopCSPSafe(aggregator: Aggregator) {
if (aggregator.accumulate) {
return function (items: any[]) {
Expand Down Expand Up @@ -1056,7 +1044,7 @@ export class SlickDataView<TData extends SlickDataItem = any> implements CustomD
if (stopRunningIfCSPSafeIsActive) {
return null;
}
const filterInfo = this.getFunctionInfo(this.filter as FilterFn<TData>);
const filterInfo = getFunctionDetails(this.filter as FilterFn<TData>);

const filterPath1 = '{ continue _coreloop; }$1';
const filterPath2 = '{ _retval[_idx++] = $item$; continue _coreloop; }$1';
Expand Down Expand Up @@ -1098,7 +1086,7 @@ export class SlickDataView<TData extends SlickDataItem = any> implements CustomD
return null;
}

const filterInfo = this.getFunctionInfo(this.filter as FilterFn<TData>);
const filterInfo = getFunctionDetails(this.filter as FilterFn<TData>);

const filterPath1 = '{ continue _coreloop; }$1';
const filterPath2 = '{ _cache[_i] = true;_retval[_idx++] = $item$; continue _coreloop; }$1';
Expand Down
12 changes: 11 additions & 1 deletion packages/common/src/core/slickGrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@
import Sortable, { SortableEvent } from 'sortablejs';
import DOMPurify from 'dompurify';
import { BindingEventService } from '@slickgrid-universal/binding';
import { createDomElement, emptyElement, extend, getInnerSize, getOffset, insertAfterElement, isDefined, isPrimitiveOrHTML, classNameToList } from '@slickgrid-universal/utils';
import {
classNameToList,
createDomElement,
emptyElement,
extend,
getInnerSize,
getOffset,
insertAfterElement,
isDefined,
isPrimitiveOrHTML,
} from '@slickgrid-universal/utils';

import {
type BasePubSub,
Expand Down
Loading

0 comments on commit 25b9a10

Please sign in to comment.