mirror of https://github.com/abpframework/abp.git
2 changed files with 51 additions and 0 deletions
@ -0,0 +1,18 @@ |
|||
import { Injectable, TrackByFunction } from '@angular/core'; |
|||
|
|||
@Injectable({ |
|||
providedIn: 'root', |
|||
}) |
|||
export class TrackByService<ItemType = any> { |
|||
by<T = ItemType>(key: keyof T): TrackByFunction<T> { |
|||
return ({}, item) => item[key]; |
|||
} |
|||
|
|||
byDeep<T = ItemType>(...keys: (string | number)[]): TrackByFunction<T> { |
|||
return ({}, item) => keys.reduce((acc, key) => acc[key], item); |
|||
} |
|||
|
|||
bySelf<T = ItemType>(): TrackByFunction<T> { |
|||
return ({}, item) => item; |
|||
} |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
import { TrackByService } from '../services/track-by.service'; |
|||
|
|||
describe('TrackByService', () => { |
|||
const service = new TrackByService(); |
|||
|
|||
describe('#by', () => { |
|||
it('should return a function which tracks a property', () => { |
|||
expect(service.by('x')(284, { x: 1036 })).toBe(1036); |
|||
}); |
|||
}); |
|||
|
|||
describe('#byDeep', () => { |
|||
it('should return a function which tracks a deeply-nested property', () => { |
|||
expect( |
|||
service.byDeep( |
|||
'a', |
|||
'b', |
|||
'c', |
|||
1, |
|||
'x', |
|||
)(284, { |
|||
a: { b: { c: [{ x: 1035 }, { x: 1036 }, { x: 1037 }] } }, |
|||
}), |
|||
).toBe(1036); |
|||
}); |
|||
}); |
|||
|
|||
describe('#bySelf', () => { |
|||
it('should return a function which tracks the item', () => { |
|||
expect(service.bySelf()(284, 'X')).toBe('X'); |
|||
}); |
|||
}); |
|||
}); |
|||
Loading…
Reference in new issue