generated from Tinkoff/angular-open-source-starter
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): new utility
maskitoTransform(value, maskitoOptions)
(#177)
- Loading branch information
1 parent
cd52fad
commit 20316f1
Showing
15 changed files
with
277 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export {MASKITO_DEFAULT_OPTIONS} from './lib/constants'; | ||
export {Maskito} from './lib/mask'; | ||
export {MaskitoOptions} from './lib/types'; | ||
export {maskitoPipe} from './lib/utils'; | ||
export {maskitoPipe, maskitoTransform} from './lib/utils'; |
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,76 @@ | ||
import {MaskitoOptions} from '../../types'; | ||
import {maskitoTransform} from '../transform'; | ||
|
||
describe('maskitoTransform', () => { | ||
const maskitoOptions: MaskitoOptions = { | ||
mask: /^\d+(,\d*)?$/, | ||
preprocessor: ({elementState, data}) => { | ||
const {value, selection} = elementState; | ||
|
||
return { | ||
elementState: { | ||
selection, | ||
value: value.replace('.', ','), | ||
}, | ||
data: data.replace('.', ','), | ||
}; | ||
}, | ||
postprocessor: ({value, selection}) => { | ||
const newValue = value.replace(/^0+/, '0'); | ||
const deletedChars = value.length - newValue.length; | ||
const [from, to] = selection; | ||
|
||
return {value: newValue, selection: [from - deletedChars, to - deletedChars]}; | ||
}, | ||
}; | ||
|
||
it('returns string if the first argument is a string', () => { | ||
expect(typeof maskitoTransform('100', maskitoOptions)).toBe('string'); | ||
}); | ||
|
||
it('returns ElementState if the first argument is a ElementState', () => { | ||
const res = maskitoTransform({value: '', selection: [0, 0]}, maskitoOptions); | ||
|
||
expect(res).toBeTruthy(); | ||
expect(typeof res).toBe('object'); | ||
}); | ||
|
||
it('formats value using preprocessor', () => { | ||
expect(maskitoTransform('100.42', maskitoOptions)).toBe('100,42'); | ||
expect( | ||
maskitoTransform( | ||
{ | ||
value: '100.42', | ||
selection: [2, 2], | ||
}, | ||
maskitoOptions, | ||
), | ||
).toEqual({value: '100,42', selection: [2, 2]}); | ||
}); | ||
|
||
it('formats value using postprocessor', () => { | ||
expect(maskitoTransform('0000,1234', maskitoOptions)).toBe('0,1234'); | ||
expect( | ||
maskitoTransform( | ||
{ | ||
value: '0000,1234', | ||
selection: [6, 6], | ||
}, | ||
maskitoOptions, | ||
), | ||
).toEqual({value: '0,1234', selection: [3, 3]}); | ||
}); | ||
|
||
it('drops invalid characters (mask expression works)', () => { | ||
expect(maskitoTransform('42Taiga UI42', maskitoOptions)).toBe('4242'); | ||
expect( | ||
maskitoTransform( | ||
{ | ||
value: '42Taiga UI42', | ||
selection: [11, 11], | ||
}, | ||
maskitoOptions, | ||
), | ||
).toEqual({value: '4242', selection: [3, 3]}); | ||
}); | ||
}); |
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,32 @@ | ||
import {MaskModel} from '../classes'; | ||
import {MASKITO_DEFAULT_OPTIONS} from '../constants'; | ||
import {ElementState, MaskitoOptions} from '../types'; | ||
|
||
export function maskitoTransform(value: string, maskitoOptions: MaskitoOptions): string; | ||
export function maskitoTransform( | ||
state: ElementState, | ||
maskitoOptions: MaskitoOptions, | ||
): ElementState; | ||
|
||
export function maskitoTransform( | ||
valueOrState: ElementState | string, | ||
maskitoOptions: MaskitoOptions, | ||
): ElementState | string { | ||
const options: Required<MaskitoOptions> = { | ||
...MASKITO_DEFAULT_OPTIONS, | ||
...maskitoOptions, | ||
}; | ||
const initialElementState: ElementState = | ||
typeof valueOrState === 'string' | ||
? {value: valueOrState, selection: [0, 0]} | ||
: valueOrState; | ||
|
||
const {elementState} = options.preprocessor( | ||
{elementState: initialElementState, data: ''}, | ||
'validation', | ||
); | ||
const maskModel = new MaskModel(elementState, options); | ||
const {value, selection} = options.postprocessor(maskModel, initialElementState); | ||
|
||
return typeof valueOrState === 'string' ? value : {value, selection}; | ||
} |
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
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
18 changes: 18 additions & 0 deletions
18
...cts/demo/src/pages/documentation/transformer/examples/utility-in-action-demo.md
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,18 @@ | ||
```ts | ||
import {maskitoTransform} from '@maskito/core'; | ||
import {maskitoNumberOptionsGenerator} from '@maskito/kit'; | ||
|
||
const maskitoOptions = maskitoNumberOptionsGenerator({ | ||
thousandSeparator: ' ', | ||
}); | ||
|
||
const definitelyValidValue = maskitoTransform('1000000', maskitoOptions); | ||
|
||
console.info(definitelyValidValue); // '1 000 000' | ||
|
||
// Framework agnostic way | index.ts | ||
inputElement.value = definitelyValidValue; | ||
|
||
// Angular way | app.component.ts | ||
this.formControl.patchValue(definitelyValidValue); | ||
``` |
15 changes: 15 additions & 0 deletions
15
projects/demo/src/pages/documentation/transformer/transformer.component.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,15 @@ | ||
import {ChangeDetectionStrategy, Component} from '@angular/core'; | ||
import {DemoPath} from '@demo/path'; | ||
|
||
@Component({ | ||
selector: 'transformer-doc-page', | ||
templateUrl: './transformer.template.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class TransformerDocPageComponent { | ||
readonly maskExpressionDocPage = `/${DemoPath.MaskExpression}`; | ||
readonly processorsDocPage = `/${DemoPath.Processors}`; | ||
readonly overwriteModeDocPage = `/${DemoPath.OverwriteMode}`; | ||
|
||
readonly utilityInActionDemo = import('./examples/utility-in-action-demo.md?raw'); | ||
} |
20 changes: 20 additions & 0 deletions
20
projects/demo/src/pages/documentation/transformer/transformer.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,20 @@ | ||
import {CommonModule} from '@angular/common'; | ||
import {NgModule} from '@angular/core'; | ||
import {RouterModule} from '@angular/router'; | ||
import {TuiAddonDocModule, tuiGenerateRoutes} from '@taiga-ui/addon-doc'; | ||
import {TuiLinkModule, TuiNotificationModule} from '@taiga-ui/core'; | ||
|
||
import {TransformerDocPageComponent} from './transformer.component'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
CommonModule, | ||
TuiAddonDocModule, | ||
TuiLinkModule, | ||
TuiNotificationModule, | ||
RouterModule.forChild(tuiGenerateRoutes(TransformerDocPageComponent)), | ||
], | ||
declarations: [TransformerDocPageComponent], | ||
exports: [TransformerDocPageComponent], | ||
}) | ||
export class TransformerDocPageModule {} |
73 changes: 73 additions & 0 deletions
73
projects/demo/src/pages/documentation/transformer/transformer.template.html
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,73 @@ | ||
<tui-doc-page | ||
header="Transformer" | ||
package="CORE" | ||
> | ||
<section> | ||
<p class="tui-space_top-0"> | ||
<strong>Maskito</strong> | ||
libraries were created to prevent user from typing invalid value. | ||
<br /> | ||
<strong>Maskito</strong> | ||
listens | ||
<code>beforeinput</code> | ||
and | ||
<code>input</code> | ||
events. Programatic (by developer) changes of input's value don't | ||
trigger these events! | ||
</p> | ||
|
||
<tui-notification> | ||
<strong>Maskito</strong> | ||
is based on the assumption that developer is capable to | ||
programatically patch input with | ||
<u>valid</u> | ||
value! | ||
</tui-notification> | ||
|
||
<p> | ||
If you need to programatically patch input's value but you are not | ||
sure that your value is valid (for example, you get it from the | ||
server), you should use | ||
<code>maskitoTransform</code> | ||
utility . | ||
</p> | ||
</section> | ||
|
||
<tui-doc-code [code]="utilityInActionDemo"></tui-doc-code> | ||
|
||
<section class="tui-space_top-12"> | ||
<h2>Next steps</h2> | ||
|
||
<p> | ||
The following sections are recommended to explore core concepts | ||
further: | ||
</p> | ||
|
||
<ul class="tui-list"> | ||
<li class="tui-list__item"> | ||
<a | ||
tuiLink | ||
[routerLink]="maskExpressionDocPage" | ||
> | ||
Mask expression | ||
</a> | ||
</li> | ||
<li class="tui-list__item"> | ||
<a | ||
tuiLink | ||
[routerLink]="processorsDocPage" | ||
> | ||
Processors | ||
</a> | ||
</li> | ||
<li class="tui-list__item"> | ||
<a | ||
tuiLink | ||
[routerLink]="overwriteModeDocPage" | ||
> | ||
Overwrite mode | ||
</a> | ||
</li> | ||
</ul> | ||
</section> | ||
</tui-doc-page> |
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