API Docs for:
Show:

WeightedList Class

Constructor

Methods

_randomIndex

() Number protected

Returns an index by weighted random distribution.

Returns:

_update

() protected chainable

Updates chached data for achieving weighted random distribution.

add

(
  • value
  • [weight]
)
Number

Add a value to the weighted list.

Parameters:

  • value Any
  • [weight] Number optional

    Optional. Defaults to 1.

Returns:

Number: The index of the item that was added.

dedupe

(
  • [mode]
)
WeightedList

Dedupes a weighted list of string values, returning a new weighted list that is guaranteed to contain only one copy of a given string value. This method differs from the unique method in that it's optimized for use only with string values, whereas unique may be used with other types (but is slower). Using dedupe with non-string values may result in unexpected behavior.

Parameters:

  • [mode] String optional

    Optional. If the original weighted list contains duplicate values with different weights, the mode specifies how those weights get transferred to the new weighted list. mode may be one of the following values:

    'first'
    Use the first weight that is found. Ignore all others.
    'sum'
    Use the sum of all weights that are found. This is the default mode.

Returns:

each

(
  • fn
  • [context]
)
chainable

Executes the supplied function for each value in the weighted list.

Parameters:

  • fn Function

    The function to execute for each value in the weighted list. The first argument passed to this function will be the value. The second argument passed to this function will be the value's index. The third argument passed to this function will be the value's weight.

  • [context] Any optional

    Optional. The context the function is called with.

every

(
  • fn
  • [context]
)
Boolean

Executes the supplied function for each value in the weighted list. Iteration stops if the supplied function does not return a truthy value.

Parameters:

  • fn Function

    The function to execute for each value in the weighted list. The first argument passed to this function will be the value. The second argument passed to this function will be the value's index. The third argument passed to this function will be the value's weight.

  • [context] Any optional

    Optional. The context the function is called with.

Returns:

Boolean: true if every value in the weighted list returns true from the supplied function, false otherwise.

filter

(
  • fn
  • [context]
)
WeightedList

Executes the supplied function for each value in the weighted list. Returns a new weighted list containing the values for which the supplied function returned a truthy value. The values in the new weighted list will retain the same weights they had in the original weighted list.

Parameters:

  • fn Function

    The function to execute for each value in the weighted list. The first argument passed to this function will be the value. The second argument passed to this function will be the value's index. The third argument passed to this function will be the value's weight.

  • [context] Any optional

    Optional. The context the function is called with.

Returns:

find

(
  • fn
  • [context]
)
Any

Executes the supplied function for each value in the weighted list, searching for the first value that matches the supplied function.

Parameters:

  • fn Function

    The function to execute for each value in the weighted list. The first argument passed to this function will be the value. The second argument passed to this function will be the value's index. The third argument passed to this function will be the value's weight. Iteration is stopped as soon as this function returns true.

  • [context] Any optional

    Optional. The context the function is called with.

Returns:

Any: The found value is returned or null is returned if no value was found.

grep

(
  • pattern
)
WeightedList

Iterates over a weighted list, returning a new weighted list with all the values that match the supplied regular expression. The values in the new weighted list will retain the same weights they had in the original weighted list.

Parameters:

  • pattern RegExp

    Regular expression to test against each item.

Returns:

indexOf

(
  • value
  • [from]
)
Number

Returns the index of the first value in the weighted list that is equal (using a strict equality check) to the specified value, or -1 if the value isn't found.

Parameters:

  • value Any
  • [from] Number optional

    Optional. The index at which to begin the search. Defaults to 0.

Returns:

invoke

(
  • methodName
)
WeightedList

Executes a named method on each value in a weighted list of objects. Values in the weighted list that do not have a function by that name will be skipped.

Parameters:

Returns:

WeightedList: A new weighted list is returned containing the return values from each method. The values in the new weighted list will retain the same weights they had in the original weighted list.

isEmpty

() Boolean

Returns true if the weighted list is empty.

Returns:

item

(
  • [index]
)
Object

Gets an item by index from the weighted list if an index is supplied. If an index is not supplied, an item is selected by weighted random distribution.

Parameters:

  • [index] Number optional

    Optional.

Returns:

Object: The item is returned or null is returned if the given index does not exist. A returned item will be an object with the following properties:
index
This item's index.
value
This item's value.
weight
This item's weight.

itemsAreEqual

(
  • a
  • b
)
Boolean

Default comparator for values stored in this weighted list. Used by the indexOf, lastIndexOf, and remove methods.

Parameters:

  • a Any
  • b Any

Returns:

lastIndexOf

(
  • value
  • [from]
)
Number

Returns the index of the last value in the weighted list that is equal (using a strict equality check) to the specified value, or -1 if the value isn't found.

Parameters:

  • value Any
  • [from] Number optional

    Optional. The index at which to begin the search. Defaults to the last index in the weighted list.

