diff --git a/CHANGELOG.md b/CHANGELOG.md index ca68a76e..034b0386 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +#### 2.1.0 Milestone Release + +- New syntax (`?:` default operator) supports fallback to RHS if the LHS is Boolean equivalent to false (PR #784) +- New syntax (`??` coalescing operator) supports fallback to RHS if the LHS is non-existent (PR #784) +- Improve regex generation for DateTime parser (PR #728) +- Truncate fractional part of numeric argument of `$pad` function (PR #729) +- Await array elements (PR #747) +- Various documentation fixes and improvements + #### 2.0.6 Maintenance Release - Protect __evaluate_entry and __evaluate_exit callbacks (PR #700) diff --git a/package.json b/package.json index e3abe6e6..120d8e56 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jsonata", - "version": "2.0.6", + "version": "2.1.0", "description": "JSON query and transformation language", "module": "jsonata.js", "main": "jsonata.js", diff --git a/website/versioned_docs/version-2.1.0/construction.md b/website/versioned_docs/version-2.1.0/construction.md new file mode 100644 index 00000000..91a45238 --- /dev/null +++ b/website/versioned_docs/version-2.1.0/construction.md @@ -0,0 +1,106 @@ +--- +id: version-2.1.0-construction +title: Building result structures +sidebar_label: Result Structures +original_id: construction +--- + +So far, we have discovered how to extract values from a JSON document, and how to manipulate the data using numeric, string and other operators. It is useful to be able to specify how this processed data is presented in the output. + +## Array constructors + +As previously observed, when a location path matches multiple values in the input document, these values are returned as an array. The values might be objects or arrays, and as such will have their own structure, but the _matched values_ themselves are at the top level in the resultant array. + +It is possible to build extra structure into the resultant array by specifying the construction of arrays (or [objects](#object-constructors)) within the location path expression. At any point in a location path where a field reference is expected, a pair of square brackets `[]` can be inserted to specify that the results of the expression within those brackets should be contained within a new array in the output. Commas are used to separate multiple expressions within the array constructor. + +Array constructors can also be used within location paths for making multiple selections without the broad brush use of wildcards. + +__Examples__ + +- The four email addresses are returned in a flat array. +
baseexponent).
+
+If `base` is not specified (i.e. this function is invoked with one argument), then the context value is used as the value of `base`.
+
+An error is thrown if the values of `base` and `exponent` lead to a value that cannot be represented as a JSON number (e.g. Infinity, complex numbers).
+
+__Examples__
+- `$power(2, 8)` => `256`
+- `$power(2, 0.5)` => `1.414213562373`
+- `$power(2, -2)` => `0.25`
+
+## `$sqrt()`
+__Signature:__ `$sqrt(number)`
+
+Returns the square root of the value of the `number` parameter.
+
+If `number` is not specified (i.e. this function is invoked with one argument), then the context value is used as the value of `number`.
+
+An error is thrown if the value of `number` is negative.
+
+__Examples__
+- `$sqrt(4)` => `2`
+- `$sqrt(2)` => `1.414213562373`
+
+## `$random()`
+__Signature:__ `$random()`
+
+Returns a pseudo random number greater than or equal to zero and less than one (0 ≤ n < 1)
+
+__Examples__
+- `$random()` => `0.7973541067127`
+- `$random()` => `0.4029142127028`
+- `$random()` => `0.6558078550072`
+
+
+## `$formatNumber()`
+__Signature:__ `$formatNumber(number, picture [, options])`
+
+Casts the `number` to a string and formats it to a decimal representation as specified by the `picture` string.
+
+The behaviour of this function is consistent with the XPath/XQuery function [fn:format-number](https://www.w3.org/TR/xpath-functions-31/#func-format-number) as defined in the XPath F&O 3.1 specification. The picture string parameter defines how the number is formatted and has the [same syntax](https://www.w3.org/TR/xpath-functions-31/#syntax-of-picture-string) as fn:format-number.
+
+The optional third argument `options` is used to override the default locale specific formatting characters such as the decimal separator. If supplied, this argument must be an object containing name/value pairs specified in the [decimal format](https://www.w3.org/TR/xpath-functions-31/#defining-decimal-format) section of the XPath F&O 3.1 specification.
+
+__Examples__
+
+- `$formatNumber(12345.6, '#,###.00')` => `"12,345.60"`
+- `$formatNumber(1234.5678, "00.000e0")` => `"12.346e2"`
+- `$formatNumber(34.555, "#0.00;(#0.00)")` => `"34.56"`
+- `$formatNumber(-34.555, "#0.00;(#0.00)")` => `"(34.56)"`
+- `$formatNumber(0.14, "01%")` => `"14%"`
+- `$formatNumber(0.14, "###pm", {"per-mille": "pm"})` => `"140pm"`
+- `$formatNumber(1234.5678, "①①.①①①e①", {"zero-digit": "\u245f"})` => `"①②.③④⑥e②"`
+
+
+## `$formatBase()`
+__Signature:__ `$formatBase(number [, radix])`
+
+Casts the `number` to a string and formats it to an integer represented in the number base specified by the `radix` argument. If `radix` is not specified, then it defaults to base 10. `radix` can be between 2 and 36, otherwise an error is thrown.
+
+__Examples__
+
+- `$formatBase(100, 2)` => `"1100100"`
+- `$formatBase(2555, 16)` => `"9fb"`
+
+
+## `$formatInteger()`
+__Signature:__ `$formatInteger(number, picture)`
+
+Casts the `number` to a string and formats it to an integer representation as specified by the `picture` string.
+
+The behaviour of this function is consistent with the two-argument version of the XPath/XQuery function [fn:format-integer](https://www.w3.org/TR/xpath-functions-31/#func-format-integer) as defined in the XPath F&O 3.1 specification. The picture string parameter defines how the number is formatted and has the same syntax as fn:format-integer.
+
+__Examples__
+
+- `$formatInteger(2789, 'w')` => `"two thousand, seven hundred and eighty-nine"`
+- `$formatInteger(1999, 'I')` => `"MCMXCIX"`
+
+## `$parseInteger()`
+__Signature:__ `$parseInteger(string, picture)`
+
+Parses the contents of the `string` parameter to an integer (as a JSON number) using the format specified by the `picture` string.
+The picture string parameter has the same format as `$formatInteger`. Although the XPath specification does not have an equivalent
+function for parsing integers, this capability has been added to JSONata.
+
+__Examples__
+
+- `$parseInteger("twelve thousand, four hundred and seventy-six", 'w')` => `12476`
+- `$parseInteger('12,345,678', '#,##0')` => `12345678`
diff --git a/website/versioned_docs/version-2.1.0/numeric-operators.md b/website/versioned_docs/version-2.1.0/numeric-operators.md
new file mode 100644
index 00000000..04121657
--- /dev/null
+++ b/website/versioned_docs/version-2.1.0/numeric-operators.md
@@ -0,0 +1,62 @@
+---
+id: version-2.1.0-numeric-operators
+title: Numeric Operators
+sidebar_label: Numeric Operators
+original_id: numeric-operators
+---
+
+## `+` (Addition)
+
+The addition operator adds the operands to produce the numerical sum. It is an error if either operand is not a number.
+
+__Example__
+
+`5 + 2` => `7`
+
+
+## `-` (Subtraction/Negation)
+
+The subtraction operator subtracts the RHS value from the LHS value to produce the numerical difference It is an error if either operand is not a number.
+
+It can also be used in its unary form to negate a number
+
+__Examples__
+
+- `5 - 2` => `3`
+- `- 42` => `-42`
+
+## `*` (Multiplication)
+
+The multiplication operator multiplies the operands to produce the numerical product. It is an error if either operand is not a number.
+
+__Example__
+
+`5 * 2` => `10`
+
+## `/` (Division)
+
+The division operator divides the RHS into the LHS to produce the numerical quotient. It is an error if either operand is not a number.
+
+__Example__
+
+`5 / 2` => `2.5`
+
+
+## `%` (Modulo)
+
+The modulo operator divides the RHS into the LHS using whole number division to produce a whole number quotient and a remainder. This operator returns the remainder. It is an error if either operand is not a number.
+
+__Example__
+
+`5 % 2` => `1`
+
+## `..` (Range)
+
+The sequence generation operator is used to create an array of monotonically increasing integer start with the number on the LHS and ending with the number on the RHS. It is an error if either operand does not evaluate to an integer. The sequence generator can only be used within an array constructor [].
+
+__Examples__
+
+- `[1..5]` => `[1, 2, 3, 4, 5]`
+- `[1..3, 7..9]` => `[1, 2, 3, 7, 8, 9]`
+- `[1..$count(Items)].("Item " & $)` => `["Item 1","Item 2","Item 3"]`
+- `[1..5].($*$)` => `[1, 4, 9, 16, 25]`
diff --git a/website/versioned_docs/version-2.1.0/other-operators.md b/website/versioned_docs/version-2.1.0/other-operators.md
new file mode 100644
index 00000000..70fd120e
--- /dev/null
+++ b/website/versioned_docs/version-2.1.0/other-operators.md
@@ -0,0 +1,151 @@
+---
+id: version-2.1.0-other-operators
+title: Other Operators
+sidebar_label: Other Operators
+original_id: other-operators
+---
+
+
+## `&` (Concatenation)
+
+The string concatenation operator is used to join the string values of the operands into a single resultant string. If either or both of the operands are not strings, then they are first cast to string using the rules of the `$string` function.
+
+__Example__
+
+`"Hello" & "World"` => `"HelloWorld"`
+
+## `? :` (Conditional)
+
+The conditional ternary operator is used to evaluate one of two alternative expressions based on the result of a predicate (test) condition. The operator takes the form:
+
+`