ALKMaps Base Types

ALKMaps custom string, number and function functions are described here.

Summary
ALKMaps Base TypesALKMaps custom string, number and function functions are described here.
ALKMaps.StringContains convenience functions for string manipulation.
Functions and Properties
startsWithTest whether a string starts with another string.
containsTest whether a string contains another string.
trimRemoves leading and trailing whitespace characters from a string.
camelizeCamel-case a hyphenated string.
formatGiven a string with tokens in the form ${token}, return a string with tokens replaced with properties from the given context object.
tokenRegExUsed to find tokens in a string.
numberRegExUsed to test strings as numbers.
isNumericDetermine whether a string contains only a numeric value.
numericIfConverts a string that appears to be a numeric value into a number.
ALKMaps.NumberContains convenience functions for manipulating numbers.
Properties
decimalSeparatorDecimal separator to use when formatting numbers.
thousandsSeparatorThousands separator to use when formatting numbers.
Functions
limitSigDigsLimit the number of significant digits on a float.
formatFormats a number for output.
ALKMaps.FunctionContains convenience functions for function manipulation.
Functions
bindBind a function to an object.
bindAsEventListenerBind a function to an object, and configure it to receive the event object as first parameter when called.
FalseA simple function to that just does “return false”.
TrueA simple function to that just does “return true”.
VoidA reusable function that returns ``undefined``.
ALKMaps.ArrayContains convenience functions for array manipulation.
Functions
filterFilter an array.

ALKMaps.String

Contains convenience functions for string manipulation.

Summary
Functions and Properties
startsWithTest whether a string starts with another string.
containsTest whether a string contains another string.
trimRemoves leading and trailing whitespace characters from a string.
camelizeCamel-case a hyphenated string.
formatGiven a string with tokens in the form ${token}, return a string with tokens replaced with properties from the given context object.
tokenRegExUsed to find tokens in a string.
numberRegExUsed to test strings as numbers.
isNumericDetermine whether a string contains only a numeric value.
numericIfConverts a string that appears to be a numeric value into a number.

Functions and Properties

startsWith

startsWith: function(str,
sub)

Test whether a string starts with another string.

Parameters

str{String} The string to test.
sub{String} The substring to look for.

Returns

{Boolean} The first string starts with the second.

contains

contains: function(str,
sub)

Test whether a string contains another string.

Parameters

str{String} The string to test.
sub{String} The substring to look for.

Returns

{Boolean} The first string contains the second.

trim

trim: function(str)

Removes leading and trailing whitespace characters from a string.

Parameters

str{String} The (potentially) space padded string.  This string is not modified.

Returns

{String} A trimmed version of the string with all leading and trailing spaces removed.

camelize

camelize: function(str)

Camel-case a hyphenated string.  Ex.  “chicken-head” becomes “chickenHead”, and “-chicken-head” becomes “ChickenHead”.

Parameters

str{String} The string to be camelized.  The original is not modified.

Returns

{String} The string, camelized

format

format: function(template,
context,
args)

