...
<obj> : array or hash of values to be sorted
<sort options> :
- path : <hashpath> <hash path> to field that is providing the sort value required
- reverse : <boolean> sort in descending order (default is to sort in ascending order)
- compare : <string> force sorting to a specific datatype
- datetime
- numeric
- string
- format : <string> a strptime compatible string containing format rules for non-ISO8601 datetime strings
...
Code Block | ||||
---|---|---|---|---|
| ||||
myHash = { "0001" : { "type": "donut", "name": "Cake", }, "0002" : { "type": "donut", "name": "Raised", }, "0003": { "type": "donut", "name": "Old Fashioned", }, "0004" : { "type": "muffin", "name": "Poppy seed", } }; sort_opt = { 'path' : ['name'] }; sorted_array = this2that:transform(myHash,sort_opt); // ["0001", "0003", "0004", "0002"] |
Code Block | ||||
---|---|---|---|---|
| ||||
myArray = [ { 'retweet_count' : 360, 'created_at' : 'Fri Aug 16 21:19:37 +0000 2013', 'in_reply_to_status_id_str' : undef, 'contributors' : undef, 'text' : "RT \@SharylAttkisson: Still waiting for White House to call back to release the White House photos from Benghazi night. I've been asking sin\x{2026}", 'user' : { 'id_str' : '13524182', 'id' : 13524182 }, 'id' : '368482201631227904', 'lang' : 'en', 'geo' : undef, 'id_str' : '368482201631227904', 'favorite_count' : 0, }, .. ] |
Providing an array as the sorted object will return the entire object entries in sorted order (sort by value)
Simple sort on a field
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
sort_opt = { 'path' : ['retweet_count'] }; sorted_array = this2that:transform(myArray,sort_opt); |
Descending sort
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
sort_opt = { 'path' : ['retweet_count'], 'reverse' : 1 }; sorted_array = this2that:transform(myArray,sort_opt); |
Force sort on <field> as a string
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
sort_opt = { 'path' : ['retweet_count'], 'compare' : 'string' }; sorted_array = this2that:transform(myArray,sort_opt); |
Sort on a date field
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
sort_opt = { 'path' : ['created_at'], 'compare' : 'datetime', 'format' : '%a %b %d %H:%M:%S %z %Y' }; sorted_array = this2that:transform(myArray,sort_opt); |
Sort on multiple fields, retweet_count (descending) then favorite_count (ascending)
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
sort_opt = [ { 'path' : ['retweet_count'], 'reverse' : 1 }, { 'path' : ['favorite_count'] } ]; sorted_array = this2that:transform(myArray,sort_opt); |
Skip the first 5 entries
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
sort_opt = { 'path' : ['retweet_count'] }; global_opt = { 'index' : 4 }; sorted_array = this2that:transform(myArray,sort_opt,global_opt); |
Skip 5, only return 7 entries
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
sort_opt = { 'path' : ['retweet_count'] }; global_opt = { 'index' : 4, 'limit' : 7 }; sorted_array = this2that:transform(myArray,sort_opt,global_opt); |
Sort the values of a hash
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
sort_opt = { 'path' : ['retweet_count'], 'reverse' : 1 }; sorted_hash_keys = this2that:transform(myHash{['path', 'to', 'keys']},sort_opt); |
xml2json
xml2json(<string>|,<map>)
- convert a string of XML to JSON
...
With attribute_prefix you can change the attribute prefix from the default '$'
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
jstr = this2that:xml2json(xmlstring,{ 'attribute_prefix' : '_:_' }); |
...
A similar option is available to change the content key
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
jstr = this2that:xml2json(xmlstring,{ 'content_key' : '-t-' }); |
...
Returns the ascii value of the passed number
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
myChar = this2that:chr(101); // 'e' myUnicode = this2that:chr(0x010f); // '?' |
...
KRL doesn't distinguish chars from string, so if the string provided has more than one character, only the first character will be processed
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
myNum = this2that:ord("e"); // 101 myUnicode = this2that:ord("?"); // 0x263A |
...
Returns the ascii value of the passed numbers concatenated into a string
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
myStr = this2that:pack([101, 102, 103]); // 'efg' |
...
unpack
unpack(<string>);
Returns an array of ordinal values
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
myArray = this2that:unpack("hij"); // [104, 105, 106] |
pack is restricted to assembling strings
...
Specifying an end-of-line character will format the base64 string into 74 character blocks
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
b64 = this2that:string2base64("the rain in spain","\n") |
Omitting the <eol> character will return an unformatted base64 string
base642string
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
txt = this2that:base642string("U3VwZXJEdXBlcjogYXNjaWk=") |
...
The url-safe base64 conversions use "-" and "_" instead of "+" and "/"
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
url64 = this2that:url2base64("3+4/7 = 1"); |
base642url
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
urltxt = this2that:base642url("Mys0LzcgPSAx"); |
...