kinc
Uses one or more keys to locate and increment a value in a Map, Object, or other collection. Supports nesting, loose key matching, and more.
Installation
Requires Node.js 8.3.0 or above.
npm i kinc
API
The module exports an inc()
function that has one other function attached to it as a method: inc.all()
.
inc()
Parameters
- Bindable:
collection
(Array, Map, Object, Set, Typed Array, or WeakMap): The key-value collection with the value to be incremented. keychain
(any, or array of any): The key at which the value to be incremented is located, or an array of nested keys. If the key or key chain does not exist, it will be created, with a starting value of theinit
argument if it is set, otherwise0
.- Optional:
addend
(number): The number to add to the existing value. Defaults to1
. Set this to a negative number if you want to decrement. - Optional: Object argument:
arrays
/maps
/sets
/weakMaps
(arrays of classes/strings): Arrays of classes and/or string names of classes that should be treated as equivalent toArray
/Map
/Set
/WeakMap
(respectively).construct
(function or false): A callback which constructs a new collection in the process of generating a nested key chain that does not already exist. The function is passed a zero-based numeric index indicating the level of nesting, and is expected to return a new collection object. To disable keychain construction altogether, set this tofalse
.elseReturn
(any): A value to return in the event that no edits were made. Only takes effect if noelseThrow
is specified. Defaults toundefined
.elseThrow
(Error or string): An error to be thrown in the event that no edits were made. A string will be wrapped in anError
object automatically.getType
(function): A callback which specifies the type of collection to be created in the process of generating a nested key chain that does not already exist. This callback is only used ifconstruct
is not set. The function is passed a zero-based numeric index indicating the level of nesting and is expected to return a class (Object
,Array
,Map
, etc.) or the string name of a class ('Object'
,'Array'
,'Map'
, etc.). IfgetType
is not specified, thetypes
argument will be used if present.get
(function): A callback which, if provided, will override the built-in code that fetches an individual key from a collection. Use this if you need to support collections whose custom APIs preclude the use of parameters likemaps
. The callback will be called with five arguments: the collection, the key, the options object, the fallback to return if the key is not found, and a callback for the built-in get behavior (to which your customget
callback can defer if it determines that it doesn’t need to override the default behavior after all).init
(number): A number used as the starting value if incrementing a key that doesn’t exist. If omitted, the initial number will be0
.loose
(boolean): Whether or not to evaluate keys loosely (as defined bylooselyEquals
). Defaults tofalse
.looselyEquals
(function): A callback that accepts two values and returnstrue
if they are to be considered equivalent orfalse
otherwise. This argument is only used ifloose
istrue
. If omitted, the default behavior will, among other things, consider arrays/objects to be equal if they have the same entries.overwriteAncestors
(boolean): Whether or not to delete non-objects if necessary to resolve thekeychain
. If set tofalse
, then the module will throw an error ifkeychain
references a non-collection. Only applies ifelseThrow
is not set. Defaults tofalse
.preferStrict
(boolean): Only applies ifloose
istrue
. Iftrue
, then strictly-identical keys will be preferred over loosely-equivalent keys. Otherwise, the first loosely-equivalent key found will be used, even if a strictly-identical one comes later. Defaults tofalse
.set
(function): A callback which, if provided, will override the built-in code that sets the value of an individual key in a collection. Use this if you need to support collections whose custom APIs preclude the use of parameters likemaps
. The callback will be called with five arguments: the collection, the key, the new value, the options object, and a callback for the built-in set behavior (to which your customset
callback can defer if it determines that it doesn’t need to override the default behavior after all).type
ortypes
(function, string, or array of functions/strings): A class, or its string name, that should be used to construct new collections if a nested key chain does not already exist. To specify different collection types for each level of nesting, put the types into an array. Thetypes
argument is used only ifconstruct
andgetType
are not specified. If neither argument is specified, newly-created nested collections will be of the same type ascollection
.
Return Value
The new sum value.
Examples
Arrays
const inc = const arr = 10 20 30 // 15arr // [15, 20, 30]
Maps
const inc = const map = 'key' 99 // 100map // 100 const nestedMap = // 3nestedMap // 3
Objects
const inc = const obj = key1: {} // 1objkey1key2 // 1
inc.all()
Use this method if you want to apply the same incrementation to multiple keys.
Parameters
The parameters are the same as the main function, except that the second parameter is called keychains
and accepts an array of keychain
arguments.
Return Value
An array of newly-incremented values corresponding to the array of keychains.
Example
const inc = const obj = a: 1 b: 2 c: 3incallobj 'a' 'b' 10 // [11, 12] obja // 11objb // 12objc // 3
Related
The “k” family of modules works on keyed/indexed collections.
The “v” family of modules works on any collection of values.