@fraczak/k 2.0.1 → 2.0.2

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.
Files changed (3) hide show
  1. package/README.md +48 -19
  2. package/package.json +1 -1
  3. package/run.mjs +3 -14
package/README.md CHANGED
@@ -94,6 +94,27 @@ the constant value. E.g.:
94
94
  {123 int, "kScript" str, true bool, null null} :
95
95
  "any" --> {"int":123,"str":"kScript","bool":true,"null":null}
96
96
 
97
+ Those values, i.e., `strings`, `integers`, `booleans`, and `null`, admit the
98
+ projection to the canonicat string representation of the value, e.g.:
99
+
100
+ .2 :
101
+ 2 --> {}
102
+ "2" --> {}
103
+ 4 ... undefined
104
+ true ... undefined
105
+
106
+ .true :
107
+ true --> {}
108
+ "true" --> {}
109
+ 4 ... undefined
110
+ "toto" ... undefined
111
+
112
+ .null :
113
+ null --> {}
114
+ "null" --> {}
115
+ 4 ... undefined
116
+ false ... undefined
117
+
97
118
  #### Vector product
98
119
 
99
120
  Vector product can be seen as an abbreviation for product whose field
@@ -111,37 +132,45 @@ can be written as `[.toto, .titi, 123]`.
111
132
 
112
133
  ### Pragmatic extensions, aka "standard library"
113
134
 
114
- - `GT` : -- identity for lists of decreasing elements; undefined otherwise
135
+ - `GT` -- identity for lists of decreasing elements; undefined otherwise
115
136
 
137
+ GT:
116
138
  [4,3] --> [4,3]
117
139
  [3,4] ... undefined
118
140
  [] --> []
119
141
  [4,3,0] --> [4,3,0]
120
142
 
121
- - `EQ` : -- identity for lists of equal elements; undefined otherwise
143
+ - `EQ` -- identity for lists of equal elements; undefined otherwise
122
144
 
145
+ EQ:
123
146
  [4,4] --> [4,4]
124
147
  [4,5] ... undefined
125
148
  [4,4,4] --> [4,4,4]
126
149
  [] --> []
127
150
 
128
- - `{PLUS plus, TIMES times}` :
151
+ - `PLUS` and `TIMES` -- sum and product of lists of numbers
129
152
 
153
+ {PLUS plus, TIMES times} :
130
154
  [1,2] --> {"plus":3,"times":2}
131
155
  [2,2,2] --> {"plus":6,"times":8}
132
156
  [] --> {"plus":0,"times":1}
133
157
 
134
- - `CONCAT` :
158
+ - `CONCAT` -- concatenation of lists of strings
135
159
 
160
+ CONCAT:
161
+ ["a","bc","d"] --> "abcd"
136
162
  ["a","bc","d"] --> "abcd"
137
163
 
138
- - `toJSON` :
164
+ - `toJSON` and `fromJSON` -- conversion to and from JSON strings
139
165
 
166
+ toJSON:
140
167
  {"a": 12} --> "{\"a\":12}"
141
168
 
142
- - `fromJSON` :
169
+ fromJSON:
170
+ "{\"a\":12}" --> {"a":12}
171
+ "2.12" --> 2.12
172
+ "[1,2.11,0.3e-32]" --> [1,2.11,3e-33]
143
173
 
144
- "{\"a\":12}" --> {"a":12}
145
174
 
146
175
  - other predefined parial functions are: `DIV`, `FDIV`, `CONS`, `SNOC`, `toDateMsec`,
147
176
  `toDateStr`, and `_log!`.
@@ -269,11 +298,11 @@ within `k`-expression is again prefixed by `$`.
269
298
 
270
299
  ## `k` (a command-line JSON processor using `k` syntax)
271
300
 
272
- There is a wrapper, `./node_modules/.bin/k` , which makes it easy to
301
+ There is a wrapper, `k` (`./node_modules/.bin/k`), which makes it easy to
273
302
  run the language from command line.
274
303
 
275
- > ./node_modules/.bin/k
276
- ... errors ...
304
+ > k
305
+ ... errors ...
277
306
  Usage: ./node_modules/.bin/k ( k-expr | -k k-file) [ -1 ] [ json-file ]
