Skip to content

Commit 283f978

Browse files
committed
extend is and isNot helper with support for RegExp as second argument
1 parent 4f38a92 commit 283f978

File tree

6 files changed

+40
-12
lines changed

6 files changed

+40
-12
lines changed

docs/documentation/utilities/is-&-isnot.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ html`${when(is(status, 'pending'), html`<p>loading...</p>`, html`<p>done</p>`)}`
1818

1919
Both the `is` and `isNot` take two arguments, a state or some data, and a value or a checker. They will always return a boolean as the result.
2020

21+
### RegExp
22+
23+
You can also use `is` and `isNot` with regular expressions to check if a string matches a pattern.
24+
25+
```javascript
26+
const [email, setEmail] = state('');
27+
28+
const isEmailValid = is(email, /^[^\s@]+@[^\s@]+\.[^\s@]+$/);
29+
const isEmailInvalid = isNot(email, /^[^\s@]+@[^\s@]+\.[^\s@]+$/);
30+
```
31+
2132
### Checker
2233

2334
The more advance way to use the `is` and `isNot` utilities is by providing a function as second argument that is called with the value and must return a boolean.
@@ -32,4 +43,4 @@ const isGreaterThanTen = is(count, (n) => n > 10)
3243
const isNotPending = isNot(status, (st) => st !== 'pending')
3344
```
3445

35-
When they consume state as first argument, their result get re-evaluated with every change which allows it to be handy in quick validators.
46+
When they consume `StateGetter` as first argument, their result get re-evaluated with every change which allows it to be handy in quick validators.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@beforesemicolon/markup",
3-
"version": "1.14.1",
3+
"version": "1.15.0",
44
"description": "Reactive HTML Templating System",
55
"engines": {
66
"node": ">=18.16.0"

src/helpers/is-not.helper.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ describe('isNot', () => { // @ts-ignore
44

55
it('should handle isNot', () => {
66
expect(isNot(() => true, true)()).toBe(false)
7+
expect(isNot(() => true, false)()).toBe(true)
78
expect(isNot(() => true, () => false)()).toBe(true)
9+
expect(isNot(12, /[0-9]+/)()).toBe(false)
10+
expect(isNot(12, /^[0-9]$/)()).toBe(true)
11+
expect(isNot('idle', /^(idle|loading)$/)()).toBe(false)
812
})
913
})

src/helpers/is-not.helper.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import { is } from './is.helper.ts'
88
/**
99
* checks whether the state value is NOT equal to provided value or return value of the function checker
1010
* @param st
11-
* @param checker
11+
* @param dataOrCheckerFn
1212
*/
1313
export const isNot =
1414
<T>(
1515
st: T | StateGetter<T>,
16-
checker: HelperValueChecker<T> | AnythingButAFunction<T>
16+
dataOrCheckerFn: HelperValueChecker<T> | AnythingButAFunction<T>
1717
) =>
1818
() =>
19-
!is(st, checker)()
19+
!is(st, dataOrCheckerFn)()

src/helpers/is.helper.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ describe('is', () => { // @ts-ignore
44

55
it('should handle is', () => {
66
expect(is(() => true, true)()).toBe(true)
7-
expect(is(() => true, () =>false)()).toBe(false)
7+
expect(is(() => true, false)()).toBe(false)
8+
expect(is(() => true, () => false)()).toBe(false)
9+
expect(is(12, /[0-9]+/)()).toBe(true)
10+
expect(is(12, /^[0-9]$/)()).toBe(false)
11+
expect(is('idle', /^(idle|loading)$/)()).toBe(true)
812
})
913
})

src/helpers/is.helper.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,23 @@ import { val } from './val.ts'
88
/**
99
* checks whether the state value is equal to provided value or return value of the function checker
1010
* @param st
11-
* @param checker
11+
* @param dataOrCheckerFn
1212
*/
1313
export const is =
1414
<T>(
1515
st: T | StateGetter<T>,
16-
checker: HelperValueChecker<T> | AnythingButAFunction<T>
16+
dataOrCheckerFn: HelperValueChecker<T> | AnythingButAFunction<T>
1717
) =>
18-
() =>
19-
typeof checker === 'function'
20-
? Boolean((checker as HelperValueChecker<T>)(val(st)))
21-
: val(st) === checker
18+
() => {
19+
const value = val<T>(st)
20+
21+
if (typeof dataOrCheckerFn === 'function') {
22+
return Boolean((dataOrCheckerFn as HelperValueChecker<T>)(value))
23+
}
24+
25+
if (dataOrCheckerFn instanceof RegExp) {
26+
return dataOrCheckerFn.test(String(value))
27+
}
28+
29+
return value === dataOrCheckerFn
30+
}

0 commit comments

Comments
 (0)