Showing posts with label Method. Show all posts
Showing posts with label Method. Show all posts

2009-09-15

Ordinal Numbers

Below are two similar techniques for generating ordinal numbers such as 1st, 2nd, 3rd, from simple integers such as 1, 2, 3 respectively. I needed this for a fairly complex user interface where I wanted to setup a tool tip that said 'Select 1st choice:', 'Select 2nd choice', etc.
Below are both a method and a function. You only need one, not both. All that differs is how you call them. The documentation in both show the differences in sample uses.


Number.prototype.ordinal = function () {
//-------------------------------------------------------------------------
//-- O R D I N A L
//-------------------------------------------------------------------------
//-- Generic: Yes, for ECMAScript, ExtendScript, and JavaScript
//-------------------------------------------------------------------------
//-- Purpose: To take a number and return a string with that number
//-- converted to an ordinal number. Thus 1 becomes 1st. 2 becomes 2nd.
//-- 3 becomes 3rd. 12 becomes 12th.
//-------------------------------------------------------------------------
//-- Arguments: none, this is a method.
//-------------------------------------------------------------------------
//-- Calls: Nothing.
//-------------------------------------------------------------------------
//-- Returns: a string with an ordinal version of the number unless the
//-- original number is not deemed to be an integer.
//-------------------------------------------------------------------------
//-- Sample Uses:
//-- Number(13).ordinal() ;
//-- for ( i = 0 ; 100 >= i ; i++ ) {
//-- $.writeln( i.ordinal() );
//-- }
//-------------------------------------------------------------------------
//-- Notes: Tested for 0 through 200.
//-------------------------------------------------------------------------
//-- Modified: 2009.09.07 by Jon S. Winters of electronic publishing support
//-- original, undocumented concept un acknowledged but listed at:
//-- "http://querylog.com/q/javascript+string+ordinal"
//-- eps@electronicpublishingsupport.com
//-- For additional information on cardinal numbers, check out:
//-- http://en.wikipedia.org/wiki/Names_of_numbers_in_English#Ordinal_numbers
//-------------------------------------------------------------------------

//-- Start by varifying that the orginal is an integer
var asInt = parseInt ( this ) ;
if ( isNaN ( asInt ) ) { return this ; }
//-- Make a string version of the text to allow regular expression
//-- matching for the numbers.
var asString = this.toString();
//-- Do a series of if statements to concatinate the appropriate
//-- suffix to the number. All 1, 2, 3 except for 11, 12, 13
//-- have common suffixs. All others are th's.
//-- Check for items ending with 1 except for 11 and add st
if ( asInt == 1 || asString.match( new RegExp ( '[^1]1$' ) ) ) return asInt + 'st' ;
//-- Check for items ending with 2 except for 12 and add nd
if ( asInt == 2 || asString.match( new RegExp ( '[^1]2$' ) ) ) return asInt + 'nd' ;
//-- Check for items ending with 3 except for 13 and add rd
if ( asInt == 3 || asString.match( new RegExp ( '[^1]3$' ) ) ) return asInt + 'rd' ;
//-- All but these end with th. This includes 0.
return asInt + 'th';
}
function ordinal ( value ) {
//-------------------------------------------------------------------------
//-- O R D I N A L
//-------------------------------------------------------------------------
//-- Generic: Yes, for ECMAScript, ExtendScript, and JavaScript
//-------------------------------------------------------------------------
//-- Purpose: To take a number and return a string with that number
//-- converted to an ordinal number. Thus 1 becomes 1st. 2 becomes 2nd.
//-- 3 becomes 3rd. 12 becomes 12th.
//-------------------------------------------------------------------------
//-- Arguments: one, the numberical value. This should be an integer.
//-------------------------------------------------------------------------
//-- Calls: Nothing.
//-------------------------------------------------------------------------
//-- Returns: a string with an ordinal version of the number unless the
//-- original number is not deemed to be an integer.
//-------------------------------------------------------------------------
//-- Sample Use: ordinal(13)
//-------------------------------------------------------------------------
//-- Notes: Tested for 0 through 200.
//-------------------------------------------------------------------------
//-- Modified: 2009.09.07 by Jon S. Winters of electronic publishing support
//-- original, undocumented concept un acknowledged but listed at:
//-- "http://querylog.com/q/javascript+string+ordinal"
//-- eps@electronicpublishingsupport.com
//-- For additional information on cardinal numbers, check out:
//-- http://en.wikipedia.org/wiki/Names_of_numbers_in_English#Ordinal_numbers
//-------------------------------------------------------------------------

//-- Start by varifying that the orginal is an integer
//-- return the original value if it is not
var asInt = parseInt ( value ) ;
if ( isNaN ( asInt ) ) { return value ; }
//-- Make a string version of the text to allow regular expression
//-- matching for the numbers.
var asString = value.toString();
//-- Do a series of if statements to concatinate the appropriate
//-- suffix to the number. All 1, 2, 3 except for 11, 12, 13
//-- have common suffixs. All others are th's.
//-- Check for items ending with 1 except for 11 and add st
if ( asInt == 1 || asString.match( new RegExp ( '[^1]1$' ) ) ) return asInt + 'st' ;
//-- Check for items ending with 2 except for 12 and add nd
if ( asInt == 2 || asString.match( new RegExp ( '[^1]2$' ) ) ) return asInt + 'nd' ;
//-- Check for items ending with 3 except for 13 and add rd
if ( asInt == 3 || asString.match( new RegExp ( '[^1]3$' ) ) ) return asInt + 'rd' ;
//-- All but these end with th. This includes 0.
return asInt + 'th';
}