Given a string with tokens in the form ${token}, return a string with tokens replaced with properties from the given context object.  Represent a literal “${“ by doubling it, e.g.  “${${“.

Parameters

template{String} A string with tokens to be replaced.  A template has the form “literal ${token}” where the token will be replaced by the value of context[“token”].
context{Object} An optional object with properties corresponding to the tokens in the format string.  If no context is sent, the window object will be used.
args{Array} Optional arguments to pass to any functions found in the context.  If a context property is a function, the token will be replaced by the return from the function called with these arguments.

Returns

{String} A string with tokens replaced from the context object.

tokenRegEx

Used to find tokens in a string.  Examples: ${a}, ${a.b.c}, ${a-b}, ${5}

numberRegEx

Used to test strings as numbers.

isNumeric

isNumeric: function(value)

Determine whether a string contains only a numeric value.

Examples

ALKMaps.String.isNumeric("6.02e23") // true
ALKMaps.String.isNumeric("12 dozen") // false
ALKMaps.String.isNumeric("4") // true
ALKMaps.String.isNumeric(" 4 ") // false

Returns

{Boolean} String contains only a number.

numericIf

numericIf: function(value)

Converts a string that appears to be a numeric value into a number.

Parameters

value{String}

Returns

{Number|String} a Number if the passed value is a number, a String otherwise.

ALKMaps.Number

Contains convenience functions for manipulating numbers.

Summary
Properties
decimalSeparatorDecimal separator to use when formatting numbers.
thousandsSeparatorThousands separator to use when formatting numbers.
Functions
limitSigDigsLimit the number of significant digits on a float.
formatFormats a number for output.

Properties

decimalSeparator

Decimal separator to use when formatting numbers.

thousandsSeparator

Thousands separator to use when formatting numbers.

Functions

limitSigDigs

limitSigDigs: function(num,
sig)

Limit the number of significant digits on a float.

Parameters

num{Float}
sig{Integer}

Returns

{Float} The number, rounded to the specified number of significant digits.

format

format: function(num,
dec,
tsep,
dsep)

Formats a number for output.

Parameters

num{Float}
dec{Integer} Number of decimal places to round to.  Defaults to 0.  Set to null to leave decimal places unchanged.
tsep{String} Thousands separator.  Default is “,”.
dsep{String} Decimal separator.  Default is “.”.

Returns

{String} A string representing the formatted number.

ALKMaps.Function

Contains convenience functions for function manipulation.

Summary
Functions
bindBind a function to an object.
bindAsEventListenerBind a function to an object, and configure it to receive the event object as first parameter when called.
FalseA simple function to that just does “return false”.
TrueA simple function to that just does “return true”.
VoidA reusable function that returns ``undefined``.

Functions

bind

bind: function(func,
object)

Bind a function to an object.  Method to easily create closures with ‘this’ altered.

Parameters

func{Function} Input function.
object{Object} The object to bind to the input function (as this).

Returns

{Function} A closure with ‘this’ set to the passed in object.

bindAsEventListener

bindAsEventListener: function(func,
object)

Bind a function to an object, and configure it to receive the event object as first parameter when called.

Parameters

func{Function} Input function to serve as an event listener.
object{Object} A reference to this.

Returns

{Function}

False

False : function()

A simple function to that just does “return false”.  We use this to avoid attaching anonymous functions to DOM event handlers, which causes “issues” on IE<8.

Usage

document.onclick = ALKMaps.Function.False;

Returns

{Boolean}

True

True : function()

A simple function to that just does “return true”.  We use this to avoid attaching anonymous functions to DOM event handlers, which causes “issues” on IE<8.

Usage

document.onclick = ALKMaps.Function.True;

Returns

{Boolean}

Void

Void: function()

A reusable function that returns ``undefined``.

Returns

{undefined}

ALKMaps.Array

Contains convenience functions for array manipulation.

Summary
Functions
filterFilter an array.

Functions

filter

filter: function(array,
callback,
caller)

Filter an array.  Provides the functionality of the Array.prototype.filter extension to the ECMA-262 standard.  Where available, Array.prototype.filter will be used.

Based on well known example from https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/filter

Parameters

array{Array} The array to be filtered.  This array is not mutated.  Elements added to this array by the callback will not be visited.
callback{Function} A function that is called for each element in the array.  If this function returns true, the element will be included in the return.  The function will be called with three arguments: the element in the array, the index of that element, and the array itself.  If the optional caller parameter is specified the callback will be called with this set to caller.
caller{Object} Optional object to be set as this when the callback is called.

Returns

{Array} An array of elements from the passed in array for which the callback returns true.

startsWith: function(str,
sub)
Test whether a string starts with another string.
contains: function(str,
sub)
Test whether a string contains another string.
trim: function(str)
Removes leading and trailing whitespace characters from a string.
camelize: function(str)
Camel-case a hyphenated string.
format: function(template,
context,
args)
Given a string with tokens in the form ${token}, return a string with tokens replaced with properties from the given context object.
isNumeric: function(value)
Determine whether a string contains only a numeric value.
numericIf: function(value)
Converts a string that appears to be a numeric value into a number.
limitSigDigs: function(num,
sig)
Limit the number of significant digits on a float.
format: function(num,
dec,
tsep,
dsep)
Formats a number for output.
bind: function(func,
object)
Bind a function to an object.
bindAsEventListener: function(func,
object)
Bind a function to an object, and configure it to receive the event object as first parameter when called.
False : function()
A simple function to that just does “return false”.
True : function()
A simple function to that just does “return true”.
Void: function()
A reusable function that returns ``undefined``.
filter: function(array,
callback,
caller)
Filter an array.
Close