Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: hashpath -> hash path

...

<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
themeConfluence
langjavascript
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"]

 

Given an array of similar objects such as returned by the twitter user_timeline function, an abbreviated object is shown below

 

Code Block
themeConfluence
langjavascript
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
themeConfluence
langjavascript
titleSort by number of retweets
langjavascript
sort_opt = {
	'path' : ['retweet_count']
};
 
sorted_array = this2that:transform(myArray,sort_opt);

 

Descending sort

Code Block
themeConfluence
langjavascript
titleSort by number of retweets in descending order
langjavascript
sort_opt = {
	'path' : ['retweet_count'],
	'reverse' : 1
};
 
sorted_array = this2that:transform(myArray,sort_opt);

 

Force sort on <field> as a string

Code Block
themeConfluence
langjavascript
titleSort by number of retweets in alphabetical order
langjavascript
sort_opt = {
	'path' : ['retweet_count'],
	'compare' : 'string'
};
 
sorted_array = this2that:transform(myArray,sort_opt);

 

Sort on a date field

Code Block
themeConfluence
langjavascript
titleSort by number of retweets by date created
langjavascript
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
themeConfluence
langjavascript
titleSort by number of retweets and favoriteslangjavascript
sort_opt = [
	{
	'path' : ['retweet_count'],
	'reverse' : 1
	},
	{
	'path' : ['favorite_count']
	}
 ];

sorted_array = this2that:transform(myArray,sort_opt);

 

Skip the first 5 entries

Code Block
themeConfluence
langjavascript
titleSort by number of retweets
langjavascript
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
themeConfluence
langjavascript
titleSort by number of retweets
langjavascript
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
themeConfluence
langjavascript
titleSort by number of retweets in descending orderlangjavascript
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
language
languagejavascript
themeConfluence
javascriptlangjavascript
jstr = this2that:xml2json(xmlstring,{
	'attribute_prefix' : '_:_'
});

...

A similar option is available to change the content key

Code Block
language
languagejavascript
themeConfluencejavascript
jstr = this2that:xml2json(xmlstring,{
	'content_key' : '-t-'
});

...

Returns the ascii value of the passed number

Code Block
languagejavascript
themeConfluence
languagejavascript
langjavascript
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
languagejavascript
themeConfluence
languagejavascript
langjavascript
myNum = this2that:ord("e");				// 101
 
myUnicode = this2that:ord("?");			// 0x263A

...

Returns the ascii value of the passed numbers concatenated into a string

Code Block
languagejavascript
themeConfluence
languagejavascript
langjavascript
myStr = this2that:pack([101, 102, 103]);			// 'efg'
 

...

unpack

unpack(<string>);

Returns an array of ordinal values

Code Block
languagejavascript
themeConfluence
languagejavascript
langjavascript
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
language
languagejavascript
themeConfluence
javascriptlangjavascript
b64 = this2that:string2base64("the rain in spain","\n")

Omitting the <eol> character will return an unformatted base64 string

base642string

Code Block
languagejavascript
themeConfluencelanguagejavascript
langjavascript
txt = this2that:base642string("U3VwZXJEdXBlcjogYXNjaWk=")

...

The url-safe base64 conversions use "-" and "_" instead of "+" and "/"

Code Block
languagejavascript
themeConfluencelanguagejavascript
langjavascript
url64 = this2that:url2base64("3+4/7 = 1");

base642url

Code Block
languagejavascript
themeConfluencelanguagejavascript
langjavascript
urltxt = this2that:base642url("Mys0LzcgPSAx");

...