Returns:

map

(
  • fn
  • [context]
)
WeightedList

Executes the supplied function for each value in the weighted list and returns a new weighted list containing all the values returned by the supplied function. The values in the new weighted list will retain the same weights they had in the original weighted list.

Parameters:

  • fn Function

    The function to execute for each value in the weighted list. The first argument passed to this function will be the value. The second argument passed to this function will be the value's index. The third argument passed to this function will be the value's weight.

  • [context] Any optional

    Optional. The context the function is called with.

Returns:

partition

(
  • fn
  • [context]
)
Object

Partitions a weighted list into two new weighted lists, one with the values for which the supplied function returns true, and one with the values for which the function returns false. The values in the new weighted lists will retain the same weights they had in the original weighted list.

Parameters:

  • fn Function

    The function to execute for each value in the weighted list. The first argument passed to this function will be the value. The second argument passed to this function will be the value's index. The third argument passed to this function will be the value's weight.

  • [context] Any optional

    Optional. The context the function is called with.

Returns:

Object: An object with two properties: matches and rejects. Each is a weighted list containing the items that were selected or rejected by the test function.

reduce

(
  • initialValue
  • fn
  • [context]
)
Any

Executes the supplied function for each value in the weighted list, "folding" the weighted list into a single value.

Parameters:

  • initialValue Any
  • fn Function

    The function to execute for each value in the weighted list. The first argument passed to this function will be the value returned from the previous iteration or the initial value if this is the first iteration. The second argument passed to this function will be the current value in the weighted list. The third argument passed to this function will be the current value's index. The fourth argument passed to this function will be the current value's weight.

  • [context] Any optional

    Optional. The context the function is called with.

Returns:

Any: Final result from iteratively applying the given function to each value in the weighted list.

reject

(
  • fn
  • [context]
)
WeightedList

The inverse of the filter method. Executes the supplied function for each value in the weighted list. Returns a new weighted list containing the values for which the supplied function returned false. The values in the new weighted list will retain the same weights they had in the original weighted list.

Parameters:

  • fn Function

    The function to execute for each value in the weighted list. The first argument passed to this function will be the value. The second argument passed to this function will be the value's index. The third argument passed to this function will be the value's weight.

  • [context] Any optional

    Optional. The context the function is called with.

Returns:

remove

(
  • value
  • [all]
)
Number

Removes the first or all occurrences of a value from the weighted list. This may cause remaining values to be reindexed.

Parameters:

  • value Any
  • [all] Boolean optional

    Optional. If true, removes all occurances.

Returns:

Number: The number of items that were removed.

removeIndex

(
  • index
)
chainable

Removes a value from the weighted list by index. This may cause remaining values to be reindexed.

Parameters:

size

() Number

Returns the number of values in the weighted list.

Returns:

some

(
  • fn
  • [context]
)
Boolean

Executes the supplied function for each value in the weighted list. Returning a truthy value from the function will stop the processing of remaining values.

Parameters:

  • fn Function

    The function to execute for each value in the weighted list. The first argument passed to this function will be the value. The second argument passed to this function will be the value's index. The third argument passed to this function will be the value's weight.

  • [context] Any optional

    Optional. The context the function is called with.

Returns:

Boolean: true if the function returns a truthy value on any of the values in the weighted list; false otherwise.

toArray

() Array

Provides an array of values.

Returns:

toJSON

() Array

Provides an array of values for JSON.stringify.

Returns:

unique

(
  • [mode]
)
WeightedList

Returns a copy of the weighted list with duplicate value removed.

Parameters:

  • [mode] String optional

    Optional. If the original weighted list contains duplicate values with different weights, the mode specifies how those weights get transferred to the new weighted list. mode may be one of the following values:

    'first'
    Use the first weight that is found. Ignore all others.
    'sum'
    Use the sum of all weights that are found. This is the default mode.

Returns:

update

(
  • index
  • value
  • weight
)
chainable

Change the value and weight of a value that is already in the weighted list.

Parameters:

updateValue

(
  • index
  • value
)
chainable

Change the value of a value that is already in the weighted list.

Parameters:

updateWeight

(
  • index
  • weight
)
chainable

Change the weight of a value that is already in the weighted list.

Parameters:

value

(
  • [index]
)
Any

Gets a value by index from the weighted list if an index is supplied. If an index is not supplied, a value is selected by weighted random distribution.

Parameters:

  • [index] Number optional

    Optional.

Returns:

Any: The value is returned or null is returned if the given index does not exist.

weight

(
  • [index]
)
Number

Gets a value's weight by index from the weighted list if an index is supplied. If an index is not supplied, a value is selected by weighted random distribution.

Parameters:

  • [index] Number optional

    Optional.

Returns:

Number: The weight is returned or null is returned if the given index does not exist.