Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The this2that library provides utility functions for converting from one object to another

General Functions

transform

transform(<obj>,<sort options> |,<global options>)

ParameterDatatypeRequired
<obj>hash/map or array 
<sort options>hash or array of hashes 
<global options>hash 

<obj> : array or hash of values to be sorted

<sort options> :

  • path : <hashpath> 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
<global options> : parameters applied after the <obj> has been sorted
  • index : <integer> 0-based index of which element of the sorted array to return to the user
  • limit : <integer> number of elements of the sorted array to return
A note on datetime formats.  The sorting algorithm should recognize ISO8601 and common internet and email formats.  The format returned by Twitter is slightly different and requires a format string to allow the sort mechanism to parse correctly.  The Twitter format string is demonstrated in examples below.
Providing a hash as the obj, will return an array of the hash keys sorted by the provided parameters (sort by reference)
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
titleSort by number of retweets
langjavascript
sort_opt = {
	'path' : ['retweet_count']
};
 
sorted_array = this2that:transform(myArray,sort_opt);

 

Descending sort

Code Block
themeConfluence
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
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
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
titleSort by number of retweets and favorites
langjavascript
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
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
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
titleSort by number of retweets in descending order
langjavascript
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

...