2009-09-14

Time Zone Converter

This is an interesting thing. I needed to convert the time in some sports agate files from EST or EDT as sent by the Associated Press to PST or PDT depending upon the time of year. That is a 3 hour shift. But in case I or someone needed to do this for another time zone I wanted an easy way to do this.
I took an odd approach with an Array Rotation Method. Take a look at both the method's and the function's documentation for details.
I've been intending to get this posted for some weeks, but have been tied up with projects. Let me know if you find it helpful.


/*
Time zone converter.
*/



Array.prototype.rotate = function ( offset ) {
//-------------------------------------------------------------------------
//-- R O T A T E
//-------------------------------------------------------------------------
//-- Generic: Very. Should work within any ECMA based language that
//-- supports .push(), .pop(), .shift(), and .unshift()
//-------------------------------------------------------------------------
//-- Purpose: To rotate an array around such that the end is pushed to the
//-- front, or the front is pushed onto the end. See the sample use.
//-------------------------------------------------------------------------
//-- Arguments: 1) An offset number. See samples for results.
//-- when the argument is not specified, the value defaults to 1.
//-- When a non-integer value is provided, not changes are made.
//-------------------------------------------------------------------------
//-- Calls: Nothing, but uses the 4 ExtendScript methods
//-- .push(), .pop(), .shift(), and .unshift() which exist is JavaScript
//-------------------------------------------------------------------------
//-- Returns: Nothing. Modifies the original array.
//-------------------------------------------------------------------------
//-- Sample Use: With this method declared BEFORE its use...
//~ var a = [0,1,2,3,4,5,6,7,8,9].rotate(3) ; //-- [3,4,5,6,7,8,9,0,1,2]
//~ var b = [0,1,2,3,4,5,6,7,8,9].rotate(-3) ; //-- [7,8,9,0,1,2,3,4,5,6]
//~ var c = [0,1,2,3,4,5,6,7,8,9].rotate(0) ; //-- [0,1,2,3,4,5,6,7,8,9]
//~ var d = [0,1,2,3,4,5,6,7,8,9].rotate() ; //-- [1,2,3,4,5,6,7,8,9,0]
//~ var e = [0,1,2,3,4,5,6,7,8,9].rotate('Jiggy') ; //-- [0,1,2,3,4,5,6,7,8,9]
//~ var timeDifference = - 7 ; //-- 7 hours
//~ var hourSomewhere = 14 ; //-- 2 PM (GMT?)
//~ var timeHere = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23].rotate(timeDifference)[hourSomewhere] //-- 7 AM
//-------------------------------------------------------------------------
//-- Notes: This is a mehod, not a function. It is availabe to all arrays
//-- when it is declared before its use (put it at the very top of
//-- the script file or call it with a #include).
//-------------------------------------------------------------------------
//-- Written: 2009.08.28 by Jon S. Winters of electronic publishing support
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------
if ( arguments[0] == undefined ) {
offset = 1 ; //-- Default rotate up one if an arguement isn't supplied
}
//-- Make sure we can get an integer version of what was passed
offset = parseInt ( offset )

//-- Check for valid argument, if not return the original array
if ( isNaN ( offset ) || offset == 0 ) { return this ; }

//-- If the offset is negative, then rotate the end to the front
if ( 0 > offset ) {
for ( var rIndex = 0 ; rIndex > offset ; rIndex-- ) { this.unshift(this.pop()) ; }
return this ;
}
//-- implied else
//-- Rotate from the front to the end
for ( var rIndex = 0 ; offset > rIndex ; rIndex++ ) { this.push(this.shift()) ; }
return this ;
}
//




