rbjq 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 52c3e4deac2047a2c2d54cc338b64eda7157a3d1670b2af975f439eabe69ca23
4
+ data.tar.gz: c88f49272ab46fbf4ec2279166cda8f96aae55b19dc0b750fe23c20b6dbff9a9
5
+ SHA512:
6
+ metadata.gz: 030c8a230a52de382fcd134f2717e6b8831dae68eb99022877f64166bf468a937476bf3e64b644c401591ca9d1648143bbfb64fa31ba53993c1325b162f4dbfb
7
+ data.tar.gz: 12057e4fc6ddb714416cb8704e9602f826fdd843eec0c02d4c68e711dc38a7dd11557216a7467d7fda5ed542ff2d5458b34503e9f0c87ec7f5deef1a2e1ce1c1
data/LICENSE ADDED
@@ -0,0 +1,121 @@
1
+ Creative Commons Legal Code
2
+
3
+ CC0 1.0 Universal
4
+
5
+ CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
6
+ LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
7
+ ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
8
+ INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
9
+ REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
10
+ PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
11
+ THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
12
+ HEREUNDER.
13
+
14
+ Statement of Purpose
15
+
16
+ The laws of most jurisdictions throughout the world automatically confer
17
+ exclusive Copyright and Related Rights (defined below) upon the creator
18
+ and subsequent owner(s) (each and all, an "owner") of an original work of
19
+ authorship and/or a database (each, a "Work").
20
+
21
+ Certain owners wish to permanently relinquish those rights to a Work for
22
+ the purpose of contributing to a commons of creative, cultural and
23
+ scientific works ("Commons") that the public can reliably and without fear
24
+ of later claims of infringement build upon, modify, incorporate in other
25
+ works, reuse and redistribute as freely as possible in any form whatsoever
26
+ and for any purposes, including without limitation commercial purposes.
27
+ These owners may contribute to the Commons to promote the ideal of a free
28
+ culture and the further production of creative, cultural and scientific
29
+ works, or to gain reputation or greater distribution for their Work in
30
+ part through the use and efforts of others.
31
+
32
+ For these and/or other purposes and motivations, and without any
33
+ expectation of additional consideration or compensation, the person
34
+ associating CC0 with a Work (the "Affirmer"), to the extent that he or she
35
+ is an owner of Copyright and Related Rights in the Work, voluntarily
36
+ elects to apply CC0 to the Work and publicly distribute the Work under its
37
+ terms, with knowledge of his or her Copyright and Related Rights in the
38
+ Work and the meaning and intended legal effect of CC0 on those rights.
39
+
40
+ 1. Copyright and Related Rights. A Work made available under CC0 may be
41
+ protected by copyright and related or neighboring rights ("Copyright and
42
+ Related Rights"). Copyright and Related Rights include, but are not
43
+ limited to, the following:
44
+
45
+ i. the right to reproduce, adapt, distribute, perform, display,
46
+ communicate, and translate a Work;
47
+ ii. moral rights retained by the original author(s) and/or performer(s);
48
+ iii. publicity and privacy rights pertaining to a person's image or
49
+ likeness depicted in a Work;
50
+ iv. rights protecting against unfair competition in regards to a Work,
51
+ subject to the limitations in paragraph 4(a), below;
52
+ v. rights protecting the extraction, dissemination, use and reuse of data
53
+ in a Work;
54
+ vi. database rights (such as those arising under Directive 96/9/EC of the
55
+ European Parliament and of the Council of 11 March 1996 on the legal
56
+ protection of databases, and under any national implementation
57
+ thereof, including any amended or successor version of such
58
+ directive); and
59
+ vii. other similar, equivalent or corresponding rights throughout the
60
+ world based on applicable law or treaty, and any national
61
+ implementations thereof.
62
+
63
+ 2. Waiver. To the greatest extent permitted by, but not in contravention
64
+ of, applicable law, Affirmer hereby overtly, fully, permanently,
65
+ irrevocably and unconditionally waives, abandons, and surrenders all of
66
+ Affirmer's Copyright and Related Rights and associated claims and causes
67
+ of action, whether now known or unknown (including existing as well as
68
+ future claims and causes of action), in the Work (i) in all territories
69
+ worldwide, (ii) for the maximum duration provided by applicable law or
70
+ treaty (including future time extensions), (iii) in any current or future
71
+ medium and for any number of copies, and (iv) for any purpose whatsoever,
72
+ including without limitation commercial, advertising or promotional
73
+ purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
74
+ member of the public at large and to the detriment of Affirmer's heirs and
75
+ successors, fully intending that such Waiver shall not be subject to
76
+ revocation, rescission, cancellation, termination, or any other legal or
77
+ equitable action to disrupt the quiet enjoyment of the Work by the public
78
+ as contemplated by Affirmer's express Statement of Purpose.
79
+
80
+ 3. Public License Fallback. Should any part of the Waiver for any reason
81
+ be judged legally invalid or ineffective under applicable law, then the
82
+ Waiver shall be preserved to the maximum extent permitted taking into
83
+ account Affirmer's express Statement of Purpose. In addition, to the
84
+ extent the Waiver is so judged Affirmer hereby grants to each affected
85
+ person a royalty-free, non transferable, non sublicensable, non exclusive,
86
+ irrevocable and unconditional license to exercise Affirmer's Copyright and
87
+ Related Rights in the Work (i) in all territories worldwide, (ii) for the
88
+ maximum duration provided by applicable law or treaty (including future
89
+ time extensions), (iii) in any current or future medium and for any number
90
+ of copies, and (iv) for any purpose whatsoever, including without
91
+ limitation commercial, advertising or promotional purposes (the
92
+ "License"). The License shall be deemed effective as of the date CC0 was
93
+ applied by Affirmer to the Work. Should any part of the License for any
94
+ reason be judged legally invalid or ineffective under applicable law, such
95
+ partial invalidity or ineffectiveness shall not invalidate the remainder
96
+ of the License, and in such case Affirmer hereby affirms that he or she
97
+ will not (i) exercise any of his or her remaining Copyright and Related
98
+ Rights in the Work or (ii) assert any associated claims and causes of
99
+ action with respect to the Work, in either case contrary to Affirmer's
100
+ express Statement of Purpose.
101
+
102
+ 4. Limitations and Disclaimers.
103
+
104
+ a. No trademark or patent rights held by Affirmer are waived, abandoned,
105
+ surrendered, licensed or otherwise affected by this document.
106
+ b. Affirmer offers the Work as-is and makes no representations or
107
+ warranties of any kind concerning the Work, express, implied,
108
+ statutory or otherwise, including without limitation warranties of
109
+ title, merchantability, fitness for a particular purpose, non
110
+ infringement, or the absence of latent or other defects, accuracy, or
111
+ the present or absence of errors, whether or not discoverable, all to
112
+ the greatest extent permissible under applicable law.
113
+ c. Affirmer disclaims responsibility for clearing rights of other persons
114
+ that may apply to the Work or any use thereof, including without
115
+ limitation any person's Copyright and Related Rights in the Work.
116
+ Further, Affirmer disclaims responsibility for obtaining any necessary
117
+ consents, permissions or other rights required for any use of the
118
+ Work.
119
+ d. Affirmer understands and acknowledges that Creative Commons is not a
120
+ party to this document and has no duty or obligation with respect to
121
+ this CC0 or use of the Work.
data/LICENSE-jq ADDED
@@ -0,0 +1,176 @@
1
+ jq is copyright (C) 2012 Stephen Dolan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+
23
+
24
+ jq's documentation (everything found under the docs/ subdirectory in
25
+ the source tree) is licensed under the Creative Commons CC BY 3.0
26
+ license, which can be found at:
27
+
28
+ https://creativecommons.org/licenses/by/3.0/
29
+
30
+ The documentation website includes a copy of Twitter's Bootstrap and
31
+ relies on Bonsai, Liquid templates and various other projects, look
32
+ them up for detailed licensing conditions.
33
+
34
+
35
+
36
+ jq incorporates David M. Gay's dtoa.c and g_fmt.c, which bear the
37
+ following notices:
38
+
39
+ dtoa.c:
40
+ The author of this software is David M. Gay.
41
+
42
+ Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
43
+
44
+ Permission to use, copy, modify, and distribute this software for any
45
+ purpose without fee is hereby granted, provided that this entire notice
46
+ is included in all copies of any software which is or includes a copy
47
+ or modification of this software and in all copies of the supporting
48
+ documentation for such software.
49
+
50
+ THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
51
+ WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
52
+ REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
53
+ OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
54
+
55
+ g_fmt.c:
56
+ The author of this software is David M. Gay.
57
+
58
+ Copyright (c) 1991, 1996 by Lucent Technologies.
59
+
60
+ Permission to use, copy, modify, and distribute this software for any
61
+ purpose without fee is hereby granted, provided that this entire notice
62
+ is included in all copies of any software which is or includes a copy
63
+ or modification of this software and in all copies of the supporting
64
+ documentation for such software.
65
+
66
+ THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
67
+ WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
68
+ REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
69
+ OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
70
+
71
+
72
+
73
+ jq uses parts of the open source C library "decNumber", which is distributed
74
+ under the following license:
75
+
76
+
77
+ ICU License - ICU 1.8.1 and later
78
+
79
+ COPYRIGHT AND PERMISSION NOTICE
80
+
81
+ Copyright (c) 1995-2005 International Business Machines Corporation and others
82
+ All rights reserved.
83
+
84
+ Permission is hereby granted, free of charge, to any person obtaining a
85
+ copy of this software and associated documentation files (the
86
+ "Software"), to deal in the Software without restriction, including
87
+ without limitation the rights to use, copy, modify, merge, publish,
88
+ distribute, and/or sell copies of the Software, and to permit persons
89
+ to whom the Software is furnished to do so, provided that the above
90
+ copyright notice(s) and this permission notice appear in all copies of
91
+ the Software and that both the above copyright notice(s) and this
92
+ permission notice appear in supporting documentation.
93
+
94
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
95
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
96
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
97
+ OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
98
+ HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
99
+ INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
100
+ FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
101
+ NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
102
+ WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
103
+
104
+ Except as contained in this notice, the name of a copyright holder
105
+ shall not be used in advertising or otherwise to promote the sale, use
106
+ or other dealings in this Software without prior written authorization
107
+ of the copyright holder.
108
+
109
+ --------------------------------------------------------------------------------
110
+ All trademarks and registered trademarks mentioned herein are the property of their respective owners.
111
+
112
+
113
+
114
+ jv_thread.h is copied from Heimdal's lib/base/heimbase.h and some code
115
+ in jv.c is copied from Heimdal's lib/base/dll.c:
116
+
117
+
118
+ Portions Copyright (c) 2016 Kungliga Tekniska Högskolan
119
+ (Royal Institute of Technology, Stockholm, Sweden).
120
+ All rights reserved.
121
+
122
+ Redistribution and use in source and binary forms, with or without
123
+ modification, are permitted provided that the following conditions
124
+ are met:
125
+
126
+ 1. Redistributions of source code must retain the above copyright
127
+ notice, this list of conditions and the following disclaimer.
128
+
129
+ 2. Redistributions in binary form must reproduce the above copyright
130
+ notice, this list of conditions and the following disclaimer in the
131
+ documentation and/or other materials provided with the distribution.
132
+
133
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
134
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
135
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
136
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
137
+ COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
138
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
139
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
140
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
141
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
142
+ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
143
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
144
+ OF THE POSSIBILITY OF SUCH DAMAGE.
145
+
146
+
147
+
148
+ jq uses a modified version of NetBSD implementation strptime(),
149
+ which is distributed under the following license:
150
+
151
+ Copyright (c) 1997, 1998, 2005, 2008 The NetBSD Foundation, Inc.
152
+ All rights reserved.
153
+
154
+ This code was contributed to The NetBSD Foundation by Klaus Klein.
155
+ Heavily optimised by David Laight
156
+
157
+ Redistribution and use in source and binary forms, with or without
158
+ modification, are permitted provided that the following conditions
159
+ are met:
160
+ 1. Redistributions of source code must retain the above copyright
161
+ notice, this list of conditions and the following disclaimer.
162
+ 2. Redistributions in binary form must reproduce the above copyright
163
+ notice, this list of conditions and the following disclaimer in the
164
+ documentation and/or other materials provided with the distribution.
165
+
166
+ THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
167
+ ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
168
+ TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
169
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
170
+ BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
171
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
172
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
173
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
174
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
175
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
176
+ POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,155 @@
1
+ # rbjq
2
+
3
+ [![test](https://github.com/winebarrel/rbjq/actions/workflows/test.yml/badge.svg)](https://github.com/winebarrel/rbjq/actions/workflows/test.yml)
4
+ [![codecov](https://codecov.io/gh/winebarrel/rbjq/graph/badge.svg)](https://codecov.io/gh/winebarrel/rbjq)
5
+ [![AI Generated](https://img.shields.io/badge/AI%20Generated-Claude-orange?logo=anthropic)](https://claude.com/product/claude-code)
6
+
7
+ A pure Ruby implementation of [jq](https://jqlang.github.io/jq/). It has no
8
+ native extensions and no runtime dependencies, and works both as a library
9
+ and as a jq-compatible command.
10
+
11
+ ## Installation
12
+
13
+ ```sh
14
+ gem install rbjq
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ### Library
20
+
21
+ ```ruby
22
+ require 'rbjq'
23
+
24
+ Rbjq.query({ 'users' => [{ 'name' => 'a', 'age' => 20 }, { 'name' => 'b', 'age' => 40 }] },
25
+ '.users[] | select(.age > $min) | .name', vars: { min: 30 })
26
+ #=> ["b"]
27
+
28
+ # Compile once, run many times
29
+ prog = Rbjq.compile('map(. * 2)')
30
+ prog.run([1, 2, 3]) #=> [[2, 4, 6]]
31
+ prog.each([1, 2]) { |v| p v } # streams outputs
32
+
33
+ # JSON text in (may contain several values), Ruby values out
34
+ Rbjq.query_json('{"a":1} {"a":2}', '.a') #=> [1, 2]
35
+
36
+ # `input` / `inputs`
37
+ Rbjq.compile('[inputs]').run(nil, inputs: [1, 2, 3]) #=> [[1, 2, 3]]
38
+ ```
39
+
40
+ #### jq functions in Ruby
41
+
42
+ You can implement jq functions in Ruby, either for one program or for every
43
+ program compiled afterwards:
44
+
45
+ ```ruby
46
+ # for one program
47
+ Rbjq.query([1, 2], '.[] | double', functions: { 'double' => ->(x) { x * 2 } }) #=> [2, 4]
48
+
49
+ # for every program compiled afterwards
50
+ Rbjq.define_function('add_n') { |x, n| x + n }
51
+ Rbjq.query(3, 'add_n(1, 10)') #=> [4, 13]
52
+ Rbjq.undefine_function('add_n')
53
+ ```
54
+
55
+ - The first parameter is the input; the others are the arguments, given as
56
+ evaluated values like jq's `$x` parameters. When an argument produces
57
+ several values, the function is called for each combination, with the
58
+ first argument varying slowest (`f(1,2; 10,20)` is called with `1, 10`,
59
+ `1, 20`, `2, 10`, `2, 20`).
60
+ - The input and the arguments are deep-frozen copies: a function cannot
61
+ change jq's values in place (modifying them raises `FrozenError`).
62
+ - The number of jq arguments comes from the callable's arity. For optional or
63
+ rest parameters, give it in the name: `'count/2' => ->(x, *args) { ... }`.
64
+ - A function returns one value. Raise `Rbjq::JQError` to make a jq error that
65
+ `try` can catch; other exceptions propagate to the caller.
66
+ - Lookup order: jq definitions (`def`) in the program, then `functions:`,
67
+ then `Rbjq.define_function`, then the builtins. Functions defined with
68
+ `Rbjq.define_function` are captured when a program is compiled.
69
+
70
+ Values are plain Ruby objects (`nil`, `true`, `false`, `Integer`, `Float`,
71
+ `String`, `Array`, `Hash`). Symbol keys are converted to strings.
72
+
73
+ Errors:
74
+
75
+ - `Rbjq::CompileError`: syntax errors and undefined functions or variables
76
+ - `Rbjq::JQError`: runtime errors (`#value` holds the error value, e.g. from `error({...})`)
77
+ - `Rbjq::ParseError` (a `JQError`): invalid JSON
78
+
79
+ ### Command line
80
+
81
+ ```sh
82
+ echo '{"foo": [1, 2]}' | rbjq '.foo | add'
83
+ rbjq -n --arg name world '"hello \($name)"'
84
+ rbjq -r '.[] | @csv' data.json
85
+ ```
86
+
87
+ Supported options: `-n`, `-r`, `-j`, `--raw-output0`, `-a`, `-c`, `-s`, `-R`,
88
+ `-S`, `-e`, `-C`, `-M`, `--tab`, `--indent n`, `-f file`, `--arg`,
89
+ `--argjson`, `--slurpfile`, `--rawfile`, `--args`, `--jsonargs`, `--stream`,
90
+ `--seq`, `--unbuffered`. Exit codes follow jq: 1 or 4 with `-e`, 2 for usage
91
+ errors and unreadable files, 3 for compile errors, and 5 for runtime errors
92
+ and invalid JSON input (decided by the last input, like jq).
93
+
94
+ ## Compatibility
95
+
96
+ rbjq targets jq 1.8.2:
97
+
98
+ - Full language: pipes, generators, `reduce`/`foreach`, `if/elif/else`,
99
+ `try/catch`, `?`, `label`/`break`, `def` with filter and `$value`
100
+ parameters, recursion, destructuring including `?//`, string interpolation,
101
+ `@format` strings, `$__loc__`, optional/alternative operators, and all
102
+ assignment operators (`=`, `|=`, `+=`, `//=`, ...)
103
+ - Path expressions: `path/1`, `paths`, `getpath`/`setpath`/`delpaths`, `del`,
104
+ `pick`, `to_entries`, `tostream`/`fromstream`/`truncate_stream`, `--stream`
105
+ - The builtins of jq 1.8.2, including regex functions (`test`, `match`,
106
+ `capture`, `scan`, `splits`, `sub`, `gsub`), date functions, math functions
107
+ (including `j0`/`y0` and friends), `limit`, `skip`, `first`, `nth`,
108
+ `until`, `repeat`, `walk`, `pick`, `toboolean`, `trimstr`, `@urid`,
109
+ `input`/`inputs`, `input_line_number`, `getpath`, `bsearch`, `JOIN`, ...
110
+ - jq's error messages, including JSON parse errors
111
+ - Depth limits like jq: values nested more than 10,000 levels deep are
112
+ reported as too deep (parsing, printing, comparison, paths, ...)
113
+ - Output: jq's number formatting, pretty printing (`--indent`, `--tab`),
114
+ colors (`JQ_COLORS`), `--sort-keys`, `--ascii-output`, `--seq`
115
+
116
+ Tests:
117
+
118
+ - `test/upstream_test.rb`: jq's own test suite from jq 1.8.2 (879 cases;
119
+ 15 are skipped). `rake test` downloads it into `test/jq/` first
120
+ (`rake jq:download`; the files are pinned by tag and SHA-256).
121
+ - `test/spec/`: specification tests by area, in jq's test format, whose
122
+ expected outputs were recorded from jq 1.8.2
123
+ - `test/fixtures/jq_compat.tsv`: 650+ more cases recorded from jq 1.8.2
124
+
125
+ Known differences:
126
+
127
+ - Numbers are IEEE 754 doubles, like jq built without decNumber
128
+ (`have_decnum` is `false`): number literals are not preserved, so `1.0`
129
+ prints as `1` and `100000000000000000001` as `100000000000000000000`.
130
+ - `import` / `include` (modules) are not supported.
131
+ - Regular expressions use Ruby's Onigmo engine (close to jq's Oniguruma).
132
+ - `strftime` always uses the C locale, and `gamma` is `lgamma`.
133
+ - Bessel functions may differ from libm in the last digits.
134
+
135
+ ### Performance and recursion
136
+
137
+ Evaluation is a tree-walking interpreter on Ruby blocks. Expect roughly 10x
138
+ the run time of the C jq on large inputs.
139
+
140
+ The evaluator uses the Ruby stack, so the maximum recursion depth of a jq
141
+ program depends on the stack size and the platform. The `rbjq` command runs
142
+ with a 1GB thread stack, which is enough for tens of thousands of levels.
143
+ If the stack is exhausted, it reports `stack overflow` and exits with
144
+ status 5. You can change the size with the `RUBY_THREAD_VM_STACK_SIZE` and
145
+ `RUBY_THREAD_MACHINE_STACK_SIZE` environment variables. When you use the
146
+ library with deep recursion, run it in a thread and start Ruby with these
147
+ variables set to larger values. With the default Ruby stack, the limit is
148
+ around 1,000 levels.
149
+
150
+ ## Development
151
+
152
+ ```sh
153
+ bundle install
154
+ bundle exec rake test # downloads jq's test suite on the first run
155
+ ```
data/exe/rbjq ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # jq programs can recurse deeply; the evaluator uses the Ruby stack, so run it
5
+ # in a thread with a large stack. Thread stack sizes are read from the
6
+ # environment at interpreter start-up, hence the one-time re-exec.
7
+ STACK_SIZE = (1024 * 1024 * 1024).to_s
8
+ unless ENV['RBJQ_STACK_CONFIGURED']
9
+ ENV['RBJQ_STACK_CONFIGURED'] = '1'
10
+ ENV['RUBY_THREAD_VM_STACK_SIZE'] ||= STACK_SIZE
11
+ ENV['RUBY_THREAD_MACHINE_STACK_SIZE'] ||= STACK_SIZE
12
+ begin
13
+ require 'rbconfig'
14
+ exec(RbConfig.ruby, __FILE__, *ARGV)
15
+ rescue SystemCallError
16
+ # fall through and run with the default stack
17
+ end
18
+ end
19
+
20
+ $LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
21
+ require 'rbjq/cli'
22
+
23
+ Thread.report_on_exception = false
24
+ status = Thread.new { Rbjq::CLI.start(ARGV) }.value
25
+ exit status
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rbjq
4
+ # Bessel functions of the first and second kind (j0, j1, jn, y0, y1, yn).
5
+ # Ruby's Math module does not provide them.
6
+ #
7
+ # J_n uses Bessel's integral, which the trapezoidal rule evaluates to
8
+ # machine precision because the integrand is periodic. Y_0 and Y_1 use
9
+ # their power series (summed exactly with Rational) for moderate x and the
10
+ # asymptotic expansion for large x; Y_n uses the forward recurrence.
11
+ module Bessel
12
+ module_function
13
+
14
+ EULER = 0.5772156649015329
15
+
16
+ def jn(n, x)
17
+ x = x.to_f
18
+ return Float::NAN if x.nan?
19
+ return 0.0 if x.infinite?
20
+
21
+ n = n.to_i
22
+ return (n.even? ? 1 : -1) * jn(-n, x) if n.negative?
23
+ return (n.even? ? 1 : -1) * jn(n, -x) if x.negative?
24
+ return (n.zero? ? 1.0 : 0.0) if x.zero?
25
+
26
+ return asymptotic_j(n, x) if x > 50 && x > n * n
27
+
28
+ # J_n(x) = 1/pi * integral_0^pi cos(n t - x sin t) dt
29
+ m = (x + n).ceil + 40
30
+ h = Math::PI / m
31
+ sum = 0.5 * (1.0 + Math.cos(n * Math::PI))
32
+ (1...m).each { |i| sum += Math.cos((n * i * h) - (x * Math.sin(i * h))) }
33
+ sum * h / Math::PI
34
+ end
35
+
36
+ def yn(n, x)
37
+ x = x.to_f
38
+ return Float::NAN if x.nan? || x.negative?
39
+ return -Float::INFINITY if x.zero?
40
+ return 0.0 if x.infinite?
41
+
42
+ n = n.to_i
43
+ return (n.even? ? 1 : -1) * yn(-n, x) if n.negative?
44
+ return y0(x) if n.zero?
45
+ return y1(x) if n == 1
46
+
47
+ a = y0(x)
48
+ b = y1(x)
49
+ (1...n).each do |k|
50
+ a, b = b, ((2.0 * k / x) * b) - a
51
+ break if b.infinite?
52
+ end
53
+ b
54
+ end
55
+
56
+ def y0(x)
57
+ x = x.to_f
58
+ return Float::NAN if x.nan? || x.negative?
59
+ return -Float::INFINITY if x.zero?
60
+ return asymptotic_y(0, x) if x > 30
61
+
62
+ # Y0 = 2/pi (ln(x/2) + gamma) J0 + 2/pi * sum_{k>=1} (-1)^(k+1) H_k (x^2/4)^k / (k!)^2
63
+ q = (Rational(x)**2) / 4
64
+ term = Rational(1)
65
+ harmonic = Rational(0)
66
+ sum = Rational(0)
67
+ k = 0
68
+ loop do
69
+ k += 1
70
+ term = term * q / (k * k)
71
+ harmonic += Rational(1, k)
72
+ t = term * harmonic
73
+ sum += k.odd? ? t : -t
74
+ break if k > 5 && t.abs < sum.abs * Rational(1, 10**20)
75
+ end
76
+ 2.0 / Math::PI * (((Math.log(x / 2) + EULER) * jn(0, x)) + sum.to_f)
77
+ end
78
+
79
+ def y1(x)
80
+ x = x.to_f
81
+ return Float::NAN if x.nan? || x.negative?
82
+ return -Float::INFINITY if x.zero?
83
+ return asymptotic_y(1, x) if x > 30
84
+
85
+ # Y1 = 2/pi (ln(x/2) + gamma) J1 - 2/(pi x)
86
+ # - 1/pi sum_{k>=0} (-1)^k (H_k + H_{k+1}) (x/2)^(2k+1) / (k! (k+1)!)
87
+ half = Rational(x) / 2
88
+ q = half * half
89
+ term = half # (x/2)^(2k+1) / (k! (k+1)!) at k = 0
90
+ hk = Rational(0)
91
+ hk1 = Rational(1)
92
+ sum = term * (hk + hk1)
93
+ k = 0
94
+ loop do
95
+ k += 1
96
+ term = term * q / (k * (k + 1))
97
+ hk = hk1
98
+ hk1 += Rational(1, k + 1)
99
+ t = term * (hk + hk1)
100
+ sum += k.even? ? t : -t
101
+ break if k > 5 && t.abs < sum.abs * Rational(1, 10**20)
102
+ end
103
+ (2.0 / Math::PI * (Math.log(x / 2) + EULER) * jn(1, x)) - (2.0 / (Math::PI * x)) - (sum.to_f / Math::PI)
104
+ end
105
+
106
+ # Hankel asymptotic expansion for large x
107
+ def hankel_pq(n, x)
108
+ mu = 4.0 * n * n
109
+ p = 1.0
110
+ q = 0.0
111
+ term = 1.0
112
+ k = 1
113
+ loop do
114
+ term *= (mu - (((2 * k) - 1)**2)) / (k * 8.0 * x)
115
+ break if term.abs < 1e-17 || k > 60
116
+
117
+ if k.odd?
118
+ q += (k % 4 == 1 ? term : -term)
119
+ else
120
+ p += (k % 4 == 2 ? -term : term)
121
+ end
122
+ k += 1
123
+ end
124
+ [p, q]
125
+ end
126
+
127
+ def asymptotic_j(n, x)
128
+ p, q = hankel_pq(n, x)
129
+ w = x - (((n / 2.0) + 0.25) * Math::PI)
130
+ Math.sqrt(2.0 / (Math::PI * x)) * ((p * Math.cos(w)) - (q * Math.sin(w)))
131
+ end
132
+
133
+ def asymptotic_y(n, x)
134
+ p, q = hankel_pq(n, x)
135
+ w = x - (((n / 2.0) + 0.25) * Math::PI)
136
+ Math.sqrt(2.0 / (Math::PI * x)) * ((p * Math.sin(w)) + (q * Math.cos(w)))
137
+ end
138
+ end
139
+ end