-
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.
Merge pull request #2 from actionanand/features/1-angular-pipes
Features/1 angular pipes
- Loading branch information
Showing
6 changed files
with
107 additions
and
7 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
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,20 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
|
||
@Pipe({ | ||
name: 'sort', | ||
standalone: true, | ||
pure: false, | ||
}) | ||
export class SortPipe implements PipeTransform { | ||
transform(value: string[] | number[], direction: 'asc' | 'des' = 'asc') { | ||
const sorted = [...value]; | ||
sorted.sort((a, b) => { | ||
if (direction === 'asc') { | ||
return a > b ? 1 : -1; | ||
} else { | ||
return a > b ? -1 : 1; | ||
} | ||
}); | ||
return sorted; | ||
} | ||
} |
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,29 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
|
||
@Pipe({ | ||
name: 'tempOddEven', | ||
standalone: true, | ||
}) | ||
export class TempOddEvenPipe implements PipeTransform { | ||
transform(value: string | number, isOdd: boolean): string { | ||
let val: number; | ||
let outputTemp: number; | ||
let symbol: '°F' | '°C'; | ||
|
||
if (typeof value === 'string') { | ||
val = parseFloat(value); | ||
} else { | ||
val = value; | ||
} | ||
|
||
if (isOdd) { | ||
outputTemp = val * (9 / 5) + 32; | ||
symbol = '°F'; | ||
} else { | ||
outputTemp = (val - 32) * (5 / 9); | ||
symbol = '°C'; | ||
} | ||
|
||
return `${outputTemp.toFixed(2)} ${symbol}`; | ||
} | ||
} |
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,35 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
|
||
@Pipe({ | ||
name: 'temp', | ||
standalone: true, | ||
}) | ||
export class TempPipe implements PipeTransform { | ||
transform(value: string | number, inputType: 'cel' | 'fah', outputType?: 'cel' | 'fah'): string { | ||
let val: number; | ||
let outputTemp: number; | ||
let symbol: '°F' | '°C'; | ||
|
||
if (typeof value === 'string') { | ||
val = parseFloat(value); | ||
} else { | ||
val = value; | ||
} | ||
|
||
if (inputType === 'cel' && outputType === 'fah') { | ||
outputTemp = val * (9 / 5) + 32; | ||
} else if (inputType === 'fah' && outputType === 'cel') { | ||
outputTemp = (val - 32) * (5 / 9); | ||
} else { | ||
outputTemp = val; | ||
} | ||
|
||
if (!outputType) { | ||
symbol = inputType === 'cel' ? '°C' : '°F'; | ||
} else { | ||
symbol = outputType === 'cel' ? '°C' : '°F'; | ||
} | ||
|
||
return `${outputTemp.toFixed(2)} ${symbol}`; | ||
} | ||
} |