The JavaScript Superset extends the functionality of JavaScript's core objects.
Some of the functions in the JavaScript Superset are merely shortcuts (renamed versions) of existing JavaScript functionality.
Most algorithms, however, offer single-command convenience for new functionality.
Note that these functions extend JavaScript 1.5; some of these extensions are included in newer versions of JavaScript but that doesn't help those who don't yet have access to them.
[ Download the JavaScript Superset here ]
The JavaScript Superset was written by a developer for developers; as such, it contains very few error checks.
This is by design -- error checks slow execution speed.
(In other words, there is almost no type-checking and few try-catch blocks.)
If I were implementing the JavaScript Superset as a team resource, I would probably add such checks but this is unnecessary in my own work.
Also note there are no "localization" options -- specifically, the phone number and currency functions assume U.S.A. formats and would have to be (manually) altered for any localization needs.
These functions are designed to work as a set; many functions rely on other functions being present.
You can strip out any undesirable functions as long as any dependent functionality remains.
Be aware that any errors -- especially "Object doesn't support this property or method" -- are indicative of missing functionality.
I could of course rewrite the entire JavaScript Superset so each function is independent but that would require LOTS of duplicate code, something I think is a terrible developer practice;
my coding philosophy is "less is more". To further this philosophy, you may wish to compact this code via an obfuscator.
Finally, the functions below are listed in alphabetical order (under their respective category) for ease of reference.
In the actual code, most functions are in alphabetical order (again, under their respective category) but a few are not;
those that are not have been "logically" grouped together (at least according to my logic).
Contact javascript.superset@IQ01.com with questions or comments.
[ Last update: 23 April 2009 ]
The function descriptions below are arranged in the following format...
functionName
|
Type:
|
class
: inherent to a specific class; without a referring object, the function cannot be called
constant
: a property that can be called directly from an inherent JavaScript object (e.g., String.CRLF)
global
: can be called directly or from an inherent JavaScript object (e.g., Alert() or Number.random())
prototype
: extends an inherent JavaScript object's functionality; the function uses the value of the calling object to calculate its return value
| |
Parameters:
|
The function's parameters (if any) are listed here, along with their value types; the default value (if any) is shown in parentheses.
If no parameters are listed, any passed parameters are ignored.
| |
Description:
|
An explanation of what the function does or returns.
| |
Example:
|
Example code that can be copied and pasted for verification.
(Click the "Example" link to execute the code immediately from this page.)
|
Alert |
Type:
| global | |
Parameters:
| text
[string]
| |
Description:
|
A glorified version of the standard alert() function, this version simply adds decorative trim.
| |
Example:
| Alert("Hello world!"); |
COUNTER |
Type:
| global | |
Description:
|
A global counter variable used with the Counter() and ResetCounter() methods (below).
| |
Example:
| var x = COUNTER; alert(x); |
Counter |
Type:
| global | |
Description:
|
Returns the current value of the global variable [COUNTER] (above) and increments its value by one.
(Click the Example below mutliple times to see the effect.)
| |
Example:
| var x = Counter(); alert(x); |
Default |
Type:
| global | |
Parameters:
| value
[variant]
defaultValue
[variant]
| |
Description:
|
Assigns default values to passed parameters, since JavaScript does not support parameter
defaults. If [value] is undefined (i.e., a function is called without passing a required parameter), Default
returns [defaultValue]; otherwise, it returns [value]. NOTE: NULL is not the same as undefined!
| |
Example:
| var someParameter; var x = Default(someParameter, 10); alert(x); |
Element |
Type:
| global | |
Parameters:
| id
[string]
| |
Description:
|
Returns a reference to the document element indicated by [id].
If NULL is returned, either the element does not exist in the current document (check the spelling of [id]
and the desired element's id) or the current browser's Document Object Model does not support referencing elements
in this way (which is an effective means of determining browser capabilities). (For the example, I have included
an empty DIV with and ID of "JavaScriptSupersetSampleDiv" on the page.)
| |
Example:
| var pageElement = Element("JavaScriptSupersetSampleDiv"); if (pageElement == null) { alert("uh-oh"); } else { alert("groovy"); } pageElement = Element("SomeOtherPageElementId"); if (pageElement == null) { alert("uh-oh"); } else { alert("groovy"); } |
Include |
Type:
| global | |
Parameters:
| filePath
[string]
| |
Description:
|
Includes a separate JavaScript file within the current code at the point where Include() is called.
(Developer Note: Although this allows for easy inclusion of external JavaScript files, the consensus seems to be,
for the sake of speed, only one JavaScript file be included on a page.
Thus, it's best to consolidate all of your JavaScript files into one source if possible.)
| |
Example:
| //Include("some_code.js"); // uncomment this in your page before using |
NewObject |
Type:
| global | |
Parameters:
| object
[object]
| |
Description:
|
Allows the creation of objects using true prototypal inheritance.
(For more information on prototypal inheritance, see http://javascript.crockford.com/prototypal.html).
| |
Example:
| var oldObject = null; var myObject = new NewObject(oldObject); alert(myObject); |
Nil |
Type:
| global | |
Description:
|
Displays a message indicating the current functionality (of whatever) has not yet been implemented.
| |
Example:
| Nil(); |
ResetCounter |
Type:
| global | |
Parameters:
| value
[integer
(0)
]
| |
Description:
|
Resets the value of [COUNTER] (above) to the supplied value, usually zero.
| |
Example:
| ResetCounter(); alert(COUNTER); ResetCounter(12); alert(COUNTER); |
| Ajax |
Type:
| global | |
Parameters:
| url
[string]
callbackFn
[function
(function() { } [empty function])
]
| |
Description:
|
Creates and returns an Ajax object for use with asynchronous internet interactions.
| |
Example:
| alert(new Ajax("http://iq01.com")); |
updateGet |
Type:
| class | |
Parameters:
| data
[variant (should be in a key-value form; e.g., "key1=value1&key2=value2...")]
| |
Description:
|
Allows Ajax interactions via a GET method.
Returns TRUE if the transaction was successful; otherwise, returns FALSE.
| |
Example:
| var x = new Ajax("http://iq01.com/now.php"); alert(x.updateGet()); alert(x.updateGet("dateFormat=His")); |
updatePost |
Type:
| class | |
Parameters:
| data
[variant (should be in a key-value form; e.g., "key1=value1&key2=value2...")]
| |
Description:
|
Allows Ajax interactions via a POST method.
Returns TRUE if the transaction was successful; otherwise, returns FALSE.
| |
Example:
| var x = new Ajax("http://iq01.com/now.php"); alert(x.updatePost()); alert(x.updatePost("dateFormat=His")); |
| average |
Type:
| prototype | |
Description:
|
Returns the average (numeric) value of a given array. If the given array contains any non-numeric value(s),
the result will always be NaN (Not a Number).
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.average()); |
compact |
Type:
| prototype | |
Description:
|
Removes all empty (undefined) elements from an array.
| |
Example:
| var x = new Array(1000); x[1] = 1; x[2] = 2; x[33] = 3; x[49] = 4; x[500] = 5; alert(x.compact()); |
emptyElements |
Type:
| prototype | |
Description:
|
Returns an array of empty-element (undefined) index(es) in a given array. (The original array is not altered.)
| |
Example:
| var x = [1, undefined, 3, 4, undefined, undefined, 7, 8, undefined, 10]; alert(x.emptyElements()); |
erase |
Type:
| prototype | |
Description:
|
Erases all elements from a given array and returns the result (which will always be an empty array).
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.erase().length); |
every |
Type:
| prototype | |
Parameters:
| callback
[boolean function]
| |
Description:
|
Returns a boolean value indicating if the given [callback] function returns TRUE for every element in the array.
(This is in contrast to the [some] function below.)
Note this function assumes the [callback] function returns a boolean value.
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.every(function (z) { return (z > 0); })); alert(x.every(function (z) { return (z > 3); })); alert(x.every(function (z) { return (z < 100); })); |
filter |
Type:
| prototype | |
Parameters:
| callback
[boolean function]
| |
Description:
|
Returns a new array composed of the original array's values that are TRUE for the [callback] function.
Note this function assumes the [callback] function returns a boolean value.
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.filter(function (z) { return (z.isEven()); })); alert(x.filter(function (z) { return (z.isOdd()); })); |
first |
Type:
| prototype | |
Parameters:
| nonEmpty
[boolean
(FALSE)
]
| |
Description:
|
Returns the first element of an array.
If [nonEmpty] is TRUE, returns the first non-empty element of the array (if one exists).
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.first()); x = [undefined, undefined, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.first(true)); |
flatten |
Type:
| prototype | |
Description:
|
Returns a new, one-dimensional array of a given array's "flattened" values.
In other words, any multi-dimensional elements are flattened into one-dimensional elements.
| |
Example:
| var x = [1, 2, [3, [4, [5]], 6], 7, [8, [9, 10]]]; alert(x.length + " - " + x.flatten() + " - " + x.flatten().length); |
forEach |
Type:
| prototype | |
Parameters:
| callback
[function]
| |
Description:
|
Calls the given [callback] function for each element in the array.
Note that unlike the [map] and [using] functions (below), this function has no return value.
| |
Example:
| var x = ["ann", "bob", "dave", "mary", "nancy", "steve"]; alert(x.forEach(function (z) { alert(z + " rules!"); })); // should return "undefined" as the last alert |
has |
Type:
| prototype | |
Parameters:
| value
[variant]
| |
Description:
|
This is an alias for the Array.hasValue function; see that function for more info.
| |
Example:
| // see Array.hasValue function for examples |
hasEmptyElements |
Type:
| | |
Description:
|
Returns TRUE of FALSE depending on if a given array has any empty (undefined) elements.
| |
Example:
| var x = [1, 2, 3, 4, 5]; alert(x.hasEmptyElements()); x = [1, 2, undefined, 4, 5]; alert(x.hasEmptyElements()); |
hasValue |
Type:
| prototype | |
Parameters:
| value
[variant]
| |
Description:
|
Searches through a given array and returns a boolean value indicating if the array contains [value].
To find the index of [value] within an array, use Array.indexOf() or Array.search() instead.
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.hasValue(4)); alert(x.hasValue("ann")); |
indexCount |
Type:
| prototype | |
Parameters:
| value
[variant]
| |
Description:
|
Returns the number of times the variant [value] appears in a given array.
| |
Example:
| var x = [8, 8, 8, 5, 5, 5, 1, 2, 1, 2]; alert(x.indexCount(8)); alert(x.indexCount(1)); alert(x.indexCount(0)); |
indexOf |
Type:
| prototype | |
Parameters:
| value
[variant]
| |
Description:
|
Searches through a given array and returns the index of [value].
If [value] is not found, returns -1.
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.indexOf(4)); alert(x.indexOf("ann")); |
initialize |
Type:
| prototype | |
Parameters:
| value
[variant
(0)
]
| |
Description:
|
Sets all elements of an array to the supplied value.
| |
Example:
| var x = new Array(25); x.initialize(); alert(x); x.initialize(12); alert(x); |
isEmpty |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if an array is empty (has no elements).
| |
Example:
| var x = []; alert(x.isEmpty()); x[0] = 4; alert(x.isEmpty()); |
itemList |
Type:
| prototype | |
Parameters:
| listCount
[integer
(2)
]
| |
Description:
|
Returns a new array of random elements from the given array.
(The number of elements in the returned array is defined by [listCount].)
If the given array has less than 3 elements or [listCount] is out of bounds of the given array,
the original array is returned instead.
| |
Example:
| var x = ["ann", "bob", "dave", "mary", "nancy", "steve"]; alert(x.itemList()); // possible outcome: mary,steve alert(x.itemList(3)); // possible outcome: mary,steve,nancy |
last |
Type:
| prototype | |
Parameters:
| nonEmpty
[boolean
(false)
]
| |
Description:
|
Returns the last element of an array. If [nonEmpty] is TRUE, returns the last non-empty element.
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, undefined, undefined]; alert(x.last()); alert(x.last(true)); |
lastIndexOf |
Type:
| prototype | |
Parameters:
| value
[variant]
| |
Description:
|
Performs the same function as Array.indexOf() (above) but searches backwards through the array,
beginning with the final element. If [value] is not found, returns -1.
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.lastIndexOf(4)); alert(x.lastIndexOf("ann")); |
map |
Type:
| prototype | |
Parameters:
| callback
[function]
| |
Description:
|
This is a duplicate of the [using] function (below) but is included for completeness.
| |
Example:
| var x = ["ann", "bob", "dave", "mary", "nancy", "steve"]; alert(x.map(function (z) { return z + " rules!"; })); |
maxValue |
Type:
| prototype | |
Description:
|
Intended for arrays containing numeric values, returns the array's maximum value.
For arrays with non-numeric values, the results are unpredictable.
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.maxValue()); |
minValue |
Type:
| prototype | |
Description:
|
Intended for arrays containing numeric values, returns the array's minimum value.
For arrays with non-numeric values, the results are unpredictable.
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.minValue()); |
product |
Type:
| prototype | |
Description:
|
Returns the product of all elements of an array. This is mostly used in statistical applications.
Note any non-numeric entries will return "NaN".
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.product()); var x = [1, 2, 3, 4, 5, 6, 7, "x", 8, 9, 10]; alert(x.product()); |
random |
Type:
| prototype | |
Parameters:
| remove
[boolean
(true)
]
| |
Description:
|
Returns a random element from the given array.
If [remove] is TRUE (the default), the element is also removed from the array.
Repeatedly calling this function (with the default value of TRUE) while the given array's length is greater than zero
will produce a list of unique values in random order. This is especially useful for randomly displaying
advertisements on a page.
| |
Example:
| var x = ["ann", "bob", "dave", "mary", "nancy", "steve"]; alert(x.random(false)); // returned value remains in array while (x.length) { alert(x.random()); } |
randomize |
Type:
| prototype | |
Parameters:
| min
[integer
(1)
]
max
[integer
(100)
]
ensureUnique
[boolean
(false)
]
| |
Description:
|
Populates a given array's elements with random (numeric) values between [min] and [max] (inclusive).
If [ensureUnique] is set to TRUE, randomize() attempts to ensure each value within the array is unique.
However, before doing so it first checks to see if there are enough unique values for the array --
obviously, if there are only, say, 3 values to fill 20 elements, some values will repeat and [ensureUnique] is irrelevant.
| |
Example:
| var x = new Array(25); x.randomize(20, 30); alert(x); |
rgbGradient |
Type:
| global | |
Parameters:
| startColor
[string]
endColor
[string]
stepCount
[integer]
| |
Description:
|
Returns a [stepCount]-element array of intermediate color values between [startColor] and [endColor], inclusive.
The returned values can be used to create a gradient effect via a TABLE or DIV.
| |
Example:
| alert(Array.rgbGradient("#ffffff", "#000000", 10)); |
rgbToHex |
Type:
| prototype | |
Description:
|
Returns a 6-character string of a hex color from a given array.
(The function assumes the array's first 3 elements are the red, green and blue components respectively;
any other array elements are ignored.)
Note the returned string does not include the leading pound sign (#).
| |
Example:
| var x = [255, 153, 0]; alert(x.rgbToHex()); |
rotate |
Type:
| prototype | |
Parameters:
| toLeft
[boolean
(true)
]
| |
Description:
|
Rotates an array's elements to either the left or right, as specified by [toLeft].
If [toLeft] is TRUE (the default), the first element becomes the last and all other elements move to the
next lower position. If [toLeft] is FALSE, the last element becomes the first and all other elements move to
the next higher position.
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.rotate()); alert(x.rotate(false)); |
rotateLeft |
Type:
| prototype | |
Description:
|
Rotates an array's elements to the left.
This function is equivalent to the default behavior of Array.rotate() but is included for completeness.
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.rotateLeft()); |
rotateRight |
Type:
| prototype | |
Description:
|
Rotates an array's elements to the right.
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.rotateRight()); |
search |
Type:
| prototype | |
Parameters:
| value
[variant]
| |
Description:
|
This performs exactly the same function as Array.indexOf() (above).
A duplicate function was created for those programmers used to using regular expressions.
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.search(4)); alert(x.search("ann")); |
shuffle |
Type:
| prototype | |
Description:
|
Randomly rearranges the values of an array.
(This function will alter the original array; to preserve the order of its contents, use this on a copy of the array.)
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.shuffle()); |
some |
Type:
| prototype | |
Parameters:
| callback
[boolean function]
| |
Description:
|
Returns a boolean value indicating if the given [callback] function returns TRUE for any element in the array.
(This is in contrast to the [every] function above.)
Note this function assumes the [callback] function returns a boolean value.
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.some(function (z) { return (z.isEven()); })); alert(x.some(function (z) { return (z.isOdd()); })); alert(x.some(function (z) { return (z > 100); })); |
sortNumeric |
Type:
| prototype | |
Parameters:
| isReverse
[boolean
(false)
]
| |
Description:
|
Sorts the elements of an array according to their numerical values.
If [isReverse] is TRUE, the array is sorted in "reverse" order (highest to lowest value).
| |
Example:
| var x = [8, 7, 10, 2, 3, 1, 6, 4, 5, 9]; alert(x.sortNumeric()); alert(x.sortNumeric(true)); |
sortString |
Type:
| prototype | |
Description:
|
Sorts the elements of an array according to their string values.
This will place both 10 and 100 before 2.
| |
Example:
| var x = [8, 7, 10, 2, 3, 1, 6, 4, 5, 9]; alert(x.sortString()); |
sum |
Type:
| prototype | |
Description:
|
Returns the sum of an array. If the given array contains any non-numeric value(s), the result will be
a (perhaps unpredictable) concatenated string of the array's values, based on the JavaScript rules for
adding strings together.
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.sum()); x = [1, 2, "x", 3, 4, 5, 6, "y", 7, 8, 9, 10]; alert(x.sum()); |
swap |
Type:
| prototype | |
Parameters:
| first
[integer
(0)
]
second
[integer
(0)
]
| |
Description:
|
Swaps the two elements specified by [first] and [second].
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; x.swap(2, 4); alert(x); x.swap(2, 4); alert(x); |
test |
Type:
| prototype | |
Parameters:
| value
[variant]
| |
Description:
|
This performs exactly the same function as Array.hasValue() (above).
A duplicate function was created for those programmers used to using regular expressions.
| |
Example:
| var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; alert(x.test(4)); alert(x.test("ann")); |
toLowerCase |
Type:
| prototype | |
Description:
|
Sets all string elements of an array to their lowercase equivalents; non-string elements are unchanged.
| |
Example:
| var x = [1, "abc", 2, "DEF", "gHi", 3, 4]; alert(x.toLowerCase()); |
toUpperCase |
Type:
| prototype | |
Description:
|
Sets all string elements of an array to their uppercase equivalents; non-string elements are unchanged.
| |
Example:
| var x = [1, "abc", 2, "DEF", "gHi", 3, 4]; alert(x.toUpperCase()); |
unique |
Type:
| prototype | |
Description:
|
Returns only the unique (non-duplicate) elements of an array.
Note this function compares both values and types; thus in the third example, there appear to be
duplicates in the returned array but this is only because they are of different types (strings and numerics).
| |
Example:
| var x = [1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]; alert(x.unique()); x = [2, 2, 3, 3, 3, 1, 1, 3, 3, 1, 5, 5, 4, 5, 5, 5, 4, 4, 4]; alert(x.unique()); x = [2, 2, 3, 3, 3, 1, 1, 3, "3", 1, 5, 5, 4, "5", 5, 5, 4, 4, 4]; alert(x.unique()); |
using |
Type:
| prototype | |
Parameters:
| functionObject
[function]
| |
Description:
|
Iterates through an array, applies the function [functionObject] to every element, and returns a new array
of the results. The returned array should have the same length as the originating array.
| |
Example:
| var x = ["ann", "bob", "dave", "mary", "nancy", "steve"]; alert(x.using(function (z) { return z + " rules!"; })); |
| yesNo |
Type:
| prototype | |
Description:
|
Returns a string containing either "Yes" or "No" depending on the boolean value.
| |
Example:
| alert(true.yesNo()); alert(false.yesNo()); |
| compact |
Type:
| prototype | |
Parameters:
| showTime
[boolean
(false)
]
| |
Description:
|
Returns an 8-character string of the date in the form YYYYMMDD. (If [showTime] is TRUE, the time is appended
to the date as a 6-character string in the form HHMMSS, for a total of 14 characters.)
| |
Example:
| var now = new Date(); alert(now.compact()); alert(now.compact(true)); |
daysInYear |
Type:
| prototype | |
Description:
|
Returns the number of days in a given year (either 365 or 366 for leap years).
| |
Example:
| var now = new Date(); alert(now.daysInYear()); |
daysLeftInYear |
Type:
| prototype | |
Description:
|
Returns the number of days remaining in the year from the given date.
| |
Example:
| var now = new Date(); alert(now.daysLeftInYear()); |
dayOfYear |
Type:
| prototype | |
Description:
|
Returns the day of the year (1-366) for a given date.
| |
Example:
| var now = new Date(); alert(now.dayOfYear()); |
firstDay |
Type:
| prototype | |
Parameters:
| isGrid
[boolean
(false)
]
| |
Description:
|
Returns the starting weekday number of the first day of the month.
[isGrid] is related to how the render methods (below) draw the relevant month or year calendars.
For a grid layout, [isGrid] should be TRUE; for a row layout (the default), [isGrid] should be FALSE.
| |
Example:
| var date = new Date(2000, 0, 1); alert(date.firstDay()); // should return 6 (Saturday) for 1 January 2000 alert(date.firstDay(true)); // should return -5 for grid layout |
format |
Type:
| prototype | |
Parameters:
| formatString
[string
("l, F d, Y @ H:i:s")
]
| |
Description:
|
Returns a string containing the specified date value formatted according to the parameters of PHP's Date command
(see http://www.php.net/date for more info). Currently, the only supported parameters are
"AaDdFGgHhijLlMmNnoSstUuWwYyZz" (in alphabetical order); any characters in the [formatString] that are not
supported (such as punctuation) will simply be placed in the returned string as supplied.
Note this function does not support "escaped" characters so you cannot, for example, use the word "at" in
your [formatString] since both "a" and "t" are supported parameters.
| |
Example:
| var date = new Date(); alert(date.format()); alert(date.format("")); // empty formatString replaced with default alert(date.format("--> l, jS of F, Y @ g:i:s A!!! <--")); alert(date.format("??? the date is: Ymd ???")); // is this what you want? |
isLeapYear |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given date is in a leap year.
| |
Example:
| var now = new Date(); alert(now.isLeapYear()); |
monthDays |
Type:
| prototype | |
Description:
|
Returns an array of the number of days in each month for a given date's year.
| |
Example:
| var now = new Date(); alert(now.monthDays()); |
monthName |
Type:
| prototype | |
Description:
|
Returns the full name of the month for a given date.
| |
Example:
| var date = new Date(); alert(date.monthName()); |
monthNames |
Type:
| global / prototype | |
Description:
|
Returns an array of the full month names of the year (January, February, etc.).
| |
Example:
| alert(Date.monthNames()); |
renderMonthGrid |
Type:
| prototype | |
Parameters:
| isMonthOnly
[boolean
(true)
]
| |
Description:
|
Generates a calendar grid for the specified month. This method is designed to be used with other calendar functions.
Thus, if [isMonthOnly] is TRUE (the default), both the month name and year are displayed; otherwise, only the month
name is. (Note: The examples below open 2 pop-up windows; make sure you allow pop-ups if you want to see them.
Refer to the source code for more information.)
| |
Example:
| var newWindow1 = window.open("", "", "", false); newWindow1.document.write((new Date()).renderMonthGrid()); var newWindow2 = window.open("", "", "", false); newWindow2.document.write((new Date()).renderMonthGrid(false)); |
renderMonthRow |
Type:
| prototype | |
Parameters:
| isMonthOnly
[boolean
(true)
]
| |
Description:
|
Generates a calendar month in a single row.
(Note: The examples below open 2 pop-up windows; make sure you allow pop-ups if you want to see them.
Refer to the source code for more information.)
| |
Example:
| var newWindow1 = window.open("", "", "", false); newWindow1.document.write((new Date()).renderMonthRow()); var newWindow2 = window.open("", "", "", false); newWindow2.document.write((new Date()).renderMonthRow(false)); |
renderYearGrid |
Type:
| prototype | |
Parameters:
| columnCount
[integer
(4)
]
| |
Description:
|
Generates a calendar grid for the specified year. [columnCount] specifies the number of columns (months across)
to display. (Note: The examples below open 2 pop-up windows; make sure you allow pop-ups if you want to see them.
Refer to the source code for more information.)
| |
Example:
| var newWindow1 = window.open("", "", "", false); newWindow1.document.write((new Date()).renderYearGrid()); var newWindow2 = window.open("", "", "", false); newWindow2.document.write((new Date()).renderYearGrid(6)); |
renderYearRow |
Type:
| prototype | |
Description:
|
Generates a calendar with each month on a separate row. All days are aligned in the same column.
(Note: The example below opens a pop-up window; make sure you allow pop-ups if you want to see them.
Refer to the source code for more information.)
| |
Example:
| var newWindow1 = window.open("", "", "", false); newWindow1.document.write((new Date()).renderYearRow()); |
weekDay |
Type:
| prototype | |
Description:
|
Returns the full name of the day for a given date.
| |
Example:
| var date = new Date(); alert(date.weekDay()); |
weekDays |
Type:
| global / prototype | |
Description:
|
Returns an array of the full day names of the week (Sunday, Monday, etc.).
| |
Example:
| alert(Date.weekDays()); |
weekOfYear |
Type:
| prototype | |
Parameters:
| startDay
[integer
(1 : Monday)
]
| |
Description:
|
Returns an integer value of the specified date's week of the year.
| |
Example:
| alert((new Date()).weekOfYear()); alert((new Date()).weekOfYear(4)); // make Wednesday the start of the week |
| $ |
Type:
| | |
Description:
|
This is an alias for the document.Element function; see that function for more info.
| |
Example:
| // see document.Element function for examples |
addStyleRule |
Type:
| | |
Parameters:
| rule
[string]
sheet
[string
(0)
]
| |
Description:
|
Allows for programmatic rule additions to the document's stylesheet(s).
| |
Example:
| document.addStyleRule(".rule1 { background-color: #f00; }"); |
allowsCookies |
Type:
| | |
Description:
|
Returns a boolean value indicating if the client machine accepts cookies.
| |
Example:
| alert("cookies allowed? " + document.allowsCookies().yesNo()); |
deleteCookie |
Type:
| | |
Parameters:
| name
[string]
path
[string]
domain
[string]
| |
Description:
|
Deletes a cookie from the client machine.
| |
Example:
| document.deleteCookie("someCookie"); |
deleteStyleRule |
Type:
| | |
Description:
|
Allows for programmatic rule deletions from the document's stylesheet(s).
| |
Example:
| document.addStyleRule(".rule1 { background-color: #f00; }"); |
dimensions |
Type:
| prototype | |
Description:
|
Returns an object containing the document's width and height as properties.
(This may not work for all browsers; in such cases, an object of {-1, -1} is returned.)
| |
Example:
| var x = document.dimensions(); alert(x.width + " x " + x.height); |
element |
Type:
| prototype | |
Parameters:
| id
[string]
| |
Description:
|
A duplicate of the Element function (above). Added to the document object for object-oriented compliance.
| |
Example:
| var pageElement = document.element("JavaScriptSupersetSampleDiv"); if (pageElement == null) { alert("uh-oh"); } else { alert("groovy"); } |
getCookie |
Type:
| | |
Parameters:
| name
[string]
| |
Description:
|
Returns the value for a given cookie (via [name]); if the named cookie is not found, returns NULL.
| |
Example:
| alert(document.getCookie("someRandomCookieName")); // shouldn't exist yet document.setCookie("someRandomCookieName", "someRandomCookieValue"); alert(document.getCookie("someRandomCookieName")); document.deleteCookie("someRandomCookieName"); |
path |
Type:
| global | |
Description:
|
Returns various information about the current page's URL and query string. To see how this object's properties
change, add or change various query string values to the URL in the browser window (with a [z] key if desired).
Note this method expects query string values to be encoded via the inherent [encodeURI] method;
if not, unexpected results can occur. Also note if there are multiple keys with the same key name --
multiple [z]s in the example below -- an array of their values is returned.
| |
Example:
| var pathInfo = ""; pathInfo += "url: " + document.path.url + "\r\n"; pathInfo += "protocol: " + document.path.protocol + "\r\n"; pathInfo += "domain: " + document.path.domain + "\r\n"; pathInfo += "subDirectories: " + document.path.subDirectories + "\r\n"; pathInfo += "fileName: " + document.path.fileName + "\r\n"; pathInfo += "queryString: " + document.path.queryString + "\r\n"; pathInfo += "keyValue['z']: " + document.path.keyValue['z'] + "\r\n"; alert(pathInfo); |
setCookie |
Type:
| | |
Parameters:
| name
[string]
value
[string]
expiry
[string]
options
[object
({} (empty Object))
]
| |
Description:
|
Sets a named cookie's value to [value]; if the named cookie does not yet exist, it is created.
| |
Example:
| alert(document.getCookie("someRandomCookieName")); // shouldn't exist yet document.setCookie("someRandomCookieName", "someRandomCookieValue"); alert(document.getCookie("someRandomCookieName")); document.deleteCookie("someRandomCookieName"); |
styleRule |
Type:
| | |
Parameters:
| rule
[string]
deleteRule
[boolean
(FALSE)
]
| |
Description:
|
Returns (if [deleteRule] is FALSE, the default) or removes (if [deleteRule] is TRUE) the specified stylesheet rule.
If the rule does not exist, returns NULL.
Note you MUST include all characters of the rule's name, including prefixed periods, etc., for the correct rule to be found (assuming it exists).
| |
Example:
| alert(document.styleRule("PageHeader")); // doesn't exist; NULL alert(document.styleRule(".PageHeader")); // note prefixed period alert(document.styleRule("someUndefinedStyleRule")); |
styleSheet |
Type:
| prototype | |
Description:
|
Returns an object of the document's stylesheet. Note there must be at least one STYLE tag defined in
the document for an object to be returned. This may not work for all browsers; in such cases, NULL is returned.
| |
Example:
| alert(document.styleSheet()); |
| addMethod |
Type:
| prototype | |
Parameters:
| functionName
[string]
functionBody
[string]
| |
Description:
|
A shortcut for adding new functionality to JavaScript's core objects, this method can be used to create
incredibly complex DHTML experiences. Theoretically, this function could be used to allow JavaScript to create
its own runtime functions!
[See the source code (link above) for a complete, albeit very simple, HTML example for using this method.]
| |
Example:
| Number.addMethod("huh", function () { return this + "? huh?"; }); var x = 12; alert(x.huh()); |
| MersenneTwister |
Type:
| global | |
Parameters:
| seed
[integer
(current time-stamp)
]
| |
Description:
|
A global class for generating pseudorandom numbers, the Mersenne-Twister algorithm is extremely fast and effective.
Note however, it is NOT cryptographically secure so it should not be used for encrypting data.
See the following sections for the methods of this class.
[Note: See the source code (link above) for copyright notices and other information.]
| |
Example:
| var mt = new MersenneTwister(); alert(mt); |
random |
Type:
| class | |
Description:
|
Returns a 53-bit floating-point random number.
| |
Example:
| var mt = new MersenneTwister(); var x = mt.random(); alert(x); |
randomFloat |
Type:
| class | |
Description:
|
Returns a 32-bit floating-point random number.
| |
Example:
| var mt = new MersenneTwister(); var x = mt.randomFloat(); alert(x); |
randomInt |
Type:
| class | |
Description:
|
Returns a 32-bit integer random number.
| |
Example:
| var mt = new MersenneTwister(); var x = mt.randomInt(); alert(x); |
randomize |
Type:
| class | |
Parameters:
| seed
[integer
(current time-stamp)
]
| |
Description:
|
Seeds a MersenneTwister object with the desired [seed] value. Note that calling this method is not actually necessary,
as it is called automatically via the constructor using the current time-stamp as the seed value.
However, calling this method with a specific seed value will re-initialize the object's "base" values and produce
a repeatable set of pseudorandom numbers; this can aid in debugging or statistical analysis purposes.
| |
Example:
| var mt = new MersenneTwister(); mt.randomize(123456); alert(mt); |
| clamp |
Type:
| prototype | |
Parameters:
| minimum
[numeric]
maximum
[numeric]
| |
Description:
|
Returns a numeric value within the range of [minimum] and [maximum] (inclusive).
| |
Example:
| var x = 47; alert(x.clamp(12, 21)); alert(x.clamp(12, 94)); alert(x.clamp(89, 123)); |
cToF |
Type:
| global / prototype | |
Description:
|
Converts a given temperature from Celsius to Fahrenheit.
| |
Example:
| var boilingPoint = 100; alert(boilingPoint.cToF()); |
decimalToDegrees |
Type:
| global / prototype | |
Description:
|
Returns an object with degrees, minutes and seconds properties.
While the degrees and minutes should be integers, the seconds will (usually) be a floating-point number
(and often extremely close to a "normalized" value due to JavaScript's notorious rounding errors -- see the example for what I mean).
Note that if the value is greater than 360 (degrees), the returned value will be 360 or less.
| |
Example:
| var x = (527.6575).decimalToDegrees(); alert(x.degrees + "° " + x.minutes + "' " + x.seconds + "\""); |
degreesToDecimal |
Type:
| global | |
Parameters:
| degrees
[integer
(0)
]
minutes
[integer
(0)
]
seconds
[integer
(0)
]
| |
Description:
|
Converts a coordinate value from degrees-minutes-seconds into its floating-point equivalent.
| |
Example:
| alert(Number.degreesToDecimal(167, 39, 27)); |
degreesToRadians |
Type:
| global / prototype | |
Parameters:
| degrees
[integer
(0)
]
| |
Description:
|
Converts a [degrees] value to its radians equivalent.
| |
Example:
| var x = 45; alert(x.degreesToRadians()); |
digitalSum |
Type:
| prototype | |
Description:
|
Returns the digital sum of a given number (the sum of the individual digits of a numeric value).
The result will always be between 0 and 9 (inclusive).
| |
Example:
| var x = 1234567890; alert(x.digitalSum()); x = 123; alert(x.digitalSum()); x = 890; alert(x.digitalSum()); |
factorial |
Type:
| prototype | |
Description:
|
Returns the factorial of a given number.
| |
Example:
| var x = 12; alert(x.factorial()); |
factors |
Type:
| prototype | |
Parameters:
| onlyCheckPrime
[boolean
(false)
]
| |
Description:
|
Returns an array of factors for a given number (except 1 and itself).
If the length of the returned array is zero (empty), the given number is either prime, negative or not an integer.
If [onlyCheckPrime] is TRUE, the function returns immediately upon finding a factor (meaning the value is not prime).
| |
Example:
| var x = 12; alert(x + ": " + x.factors()); x = 37; // 37 is prime so no factors (empty array) returned alert(x + ": " + x.factors()); var x = 1.23; // not an integer, no factors alert(x + ": " + x.factors()); |
fibonacci |
Type:
| prototype | |
Description:
|
Returns the Fibonacci sequential (one-based) term for a given number.
The original (term) number must be 1 or greater; otherwise, fibonacci() always returns 0.
| |
Example:
| var x = 12; alert(x.fibonacci()); |
format |
Type:
| prototype | |
Parameters:
| separator
[string
(, [comma])
]
decimalCharacter
[string
(, [period])
]
| |
Description:
|
Returns a formatted string for a number using [separator] as the thousands separator and [decimalCharacter]
as the fraction separator. (Note: JavaScript has some notorious mathematical errors, which will become apparent
in the second example. To avoid this error, it is recommended this method only be called using integer values.)
| |
Example:
| var x = 1234567890; alert(x.format()); var x = 1234567890.5678; alert(x.format()); alert(x.format(".", ",")); |
fraction |
Type:
| prototype | |
Description:
|
Returns the fractional (mantissa) portion of a given number.
This function is in contrast to Number.integer() (below).
| |
Example:
| alert(Math.PI.fraction()); |
fToC |
Type:
| global / prototype | |
Description:
|
Converts a given temperature from Fahrenheit to Celsius.
| |
Example:
| var boilingPoint = 212; alert(boilingPoint.fToC()); |
integer |
Type:
| prototype | |
Description:
|
Returns the integer (characteristic) portion of a given number.
This is equivalent to the Math.floor() function.
| |
Example:
| alert(Math.PI.integer()); |
i2p |
Type:
| prototype | |
Description:
|
Returns a string of pixel units (px) from a given number.
This is mostly used when converting an integer value into its equivalent unit for stylesheet attributes.
| |
Example:
| var x = 12; alert(x.i2p()); |
intToUnit |
Type:
| prototype | |
Parameters:
| unit
[string]
| |
Description:
|
Returns a string of a given number with the [unit] appended.
| |
Example:
| var x = 12; alert(x.intToUnit("power")); |
isEven |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given numeric (integer) value is even.
| |
Example:
| var x = 12; alert(x.isEven()); x = 37; alert(x.isEven()); |
isInteger |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given numeric value is an integer.
| |
Example:
| var x = Math.PI; alert(x.isInteger()); x = 12; alert(x.isInteger()); |
isOdd |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given numeric (integer) value is odd.
| |
Example:
| var x = 12; alert(x.isOdd()); x = 37; alert(x.isOdd()); |
isPrime |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given numeric (integer) value is prime.
| |
Example:
| var x = 12; alert(x.isPrime()); x = 37; alert(x.isPrime()); |
p2i |
Type:
| prototype | |
Description:
|
Returns the integer equivalent of a pixel measurement. This is the reverse of Number.i2p() (above).
| |
Example:
| var x = "12px"; alert(x.p2i()); |
pad |
Type:
| prototype | |
Parameters:
| padCount
[integer
(2)
]
| |
Description:
|
Returns a string of the original number plus any leading zero(s) necessary to reach the desired length.
This is useful when formatting numeric values using a monospaced font.
Note [padCount] represents the TOTAL length of the (desired) returned string, not the number of zeros to add.
| |
Example:
| var x = 12; alert(x.pad(4)); |
padBack |
Type:
| prototype | |
Parameters:
| padCount
[integer
(2)
]
| |
Description:
|
Adds [padCount] number of zeroes to a given number.
| |
Example:
| alert((12).padBack(8)); |
radiansToDegrees |
Type:
| global / prototype | |
Parameters:
| radians
[integer
(0)
]
| |
Description:
|
Converts a [radians] value to its degrees equivalent.
| |
Example:
| var x = Math.PI / 4; alert(x.radiansToDegrees()); |
random |
Type:
| global / prototype | |
Parameters:
| min
[float
(1)
]
max
[float
(100)
]
| |
Description:
|
Returns a random integer value between [min] and [max] (inclusive).
| |
Example:
| alert(Number.random(18, 29)); |
sign |
Type:
| prototype | |
Description:
|
Returns -1, 0 or 1 (-1 for negative numbers; 1 for positive numbers; otherwise, 0).
| |
Example:
| var x = -3456; alert(x.sign()); x = 0; alert(x.sign()); x = 12; alert(x.sign()); |
toBase |
Type:
| prototype | |
Parameters:
| baseTo
[integer
(10)
]
baseFrom
[integer
(10)
]
| |
Description:
|
Returns a string representing the given number converted from [baseFrom] into [baseTo].
| |
Example:
| var x = 47; alert(x.toBase(5)); alert(x.toBase(5).toBase(10, 5)); |
toBinary |
Type:
| prototype | |
Parameters:
| baseFrom
[integer
(10)
]
| |
Description:
|
Returns a string representing the given number converted to its binary (base 2) equivalent.
| |
Example:
| var x = 47; alert(x.toBinary()); |
toDegreeString |
Type:
| prototype | |
Description:
|
Returns a given numeric value into a formatted string displaying degrees, minutes and seconds (as for geographic coordinates).
| |
Example:
| var x = 360 / 7; // one-seventh of a circle alert(x.toDegreeString()); |
toHex |
Type:
| prototype | |
Parameters:
| baseFrom
[integer
(10)
]
| |
Description:
|
Returns a string representing the given number converted to its hexadecimal (base 16) equivalent.
| |
Example:
| var x = 47; alert(x.toHex()); |
triangle |
Type:
| prototype | |
Description:
|
Returns an integer sum of all triangle numbers up to the given value.
| |
Example:
| var bowlingPinRows = 4; alert(bowlingPinRows.triangle()); |
unitToInt |
Type:
| prototype | |
Description:
|
Returns the integer equivalent of a given value using a unit measurement.
| |
Example:
| var x = "12power"; alert(x.unitToInt()); |
| isArray |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if the given object is an Array.
| |
Example:
| var x = ""; alert(x.isArray()); x = []; alert(x.isArray()); |
isBoolean |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if the given object is a Boolean.
| |
Example:
| var x = ""; alert(x.isBoolean()); x = true; alert(x.isBoolean()); |
isDate |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if the given object is a Date.
| |
Example:
| var x = ""; alert(x.isDate()); x = new Date(); alert(x.isDate()); |
isError |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if the given object is an Error.
| |
Example:
| var x = ""; alert(x.isError()); x = new Error(); alert(x.isError()); |
isFunction |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if the given object is a Function.
| |
Example:
| var x = ""; alert(x.isFunction()); x = function() {}; alert(x.isFunction()); |
isNumber |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if the given object is a Number.
| |
Example:
| var x = ""; alert(x.isNumber()); x = 12; alert(x.isNumber()); |
isObject |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if the given object is an Object.
| |
Example:
| var x = ""; alert(x.isObject()); x = {}; alert(x.isObject()); |
isRegExp |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if the given object is a regular expression (RegExp).
| |
Example:
| var x = ""; alert(x.isRegExp()); x = /[a-z0-9]*/gi; alert(x.isRegExp()); |
isString |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if the given object is a String.
| |
Example:
| var x = 12; alert(x.isString()); x = ""; alert(x.isString()); |
rgbToHex |
Type:
| prototype | |
Description:
|
Returns a 6-character string of a hex color from a given array.
I added this method to the Object prototype to simplify conversions.
(See the Array.rgbToHex function for more info.)
| |
Example:
| alert(({ red: 255, green: 192, blue: 128 }).rgbToHex()); |
toString |
Type:
| prototype | |
Description:
|
This function is only provided just in case the (inherent) toString() method isn't available.
(Such a scenario should only happen with older browsers but you never know....)
| |
Example:
| var x = (123).toString(); alert(x + " : " + x.isString()); |
type |
Type:
| prototype | |
Description:
|
Returns a string value indicating the object's internal type.
(The returned value will be one of the values from the Object.types array; see that function for more info.)
| |
Example:
| var x = 12; alert(x.type()); x = ""; alert(x.type()); |
types |
Type:
| global / prototype | |
Description:
|
Returns an array of JavaScript's inherent object types.
| |
Example:
| alert(Object.types()); |
| asciiDecode |
Type:
| prototype | |
Description:
|
Decodes all (ASCII) encoded values in a given string and returns the results.
| |
Example:
| alert("IQ01".asciiDecode()); |
asciiEncode |
Type:
| prototype | |
Description:
|
ASCII-encodes (in the form &#XX;) each character in a given string and returns the results.
| |
Example:
| alert("IQ01".asciiEncode()); |
camelToGlobal |
Type:
| prototype | |
Description:
|
Converts a word in camel-case (initialLetterLowerCase) to its equivalent in global-case (ALL_CAPS_WITH_UNDERSCORES).
| |
Example:
| alert("doesThisWork".camelToGlobal()); |
camelToPascal |
Type:
| prototype | |
Description:
|
Converts a word in camel-case (initialLetterLowerCase) to its equivalent in pascal-case (InitialLetterUpperCase).
| |
Example:
| alert("doesThisWork".camelToPascal()); |
capitalToDelimiter |
Type:
| prototype | |
Parameters:
| delimiter
[string
("-")
]
| |
Description:
|
Converts the capital letters of a word (variable name) to a delimited equivalent.
| |
Example:
| alert("doesThisWork".capitalToDelimiter()); alert("doesThisWork".capitalToDelimiter("_")); |
clean |
Type:
| prototype | |
Parameters:
| replacement
[string
(" " [space])
]
| |
Description:
|
Replaces multiple instances of white space (space, tab, etc.) with the value of [replacement].
| |
Example:
| var x = "does \t\t this \t work?"; alert(x + String.CRLF + x.clean()); |
CR |
Type:
| constant | |
Description:
|
The carriage-return character.
| |
Example:
| alert("first" + String.CR + "second"); |
CRLF |
Type:
| constant | |
Description:
|
A combination of the carriage-return and line-feed characters; this is usually the preferred constant to use
for formatting non-HTML text.
| |
Example:
| alert("first" + String.CRLF + "second"); |
degreesToDecimal |
Type:
| prototype | |
Description:
|
Returns a numeric (floating point) value from a degrees-minutes-seconds formatted string.
This is useful for converting geographic coordinates into their decimal equivalents.
Note that because JavaScript strings must be enclosed in either single or double quotes, you will have to escape either the minutes (') or seconds (") symbol, depending on your coding style (see the examples for more info).
Also note that because typing the degree symbol (°) may not be readily apparent, you may substitute an asterisk (*) for the degree symbol instead.
| |
Example:
| alert("167*39'27\"".degreesToDecimal()); // note asterisk as degrees alert('167°39\'27"'.degreesToDecimal()); // note escaped minutes symbol alert("167°39'27\"".degreesToDecimal()); // note escaped seconds symbol |
delimiterToCapital |
Type:
| prototype | |
Parameters:
| delimiter
[string
("-")
]
| |
Description:
|
Converts the delimitered letters of a string to capitals.
| |
Example:
| alert("does-this-work".delimiterToCapital()); |
dummyText |
Type:
| global / prototype | |
Parameters:
| minCount
[integer
(10)
]
maxCount
[integer
(100)
]
| |
Description:
|
Generates gibberish for use as placeholder text.
| |
Example:
| alert(String.dummyText(20, 50)); |
endsWith |
Type:
| prototype | |
Parameters:
| value
[string]
ignoreCase
[boolean
(TRUE)
]
| |
Description:
|
Returns a boolean value indicating if a given string ends with the supplied [value].
By default, this function ignores case.
| |
Example:
| alert("JavaScript".endsWith("SCRIPT")); alert("JavaScript".endsWith("SCRIPT", false)); |
extract |
Type:
| prototype | |
Parameters:
| regExp
[Regular Expression]
| |
Description:
|
Returns a string of characters matching the given regular expression.
If [regExp] is not supplied, the original string is returned.
| |
Example:
| var x = "my phone number is 888-555-1212"; alert(x.extract(/\d/)); alert(x.extract()); |
extractAlpha |
Type:
| prototype | |
Description:
|
Extracts alphabetical and whitespace characters from a given string.
| |
Example:
| var x = "my phone number is 888-555-1212"; alert(x.extractAlpha()); |
extractAlphaNumeric |
Type:
| prototype | |
Description:
|
Extracts alphabetical, numeric and whitespace characters from a given string.
| |
Example:
| var x = "my phone number is 888-555-1212"; alert(x.extractAlphaNumeric()); |
extractMath |
Type:
| prototype | |
Description:
|
Extracts mathematical characters from a given string.
For this purpose, mathematical characters are digits (0-9), plus and minus symbols (+-), decimals (.), and "E" for exponential notation.
Extracted characters should return valid numeric values if converted to a Number.
| |
Example:
| var x = "$123,456,789.1234"; alert(parseFloat(x.extractMath()) + .00005); |
extractNumeric |
Type:
| prototype | |
Description:
|
Extracts numeric characters from a given string.
| |
Example:
| var x = "my phone number is 888-555-1212"; alert(x.extractNumeric()); |
format |
Type:
| prototype | |
Parameters:
|
[comma-delimited elements (arguments)]
| |
Description:
|
Returns a string with the individual elements assigned to their respective placeholders (defined by {x}
where x is the zero-based number of the placeholder).
| |
Example:
| alert("yes, {1} really {0} {2}!".format("does", "this", "work")); alert("{2}, {1}, {0}, Go!".format(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); |
globalToCamel |
Type:
| prototype | |
Description:
|
Converts a word in global-case (ALL_CAPS_WITH_UNDERSCORES) to its equivalent in camel-case (initialLetterLowerCase).
| |
Example:
| alert("DOES_THIS_WORK".globalToCamel()); |
globalToPascal |
Type:
| prototype | |
Description:
|
Converts a word in global-case (ALL_CAPS_WITH_UNDERSCORES) to its equivalent in pascal-case (InitialLetterUpperCase).
| |
Example:
| alert("DOES_THIS_WORK".globalToPascal()); |
has |
Type:
| prototype | |
Parameters:
| text
[string]
| |
Description:
|
This is an alias for the inherent String.indexOf function.
| |
Example:
| var x = "does this work?"; alert(x.has("work")); alert(x.has("play")); |
hexToRgb |
Type:
| prototype | |
Description:
|
Returns an object consisting of (decimal) red, green and blue values from a given hex color.
| |
Example:
| var x = "#ff9900".hexToRgb(); alert(x.red + ", " + x.green + ", " + x.blue); |
indexCount |
Type:
| prototype | |
Parameters:
| what
[string]
| |
Description:
|
Returns the number of times the string [what] appears in a given string.
Like the regular [indexOf] method, this method is case-sensitive.
| |
Example:
| alert("123-456-7890".indexCount("-")); alert("123-456-7890".indexCount("x")); |
isAlpha |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given string is alphabetical.
| |
Example:
| alert("a".isAlpha()); alert("1".isAlpha()); |
isAlphaNumeric |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given string is alphabetical or numeric.
| |
Example:
| alert("a1".isAlphaNumeric()); alert("$".isAlphaNumeric()); |
isCreditCard |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given string represents a valid credit card number using the Luhn algorithm.
This function assumes a numeric string without spaces or punctuation -- use extractNumeric() to ensure a valid string.
| |
Example:
| alert("4111111111111111".isCreditCard()); alert("4012345678901234".isCreditCard()); alert("4111 1111 1111 1111".extractNumeric().isCreditCard()); |
isCurrency |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given string is composed of currency characters.
| |
Example:
| alert("$1".isCurrency()); alert("money".isCurrency()); |
isDateString |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given string represents a valid (JavaScript) date format.
| |
Example:
| alert("1/00/2001".isDateString()); // day must be in the range of 1-31 alert("date".isDateString()); alert("1/11/2001".isDateString()); |
isDigit |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given string is composed of numeric digits.
| |
Example:
| alert("49".isDigit()); alert("a".isDigit()); |
isEmail |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given string is a valid email address.
| |
Example:
| alert("javascript.superset@iq01.com".isEmail()); alert("my@email_address".isEmail()); |
isEmpty |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating whether a given string is empty (has no characters) or not.
| |
Example:
| alert("".isEmpty()); alert("IQ01.com".isEmpty()); |
isLowerCase |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given string is lowercase alphabetical.
| |
Example:
| alert("a".isLowerCase()); alert("A".isLowerCase()); |
isName |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a gven string is a valid "name" (such as for a person, business or document).
For my purposes, a valid name can only contain letters, numbers, periods, apostrophes and spaces.
(If your "name" allows other characters, update this function's regular expression accordingly.)
| |
Example:
| alert("Joe's Cafe".isName()); alert("#*@$!!".isName()); // cartoon profanity |
isNumeric |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given string is numeric.
| |
Example:
| alert("1".isNumeric()); alert("a".isNumeric()); |
isPhoneNumber |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given string is a valid (U.S.A.) phone number.
Valid phone numbers are in the form XXX-XXX-XXXX; the Area and Exchange Codes
(the first and second groups of 3-digit numbers, respectively)
cannot begin with 0 or 1, cannot end with 11, and cannot be 555 (which is why I had to change the sample exchange
code below from 555 to 777).
Note this function automatically considers only the "standard" 10-digit phone number;
it ignores any "prefix" codes (such as would be needed for dialing internationally), as well as the extension (if any).
| |
Example:
| alert("888-777-1212".isPhoneNumber()); alert("011-888-777-1212 ext 123".isPhoneNumber()); // international caller alert("888-555-1212".isPhoneNumber()); alert("911-911-9111".isPhoneNumber()); |
isProperName |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given string is a valid proper (person's) name.
For my purposes, a valid proper name can only contain letters, periods, apostrophes and spaces.
| |
Example:
| alert("Joey Joe Joe Jr. Shabadoo".isProperName()); alert("Agent 012".isProperName()); |
isSocialSecurity |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given string is a valid (U.S.) Social Security number.
This function ignores the infamous Woolworth's number (078-05-1120) as well as other considerations;
visit http://en.wikipedia.org/wiki/Social_security_number for more information about proper Social Security number formats and values.
| |
Example:
| alert("078-05-1120".isSocialSecurity()); // woolworth alert("666-66-6666".isSocialSecurity()); // number of the beast alert("001-01-0001".isSocialSecurity()); // "legal" but questionable |
isStrongPassword |
Type:
| prototype | |
Parameters:
| minLength
[integer
(6)
]
useSuperStrong
[boolean
(false)
]
| |
Description:
|
Returns a boolean value indicating if a given string is a strong password.
(Note [minLength] cannot be less than 6; if it is, it is set to 6.)
Strong passwords are defined as being at least 6 characters long and having at least 3 of the following 4 types
of characters: (1) uppercase alphabetical; (2) lowercase alphabetical; (3) numbers; (4) symbols.
If [useSuperStrong] is TRUE, all 4 types of characters must be in the string.
| |
Example:
| alert("myPassword".isStrongPassword()); alert("ag00dPa$$word?".isStrongPassword()); alert("g00dOne".isStrongPassword(8)); alert("g00dOne?".isStrongPassword(8)); alert("ag00dPassword".isStrongPassword(12, true)); alert("isThisAg00dPassword?".isStrongPassword(12, true)); |
isSymbol |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given string is composed of symbols.
| |
Example:
| alert("$".isSymbol()); alert("a".isSymbol()); alert("1".isSymbol()); |
isUpperCase |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given string is uppercase alphabetical.
| |
Example:
| alert("A".isUpperCase()); alert("a".isUpperCase()); |
isVariableName |
Type:
| prototype | |
Description:
|
Returns a boolean value indicating if a given string is an acceptable JavaScript variable name.
Valid JavaScript variable names must (1) begin with a letter, dollar sign ($), or underscore (_);
(2) may contain letters, numbers (as long as the name does not begin with a number), dollar signs and underscores;
and (3) must not be a JavaScript reserved word (see the function for more info).
| |
Example:
| alert("x".isVariableName()); alert("$".isVariableName()); alert("___".isVariableName()); alert("myVar_$29".isVariableName()); alert("1a".isVariableName()); // can't begin with a number alert("case".isVariableName()); // can't be a reserved word alert("CASE".isVariableName()); |
left |
Type:
| prototype | |
Parameters:
| count
[integer
(1)
]
| |
Description:
|
Returns the left [count] characters of a string.
| |
Example:
| alert("does this work?".left(4)); |
LF |
Type:
| constant | |
Description:
|
The line-feed character.
| |
Example:
| alert("first" + String.LF + "second"); |
literalizeHtml |
Type:
| prototype | |
Description:
|
Converts HTML tags to their literal equivalents (i.e., "<" becomes "<", ">" becomes ">"
and "&" becomes "&").
| |
Example:
| alert("does this <b>really</b> work?".literalizeHtml()); |
mid |
Type:
| prototype | |
Parameters:
| position
[integer
(0)
]
count
[integer
(1)
]
| |
Description:
|
Returns [count] characters of a string beginning at [position]. This is a duplicate of the inherent [substr] method.
| |
Example:
| alert("does this work?".mid(5, 4)); |
p2i |
Type:
| prototype | |
Description:
|
This is an alias for the String.unitToInt function; see that function for more info.
| |
Example:
| // see String.unitToInt function for examples |
pad |
Type:
| prototype | |
Parameters:
| padCount
[integer
(2)
]
padCharacter
[string
(" " [space])
]
toFront
[boolean
(true)
]
| |
Description:
|
Adds the necessary number of [padCharacter] characters to a string to make the total length [padCount] long.
If [toFront] is TRUE (the default), the characters are appended to the front of the string; otherwise, they're
appended to the end. This is often used for formatting monospaced text (such as programming examples).
Note [padCount] represents the TOTAL length of the (desired) returned string, not the number of characters to add.
| |
Example:
| alert("short".pad(8, ".")); alert("short".pad(12, "-", false)); |
padBack |
Type:
| prototype | |
Parameters:
| padCount
[integer
(2)
]
padCharacter
[string
(" " [space])
]
| |
Description:
|
Performs the exact function as String.pad() (above) but always adds extra characters to the end of the string.
| |
Example:
| alert("short".padBack(8, ".")); |
pascalToCamel |
Type:
| prototype | |
Description:
|
Converts a word in pascal-case (InitialLetterUpperCase) to its equivalent in camel-case (initialLetterLowerCase).
| |
Example:
| alert("DoesThisWork".pascalToCamel()); |
pascalToGlobal |
Type:
| prototype | |
Description:
|
Converts a word in pascal-case (InitialLetterUpperCase) to its equivalent in global-case (ALL_CAPS_WITH_UNDERSCORES).
| |
Example:
| alert("DoesThisWork".pascalToGlobal()); |
phoneNumberParts |
Type:
| prototype | |
Parameters:
| extensionDelimiter
[string
("x")
]
| |
Description:
|
Returns an object consisting of a phone number (the "number" property) and its extension (the "extension" property).
(If there is no extension, the "extension" property is empty/undefined.)
Note the returned object's elements contain only numeric characters.
| |
Example:
| var x = "888-555-1212".phoneNumberParts(); alert(x.number + " x " + x.extension); x = "888-555-1212 ext. 9876".phoneNumberParts(); alert(x.number + " x " + x.extension); x = "8885551212x9876".phoneNumberParts(); alert(x.number + " x " + x.extension); x = "888-555-1212 / 9876".phoneNumberParts("/"); alert(x.number + " x " + x.extension); |
random |
Type:
| global / prototype | |
Parameters:
| minLength
[integer
(1)
]
maxLength
[integer
(8)
]
| |
Description:
|
Returns a randomly generated string of characters from [minLength] to [maxLength] long.
Note there is no spell checking so returned strings may be complete gibberish.
| |
Example:
| alert(String.random()); |
repeat |
Type:
| global / prototype | |
Parameters:
| character
[string
(" " [space])
]
count
[integer
(1)
]
| |
Description:
|
Returns a string [count] long, consisting only of [character].
| |
Example:
| alert(String.repeat("x", 16)); |
reverse |
Type:
| prototype | |
Description:
|
Returns the characters of a string in reverse order (e.g., "abcde" becomes "edcba").
| |
Example:
| alert("does this work?".reverse()); |
right |
Type:
| prototype | |
Parameters:
| count
[integer
(1)
]
| |
Description:
|
Returns the right [count] characters of a string.
| |
Example:
| alert("does this work?".right(5)); |
shuffle |
Type:
| prototype | |
Description:
|
Returns an "anagram" of a given string (using all characters, not just letters).
| |
Example:
| alert("does this work?".shuffle()); |
startsWith |
Type:
| prototype | |
Parameters:
| value
[string]
ignoreCase
[boolean
(TRUE)
]
| |
Description:
|
Returns a boolean value indicating if a given string starts with the supplied [value].
By default, this function ignores case.
| |
Example:
| alert("JavaScript".startsWith("JAVA")); alert("JavaScript".startsWith("JAVA", false)); |
strip |
Type:
| prototype | |
Parameters:
| string
[string]
| |
Description:
|
Removes a string of characters ([string]) from a given string.
If [string] is not found, the original string is returned.
| |
Example:
| alert("does this work?".strip("es")); |
stripHtml |
Type:
| prototype | |
Description:
|
Removes embeded HTML tags from a string.
| |
Example:
| alert("does this <b>really</b> work?".stripHtml()); |
stripPunctuation |
Type:
| prototype | |
Description:
|
Removes all punctuation from a given string.
| |
Example:
| var x = "does \"this\" work?"; alert(x + String.CRLF + x.stripPunctuation()); |
TAB |
Type:
| constant | |
Description:
|
The tab character.
| |
Example:
| alert("first" + String.TAB + "second"); |
toBase |
Type:
| prototype | |
Parameters:
| baseTo
[integer
(10)
]
baseFrom
[integer
(10)
]
| |
Description:
|
This is a String equivalent of Number.toBase() (above); see that function for more information.
A String version is necessary as some number bases (such as hexadecimal) contain non-numeric characters.
| |
Example:
| alert("c0de".toBase(10, 16)); |
toRandomCase |
Type:
| prototype | |
Description:
|
Randomly sets the individual characters of a string to uppercase or lowercase.
As this is a random function, repeatedly calling the function on the same string will return different results.
| |
Example:
| alert("does this work?".toRandomCase()); |
toTitleCase |
Type:
| prototype | |
Description:
|
Capitalizes the first character of each word in a string.
| |
Example:
| alert("does this work?".toTitleCase()); |
trim |
Type:
| prototype | |
Description:
|
Removes leading and trailing whitespace from a string.
| |
Example:
| var x = " |does this work?| "; var y = x.trim(); alert(x + " (" + x.length + ")" + String.CRLF + y + " (" + y.length + ")"); |
trimLeft |
Type:
| prototype | |
Description:
|
Removes leading whitespace from a string.
| |
Example:
| var x = " |does this work?| "; var y = x.trimLeft(); alert(x + " (" + x.length + ")" + String.CRLF + y + " (" + y.length + ")"); |
trimRight |
Type:
| prototype | |
Description:
|
Removes trailing whitespace from a string.
| |
Example:
| var x = " |does this work?| "; var y = x.trimRight(); alert(x + " (" + x.length + ")" + String.CRLF + y + " (" + y.length + ")"); |
unitToInt |
Type:
| prototype | |
Description:
|
This is an alias for the inherent parseInt function.
| |
Example:
| var x = "12"; alert(x.unitToInt()); |
|
|