Skip to content

Commit

Permalink
adds trim magic
Browse files Browse the repository at this point in the history
  • Loading branch information
eaCe committed Jul 20, 2023
1 parent 9210902 commit 8847181
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,8 @@ You may pass an array of values as the first argument.
<div x-data x-text="$hasClass('foo')" class="foo"></div>
```

### `$trim()`

```html
<div x-data x-text="$trim(' foo ')"></div>
```
11 changes: 11 additions & 0 deletions src/magics.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,15 @@ export default function Magics (Alpine) {

return false
});

/**
* trim the string
*/
Alpine.magic('trim', () => subject => {
if (subject && typeof subject === 'string') {
return subject.trim();
}

return subject;
});
}
6 changes: 6 additions & 0 deletions tests/magics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,9 @@ test('should check whether the element has a specific class or not', async () =>
mutateDom();
expect(document.querySelector('.foo').innerHTML).toEqual('false');
});

test('should trim the string', async () => {
document.body.innerHTML = `<div x-data x-text="$trim(' foo ')"></div>`;
mutateDom();
expect(document.querySelector('div').innerHTML).toEqual('foo');
});

0 comments on commit 8847181

Please sign in to comment.