-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
228 additions
and
15,966 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
import { RequestedResource } from '../interfaces/requested-resources.interface'; | ||
|
||
@Pipe({ name: 'applySort', pure: false }) | ||
export class ApplySortPipe implements PipeTransform { | ||
transform(requestedResources: RequestedResource[], selectedSort: string): RequestedResource[] { | ||
switch (selectedSort) { | ||
case 'storageLocationIdSort': | ||
return this.storageLocationIdSort(requestedResources, 'asc'); | ||
case 'requestDateSort': | ||
return this.requestDateSort(requestedResources); | ||
break; | ||
default: | ||
return requestedResources; | ||
} | ||
} | ||
|
||
private storageLocationIdSort(requestedResources: RequestedResource[], sortOrder: 'asc' | 'desc'): RequestedResource[] { | ||
if (!requestedResources) { | ||
return []; | ||
} | ||
|
||
// If you sort the original array, it will only work the first time. | ||
const copy = [...requestedResources]; | ||
|
||
return copy.sort((a, b) => { | ||
const storageLocationIdA = a.location?.copy?.length ? a.location.copy[0].storage_location_id : ''; | ||
const storageLocationIdB = b.location?.copy?.length ? b.location.copy[0].storage_location_id : ''; | ||
|
||
return sortOrder === 'asc' | ||
? storageLocationIdA.localeCompare(storageLocationIdB) | ||
: storageLocationIdB.localeCompare(storageLocationIdA); | ||
}); | ||
} | ||
|
||
private requestDateSort(requestedResources: RequestedResource[]): RequestedResource[] { | ||
const copy = [...requestedResources]; | ||
|
||
return copy.sort((a, b) => { | ||
|
||
const dateA = a.request[0]?.request_date ? new Date(a.request[0].request_date) : new Date('1900-01-01T00:00:00Z'); | ||
const dateB = b.request[0]?.request_date ? new Date(b.request[0].request_date) : new Date('1900-01-01T00:00:00Z'); | ||
|
||
return dateA.getTime() - dateB.getTime(); | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { StorageLocationIdSortPipe } from './storage-location-id-sort.pipe'; | ||
import { RequestedResource } from './interfaces/requested-resources.interface'; | ||
|
||
describe('StorageLocationIdSortPipe', () => { | ||
let pipe: StorageLocationIdSortPipe; | ||
|
||
beforeEach(() => { | ||
pipe = new StorageLocationIdSortPipe(); | ||
}); | ||
|
||
it('should sort the requested resources in ascending order by storage location ID', () => { | ||
const input: RequestedResource[] = [ | ||
{ location: { copy: [{ storage_location_id: 'r1', pid:'1', barcode: '1', link: '' }] } }, | ||
{ location: { copy: [{ storage_location_id: 'r5', pid:'1', barcode: '1', link: '' }] } }, | ||
]; | ||
const output = pipe.transform(input, 'asc'); | ||
expect(output[0].location.copy[0].storage_location_id).toEqual('a'); | ||
expect(output[1].location.copy[0].storage_location_id).toEqual('b'); | ||
}); | ||
|
||
it('should sort the requested resources in descending order by storage location ID', () => { | ||
const input: RequestedResource[] = [ | ||
{ location: { copy: [{ storage_location_id: 'r7', pid:'1', barcode: '1', link: '' }] } }, | ||
{ location: { copy: [{ storage_location_id: 'r1', pid:'1', barcode: '1', link: '' }] } }, | ||
]; | ||
const output = pipe.transform(input, 'desc'); | ||
expect(output[0].location.copy[0].storage_location_id).toEqual('r3'); | ||
expect(output[1].location.copy[0].storage_location_id).toEqual('r8'); | ||
}); | ||
|
||
it('should return an empty array when input is null or undefined', () => { | ||
expect(pipe.transform(null)).toEqual([]); | ||
expect(pipe.transform(undefined)).toEqual([]); | ||
}); | ||
|
||
it('should return an empty array when input is empty', () => { | ||
expect(pipe.transform([])).toEqual([]); | ||
}); | ||
}); |
Oops, something went wrong.