Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Group with other old libraries

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

...

 

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
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
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
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
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 favorites
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
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
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 order
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

...

Each of these requires an array the fields that you desire to either suppress the value or the entire element

Char

chr

chr(<integer>)

Returns the ascii value of the passed number

...

For values beyond 0x0100 in the Unicode chart, you can pass a hex value for a Unicode character and get that character back

ord

ord(<string>)

KRL doesn't distinguish chars from string, so if the string provided has more than one character, only the first character will be processed

...

For heaven's sake, if you are cutting and pasting characters between sources and KRL, make sure that your encoding is set to 'UTF-8'.  Many browsers will default to Western (ISO-8859-1) which translates approximately to garbage in the Unicode table.

pack

pack([<integer>, <integer2> .. , <integerN>] );

...

pack is restricted to assembling strings

unpack

unpack(<string>);

Returns an array of ordinal values

Code Block
languagejavascript
themeConfluence
langjavascript
myArray = this2that:unpack("hij");			// [104, 105, 106]
 

pack is restricted to assembling strings

 

Base64

string2base64

string2base64(<string>|,<eol>)

...

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

base642string

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

url2base64

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

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

base642url

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

...