diff --git a/README.md b/README.md index 0442405..a7be09f 100644 --- a/README.md +++ b/README.md @@ -114,3 +114,8 @@ You may pass an array of values as the first argument.
``` +### `$trim()` + +```html +
+``` diff --git a/src/magics.js b/src/magics.js index abf1684..7815c9b 100644 --- a/src/magics.js +++ b/src/magics.js @@ -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; + }); } diff --git a/tests/magics.spec.js b/tests/magics.spec.js index 6e1ad96..f1a54fd 100644 --- a/tests/magics.spec.js +++ b/tests/magics.spec.js @@ -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 = `
`; + mutateDom(); + expect(document.querySelector('div').innerHTML).toEqual('foo'); +}); \ No newline at end of file