Skip to content

Commit 00d36d0

Browse files
jhorbulykandrew-coleman
authored andcommitted
Add prettify option to $string() function. (#356)
1 parent fb80ccf commit 00d36d0

File tree

9 files changed

+63
-22
lines changed

9 files changed

+63
-22
lines changed

docs/string-functions.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ sidebar_label: String Functions
55
---
66

77
## `$string()`
8-
__Signature:__ `$string(arg)`
8+
__Signature:__ `$string(arg, prettify)`
99

1010
Casts the `arg` parameter to a string using the following casting rules
1111

@@ -16,9 +16,11 @@ Casts the `arg` parameter to a string using the following casting rules
1616

1717
If `arg` is not specified (i.e. this function is invoked with no arguments), then the context value is used as the value of `arg`.
1818

19+
If `prettify` is true, then "prettified" JSON is produced. i.e One line per field and lines will be indented based on the field depth.
20+
1921
__Examples__
2022

21-
- `$string(5)` => `"5"`
23+
- `$string(5)` => `"5"`
2224
- `[1..5].$string()` => `["1", "2", "3", "4", "5"]`
2325

2426
## `$length()`
@@ -35,15 +37,15 @@ __Signature:__ `$substring(str, start[, length])`
3537

3638
Returns a string containing the characters in the first parameter `str` starting at position `start` (zero-offset). If `str` is not specified (i.e. this function is invoked with only the numeric argument(s)), then the context value is used as the value of `str`. An error is thrown if `str` is not a string.
3739

38-
If `length` is specified, then the substring will contain maximum `length` characters.
40+
If `length` is specified, then the substring will contain maximum `length` characters.
3941

4042
If `start` is negative then it indicates the number of characters from the end of `str`. See [substr](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr) for full definition.
4143

4244
__Examples__
4345

44-
- `$substring("Hello World", 3)` => `"lo World"`
45-
- `$substring("Hello World", 3, 5)` => `"lo Wo"`
46-
- `$substring("Hello World", -4)` => `"orld"`
46+
- `$substring("Hello World", 3)` => `"lo World"`
47+
- `$substring("Hello World", 3, 5)` => `"lo Wo"`
48+
- `$substring("Hello World", -4)` => `"orld"`
4749
- `$substring("Hello World", -4, 2)` => `"or"`
4850

4951
## `$substringBefore()`
@@ -122,26 +124,26 @@ The `pattern` parameter can either be a string or a regular expression (regex).
122124

123125
__Examples__
124126

125-
- `$contains("abracadabra", "bra")` => `true`
126-
- `$contains("abracadabra", /a.*a/)` => `true`
127-
- `$contains("abracadabra", /ar.*a/)` => `false`
128-
- `$contains("Hello World", /wo/)` => `false`
129-
- `$contains("Hello World", /wo/i)` => `true`
127+
- `$contains("abracadabra", "bra")` => `true`
128+
- `$contains("abracadabra", /a.*a/)` => `true`
129+
- `$contains("abracadabra", /ar.*a/)` => `false`
130+
- `$contains("Hello World", /wo/)` => `false`
131+
- `$contains("Hello World", /wo/i)` => `true`
130132
- `Phone[$contains(number, /^077/)]` => `{ "type": "mobile", "number": "077 7700 1234" }`
131133

132134
## `$split()`
133135
__Signature:__ `$split(str, separator [, limit])`
134136

135-
Splits the `str` parameter into an array of substrings. If `str` is not specified, then the context value is used as the value of `str`. It is an error if `str` is not a string.
137+
Splits the `str` parameter into an array of substrings. If `str` is not specified, then the context value is used as the value of `str`. It is an error if `str` is not a string.
136138

137139
The `separator` parameter can either be a string or a regular expression (regex). If it is a string, it specifies the characters within `str` about which it should be split. If it is the empty string, `str` will be split into an array of single characters. If it is a regex, it splits the string around any sequence of characters that match the regex.
138140

139141
The optional `limit` parameter is a number that specifies the maximum number of substrings to include in the resultant array. Any additional substrings are discarded. If `limit` is not specified, then `str` is fully split with no limit to the size of the resultant array. It is an error if `limit` is not a non-negative number.
140142

141143
__Examples__
142144

143-
- `$split("so many words", " ")` => `[ "so", "many", "words" ]`
144-
- `$split("so many words", " ", 2)` => `[ "so", "many" ]`
145+
- `$split("so many words", " ")` => `[ "so", "many", "words" ]`
146+
- `$split("so many words", " ", 2)` => `[ "so", "many" ]`
145147
- `$split("too much, punctuation. hard; to read", /[ ,.;]+/)` => `["too", "much", "punctuation", "hard", "to", "read"]`
146148

147149
## `$join()`
@@ -155,7 +157,7 @@ If `separator` is not specified, then it is assumed to be the empty string, i.e.
155157

156158
__Examples__
157159

158-
- `$join(['a','b','c'])` => `"abc"`
160+
- `$join(['a','b','c'])` => `"abc"`
159161
- `$split("too much, punctuation. hard; to read", /[ ,.;]+/, 3) ~> $join(', ')` => `"too, much, punctuation"`
160162

161163
## `$match()`
@@ -278,7 +280,7 @@ __Signature:__ `$base64encode()`
278280

279281
Converts an ASCII string to a base 64 representation. Each each character in the string is treated as a byte of binary data. This requires that all characters in the string are in the 0x00 to 0xFF range, which includes all characters in URI encoded strings. Unicode characters outside of that range are not supported.
280282

281-
__Examples__
283+
__Examples__
282284

283285
- `$base64encode("myuser:mypass")` => `"bXl1c2VyOm15cGFzcw=="`
284286

@@ -288,6 +290,6 @@ __Signature:__ `$base64decode()`
288290

289291
Converts base 64 encoded bytes to a string, using a UTF-8 Unicode codepage.
290292

291-
__Examples__
293+
__Examples__
292294

293295
- `$base64decode("bXl1c2VyOm15cGFzcw==")` => `"myuser:mypass"`

src/functions.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,12 @@ const functions = (() => {
9797
}
9898

9999
/**
100-
* Stingify arguments
100+
* Stringify arguments
101101
* @param {Object} arg - Arguments
102+
* @param {boolean} [prettify] - Pretty print the result
102103
* @returns {String} String from arguments
103104
*/
104-
function string(arg) {
105+
function string(arg, prettify = false) {
105106
// undefined inputs always return undefined
106107
if (typeof arg === 'undefined') {
107108
return undefined;
@@ -121,11 +122,13 @@ const functions = (() => {
121122
value: arg,
122123
stack: (new Error()).stack
123124
};
124-
} else
125+
} else {
126+
var space = prettify ? 2 : 0;
125127
str = JSON.stringify(arg, function (key, val) {
126128
return (typeof val !== 'undefined' && val !== null && val.toPrecision && isNumeric(val)) ? Number(val.toPrecision(15)) :
127129
(val && isFunction(val)) ? '' : val;
128-
});
130+
}, space);
131+
}
129132
return str;
130133
}
131134

src/jsonata.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1658,7 +1658,7 @@ var jsonata = (function() {
16581658
staticFrame.bind('max', defineFunction(fn.max, '<a<n>:n>'));
16591659
staticFrame.bind('min', defineFunction(fn.min, '<a<n>:n>'));
16601660
staticFrame.bind('average', defineFunction(fn.average, '<a<n>:n>'));
1661-
staticFrame.bind('string', defineFunction(fn.string, '<x-:s>'));
1661+
staticFrame.bind('string', defineFunction(fn.string, '<x-b?:s>'));
16621662
staticFrame.bind('substring', defineFunction(fn.substring, '<s-nn?:s>'));
16631663
staticFrame.bind('substringBefore', defineFunction(fn.substringBefore, '<s-s:s>'));
16641664
staticFrame.bind('substringAfter', defineFunction(fn.substringAfter, '<s-s:s>'));
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr": "$string({\"string\": \"hello\"}, false)",
3+
"dataset": null,
4+
"bindings": {},
5+
"result": "{\"string\":\"hello\"}"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr": "$string({\"string\": \"hello\"}, true)",
3+
"dataset": null,
4+
"bindings": {},
5+
"result": "{\n \"string\": \"hello\"\n}"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr": "$string([\"string\", 5], false)",
3+
"dataset": null,
4+
"bindings": {},
5+
"result": "[\"string\",5]"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr": "$string([\"string\", 5], true)",
3+
"dataset": null,
4+
"bindings": {},
5+
"result": "[\n \"string\",\n 5\n]"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr": "$string({ \"string\": \"hello\", \"number\": 78.8 / 2, \"null\":null, \"boolean\": false, \"function\": $sum, \"lambda\": function(){true}, \"object\": { \"str\": \"another\", \"lambda2\": function($n){$n} }, \"array\": []}, false)",
3+
"dataset": null,
4+
"bindings": {},
5+
"result": "{\"string\":\"hello\",\"number\":39.4,\"null\":null,\"boolean\":false,\"function\":\"\",\"lambda\":\"\",\"object\":{\"str\":\"another\",\"lambda2\":\"\"},\"array\":[]}"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr": "$string({ \"string\": \"hello\", \"number\": 78.8 / 2, \"null\":null, \"boolean\": false, \"function\": $sum, \"lambda\": function(){true}, \"object\": { \"str\": \"another\", \"lambda2\": function($n){$n} }, \"array\": []}, true)",
3+
"dataset": null,
4+
"bindings": {},
5+
"result": "{\n \"string\": \"hello\",\n \"number\": 39.4,\n \"null\": null,\n \"boolean\": false,\n \"function\": \"\",\n \"lambda\": \"\",\n \"object\": {\n \"str\": \"another\",\n \"lambda2\": \"\"\n },\n \"array\": []\n}"
6+
}

0 commit comments

Comments
 (0)