Skip to content

Commit

Permalink
Merge pull request #26 from Luukkasz/isFormFieldValueUpdated
Browse files Browse the repository at this point in the history
add isFormFieldValueUpdated function
  • Loading branch information
HiddeRpl authored Sep 3, 2024
2 parents 0c5e374 + 4083c74 commit b93269a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
14 changes: 14 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<dd><p>Indicates if passed value is Form object.</p></dd>
<dt><a href="#isFormFieldUpdated">isFormFieldUpdated(ff1, ff2)</a> ⇒ <code>boolean</code></dt>
<dd><p>Indicates if FormField's value or error has changed.</p></dd>
<dt><a href="#isFormFieldValueUpdated">isFormFieldValueUpdated(ff1, ff2)</a> ⇒ <code>boolean</code></dt>
<dd><p>Indicates if FormField's value has changed.</p></dd>
<dt><a href="#isFormField">isFormField(field)</a> ⇒ <code>boolean</code></dt>
<dd><p>Indicates if passed param is FormField object.</p></dd>
<dt><a href="#createFormField">createFormField(val, [validators], [noValidate])</a> ⇒ <code>FormField</code></dt>
Expand Down Expand Up @@ -56,6 +58,18 @@
| ff1 | <code>FormField</code> | <p>First FormField.</p> |
| ff2 | <code>FormField</code> | <p>Second FormField.</p> |

<a name="isFormFieldValueUpdated"></a>

## isFormFieldValueUpdated(ff1, ff2) ⇒ <code>boolean</code>
<p>Indicates if FormField's value has changed.</p>

**Kind**: global function

| Param | Type | Description |
| --- | --- | --- |
| ff1 | <code>FormField</code> | <p>First FormField.</p> |
| ff2 | <code>FormField</code> | <p>Second FormField.</p> |

<a name="isFormField"></a>

## isFormField(field) ⇒ <code>boolean</code>
Expand Down
10 changes: 10 additions & 0 deletions lib/form-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ export function isFormFieldUpdated<T>(ff1: FormField<T>, ff2: FormField<T>): boo
return ff1.val !== ff2.val || ff1.err !== ff2.err
}

/**
* Indicates if FormField's value has changed.
* @param {FormField} ff1 - First FormField.
* @param {FormField} ff2 - Second FormField.
* @returns {boolean}
*/
export function isFormFieldValueUpdated<T>(ff1: FormField<T>, ff2: FormField<T>): boolean {
return ff1.val !== ff2.val
}

/**
* Indicates if passed param is FormField object.
* @param {any} field - Field to check.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@currency-one/form-utils",
"version": "1.0.10",
"version": "1.0.13",
"description": "Form utils",
"keywords": [
"form-utils",
Expand Down
28 changes: 27 additions & 1 deletion test/form-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
createFormField,
createPasswordFormField,
formFieldsToValues, isForm,
isFormField,
isFormField, isFormFieldValueUpdated,
} from '../lib/form-utils'

describe('form-utils', () => {
Expand Down Expand Up @@ -118,4 +118,30 @@ describe('form-utils', () => {
expect(formUpdated.field5.val).toBeTruthy()
expect(formUpdated.field5.touched).toBeTruthy()
})

it('should isFormFieldValueUpdated() return correct value', () => {
const formField1 = createFormField('')
const formField2 = createFormField('test')
const formField3 = createFormField(false)
const formField4 = createFormField(true)
const formField5 = createFormField(0)
const formField6 = createFormField(1)
const formField7 = createFormField({test: ''})
const formField8 = createFormField({test: 'test'})
const formField9 = createFormField('errorTest')
const formField10 = createFormField('errorTest')
formField10.err = 'error'

expect(isFormFieldValueUpdated(formField1, formField1)).toBe(false)
expect(isFormFieldValueUpdated(formField3, formField3)).toBe(false)
expect(isFormFieldValueUpdated(formField5, formField5)).toBe(false)
expect(isFormFieldValueUpdated(formField7, formField7)).toBe(false)

expect(isFormFieldValueUpdated(formField1, formField2)).toBe(true)
expect(isFormFieldValueUpdated(formField3, formField4)).toBe(true)
expect(isFormFieldValueUpdated(formField5, formField6)).toBe(true)
expect(isFormFieldValueUpdated(formField7, formField8)).toBe(true)

expect(isFormFieldValueUpdated(formField9, formField10)).toBe(false)
})
})

0 comments on commit b93269a

Please sign in to comment.