Skip to content

Commit

Permalink
useCompleteMask with test
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermeasn committed Nov 25, 2023
1 parent 51a734f commit f0bbcfc
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,30 @@ export function InputMask() {

);

}
```

- Mask verification completed and tested

```js
import { useCompleteMask, presets } from "mask-hooks";
import { useState } from "react";

export function InputMask() {

const maskComplete = useCompleteMask(presets.DATE_STAMP, result => !isNaN(Date.parse(result)));
const [ data, setData ] = useState(maskComplete(''));

return (

<input
value={ data.result }
onChange={ input => setData(maskComplete(input.currentTarget.value)) }
style={ { color: data.passing ? "#00ff00" : "#ff0000" } }
/>

);

}
```

Expand Down Expand Up @@ -168,7 +192,7 @@ function useMask(settings: MaskProps): <T extends Stringable>(target: T) => stri
- **Function `useCompleteMask`**: Returns a function to use the preconfigured mask with additional information in the result.

```ts
function useCompleteMask(settings: MaskProps): <T extends Stringable>(target: T) => { result: string, completed: boolean; entries: number; cleaned: string; }
function useCompleteMask(settings: MaskProps, onComplete ?: (result : string, cleaned: string) => boolean): <T extends Stringable>(target: T) => { result: string, completed: boolean; entries: number; cleaned: string; passing : boolean | null; }
```

- **Function `applyMask`**: use a mask directly on the target
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mask-hooks",
"version": "3.0.0",
"version": "3.1.0",
"description": "Lightweight package with functions and hooks for masking data inputs and outputs for Node.JS projects",
"main": "dist/common/index.js",
"module": "dist/module/index.js",
Expand Down
21 changes: 14 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Mask from './mask.class';
import type { MaskProps, Stringable } from './mask.class';
import Mask from './mask.class';

export type MaskApplicator<O = string> = <T extends Stringable>(target : T) => O;

Expand All @@ -8,6 +8,7 @@ export type CompleteMask = {
completed : boolean;
entries : number;
cleaned : string;
passing : boolean | null;
}

export function useMask(settings : MaskProps) : MaskApplicator {
Expand All @@ -17,17 +18,22 @@ export function useMask(settings : MaskProps) : MaskApplicator {

}

export function useCompleteMask(settings : MaskProps) : MaskApplicator<CompleteMask> {
export function useCompleteMask(settings : MaskProps, onComplete ?: (result : string, cleaned: string) => boolean) : MaskApplicator<CompleteMask> {

const mask = new Mask(settings);

function apply<T extends Stringable>(target : T) : CompleteMask {

const result : string = mask.apply(target);

return ({
result: mask.apply(target),
result,
completed: mask.completed,
entries: mask.entries,
cleaned: mask.cleaned
})
cleaned: mask.cleaned,
passing: (typeof onComplete === 'function' && mask.completed) ? onComplete(result, mask.cleaned) : null
});

}

return apply.bind(mask);
Expand All @@ -41,7 +47,8 @@ export function applyMask<T extends Stringable>(target : T, settings : MaskProps

}

export { default as presets, getPresetMask } from './presets.const';
export { getPresetMask, default as presets } from './presets.const';
export type { PresetOption } from './presets.const';
export { Mask };
export type { MaskProps, Stringable };
export { Mask };

10 changes: 10 additions & 0 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ describe('Functions and hooks tests', () => {

});

test('Hook useCompleteMask with test', () => {

const maskComplete = useCompleteMask(getPresetMask('DATE_STAMP'), result => !isNaN(Date.parse(result)));

expect(maskComplete('20231').passing).toBe(null);
expect(maskComplete('20231125').passing).toBe(true);
expect(maskComplete('20231150').passing).toBe(false);

});

test('Function applyMask', () => {

expect(applyMask('1a2b3c', getPresetMask('ONLY_LETTERS', { transform: 'uppercase' }))).toBe('ABC');
Expand Down

0 comments on commit f0bbcfc

Please sign in to comment.