Skip to content

Add support for compare fn on View.sortMethod #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: release/v1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions packages/datx/src/View.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@ import { action, computed, intercept, IObservableArray, observable, reaction } f

import { SORTED_NO_WRITE, UNIQUE_MODEL } from './errors';
import { error } from './helpers/format';
import { isPropertySelectorFn } from './helpers/view';
import { getModelId, getModelType } from './helpers/model/utils';
import { IIdentifier } from './interfaces/IIdentifier';
import { IModelConstructor } from './interfaces/IModelConstructor';
import { IRawView } from './interfaces/IRawView';
import { IType } from './interfaces/IType';
import { TChange } from './interfaces/TChange';
import { ISortMethod } from './interfaces/IView';
import { PureCollection } from './PureCollection';
import { PureModel } from './PureModel';

export class View<T extends PureModel = PureModel> {
public readonly modelType: IType;
@observable public sortMethod?: string|((item: T) => any);
@observable public sortMethod?: ISortMethod<T>;

private readonly __models: IObservableArray<T | IIdentifier> = observable.array([]);

constructor(
modelType: IModelConstructor<T>|IType,
protected __collection: PureCollection,
sortMethod?: string|((item: T) => any),
sortMethod?: ISortMethod<T>,
models: Array<IIdentifier|T> = [],
public unique: boolean = false,
) {
Expand Down Expand Up @@ -55,10 +57,14 @@ export class View<T extends PureModel = PureModel> {
.filter(Boolean) as Array<T>;

if (this.sortMethod) {
const sortFn = typeof this.sortMethod === 'string'
? (item) => item[this.sortMethod as 'string']
: this.sortMethod;
list.sort((a: T, b: T) => sortFn(a) - sortFn(b));
const sortFn =
typeof this.sortMethod === 'string' ? (item) => item[this.sortMethod as 'string'] : this.sortMethod;

if (isPropertySelectorFn(sortFn)) {
list.sort((a: T, b: T) => sortFn(a) - sortFn(b));
} else {
list.sort(sortFn);
}
}

const instances = observable.array(list, { deep: false });
Expand Down
6 changes: 6 additions & 0 deletions packages/datx/src/helpers/view.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ICompareFn, IPropertySelectorFn } from "../interfaces/IView";


export function isPropertySelectorFn<T>(fn: IPropertySelectorFn<T> | ICompareFn<T>): fn is IPropertySelectorFn<T> {
return fn.length === 1;
}
1 change: 1 addition & 0 deletions packages/datx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export { IReferenceOptions } from './interfaces/IReferenceOptions';
export { IViewConstructor } from './interfaces/IViewConstructor';
export { IActionsMixin } from './interfaces/IActionsMixin';
export { IMetaMixin } from './interfaces/IMetaMixin';
export { ISortMethod } from './interfaces/IView';

export { PatchType } from './enums/PatchType';
export { ReferenceType } from './enums/ReferenceType';
Expand Down
3 changes: 3 additions & 0 deletions packages/datx/src/interfaces/IView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type IPropertySelectorFn<T> = (item: T) => any;
export type ICompareFn<T> = (a: T, b: T) => number;
export type ISortMethod<T> = string | IPropertySelectorFn<T> | ICompareFn<T>;
38 changes: 37 additions & 1 deletion packages/datx/test/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
view,
} from '../src';

describe('Model', () => {
describe('view', () => {
it('should init a view', () => {
const collection = new Collection();
const viewInstance = new View('foo', collection);
Expand Down Expand Up @@ -198,6 +198,42 @@ describe('Model', () => {
expect(item2b && item2b.key).toBe(2);
});

it('should be able to sort with compare function and non-unique props', () => {
class Foo extends Model {
public static type = 'foo';

@prop public notUnique!: number;
@prop public unique!: number;
}
class AppCollection extends Collection {
public static types = [Foo];
}

const collection = new AppCollection();
const foos = collection.add([{ notUnique: 2, unique: 3 }, { notUnique: 2, unique: 1 }, { notUnique: 1, unique: 2 }], Foo);

const compareFn = (a: Foo, b: Foo): number => {
if (a.notUnique === b.notUnique){
return a.unique < b.unique ? -1 : 1
} else {
return a.notUnique < b.notUnique ? -1 : 1
}
}

const viewInstance = new View(Foo, collection, compareFn, foos);

expect(viewInstance.length).toBe(3);
const item0a = viewInstance.list[0];
const item1a = viewInstance.list[1];
const item2a = viewInstance.list[2];
expect(item0a && item0a.notUnique).toBe(1);
expect(item0a && item0a.unique).toBe(2);
expect(item1a && item1a.notUnique).toBe(2);
expect(item1a && item1a.unique).toBe(1);
expect(item2a && item2a.notUnique).toBe(2);
expect(item2a && item2a.unique).toBe(3);
});

it('should be able to remove models', () => {
class Foo extends Model {
public static type = 'foo';
Expand Down