Skip to content

Commit ff112a4

Browse files
committed
chore: rename filters to snake style, #487
BREAKING CHANGE: keys in `<liquidjs>.filters` are now in snake case (instead of camel case), identical to that in Liquid template.
1 parent 907c4de commit ff112a4

38 files changed

+119
-125
lines changed

.eslintrc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
"@typescript-eslint/no-unused-vars": ["error", { "vars": "all", "args": "none", "ignoreRestSiblings": false }]
3838
},
3939
"overrides": [{
40+
"files": ["**/filters/*.ts"],
41+
"rules": {
42+
"camelcase": "off"
43+
}
44+
}, {
4045
"files": ["**/*.js", "**/*.mjs"],
4146
"rules": {
4247
"@typescript-eslint/no-var-requires": "off"

src/builtin/filters/index.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/builtin/filters/url.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/builtin/filters/array.ts renamed to src/filters/array.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { argumentsToValue, toValue, stringify, caseInsensitiveCompare, isArray, isNil, last as arrayLast, hasOwnProperty } from '../../util/underscore'
2-
import { toArray } from '../../util/collection'
3-
import { isTruthy } from '../../render/boolean'
4-
import { FilterImpl } from '../../template/filter/filter-impl'
5-
import { Scope } from '../../context/scope'
6-
import { isComparable } from '../../drop/comparable'
1+
import { argumentsToValue, toValue, stringify, caseInsensitiveCompare, isArray, isNil, last as arrayLast, hasOwnProperty } from '../util/underscore'
2+
import { toArray } from '../util/collection'
3+
import { isTruthy } from '../render/boolean'
4+
import { FilterImpl } from '../template/filter/filter-impl'
5+
import { Scope } from '../context/scope'
6+
import { isComparable } from '../drop/comparable'
77

88
export const join = argumentsToValue((v: any[], arg: string) => toArray(v).join(arg === undefined ? ' ' : arg))
99
export const last = argumentsToValue((v: any) => isArray(v) ? arrayLast(v) : '')
@@ -25,7 +25,7 @@ export function * sort<T> (this: FilterImpl, arr: T[], property?: string): Itera
2525
}).map(tuple => tuple[0])
2626
}
2727

