ammonite
v1.5.4
Published
a functional library geared towards incremental games
Downloads
40
Maintainers
Readme
ammonite
Ammonite is a small library made for incremental games.
Features
- Small (under ~3Kb)
- Immutable leverages Immutable.js constructs
- Functional focuses on pure functions, helps ensure fewer side-effects
Installation
npm install ammonite --save
Usage
import { Amount, AmountStore, Metric, NewEntryOptions } from 'ammonite';
API
Amount
Amount is a class that Ammonite uses to control metrics. Its constructor takes a List
of an item that has the same subset of properties as Metric
(see Metric for more information). It uses a list as an easy way to maintain a record of all values—in a lot of incremental games, this ability is often lost, as it simply mutates a single value over time.
| Property | Type | Optional? | Default |
|----------------|------------------------|------------|----------|
| load
| List<T extends Metric>
| No | |
| treatAsInteger
| boolean | Yes | false |
load
is theList
treatAsInteger
will round the value after each evaluation
Methods
Amount(load, treatAsInteger)
This is the constructor function for the Amount class. Remember that it requires an interface which extends from Metric.
Example
interface Dogs {
total: number;
averageWoof: number;
averageBark: number;
}
let dogs = new Amount(List<Dogs>(), true);
getAll()
Returns all items in the load.
clone()
Returns an exact copy of Amount
.
Note: not of load
, but of the class itself.
first()
Returns the first item in the load.
last()
Returns the last item in the load.
current()
Return the last item item in the load (alias for last()
).
getSize()
Returns the size of the load.
push(n: T)
Pushes a new value to the load and returns the new load.
Note that the type of n
must satisfy T
, which is F-bounded to Metric
.
sum(prop: string)
Returns the sum of the given property. Note that the property itself should be of type number
.
Example
amount.sum('total');
includes(v: T)
Returns a true if v
is included in the load.
Returns false otherwise.
sort<C>(prop: string)
Returns a new sorted List
C
generic corresponds to the type of the propertyprop
is the property accessor for theMetric
property
Example
amount.sort<number>(prop: 'total');
amount.sort<number>(prop: 'time');
amount.sort<string>(prop: 'alphabet');
increment(inc: number, opts: NewEntryOptions<T>)
Increments the last value of the load and then returns a new load with the added value pushed onto it.
See NewEntryOptions<T>
for more information.
Example
amount.load = amount.increment(43);
amount.load = amount.increment(1.057, { exponential: true });
decrement(inc: number, opts: NewEntryOptions<T>)
Same as increment, except it substracts the given value, i.e. decrement(43)
would subtract 43 from the most recent value.
AmountStore
AmountStore<K, V>
is a class for managing all instances of Amount
.
| Property | Type | Optional? | Default |
|----------|--------|------------|------|
| store
| Map<K, Amount<V>>
| No | |
Under most cases K
will be a string, but it can also be a number.
Methods
get(key: K)
Returns the Amount
which belongs to the key.
getStore()
Returns the store.
set(key: K, value: Amount<V>)
Returns a new Map after settings a new value for the given key.
getKeys()
Returns an Iterator from the keys of store
.
update(key: K, newValue: Amount<V>)
Returns a new Map
with the updated value at the given key.
forEach(sideEffect: (value: Amount<V>, key: K, iter: Iterable<K, Amount<V>>) => any)
Performs a given side-effect.
Returns the number of times the side-effect was produced.
Metric
Metric is an interface that is F-bounded to Amount
.
| Property | Type | Optional? | Default |
|----------|--------|------------|------|
| total
| number | No | |
| time
| number | Yes | |
| [prop: string] | any | Yes | |
All generic types of Amount
must extend from Metric
. This is easy to do with extends
.
interface Click extends Metric {
total: number;
doubleClick?: boolean;
}
NewEntryOptions<T>
NewEntryOptions is the interface for new additions via increment
and decrement
.
| Property | Type | Optional? | Default |
|----------|--------|------------|------|
| exponential
| number | No | |
| extra
| any | Yes | |
exponential
determines whether the increment number is treated as an exponentialextra
is for additional metadata that may be required
FAQ
WTF is an Ammonite?
It's this.