-
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 #7 from ckapps/feature/desktop-ui-basics
feature/ingredient-amount pipe
- Loading branch information
Showing
10 changed files
with
125 additions
and
16 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
10 changes: 10 additions & 0 deletions
10
packages/frontend/src/app/modules/domain/ingredient/ingredient.module.ts
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,10 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { IngredientAmountPipe } from './pipes/ingredient-amount.pipe'; | ||
|
||
@NgModule({ | ||
declarations: [IngredientAmountPipe], | ||
imports: [CommonModule], | ||
exports: [IngredientAmountPipe], | ||
}) | ||
export class IngredientModule {} |
8 changes: 8 additions & 0 deletions
8
packages/frontend/src/app/modules/domain/ingredient/pipes/ingredient-amount.pipe.spec.ts
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,8 @@ | ||
import { IngredientAmountPipe } from './ingredient-amount.pipe'; | ||
|
||
describe('IngredientAmountPipe', () => { | ||
it('create an instance', () => { | ||
const pipe = new IngredientAmountPipe(); | ||
expect(pipe).toBeTruthy(); | ||
}); | ||
}); |
66 changes: 66 additions & 0 deletions
66
packages/frontend/src/app/modules/domain/ingredient/pipes/ingredient-amount.pipe.ts
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,66 @@ | ||
import { Inject, LOCALE_ID, Pipe, PipeTransform } from '@angular/core'; | ||
import { formatNumber } from '@angular/common'; | ||
|
||
import { Ingredient } from '@overckd/domain'; | ||
|
||
/** | ||
* Formats the amount for an ingredient | ||
*/ | ||
@Pipe({ | ||
name: 'ingredientAmount', | ||
}) | ||
export class IngredientAmountPipe implements PipeTransform { | ||
/** | ||
* The format to use for rendering numbers | ||
*/ | ||
private readonly digitFormat = '0.0-2'; | ||
|
||
constructor(@Inject(LOCALE_ID) private locale: string) {} | ||
|
||
transform(value: Ingredient, ...args: unknown[]): string { | ||
const { amount } = value; | ||
|
||
// Nothing to do, if amount isn't a number | ||
if (typeof amount !== 'number') { | ||
return amount; | ||
} | ||
|
||
// Let's see if we can render some fancy fraction | ||
const fraction = this.getNiceFraction(amount); | ||
|
||
return fraction | ||
? this.formatWithFractionSymbol(amount, fraction) | ||
: formatNumber(amount, this.locale, this.digitFormat); | ||
} | ||
|
||
private formatWithFractionSymbol(amount: number, fraction: string) { | ||
const formattedValue = formatNumber(amount, this.locale, '0.2-2'); | ||
|
||
const integer = formattedValue.substring(0, formattedValue.length - 3); | ||
|
||
return integer === '0' ? fraction : `${integer}${fraction}`; | ||
} | ||
|
||
/** | ||
* Tries to resolve the fraction for the given `value` | ||
* with a single fraction symbol. | ||
* | ||
* @param value THe numeric value | ||
*/ | ||
private getNiceFraction(value: number) { | ||
// Use 'en' here, so that we are sure about the fraction | ||
return this.getFraction(formatNumber(value, 'en', '0.0-2')); | ||
} | ||
|
||
private getFraction(value: string) { | ||
if (value.endsWith('.25')) { | ||
return '¼'; | ||
} else if (value.endsWith('.5')) { | ||
return '½'; | ||
} else if (value.endsWith('.75')) { | ||
return '¾'; | ||
} | ||
|
||
return undefined; | ||
} | ||
} |
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
20 changes: 14 additions & 6 deletions
20
...c/app/modules/domain/recipe/components/portion-converter/portion-converter.component.scss
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 |
---|---|---|
@@ -1,13 +1,21 @@ | ||
:host { | ||
font-size: 1rem; | ||
|
||
label { | ||
display: flex; | ||
flex-direction: row; | ||
align-items: baseline; | ||
.portion-converter { | ||
$hspacing: 0.25rem; | ||
|
||
> input { | ||
margin-left: 0.25rem; | ||
&__label { | ||
display: flex; | ||
flex-direction: row; | ||
align-items: baseline; | ||
} | ||
|
||
&__input { | ||
margin-left: $hspacing; | ||
} | ||
|
||
&__additional-label { | ||
margin-left: $hspacing; | ||
} | ||
} | ||
} |
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