DataSonnet Libraries
Crypto
hash(string value, string algorithm)
Calculates hash of a String value using one of the supported algorithms. The algorithm
must be one of MD2
, MD5
, SHA-1
, SHA-256
, SHA-384
, SHA-512
The response is a string containing the hash bytes.
Example:
{ hashValue: DS.Crypto.hash("HelloWorld", "MD5") }
{ "hashValue": "68e109f0f40ca72a15e05cc22786f8e6" }
hmac(string value, string secret, string algorithm)
Generates hash-based message autentication code using provided secret and a hash function algorithm. The algoritm
must be one of HmacSHA1
, HmacSHA256
or HmacSHA512
.
Example:
{ hmacValue: DS.Crypto.hmac("HelloWorld", "DataSonnet rules!", "HmacSHA256") }
{ "hmacValue": "7854220ef827b07529509f68f391a80bf87fff328dbda140ed582520a1372dc1" }
ZonedDateTime
now()
Returns the current date/time from the system UTC clock in ISO-8601 format.
Example
{ currentZuluTime: DS.ZonedDateTime.now() }
{ "currentZuluTime": "2019-08-19T18:58:38.313Z" }
offset(string datetime, string period)
Returns a copy of this datetime with the specified amount added. The datetime
parameter is in the ISO-8601 format.
The period
is a string in the ISO-8601 period format.
Example
DS.ZonedDateTime.offset("2019-07-22T21:00:00Z", "P1Y1D")
2020-07-23T21:00:00Z
format(string datetime, string inputFormat, string outputFormat)
Reformats a zoned date-time string.
Example
DS.ZonedDateTime.format("2019-07-04T21:00:00Z", "yyyy-MM-dd'T'HH:mm:ssVV", "d MMM uuuu")
4 Jul 2019
compare(string datetime1, string format1, string datetime2, string format2)
Returns 1 if datetime1 > datetime2, -1 if datetime1 < datetime2, 0 if datetime1 == datetime2.
changeTimeZone(string datetime, string format, string timezone)
Changes the date timezone, retaining the instant. This normally results in a change to the local date-time. The response is formatted using the same format as an input.
Example
DS.ZonedDateTime.changeTimeZone("2019-07-04T21:00:00-0500", "yyyy-MM-dd'T'HH:mm:ssZ", "America/Los_Angeles")
2019-07-04T19:00:00-0700
toLocalDate(string datetime, string format)
Returns only local date part of the datetime
parameter in the ISO-8601 format without the offset.
Example
DS.ZonedDateTime.toLocalDate("2019-07-04T21:00:00-0500", "yyyy-MM-dd'T'HH:mm:ssZ")
2019-07-04
LocalDateTime
now()
Returns the current date/time from the system UTC clock in ISO-8601 format without a time zone.
Example
{ currentLocalTime: DS.LocalDateTime.now() }
{ "currentLocalTime": "2019-08-19T18:58:38.313" }
offset(string datetime, string period)
Returns a copy of this datetime with the specified amount added. The datetime
parameter is in the ISO-8601 format without an offset.
The period
is a string in the ISO-8601 period format.
Example
DS.LocalDateTime.offset("2019-07-22T21:00:00", "P1Y1D")
2020-07-23T21:00:00
format(string datetime, string inputFormat, string outputFormat)
Reformats a local date-time string.
Example
DS.LocalDateTime.format("2019-07-04T21:00:00", "yyyy-MM-dd'T'HH:mm:ss", "d MMM uuuu")
4 Jul 2019
compare(string datetime1, string format1, string datetime2, string format2)
Returns 1
if datetime1 > datetime2
, -1
if datetime1 < datetime2
, and 0
if datetime1 == datetime2
.
The format1
and format2
parameters must not have an offset or time zone.
Example
DS.LocalDateTime.compare("2019-07-04T21:00:00", "yyyy-MM-dd'T'HH:mm:ss", "2019-07-04T21:00:00", "yyyy-MM-dd'T'HH:mm:ss")
0
JsonPath
select(object json, string path)
Evaluates JsonPath expression and returns the resulting JSON object. It uses the Jayway JsonPath implementation and fully supports JsonPath specification.
Example
{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ] } }
{ author: DS.JsonPath.select(payload, "$..book[-2:]..author")[0] }
{ "author": "Herman Melville" }
Util
select(object obj, string path)
Returns a value inside the object by given path separated by dot ('.').
Example
{ "name": "Foo", "language": { "name": "Java", "version": "1.8" } }
{ language: DS.Util.select(payload, 'language.name') }
{ "language": "Java" }
filterEx(array objects, string key, string value, function filter_func=function(value1, value2) value1 == value2)
Filters array of objects by given condition.
Example
{ "languages": [ { "name": "Foo", "language": "Java" }, { "name": "Bar", "language": "Scala" }, { "name": "FooBar", "language": "Java" }, { "name": "FooBar", "language": "C++" } ] }
{ nonJavaLanguages: DS.Util.filterEx(payload.languages, 'language', 'Java', function(x, y) x != y) }
[ { "name": "Bar", "language": "Scala" }, { "name": "FooBar", "language": "C++" } ]
groupBy(array arr, string keyName)
Partitions an array into a Object that contains Arrays, according to the discriminator key you define. The discriminator can be a path inside the objects to group, e.g. 'language.name'
Example
{ "languages": [ { "name": "Foo", "language": { "name": "Java", "version": "1.8" } }, { "name": "Bar", "language": { "name": "Scala", "version": "1.0" } }, { "name": "FooBar", "language": { "name": "Java", "version": "1.7" } } ] }
{ languageGroups: DS.Util.groupBy(payload.languages, 'language.name') }
{ "languageGroups": { "Java": [ { "language": { "name": "Java", "version": "1.8" }, "name": "Foo" }, { "language": { "name": "Java", "version": "1.7" }, "name": "FooBar" } ], "Scala": [ { "language": { "name": "Scala", "version": "1.0" }, "name": "Bar" } ] } }
remove(object obj, string keyName)
Removes a property with given name from the object and returns the remaining object
Example
{ "availableSeats": 45, "airlineName": "Delta", "aircraftBrand": "Boeing", "aircraftType": "717", "departureDate": "01/20/2019", "origin": "PHX", "destination": "SEA" }
DS.Util.remove(payload, 'availableSeats')
{ "airlineName": "Delta", "aircraftBrand": "Boeing", "aircraftType": "717", "departureDate": "01/20/2019", "origin": "PHX", "destination": "SEA" }
removeAll(object obj, array keyNames)
Removes all properties with names from a provided list of strings from the object and returns the remaining object
Example
{ "availableSeats": 45, "airlineName": "Delta", "aircraftBrand": "Boeing", "aircraftType": "717", "departureDate": "01/20/2019", "origin": "PHX", "destination": "SEA" }
DS.Util.removeAll(payload, ['availableSeats', 'aircraftType', 'aircraftBrand'])
{ "airlineName": "Delta", "departureDate": "01/20/2019", "origin": "PHX", "destination": "SEA" }
deepFlattenArrays(array arr)
Flattens multiple nested arrays into a single array.
Example
[ 1, 2, [ 3 ], [ 4, [ 5, 6, 7 ], { "x": "y" } ] ]
DS.Util.flattenArrays(payload)
[ 1, 2, 4, 5, 6, 7, { "x": "y" } ]
reverse(array arr)
Returns an array with elements in reverse order.
Example
[ "a", "b", "c", "d" ]
DS.Util.reverse(payload)
[ "d", "c", "b", "a", ]
parseDouble(string str)
Parses a string which contains a double number and returns its numeric representation
Example
{ "numberAsString": "123.45679" }
{ num: DS.Util.parseDouble(payload.numberAsString) }
{ "num": 123.45679 }
duplicates(array arr, function keyF=id, boolean set=true)
Returns an array containing duplicate elements from input array. An optional key function returns a value which will be used as a comparison key. If set
parameter is set to true, only the first duplicate value will be included.
Example
[ { "language": { "name": "Java8", "version": "1.8" } }, { "language": { "name": "Java8", "version": "1.8.0" } }, { "language": { "name": "Scala", "version": "2.13.0" } } ]
DS.Util.duplicates(payload, function(x) x.language.name)
[ { "language": { "name": "Java8", "version":"1.8.0" } } ]
sum(array arr)
Returns sum of all elements in the array.
Example
[ 10, 20, 30 ]
DS.Util.sum(payload)
60
round(double num, int precision)
Rounds a double to the number of digits after the decimal point
Example
{ "num": 123.562567558 }
DS.Util.round(payload.num, 6)
123.562568
counts(array arr, function keyF=id)
Returns an object where keys are the results of calling keyF on the values, and the values are the counts of values that produced the corresponding key.
Example
[ { "name": "Foo", "language": { "name": "Java", "version": "1.8" } }, { "name": "Bar", "language": { "name": "Scala", "version": "1.0" } }, { "name": "FooBar", "language": { "name": "Java", "version": "1.7" } } ]
DS.Util.counts(payload, function(x) x.language.name);
{ "Java": 2, "Scala": 1 }
mapToObject(arr, keyF, valueF=id)
Maps an array into an object, where the keys are the result of calling keyF on each value (which becomes the value at the key). If valueF is provided it gets run on the value. Duplicate keys are removed.
Example
[ { "name": "Foo", "language": { "name": "Java", "version": "1.8" } }, { "name": "Bar", "language": { "name": "Scala", "version": "1.0" } }, { "name": "FooBar", "language": { "name": "C++", "version": "n/a" } } ]
DS.Util.mapToObject(payload, function(x) x.language.name, function(v) v.language);
{ "Java": { "name": "Java", "version": "1.8" }, "C++": { "name": "C++", "version": "n/a" }, "Scala": { "name": "Scala", "version": "1.0" } };
Regex
regexFullMatch(string pattern, string input)
Matches the entire input against the pattern (anchored start and end). If there’s no match, returns null
. If there’s a match, returns a JSON object which has the following structure:
-
string
- the matched string; -
captures
- array of captured subgroups in the match, if any; -
namedCaptures
- map of named subgroups, if any;
Example
DS.Regex.regexFullMatch(@'h(?P<mid>.*)o', 'hello');
{ "captures": [ "ell" ], "namedCaptures": { "mid": "ell" }, "string": "hello" }
regexPartialMatch(string pattern, string input)
Matches the input against the pattern (unanchored). If there’s no match, returns null
. If there’s a match, returns a JSON object which has the following structure:
-
string
- the matched string; -
captures
- array of captured subgroups in the match, if any; -
namedCaptures
- map of named subgroups, if any;
Example
DS.Regex.regexPartialMatch(@'e(?P<mid>.*)o', 'hello')
{ "captures": [ "ll" ], "namedCaptures": { "mid": "ll" }, "string": "ello" }
regexScan(string pattern, string input)
Finds all matches of the input against the pattern. If there are any matches, returns an array of JSON objects which have the following structure:
-
string
- the matched string; -
captures
- array of captured subgroups in the match, if any; -
namedCaptures
- map of named subgroups, if any;
Example
DS.Regex.regexScan(@'(?P<user>[a-z]*)@(?P<domain>[a-z]*).org', 'modus@datasonnet.org,box@datasonnet.org')
[ { "captures": [ "modus", "datasonnet" ], "namedCaptures": { "domain": "datasonnet", "user": "modus" }, "string": "modus@datasonnet.org" }, { "captures": [ "box", "datasonnet" ], "namedCaptures": { "domain": "datasonnet", "user": "box" }, "string": "box@datasonnet.org" } ]
regexQuoteMeta(string str)
Returns a literal pattern string for the specified string.
Example
DS.Regex.regexQuoteMeta(@'1.5-2.0?')
"1\\.5-2\\.0\\?"
regexReplace(string str, string pattern, string replacement)
Returns the input with the first match replaced by replacement
string.
Example
DS.Regex.regexReplace('wishyfishyisishy', @'ish', 'and')
"wandyfishyisishy"
regexGlobalReplace(string str, string pattern, string replacement)
Returns the input with all matches replaced by replacement
string.
Example
DS.Regex.regexGlobalReplace('wishyfishyisishy', @'ish', 'and')
"wandyfandyisandy"
regexGlobalReplace(string str, string pattern, function replacement)
Returns the input with all matches replaced by the result of the replacement
function. The function must return string result and take a single object as an argument in the following structure:
-
string
- the matched string; -
captures
- array of captured subgroups in the match, if any; -
namedCaptures
- map of named subgroups, if any;
Example
local square(obj) = std.toString(std.pow(std.parseInt(obj.string), 2)); DS.Regex.regexGlobalReplace("xxx2yyy4zzz6aaa", "\\d", square)
"xxx4yyy16zzz36aaa"
URL
encode(string data, string encoding="UTF-8")
Translates a string into application/x-www-form-urlencoded
format using the supplied encoding scheme to obtain the bytes for unsafe characters. The default encoding is UTF-8
.
Example
DS.URL.encode('Hello World')
"Hello+World"
decode(string data, string encoding="UTF-8")
Decodes a application/x-www-form-urlencoded string using a specific encoding scheme. The supplied encoding is used to determine what characters are represented by any consecutive sequences of the form "%xy".
Example
DS.URL.decode('Hello+World')
"Hello World"