functions.json 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. [{
  2. "given":
  3. {
  4. "foo": -1,
  5. "zero": 0,
  6. "numbers": [-1, 3, 4, 5],
  7. "array": [-1, 3, 4, 5, "a", "100"],
  8. "strings": ["a", "b", "c"],
  9. "decimals": [1.01, 1.2, -1.5],
  10. "str": "Str",
  11. "false": false,
  12. "empty_list": [],
  13. "empty_hash": {},
  14. "objects": {"foo": "bar", "bar": "baz"},
  15. "null_key": null
  16. },
  17. "cases": [
  18. {
  19. "expression": "abs(foo)",
  20. "result": 1
  21. },
  22. {
  23. "expression": "abs(foo)",
  24. "result": 1
  25. },
  26. {
  27. "expression": "abs(str)",
  28. "error": "invalid-type"
  29. },
  30. {
  31. "expression": "abs(array[1])",
  32. "result": 3
  33. },
  34. {
  35. "expression": "abs(array[1])",
  36. "result": 3
  37. },
  38. {
  39. "expression": "abs(`false`)",
  40. "error": "invalid-type"
  41. },
  42. {
  43. "expression": "abs(`-24`)",
  44. "result": 24
  45. },
  46. {
  47. "expression": "abs(`-24`)",
  48. "result": 24
  49. },
  50. {
  51. "expression": "abs(`1`, `2`)",
  52. "error": "invalid-arity"
  53. },
  54. {
  55. "expression": "abs()",
  56. "error": "invalid-arity"
  57. },
  58. {
  59. "expression": "unknown_function(`1`, `2`)",
  60. "error": "unknown-function"
  61. },
  62. {
  63. "expression": "avg(numbers)",
  64. "result": 2.75
  65. },
  66. {
  67. "expression": "avg(array)",
  68. "error": "invalid-type"
  69. },
  70. {
  71. "expression": "avg('abc')",
  72. "error": "invalid-type"
  73. },
  74. {
  75. "expression": "avg(foo)",
  76. "error": "invalid-type"
  77. },
  78. {
  79. "expression": "avg(@)",
  80. "error": "invalid-type"
  81. },
  82. {
  83. "expression": "avg(strings)",
  84. "error": "invalid-type"
  85. },
  86. {
  87. "expression": "ceil(`1.2`)",
  88. "result": 2
  89. },
  90. {
  91. "expression": "ceil(decimals[0])",
  92. "result": 2
  93. },
  94. {
  95. "expression": "ceil(decimals[1])",
  96. "result": 2
  97. },
  98. {
  99. "expression": "ceil(decimals[2])",
  100. "result": -1
  101. },
  102. {
  103. "expression": "ceil('string')",
  104. "error": "invalid-type"
  105. },
  106. {
  107. "expression": "contains('abc', 'a')",
  108. "result": true
  109. },
  110. {
  111. "expression": "contains('abc', 'd')",
  112. "result": false
  113. },
  114. {
  115. "expression": "contains(`false`, 'd')",
  116. "error": "invalid-type"
  117. },
  118. {
  119. "expression": "contains(strings, 'a')",
  120. "result": true
  121. },
  122. {
  123. "expression": "contains(decimals, `1.2`)",
  124. "result": true
  125. },
  126. {
  127. "expression": "contains(decimals, `false`)",
  128. "result": false
  129. },
  130. {
  131. "expression": "ends_with(str, 'r')",
  132. "result": true
  133. },
  134. {
  135. "expression": "ends_with(str, 'tr')",
  136. "result": true
  137. },
  138. {
  139. "expression": "ends_with(str, 'Str')",
  140. "result": true
  141. },
  142. {
  143. "expression": "ends_with(str, 'SStr')",
  144. "result": false
  145. },
  146. {
  147. "expression": "ends_with(str, 'foo')",
  148. "result": false
  149. },
  150. {
  151. "expression": "ends_with(str, `0`)",
  152. "error": "invalid-type"
  153. },
  154. {
  155. "expression": "floor(`1.2`)",
  156. "result": 1
  157. },
  158. {
  159. "expression": "floor('string')",
  160. "error": "invalid-type"
  161. },
  162. {
  163. "expression": "floor(decimals[0])",
  164. "result": 1
  165. },
  166. {
  167. "expression": "floor(foo)",
  168. "result": -1
  169. },
  170. {
  171. "expression": "floor(str)",
  172. "error": "invalid-type"
  173. },
  174. {
  175. "expression": "length('abc')",
  176. "result": 3
  177. },
  178. {
  179. "expression": "length('')",
  180. "result": 0
  181. },
  182. {
  183. "expression": "length(@)",
  184. "result": 12
  185. },
  186. {
  187. "expression": "length(strings[0])",
  188. "result": 1
  189. },
  190. {
  191. "expression": "length(str)",
  192. "result": 3
  193. },
  194. {
  195. "expression": "length(array)",
  196. "result": 6
  197. },
  198. {
  199. "expression": "length(objects)",
  200. "result": 2
  201. },
  202. {
  203. "expression": "length(`false`)",
  204. "error": "invalid-type"
  205. },
  206. {
  207. "expression": "length(foo)",
  208. "error": "invalid-type"
  209. },
  210. {
  211. "expression": "length(strings[0])",
  212. "result": 1
  213. },
  214. {
  215. "expression": "max(numbers)",
  216. "result": 5
  217. },
  218. {
  219. "expression": "max(decimals)",
  220. "result": 1.2
  221. },
  222. {
  223. "expression": "max(strings)",
  224. "result": "c"
  225. },
  226. {
  227. "expression": "max(abc)",
  228. "error": "invalid-type"
  229. },
  230. {
  231. "expression": "max(array)",
  232. "error": "invalid-type"
  233. },
  234. {
  235. "expression": "max(decimals)",
  236. "result": 1.2
  237. },
  238. {
  239. "expression": "max(empty_list)",
  240. "result": null
  241. },
  242. {
  243. "expression": "merge(`{}`)",
  244. "result": {}
  245. },
  246. {
  247. "expression": "merge(`{}`, `{}`)",
  248. "result": {}
  249. },
  250. {
  251. "expression": "merge(`{\"a\": 1}`, `{\"b\": 2}`)",
  252. "result": {"a": 1, "b": 2}
  253. },
  254. {
  255. "expression": "merge(`{\"a\": 1}`, `{\"a\": 2}`)",
  256. "result": {"a": 2}
  257. },
  258. {
  259. "expression": "merge(`{\"a\": 1, \"b\": 2}`, `{\"a\": 2, \"c\": 3}`, `{\"d\": 4}`)",
  260. "result": {"a": 2, "b": 2, "c": 3, "d": 4}
  261. },
  262. {
  263. "expression": "min(numbers)",
  264. "result": -1
  265. },
  266. {
  267. "expression": "min(decimals)",
  268. "result": -1.5
  269. },
  270. {
  271. "expression": "min(abc)",
  272. "error": "invalid-type"
  273. },
  274. {
  275. "expression": "min(array)",
  276. "error": "invalid-type"
  277. },
  278. {
  279. "expression": "min(empty_list)",
  280. "result": null
  281. },
  282. {
  283. "expression": "min(decimals)",
  284. "result": -1.5
  285. },
  286. {
  287. "expression": "min(strings)",
  288. "result": "a"
  289. },
  290. {
  291. "expression": "type('abc')",
  292. "result": "string"
  293. },
  294. {
  295. "expression": "type(`1.0`)",
  296. "result": "number"
  297. },
  298. {
  299. "expression": "type(`2`)",
  300. "result": "number"
  301. },
  302. {
  303. "expression": "type(`true`)",
  304. "result": "boolean"
  305. },
  306. {
  307. "expression": "type(`false`)",
  308. "result": "boolean"
  309. },
  310. {
  311. "expression": "type(`null`)",
  312. "result": "null"
  313. },
  314. {
  315. "expression": "type(`[0]`)",
  316. "result": "array"
  317. },
  318. {
  319. "expression": "type(`{\"a\": \"b\"}`)",
  320. "result": "object"
  321. },
  322. {
  323. "expression": "type(@)",
  324. "result": "object"
  325. },
  326. {
  327. "expression": "sort(keys(objects))",
  328. "result": ["bar", "foo"]
  329. },
  330. {
  331. "expression": "keys(foo)",
  332. "error": "invalid-type"
  333. },
  334. {
  335. "expression": "keys(strings)",
  336. "error": "invalid-type"
  337. },
  338. {
  339. "expression": "keys(`false`)",
  340. "error": "invalid-type"
  341. },
  342. {
  343. "expression": "sort(values(objects))",
  344. "result": ["bar", "baz"]
  345. },
  346. {
  347. "expression": "keys(empty_hash)",
  348. "result": []
  349. },
  350. {
  351. "expression": "values(foo)",
  352. "error": "invalid-type"
  353. },
  354. {
  355. "expression": "join(', ', strings)",
  356. "result": "a, b, c"
  357. },
  358. {
  359. "expression": "join(', ', strings)",
  360. "result": "a, b, c"
  361. },
  362. {
  363. "expression": "join(',', `[\"a\", \"b\"]`)",
  364. "result": "a,b"
  365. },
  366. {
  367. "expression": "join(',', `[\"a\", 0]`)",
  368. "error": "invalid-type"
  369. },
  370. {
  371. "expression": "join(', ', str)",
  372. "error": "invalid-type"
  373. },
  374. {
  375. "expression": "join('|', strings)",
  376. "result": "a|b|c"
  377. },
  378. {
  379. "expression": "join(`2`, strings)",
  380. "error": "invalid-type"
  381. },
  382. {
  383. "expression": "join('|', decimals)",
  384. "error": "invalid-type"
  385. },
  386. {
  387. "expression": "join('|', decimals[].to_string(@))",
  388. "result": "1.01|1.2|-1.5"
  389. },
  390. {
  391. "expression": "join('|', empty_list)",
  392. "result": ""
  393. },
  394. {
  395. "expression": "reverse(numbers)",
  396. "result": [5, 4, 3, -1]
  397. },
  398. {
  399. "expression": "reverse(array)",
  400. "result": ["100", "a", 5, 4, 3, -1]
  401. },
  402. {
  403. "expression": "reverse(`[]`)",
  404. "result": []
  405. },
  406. {
  407. "expression": "reverse('')",
  408. "result": ""
  409. },
  410. {
  411. "expression": "reverse('hello world')",
  412. "result": "dlrow olleh"
  413. },
  414. {
  415. "expression": "starts_with(str, 'S')",
  416. "result": true
  417. },
  418. {
  419. "expression": "starts_with(str, 'St')",
  420. "result": true
  421. },
  422. {
  423. "expression": "starts_with(str, 'Str')",
  424. "result": true
  425. },
  426. {
  427. "expression": "starts_with(str, 'String')",
  428. "result": false
  429. },
  430. {
  431. "expression": "starts_with(str, `0`)",
  432. "error": "invalid-type"
  433. },
  434. {
  435. "expression": "sum(numbers)",
  436. "result": 11
  437. },
  438. {
  439. "expression": "sum(decimals)",
  440. "result": 0.71
  441. },
  442. {
  443. "expression": "sum(array)",
  444. "error": "invalid-type"
  445. },
  446. {
  447. "expression": "sum(array[].to_number(@))",
  448. "result": 111
  449. },
  450. {
  451. "expression": "sum(`[]`)",
  452. "result": 0
  453. },
  454. {
  455. "expression": "to_array('foo')",
  456. "result": ["foo"]
  457. },
  458. {
  459. "expression": "to_array(`0`)",
  460. "result": [0]
  461. },
  462. {
  463. "expression": "to_array(objects)",
  464. "result": [{"foo": "bar", "bar": "baz"}]
  465. },
  466. {
  467. "expression": "to_array(`[1, 2, 3]`)",
  468. "result": [1, 2, 3]
  469. },
  470. {
  471. "expression": "to_array(false)",
  472. "result": [false]
  473. },
  474. {
  475. "expression": "to_string('foo')",
  476. "result": "foo"
  477. },
  478. {
  479. "expression": "to_string(`1.2`)",
  480. "result": "1.2"
  481. },
  482. {
  483. "expression": "to_string(`[0, 1]`)",
  484. "result": "[0,1]"
  485. },
  486. {
  487. "expression": "to_number('1.0')",
  488. "result": 1.0
  489. },
  490. {
  491. "expression": "to_number('1.1')",
  492. "result": 1.1
  493. },
  494. {
  495. "expression": "to_number('4')",
  496. "result": 4
  497. },
  498. {
  499. "expression": "to_number('notanumber')",
  500. "result": null
  501. },
  502. {
  503. "expression": "to_number(`false`)",
  504. "result": null
  505. },
  506. {
  507. "expression": "to_number(`null`)",
  508. "result": null
  509. },
  510. {
  511. "expression": "to_number(`[0]`)",
  512. "result": null
  513. },
  514. {
  515. "expression": "to_number(`{\"foo\": 0}`)",
  516. "result": null
  517. },
  518. {
  519. "expression": "\"to_string\"(`1.0`)",
  520. "error": "syntax"
  521. },
  522. {
  523. "expression": "sort(numbers)",
  524. "result": [-1, 3, 4, 5]
  525. },
  526. {
  527. "expression": "sort(strings)",
  528. "result": ["a", "b", "c"]
  529. },
  530. {
  531. "expression": "sort(decimals)",
  532. "result": [-1.5, 1.01, 1.2]
  533. },
  534. {
  535. "expression": "sort(array)",
  536. "error": "invalid-type"
  537. },
  538. {
  539. "expression": "sort(abc)",
  540. "error": "invalid-type"
  541. },
  542. {
  543. "expression": "sort(empty_list)",
  544. "result": []
  545. },
  546. {
  547. "expression": "sort(@)",
  548. "error": "invalid-type"
  549. },
  550. {
  551. "expression": "not_null(unknown_key, str)",
  552. "result": "Str"
  553. },
  554. {
  555. "expression": "not_null(unknown_key, foo.bar, empty_list, str)",
  556. "result": []
  557. },
  558. {
  559. "expression": "not_null(unknown_key, null_key, empty_list, str)",
  560. "result": []
  561. },
  562. {
  563. "expression": "not_null(all, expressions, are_null)",
  564. "result": null
  565. },
  566. {
  567. "expression": "not_null()",
  568. "error": "invalid-arity"
  569. },
  570. {
  571. "description": "function projection on single arg function",
  572. "expression": "numbers[].to_string(@)",
  573. "result": ["-1", "3", "4", "5"]
  574. },
  575. {
  576. "description": "function projection on single arg function",
  577. "expression": "array[].to_number(@)",
  578. "result": [-1, 3, 4, 5, 100]
  579. }
  580. ]
  581. }, {
  582. "given":
  583. {
  584. "foo": [
  585. {"b": "b", "a": "a"},
  586. {"c": "c", "b": "b"},
  587. {"d": "d", "c": "c"},
  588. {"e": "e", "d": "d"},
  589. {"f": "f", "e": "e"}
  590. ]
  591. },
  592. "cases": [
  593. {
  594. "description": "function projection on variadic function",
  595. "expression": "foo[].not_null(f, e, d, c, b, a)",
  596. "result": ["b", "c", "d", "e", "f"]
  597. }
  598. ]
  599. }, {
  600. "given":
  601. {
  602. "people": [
  603. {"age": 20, "age_str": "20", "bool": true, "name": "a", "extra": "foo"},
  604. {"age": 40, "age_str": "40", "bool": false, "name": "b", "extra": "bar"},
  605. {"age": 30, "age_str": "30", "bool": true, "name": "c"},
  606. {"age": 50, "age_str": "50", "bool": false, "name": "d"},
  607. {"age": 10, "age_str": "10", "bool": true, "name": 3}
  608. ]
  609. },
  610. "cases": [
  611. {
  612. "description": "sort by field expression",
  613. "expression": "sort_by(people, &age)",
  614. "result": [
  615. {"age": 10, "age_str": "10", "bool": true, "name": 3},
  616. {"age": 20, "age_str": "20", "bool": true, "name": "a", "extra": "foo"},
  617. {"age": 30, "age_str": "30", "bool": true, "name": "c"},
  618. {"age": 40, "age_str": "40", "bool": false, "name": "b", "extra": "bar"},
  619. {"age": 50, "age_str": "50", "bool": false, "name": "d"}
  620. ]
  621. },
  622. {
  623. "expression": "sort_by(people, &age_str)",
  624. "result": [
  625. {"age": 10, "age_str": "10", "bool": true, "name": 3},
  626. {"age": 20, "age_str": "20", "bool": true, "name": "a", "extra": "foo"},
  627. {"age": 30, "age_str": "30", "bool": true, "name": "c"},
  628. {"age": 40, "age_str": "40", "bool": false, "name": "b", "extra": "bar"},
  629. {"age": 50, "age_str": "50", "bool": false, "name": "d"}
  630. ]
  631. },
  632. {
  633. "description": "sort by function expression",
  634. "expression": "sort_by(people, &to_number(age_str))",
  635. "result": [
  636. {"age": 10, "age_str": "10", "bool": true, "name": 3},
  637. {"age": 20, "age_str": "20", "bool": true, "name": "a", "extra": "foo"},
  638. {"age": 30, "age_str": "30", "bool": true, "name": "c"},
  639. {"age": 40, "age_str": "40", "bool": false, "name": "b", "extra": "bar"},
  640. {"age": 50, "age_str": "50", "bool": false, "name": "d"}
  641. ]
  642. },
  643. {
  644. "description": "function projection on sort_by function",
  645. "expression": "sort_by(people, &age)[].name",
  646. "result": [3, "a", "c", "b", "d"]
  647. },
  648. {
  649. "expression": "sort_by(people, &extra)",
  650. "error": "invalid-type"
  651. },
  652. {
  653. "expression": "sort_by(people, &bool)",
  654. "error": "invalid-type"
  655. },
  656. {
  657. "expression": "sort_by(people, &name)",
  658. "error": "invalid-type"
  659. },
  660. {
  661. "expression": "sort_by(people, name)",
  662. "error": "invalid-type"
  663. },
  664. {
  665. "expression": "sort_by(people, &age)[].extra",
  666. "result": ["foo", "bar"]
  667. },
  668. {
  669. "expression": "sort_by(`[]`, &age)",
  670. "result": []
  671. },
  672. {
  673. "expression": "max_by(people, &age)",
  674. "result": {"age": 50, "age_str": "50", "bool": false, "name": "d"}
  675. },
  676. {
  677. "expression": "max_by(people, &age_str)",
  678. "result": {"age": 50, "age_str": "50", "bool": false, "name": "d"}
  679. },
  680. {
  681. "expression": "max_by(people, &bool)",
  682. "error": "invalid-type"
  683. },
  684. {
  685. "expression": "max_by(people, &extra)",
  686. "error": "invalid-type"
  687. },
  688. {
  689. "expression": "max_by(people, &to_number(age_str))",
  690. "result": {"age": 50, "age_str": "50", "bool": false, "name": "d"}
  691. },
  692. {
  693. "expression": "min_by(people, &age)",
  694. "result": {"age": 10, "age_str": "10", "bool": true, "name": 3}
  695. },
  696. {
  697. "expression": "min_by(people, &age_str)",
  698. "result": {"age": 10, "age_str": "10", "bool": true, "name": 3}
  699. },
  700. {
  701. "expression": "min_by(people, &bool)",
  702. "error": "invalid-type"
  703. },
  704. {
  705. "expression": "min_by(people, &extra)",
  706. "error": "invalid-type"
  707. },
  708. {
  709. "expression": "min_by(people, &to_number(age_str))",
  710. "result": {"age": 10, "age_str": "10", "bool": true, "name": 3}
  711. }
  712. ]
  713. }, {
  714. "given":
  715. {
  716. "people": [
  717. {"age": 10, "order": "1"},
  718. {"age": 10, "order": "2"},
  719. {"age": 10, "order": "3"},
  720. {"age": 10, "order": "4"},
  721. {"age": 10, "order": "5"},
  722. {"age": 10, "order": "6"},
  723. {"age": 10, "order": "7"},
  724. {"age": 10, "order": "8"},
  725. {"age": 10, "order": "9"},
  726. {"age": 10, "order": "10"},
  727. {"age": 10, "order": "11"}
  728. ]
  729. },
  730. "cases": [
  731. {
  732. "description": "stable sort order",
  733. "expression": "sort_by(people, &age)",
  734. "result": [
  735. {"age": 10, "order": "1"},
  736. {"age": 10, "order": "2"},
  737. {"age": 10, "order": "3"},
  738. {"age": 10, "order": "4"},
  739. {"age": 10, "order": "5"},
  740. {"age": 10, "order": "6"},
  741. {"age": 10, "order": "7"},
  742. {"age": 10, "order": "8"},
  743. {"age": 10, "order": "9"},
  744. {"age": 10, "order": "10"},
  745. {"age": 10, "order": "11"}
  746. ]
  747. }
  748. ]
  749. }, {
  750. "given":
  751. {
  752. "people": [
  753. {"a": 10, "b": 1, "c": "z"},
  754. {"a": 10, "b": 2, "c": null},
  755. {"a": 10, "b": 3},
  756. {"a": 10, "b": 4, "c": "z"},
  757. {"a": 10, "b": 5, "c": null},
  758. {"a": 10, "b": 6},
  759. {"a": 10, "b": 7, "c": "z"},
  760. {"a": 10, "b": 8, "c": null},
  761. {"a": 10, "b": 9}
  762. ],
  763. "empty": []
  764. },
  765. "cases": [
  766. {
  767. "expression": "map(&a, people)",
  768. "result": [10, 10, 10, 10, 10, 10, 10, 10, 10]
  769. },
  770. {
  771. "expression": "map(&c, people)",
  772. "result": ["z", null, null, "z", null, null, "z", null, null]
  773. },
  774. {
  775. "expression": "map(&a, badkey)",
  776. "error": "invalid-type"
  777. },
  778. {
  779. "expression": "map(&foo, empty)",
  780. "result": []
  781. }
  782. ]
  783. }, {
  784. "given": {
  785. "array": [
  786. {
  787. "foo": {"bar": "yes1"}
  788. },
  789. {
  790. "foo": {"bar": "yes2"}
  791. },
  792. {
  793. "foo1": {"bar": "no"}
  794. }
  795. ]},
  796. "cases": [
  797. {
  798. "expression": "map(&foo.bar, array)",
  799. "result": ["yes1", "yes2", null]
  800. },
  801. {
  802. "expression": "map(&foo1.bar, array)",
  803. "result": [null, null, "no"]
  804. },
  805. {
  806. "expression": "map(&foo.bar.baz, array)",
  807. "result": [null, null, null]
  808. }
  809. ]
  810. }, {
  811. "given": {
  812. "array": [[1, 2, 3, [4]], [5, 6, 7, [8, 9]]]
  813. },
  814. "cases": [
  815. {
  816. "expression": "map(&[], array)",
  817. "result": [[1, 2, 3, 4], [5, 6, 7, 8, 9]]
  818. }
  819. ]
  820. }
  821. ]