Namespace util.helpers
Various utility functions that don't have anywhere else to go.
Defined in: <js/util/helpers.js>.
Constructor Attributes | Constructor Name and Description |
---|---|
Method Attributes | Method Name and Description |
---|---|
<static> |
util.helpers.bbgmPing(type)
Ping a counter at basketball-gm.com.
|
<static> |
util.helpers.bound(x, min, max)
Bound a number so that it can't exceed min and max values.
|
<static> |
util.helpers.deepCopy(obj)
Clones an object.
|
<static> |
util.helpers.error(error, req)
Display a whole-page error message to the user by calling either views.leagueError or views.globalError as appropriate.
|
<static> |
util.helpers.formatCurrency(amount, append, precision)
Format a number as currency, correctly handling negative values.
|
<static> |
util.helpers.getAbbrev(tid)
Get the team abbreviation for a team ID.
|
<static> |
util.helpers.getSeasons(selectedSeason, ignoredSeason)
Get a list of all seasons that have been played so far, including the current one.
|
<static> |
util.helpers.getTeams(selectedTid)
Get list of teams, along with some metadata
Returns an array of 30 teams.
|
<static> |
util.helpers.globalError(req)
Display a whole-page error message to the user.
|
<static> |
util.helpers.leagueError(req)
Display a whole-page error message to the user, while retaining the league menu.
|
<static> |
util.helpers.nullPad(array, length)
Pad an array with nulls or truncate it so that it has a fixed length.
|
<static> |
util.helpers.numberWithCommas(x)
Format a number with commas in the thousands places.
|
<static> |
util.helpers.playerNameLabels(pid, name, object, skills)
Generate a block of HTML with a player's name, skill labels.
|
<static> |
util.helpers.resetG()
Delete all the things from the global variable g that are not stored in league databases.
|
<static> |
util.helpers.round(value, precision)
Round a number to a certain number of decimal places.
|
<static> |
util.helpers.skillsBlock(skills)
Generate a block of HTML with a player's skill labels.
|
<static> |
util.helpers.validateAbbrev(abbrev)
Validate that a given abbreviation corresponds to a team.
|
<static> |
util.helpers.validateSeason(season)
Validate the given season.
|
<static> |
util.helpers.validateTid(tid)
Validate that a given team ID corresponds to a team.
|
Method Detail
<static>
util.helpers.bbgmPing(type)
Ping a counter at basketball-gm.com.
This should only do something if it isn't being run from a unit test and it's actually on basketball-gm.com.
- Parameters:
- {string} type
- Either "league" for a new league, or "season" for a completed season
<static>
util.helpers.bound(x, min, max)
Bound a number so that it can't exceed min and max values.
- Parameters:
- {number} x
- Input number.
- {min} min
- Minimum bounding variable.
- {max} max
- Maximum bounding variable.
<static>
util.helpers.deepCopy(obj)
Clones an object.
Taken from http://stackoverflow.com/a/3284324/786644
- Parameters:
- {Object} obj
- Object to be cloned.
<static>
util.helpers.error(error, req)
Display a whole-page error message to the user by calling either views.leagueError or views.globalError as appropriate.
- Parameters:
- {string} error
- Text of the error message to be displayed.
- {Object} req
- Optional Davis.js request object, containing the callback function and any other metadata
<static>
{string}
util.helpers.formatCurrency(amount, append, precision)
Format a number as currency, correctly handling negative values.
- Parameters:
- {number|string} amount
- Input value.
- {string=} append
- Suffix to append to the number, like "M" for things like $2M.
- {number|string|undefined} precision
- Number of decimal places. Default is 2 (like $17.62).
- Returns:
- {string} Formatted currency string.
<static>
{string}
util.helpers.getAbbrev(tid)
Get the team abbreviation for a team ID.
For instance, team ID 0 is Atlanta, which has an abbreviation of ATL. This is a convenience wrapper around validateTid, excpet it will return "FA" if you pass g.PLAYER.FREE_AGENT.
- Parameters:
- {number|string} tid
- Integer team ID.
- Returns:
- {string} Abbreviation
<static>
{Array.
util.helpers.getSeasons(selectedSeason, ignoredSeason)
Get a list of all seasons that have been played so far, including the current one.
- Parameters:
- {number=} selectedSeason
- If defined, then a season matching this year will have its "selected" property set to true.
- {number=} ignoredSeason
- If defined, then a season matching this year will not be present in the output. This is useful if you need a list of seasons that doesn't include the current season, for instance.
- Returns:
- {Array. List of seasons. Each element in the list is an object with with two properties: "season" which contains the year, and "selectedSeason" which is a boolean for whether the year matched selectedSeason.
<static>
{Array.Object}
util.helpers.getTeams(selectedTid)
Get list of teams, along with some metadata
Returns an array of 30 teams. Each array is an object with the following properties:
tid: Integer team ID (0 to 29).
cid: Integer conference ID (0=East, 1=West).
did: Integer division ID.
region: String region name.
name: String team name.
abbrev: String 3-letter team abbreviation.
pop: From http://www.forbes.com/nba-valuations/ number of people in the region, in millions of people.
popRank: Rank of population, 1=largest, 30=smallest.
selected: If selectedTid is defined, this is a boolean representing whether this team is "selected" or not (see below).
- Parameters:
- {number|string} selectedTid
- A team ID or abbrev for a team that should be "selected" (as in, from a drop down menu). This will add the "selected" key to each team object, as described above.
- Returns:
- {Array.Object} All teams.
<static>
util.helpers.globalError(req)
Display a whole-page error message to the user.
- Parameters:
- {Object} req
- Object with parameter "params" containing another object with a string representing the error message in the parameter "error".
<static>
util.helpers.leagueError(req)
Display a whole-page error message to the user, while retaining the league menu.
- Parameters:
- {Object} req
- Object with parameter "params" containing another object with a string representing the error message in the parameter "error" and an integer league ID in "lid".
<static>
{Array}
util.helpers.nullPad(array, length)
Pad an array with nulls or truncate it so that it has a fixed length.
- Parameters:
- {Array} array
- Input array.
- {number} length
- Desired length.
- Returns:
- {Array} Original array padded with null or truncated so that it has the required length.
<static>
{string}
util.helpers.numberWithCommas(x)
Format a number with commas in the thousands places.
Also, rounds the number first.
- Parameters:
- {number|string} x
- Input number.
- Returns:
- {string} Formatted number.
<static>
{string}
util.helpers.playerNameLabels(pid, name, object, skills)
Generate a block of HTML with a player's name, skill labels.
- Parameters:
- {number} pid
- Player ID number.
- {string} name
- Player name.
- {object} object
- Injury object (properties: type and gamesRemaining).
-
{Array.
} skills - Array of skill labels, like "R" for "Rebounder", etc. See: core.player.skills.
- Returns:
- {string} String of HTML-formatted skill labels, ready for output.
<static>
util.helpers.resetG()
Delete all the things from the global variable g that are not stored in league databases.
This is used to clear out values from other leagues, to ensure that the appropriate values are updated in the database when calling db.setGameAttributes.
<static>
{string}
util.helpers.round(value, precision)
Round a number to a certain number of decimal places.
- Parameters:
- {number|string} value
- Number to round.
- {number=} precision
- Number of decimal places. Default is 0 (round to integer).
- Returns:
- {string} Rounded number.
<static>
{string}
util.helpers.skillsBlock(skills)
Generate a block of HTML with a player's skill labels.
- Parameters:
-
{Array.
} skills - Array of skill labels, like "R" for "Rebounder", etc. See: core.player.skills.
- Returns:
- {string} String of HTML-formatted skill labels, ready for output.
<static>
{Array}
util.helpers.validateAbbrev(abbrev)
Validate that a given abbreviation corresponds to a team.
If the abbreviation is not valid, then g.userTid and its correspodning abbreviation will be returned.
- Parameters:
- {string} abbrev
- Three-letter team abbreviation, like "ATL".
- Returns:
- {Array} Array with two elements, the team ID and the validated abbreviation.
<static>
{number}
util.helpers.validateSeason(season)
Validate the given season.
Currently this doesn't really do anything except replace "undefined" with g.season.
- Parameters:
- {number|string|undefined} season
- The year of the season to validate. If undefined, then g.season is used.
- Returns:
- {number} Validated season (same as input unless input is undefined, currently).
<static>
{Array}
util.helpers.validateTid(tid)
Validate that a given team ID corresponds to a team.
If the team ID is not valid, then g.userTid and its correspodning abbreviation will be returned.
- Parameters:
- {number|string} tid
- Integer team ID.
- Returns:
- {Array} Array with two elements, the validated team ID and the corresponding abbreviation.