var s = 'N.Y. Mets (Redding 1-4)\
at Florida (A.Sanchez 2-4), 1:10 p.m.' ;
$.writeln ( adjustTime ( s , -3 ) )



function adjustTime ( s , offset ) {
//-------------------------------------------------------------------------
//-- A D J U S T T I M E
//-------------------------------------------------------------------------
//-- Generic: Yes, but uses the Array Method .rotate(offset).
//-- This .rotate(offset) method is provided elsewhere.
//-------------------------------------------------------------------------
//-- Purpose: To take a string, look for times and replace them with a
//-- value that is a specified number of hours different. To switch
//-- from EDT to PDT send it a -3 value.
//-------------------------------------------------------------------------
//-- Arguments: 2
//-- s: a the string to search and replact the times.
//-- offset: a number, either positive or negative to use to offset
//- the time by.
//-------------------------------------------------------------------------
//-- Calls: .rotate()
//-------------------------------------------------------------------------
//-- Returns: The string with the time swapped, or the original string
//-- if no times were found.
//-------------------------------------------------------------------------
//-- Written: 2009.08.28 by Jon S. Winters of electronic publishing support
//-- Edited Comments: 2009.10.27
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------
//-- Build, as string, not Regular Expressions
//-- the 4 different patterns we need to match.
var hrPatt = '(0?[1-9]|1[012])' ;
var minPatt = '([0-5]\\d)' ;
var AMPMpatt = '\\s+[AP]\\.?M\\.?' ;
var PMpatt = '\\s+P\\.?M\\.?'

//-- Now define the main pattern and grap the matches
var matches = s.match ( new RegExp ('(' + hrPatt + ':' + minPatt + ')' + AMPMpatt ,'gim') )
//-- Check for matches, and if there are,
//-- then process each of them within a loop
if ( matches != null ) {
for ( var mIndex = matches.length - 1 ; mIndex >= 0 ; mIndex-- ) {

var allHours = [12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11]
var allAMPM = ['a.m.','a.m.','a.m.','a.m.','a.m.','a.m.','a.m.','a.m.','a.m.','a.m.','a.m.','a.m.','p.m.','p.m.','p.m.','p.m.','p.m.','p.m.','p.m.','p.m.','p.m.','p.m.','p.m.','p.m.']
//-- get the current original string
var currentMatchString = matches[mIndex] ;
//-- Now get the specifics
//-- Get the hour as a number, the minutes as a string and the AM/PM as boolean
var originalHour = parseInt ( currentMatchString.match(new RegExp ( hrPatt + ':' , 'gi' ) )[0] ) ;
var originalMinutes = currentMatchString.match (new RegExp (':' + minPatt , 'gi' ) )[0] ;
var isPM = new RegExp ( PMpatt , 'gi' ).test ( currentMatchString ) ;
//-- Figure out where the original position of that hour in the arrays
var originalIndex = originalHour ;
if ( originalHour == 12 ) { originalIndex = 0 ; }
if ( isPM ) { originalIndex = originalIndex + 12 ; }

//-- Use the array method .rotate() to find the new values at that
//-- original Index location.
var newHour = allHours.rotate(offset)[originalIndex] ;
var newAMPM = allAMPM.rotate(offset)[originalIndex] ;
//--
s = s.replace ( currentMatchString , newHour + originalMinutes + ' ' + newAMPM ) ;
} //-- end of loop
return s ;
} //-- end of match
} //
//