278
307
  E.g., cat '{"a": 10}' | ./node_modules/.bin/k '[(),()]'
279
308
 
@@ -281,14 +310,14 @@ For example:
281
310
 
282
311
  1. One `k`-expression with one `json`-object:
283
312
 
284
- > echo '{"x": 12, "y": 13}' | ./k.coffee '{ <.x, "no x"> x, () input}'
313
+ > echo '{"x": 12, "y": 13}' | k '{ <.x, "no x"> x, () input}'
285
314
  {"x":12,"input":{"x":12,"y":13}}
286
315
 
287
316
  2. By providing only `k`-expression, the script will compile the
288
317
  `k`-expression and apply the generated function to the `stdin`,
289
318
  line by line:
290
319
 
291
- > ./node_modules/.bin/k '<["x=",.x," & y=",.y],["only x=",.x],["only y=",.y],["no x nor y"]>{CONCAT "x&y"}'
320
+ > k '<["x=",.x," & y=",.y],["only x=",.x],["only y=",.y],["no x nor y"]>{CONCAT "x&y"}'
292
321
 
293
322
  {"y": 123, "x": 432,"others": "..."} --> {"x&y":"x=432 & y=123"}
294
323
  {"x": 987} --> {"x&y":"only x=987"}
@@ -316,11 +345,11 @@ For example:
316
345
 
317
346
  We can use it by:
318
347
 
319
- > ./node_modules/.bin/k -k test.k
348
+ > k -k test.k
320
349
 
321
350
  If we want to read `json` objects from a file, e.g., `my-objects.json`, we do
322
351
 
323
- > ./node_modules/.bin/k -k test.k my-objects.json
352
+ > k -k test.k my-objects.json
324
353
  {"x&y":"x=432 & y=123"}
325
354
  {"x&y":"only x=987"}
326
355
  {"x&y":"no x nor y"}
@@ -340,11 +369,11 @@ For example:
340
369
  ### Short comparaison with `jq` tutorial examples: <https://stedolan.github.io/jq/tutorial/>
341
370
 
342
371
  1.
343
- curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.'
344
- curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | k '()' -1
372
+ curl 'https://api.github.com/repositories/5101141/commits?per_page=5' | jq '.'
373
+ curl 'https://api.github.com/repositories/5101141/commits?per_page=5' | k '()' -1
345
374
  2.
346
- curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0]'
347
- curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | k '.0' -1
375
+ curl 'https://api.github.com/repositories/5101141/commits?per_page=5' | jq '.[0]'
376
+ curl 'https://api.github.com/repositories/5101141/commits?per_page=5' | k '.0' -1
348
377
  3.
349
378
  jq '.[0] | {message: .commit.message, name: .commit.committer.name}'
350
379
  k '.0 {.commit.message message, .commit.committer.name name}' -1
@@ -366,7 +395,7 @@ For example:
366
395
 
367
396
  ## k-REPL (Read-Evaluate-Print Loop)
368
397
 
369
- Also there is a REPL, `./node_modules/.bin/k-repl`, which acts like a toy
398
+ Also there is a REPL, `k-repl` (`./node_modules/.bin/k-repl`), which acts like a toy
370
399
  shell for the language. E.g.:
371
400
 
372
401
  > ./node_modules/.bin/k-repl
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fraczak/k",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "k-language for JSON-like data transformation",
5
5
  "main": "index.mjs",
6
6
  "scripts": {
package/run.mjs CHANGED
@@ -101,20 +101,9 @@ function run(exp, value) {
101
101
  }
102
102
  return builtin[exp.ref](value);
103
103
  case "dot":
104
- if (typeof value === "string") {
105
- if (value == exp.dot) return {};
106
- return;
107
- }
108
- if (/^(0|([1-9]\d*))$/.test(value)) {
109
- if (value == exp.dot) return {};
110
- return;
111
- }
112
- if (value === true) {
113
- if (exp.dot == "true") return {};
114
- return;
115
- }
116
- if (value === false) {
117
- if (exp.dot == "false") return {};
104
+ // a hack to allow something like 'null . null' or '0 . 0' to work by returning unit
105
+ if (value === null || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
106
+ if (`${value}` === `${exp.dot}`) return {};
118
107
  return;
119
108
  }
120
109
  return value[exp.dot];