28-
export function sortNatural<T> (input: T[], property?: string) {
28+
export function sort_natural<T> (input: T[], property?: string) {
2929
input = toValue(input)
3030
const propertyString = stringify(property)
3131
const compare = property === undefined

src/builtin/filters/date.ts renamed to src/filters/date.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import strftime from '../../util/strftime'
2-
import { LiquidDate } from '../../util/liquid-date'
3-
import { toValue, stringify, isString, isNumber } from '../../util/underscore'
4-
import { FilterImpl } from '../../template/filter/filter-impl'
5-
import { TimezoneDate } from '../../util/timezone-date'
1+
import strftime from '../util/strftime'
2+
import { LiquidDate } from '../util/liquid-date'
3+
import { toValue, stringify, isString, isNumber } from '../util/underscore'
4+
import { FilterImpl } from '../template/filter/filter-impl'
5+
import { TimezoneDate } from '../util/timezone-date'
66

77
export function date (this: FilterImpl, v: string | Date, format: string, timeZoneOffset?: number) {
88
const opts = this.context.opts

src/builtin/filters/html.ts renamed to src/filters/html.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { stringify } from '../../util/underscore'
1+
import { stringify } from '../util/underscore'
22

33
const escapeMap = {
44
'&': '&amp;',
@@ -23,14 +23,14 @@ function unescape (str: string) {
2323
return stringify(str).replace(/&(amp|lt|gt|#34|#39);/g, m => unescapeMap[m])
2424
}
2525

26-
export function escapeOnce (str: string) {
26+
export function escape_once (str: string) {
2727
return escape(unescape(stringify(str)))
2828
}
2929

30-
export function newlineToBr (v: string) {
30+
export function newline_to_br (v: string) {
3131
return stringify(v).replace(/\n/g, '<br />\n')
3232
}
3333

34-
export function stripHtml (v: string) {
34+
export function strip_html (v: string) {
3535
return stringify(v).replace(/<script.*?<\/script>|<!--.*?-->|<style.*?<\/style>|<.*?>/g, '')
3636
}

src/filters/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as htmlFilters from './html'
2+
import * as mathFilters from './math'
3+
import * as urlFilters from './url'
4+
import * as arrayFilters from './array'
5+
import * as dateFilters from './date'
6+
import * as stringFilters from './string'
7+
import { Default, json } from './misc'
8+
import { FilterImplOptions } from '../template/filter/filter-impl-options'
9+
10+
export const filters: { [key: string]: FilterImplOptions } = {
11+
...htmlFilters,
12+
...mathFilters,
13+
...urlFilters,
14+
...arrayFilters,
15+
...dateFilters,
16+
...stringFilters,
17+
json,
18+
default: Default
19+
}

src/builtin/filters/math.ts renamed to src/filters/math.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { toValue, argumentsToValue } from '../../util/underscore'
1+
import { toValue, argumentsToValue } from '../util/underscore'
22

33
export const abs = argumentsToValue(Math.abs)
4-
export const atLeast = argumentsToValue(Math.max)
5-
export const atMost = argumentsToValue(Math.min)
4+
export const at_least = argumentsToValue(Math.max)
5+
export const at_most = argumentsToValue(Math.min)
66
export const ceil = argumentsToValue(Math.ceil)
7-
export const dividedBy = argumentsToValue((dividend: number, divisor: number, integerArithmetic = false) => integerArithmetic ? Math.floor(dividend / divisor) : dividend / divisor)
7+
export const divided_by = argumentsToValue((dividend: number, divisor: number, integerArithmetic = false) => integerArithmetic ? Math.floor(dividend / divisor) : dividend / divisor)
88
export const floor = argumentsToValue(Math.floor)
99
export const minus = argumentsToValue((v: number, arg: number) => v - arg)
1010
export const modulo = argumentsToValue((v: number, arg: number) => v % arg)

src/builtin/filters/misc.ts renamed to src/filters/misc.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { isFalsy } from '../../render/boolean'
2-
import { identify, isArray, isString, toValue } from '../../util/underscore'
3-
import { FilterImpl } from '../../template/filter/filter-impl'
1+
import { isFalsy } from '../render/boolean'
2+
import { identify, isArray, isString, toValue } from '../util/underscore'
3+
import { FilterImpl } from '../template/filter/filter-impl'
44

55
export function Default<T1 extends boolean, T2> (this: FilterImpl, value: T1, defaultValue: T2, ...args: Array<[string, any]>): T1 | T2 {
66
value = toValue(value)

src/builtin/filters/string.ts renamed to src/filters/string.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*
44
* * prefer stringify() to String() since `undefined`, `null` should eval ''
55
*/
6-
import { escapeRegExp, stringify } from '../../util/underscore'
7-
import { assert } from '../../util/assert'
6+
import { escapeRegExp, stringify } from '../util/underscore'
7+
import { assert } from '../util/assert'
88

99
export function append (v: string, arg: string) {
1010
assert(arguments.length === 2, 'append expect 2 arguments')
@@ -36,7 +36,7 @@ export function remove (v: string, arg: string) {
3636
return stringify(v).split(String(arg)).join('')
3737
}
3838

39-
export function removeFirst (v: string, l: string) {
39+
export function remove_first (v: string, l: string) {
4040
return stringify(v).replace(String(l), '')
4141
}
4242

@@ -66,7 +66,7 @@ export function strip (v: string, chars?: string) {
6666
return stringify(v).trim()
6767
}
6868

69-
export function stripNewlines (v: string) {
69+
export function strip_newlines (v: string) {
7070
return stringify(v).replace(/\n/g, '')
7171
}
7272

@@ -79,7 +79,7 @@ export function replace (v: string, pattern: string, replacement: string) {
7979
return stringify(v).split(String(pattern)).join(replacement)
8080
}
8181

82-
export function replaceFirst (v: string, arg1: string, arg2: string) {
82+
export function replace_first (v: string, arg1: string, arg2: string) {
8383
return stringify(v).replace(String(arg1), arg2)
8484
}
8585

0 commit comments

Comments
 (0)