1.5.4 • Published 7 years ago

ammonite v1.5.4

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

ammonite ammonite

Travis Coveralls npm license stability

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 valuesin a lot of incremental games, this ability is often lost, as it simply mutates a single value over time.

PropertyTypeOptional?Default
loadList<T extends Metric>No
treatAsIntegerbooleanYesfalse
  • load is the List
  • 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 property
  • prop is the property accessor for the Metric 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.

PropertyTypeOptional?Default
storeMap<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.

PropertyTypeOptional?Default
totalnumberNo
timenumberYes
prop: stringanyYes

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.

PropertyTypeOptional?Default
exponentialnumberNo
extraanyYes
  • exponential determines whether the increment number is treated as an exponential
  • extra is for additional metadata that may be required

FAQ

WTF is an Ammonite?

It's this.

1.5.4

7 years ago

1.5.3

7 years ago

1.5.2

7 years ago

1.5.1

7 years ago

1.5.0

7 years ago

1.4.1

7 years ago

1.4.0

7 years ago

1.3.3

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.0.1

7 years ago