feat: allow strip filter with specified char, closes #390

This commit is contained in:
Harttle
2022-03-06 00:20:51 +08:00
parent 01014edc49
commit c503cb23df
3 changed files with 33 additions and 10 deletions
+18 -4
View File
@@ -3,7 +3,7 @@
* *
* * prefer stringify() to String() since `undefined`, `null` should eval '' * * prefer stringify() to String() since `undefined`, `null` should eval ''
*/ */
import { stringify } from '../../util/underscore' import { escapeRegExp, stringify } from '../../util/underscore'
import { assert } from '../../util/assert' import { assert } from '../../util/assert'
export function append (v: string, arg: string) { export function append (v: string, arg: string) {
@@ -16,7 +16,11 @@ export function prepend (v: string, arg: string) {
return stringify(arg) + stringify(v) return stringify(arg) + stringify(v)
} }
export function lstrip (v: string) { export function lstrip (v: string, chars?: string) {
if (chars) {
chars = escapeRegExp(stringify(chars))
return stringify(v).replace(new RegExp(`^[${chars}]+`, 'g'), '')
}
return stringify(v).replace(/^\s+/, '') return stringify(v).replace(/^\s+/, '')
} }
@@ -36,7 +40,11 @@ export function removeFirst (v: string, l: string) {
return stringify(v).replace(String(l), '') return stringify(v).replace(String(l), '')
} }
export function rstrip (str: string) { export function rstrip (str: string, chars?: string) {
if (chars) {
chars = escapeRegExp(stringify(chars))
return stringify(str).replace(new RegExp(`[${chars}]+$`, 'g'), '')
}
return stringify(str).replace(/\s+$/, '') return stringify(str).replace(/\s+$/, '')
} }
@@ -48,7 +56,13 @@ export function split (v: string, arg: string) {
return arr return arr
} }
export function strip (v: string) { export function strip (v: string, chars?: string) {
if (chars) {
chars = escapeRegExp(stringify(chars))
return stringify(v)
.replace(new RegExp(`^[${chars}]+`, 'g'), '')
.replace(new RegExp(`[${chars}]+$`, 'g'), '')
}
return stringify(v).trim() return stringify(v).trim()
} }
+4
View File
@@ -156,3 +156,7 @@ export function caseInsensitiveCompare (a: any, b: any) {
export function argumentsToValue<F extends (...args: any) => any> (fn: F) { export function argumentsToValue<F extends (...args: any) => any> (fn: F) {
return (...args: Parameters<F>) => fn(...args.map(toValue)) return (...args: Parameters<F>) => fn(...args.map(toValue))
} }
export function escapeRegExp (text: string) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
}
+11 -6
View File
@@ -86,9 +86,10 @@ describe('filters/string', function () {
it('should support upcase', () => test('{{ "Parker Moore" | upcase }}', 'PARKER MOORE')) it('should support upcase', () => test('{{ "Parker Moore" | upcase }}', 'PARKER MOORE'))
it('should return empty for undefined', () => test('{{ foo | upcase }}', '')) it('should return empty for undefined', () => test('{{ foo | upcase }}', ''))
}) })
it('should support lstrip', function () { it('should support lstrip', async () => {
const src = '{{ " So much room for activities! " | lstrip }}' const src = '{{ " So much room for activities! " | lstrip }}'
return test(src, 'So much room for activities! ') await test(src, 'So much room for activities! ')
await test('{{ "foobarcoo" | lstrip: "fo" }}', 'barcoo')
}) })
it('should support prepend', function () { it('should support prepend', function () {
return test('{% assign url = "liquidmarkup.com" %}' + return test('{% assign url = "liquidmarkup.com" %}' +
@@ -115,9 +116,10 @@ describe('filters/string', function () {
'{{ my_string | replace_first: "my", "your" }}', '{{ my_string | replace_first: "my", "your" }}',
'\nTake your protein pills and put my helmet on') '\nTake your protein pills and put my helmet on')
}) })
it('should support rstrip', function () { it('should support rstrip', async () => {
return test('{{ " So much room for activities! " | rstrip }}', await test('{{ " So much room for activities! " | rstrip }}',
' So much room for activities!') ' So much room for activities!')
await test('{{ "foobarcoo" | rstrip: "fco" }}', 'foobar')
}) })
it('should support split', function () { it('should support split', function () {
return test('{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}' + return test('{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}' +
@@ -126,9 +128,12 @@ describe('filters/string', function () {
'{% endfor %}', '{% endfor %}',
'John Paul George Ringo ') 'John Paul George Ringo ')
}) })
it('should support strip', function () { it('should support strip', async () => {
return test('{{ " So much room for activities! " | strip }}', await test('{{ " So much room for activities! " | strip }}',
'So much room for activities!') 'So much room for activities!')
await test('{{ " So much room for activities! " | strip: "So " }}',
'much room for activities!')
await test('{{ "&[]{}" | strip: "&[]{}" }}', '')
}) })
it('should support strip_newlines', function () { it('should support strip_newlines', function () {
return test('{% capture string_with_newlines %}\n' + return test('{% capture string_with_newlines %}\n' +