Skip to content
Draft
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
12 changes: 9 additions & 3 deletions packages/datx/src/View.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IArraySplice, IRawModel, mapItems, mobx, removeFromArray, replaceInArra
import { ToMany } from './buckets/ToMany';
import { error } from './helpers/format';
import { getModelId, getModelType, isReference } from './helpers/model/utils';
import { isPropertySelectorFn } from './helpers/view';
import { IIdentifier } from './interfaces/IIdentifier';
import { IModelConstructor } from './interfaces/IModelConstructor';
import { IModelRef } from './interfaces/IModelRef';
Expand All @@ -11,17 +12,18 @@ import { IType } from './interfaces/IType';
import { TChange } from './interfaces/TChange';
import { PureCollection } from './PureCollection';
import { PureModel } from './PureModel';
import { SortMethod } from './types';

export class View<T extends PureModel = PureModel> extends ToMany<T> {
public readonly modelType: IType;

@mobx.observable
public sortMethod?: string | ((item: T) => any);
public sortMethod?: SortMethod<T>;

constructor(
modelType: IModelConstructor<T> | IType,
protected __collection: PureCollection,
sortMethod?: string | ((item: T) => any),
sortMethod?: SortMethod<T>,
models: Array<IIdentifier | T> = [],
public unique: boolean = false,
) {
Expand Down Expand Up @@ -50,7 +52,11 @@ export class View<T extends PureModel = PureModel> extends ToMany<T> {
? (item): any => item[this.sortMethod as 'string']
: this.sortMethod;

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

const instances = mobx.observable.array(list, { deep: false });
Expand Down
5 changes: 5 additions & 0 deletions packages/datx/src/helpers/view.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { PropertySelectorFn, CompareFn } from '../types';

export function isPropertySelectorFn<T>(fn: PropertySelectorFn<T> | CompareFn<T>): fn is PropertySelectorFn<T> {
return fn.length === 1;
}
3 changes: 3 additions & 0 deletions packages/datx/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type PropertySelectorFn<T> = (item: T) => any;
export type CompareFn<T> = (a: T, b: T) => number;
export type SortMethod<T> = string | PropertySelectorFn<T> | CompareFn<T>;
46 changes: 42 additions & 4 deletions packages/datx/test/view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,44 @@ describe('View', () => {
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';

@Attribute()
public notUnique!: number;
@Attribute()
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 Expand Up @@ -277,19 +315,19 @@ describe('View', () => {
if (mobx.useRealMobX) {
viewInstance.list.push(foo1);
expect(viewInstance).toHaveLength(4);

viewInstance.list.unshift(foo2);
expect(viewInstance).toHaveLength(5);

viewInstance.list.splice(2, 2);
expect(viewInstance).toHaveLength(3);
} else {
viewInstance.list = [...viewInstance.list, foo1];
expect(viewInstance).toHaveLength(4);

viewInstance.list = [foo2, ...viewInstance.list];
expect(viewInstance).toHaveLength(5);

viewInstance.list = [...viewInstance.list.slice(0, 2), ...viewInstance.list.slice(4)];
expect(viewInstance).toHaveLength(3);
}
Expand Down