Skip to content

Commit 562cc8a

Browse files
committed
feat: allow user-defined string formats (close #1227)
This commit extends the "def" syntax to define string formats. By writing `def @foo: ...;`, `@foo` string format is available in the following query. This idea is influenced by jaq.
1 parent 588ff18 commit 562cc8a

File tree

11 files changed

+1256
-1169
lines changed

11 files changed

+1256
-1169
lines changed

docs/content/manual/dev/manual.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,13 @@ sections:
21952195
Note that the slashes, question mark, etc. in the URL are
21962196
not escaped, as they were part of the string literal.
21972197
2198+
You can also define custom string formats using "def" syntax,
2199+
just like defining functions.
2200+
2201+
def @foo: gsub("a+"; ".");
2202+
2203+
Unlike functions, string formats cannot take arguments.
2204+
21982205
examples:
21992206
- program: '@html'
22002207
input: '"This works if x < y"'
@@ -2212,6 +2219,10 @@ sections:
22122219
input: '"VGhpcyBpcyBhIG1lc3NhZ2U="'
22132220
output: ['"This is a message"']
22142221

2222+
- program: 'def @foo: gsub("a+"; "."); @foo, @foo "a \(.) \(. + .) a"'
2223+
input: '"aba"'
2224+
output: ['".b."', '"a .b. .b.b. a"']
2225+
22152226
- title: "Dates"
22162227
body: |
22172228

jq.1.prebuilt

Lines changed: 21 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)