Skip to content

Commit 69b2c58

Browse files
YacovGoldYacov
andauthored
feat: add unregisterFilter method (#946)
* feat: add unregisterFilter method * docs: show how to re-register built-in filters --------- Co-authored-by: Yacov <yacov@noemail>
1 parent 88ae297 commit 69b2c58

3 files changed

Lines changed: 52 additions & 2 deletions

File tree

docs/source/tutorials/register-filters-tags.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,23 @@ See existing filter implementations here: <https://github.com/harttle/liquidjs/t
6262

6363
## Unregister Tags/Filters
6464

65-
In some cases it's desirable to disable some tags/filters (see [#324](https://github.com/harttle/liquidjs/issues/324)). You'll need to register a dummy tag/filter that throws a corresponding Error.
65+
Filters can be unregistered by name:
66+
67+
```javascript
68+
engine.unregisterFilter('plus')
69+
```
70+
71+
With [`strictFilters`][strict-filters] enabled, using an unregistered filter will throw an error. Otherwise, the filter will be skipped.
72+
73+
Built-in filters can be registered again using the exported `filters` object:
74+
75+
```javascript
76+
import { filters } from 'liquidjs'
77+
78+
engine.registerFilter('plus', filters.plus)
79+
```
80+
81+
To disable a tag, or to make a disabled filter throw regardless of `strictFilters`, register a dummy implementation that throws a corresponding error (see [#324](https://github.com/harttle/liquidjs/issues/324)):
6682

6783
```javascript
6884
// disable a tag
@@ -81,3 +97,5 @@ function disabledFilter(name) {
8197
}
8298
engine.registerFilter('plus', disabledFilter('plus'));
8399
```
100+
101+
[strict-filters]: /tutorials/options.html#strict

src/liquid.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ export class Liquid {
101101
public registerFilter (name: string, filter: FilterImplOptions) {
102102
this.filters[name] = filter
103103
}
104+
public unregisterFilter (name: string) {
105+
delete this.filters[name]
106+
}
104107
public registerTag (name: string, tag: TagClass | TagImplOptions) {
105108
this.tags[name] = isFunction(tag) ? tag : createTagClass(tag)
106109
}

test/integration/liquid/register-filters.spec.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Liquid } from '../../../src/liquid'
1+
import { Liquid, filters } from '../../../src'
22

33
describe('liquid#registerFilter()', function () {
44
let liquid: Liquid
@@ -67,3 +67,32 @@ describe('liquid#registerFilter()', function () {
6767
await expect(new Liquid({ strictFilters: true }).parseAndRender('{{ 1 | constructor }}')).rejects.toThrow('undefined filter')
6868
})
6969
})
70+
71+
describe('liquid#unregisterFilter()', function () {
72+
let liquid: Liquid
73+
beforeEach(() => { liquid = new Liquid() })
74+
75+
it('should unregister a custom filter', async () => {
76+
liquid.registerFilter('greet', value => `hello ${value}`)
77+
liquid.unregisterFilter('greet')
78+
const html = await liquid.parseAndRender('{{ "world" | greet }}')
79+
return expect(html).toBe('world')
80+
})
81+
82+
it('should unregister a built-in filter', () => {
83+
liquid = new Liquid({ strictFilters: true })
84+
liquid.unregisterFilter('upcase')
85+
return expect(liquid.parseAndRender('{{ "foo" | upcase }}')).rejects.toThrow('undefined filter: upcase')
86+
})
87+
88+
it('should support re-registering a built-in filter', async () => {
89+
liquid.unregisterFilter('upcase')
90+
liquid.registerFilter('upcase', filters.upcase)
91+
const html = await liquid.parseAndRender('{{ "foo" | upcase }}')
92+
return expect(html).toBe('FOO')
93+
})
94+
95+
it('should not throw for an unknown filter', () => {
96+
expect(() => liquid.unregisterFilter('unknown')).not.toThrow()
97+
})
98+
})

0 commit comments

Comments
 (0)