WeightedList Class
Constructor
WeightedList
()
Item Index
Methods
_update
()
protected
chainable
Updates chached data for achieving weighted random distribution.
add
-
value
-
[weight]
Add a value to the weighted list.
Parameters:
-
value
Any -
[weight]
Number optionalOptional. Defaults to 1.
Returns:
dedupe
-
[mode]
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 optionalOptional. 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]
Executes the supplied function for each value in the weighted list.
Parameters:
-
fn
FunctionThe 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 optionalOptional. The context the function is called with.
every
-
fn
-
[context]
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
FunctionThe 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 optionalOptional. The context the function is called with.
Returns:
filter
-
fn
-
[context]
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
FunctionThe 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 optionalOptional. The context the function is called with.
Returns:
find
-
fn
-
[context]
Executes the supplied function for each value in the weighted list, searching for the first value that matches the supplied function.
Parameters:
-
fn
FunctionThe 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 optionalOptional. The context the function is called with.
Returns:
grep
-
pattern
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
RegExpRegular expression to test against each item.
Returns:
indexOf
-
value
-
[from]
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 optionalOptional. The index at which to begin the search. Defaults to 0.
Returns:
invoke
-
methodName
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:
-
methodName
String
Returns:
item
-
[index]
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 optionalOptional.
Returns:
- index
- This item's index.
- value
- This item's value.
- weight
- This item's weight.
itemsAreEqual
-
a
-
b
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]
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 optionalOptional. The index at which to begin the search. Defaults to the last index in the weighted list.
Returns:
map
-
fn
-
[context]
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
FunctionThe 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 optionalOptional. The context the function is called with.
Returns:
partition
-
fn
-
[context]
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
FunctionThe 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 optionalOptional. The context the function is called with.
Returns:
reduce
-
initialValue
-
fn
-
[context]
Executes the supplied function for each value in the weighted list, "folding" the weighted list into a single value.
Parameters:
-
initialValue
Any -
fn
FunctionThe 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 optionalOptional. The context the function is called with.
Returns:
reject
-
fn
-
[context]
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
FunctionThe 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 optionalOptional. The context the function is called with.
Returns:
remove
-
value
-
[all]
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 optionalOptional. If true, removes all occurances.
Returns:
removeIndex
-
index
Removes a value from the weighted list by index. This may cause remaining values to be reindexed.
Parameters:
-
index
Number
some
-
fn
-
[context]
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
FunctionThe 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 optionalOptional. The context the function is called with.
Returns:
unique
-
[mode]
Returns a copy of the weighted list with duplicate value removed.
Parameters:
-
[mode]
String optionalOptional. 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
Change the value and weight of a value that is already in the weighted list.
updateValue
-
index
-
value
Change the value of a value that is already in the weighted list.
Parameters:
-
index
Number -
value
Any
updateWeight
-
index
-
weight
Change the weight of a value that is already in the weighted list.
value
-
[index]
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 optionalOptional.
Returns:
weight
-
[index]
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 optionalOptional.