pushmap
v0.2.3
Published
Give power to any map. PushMap provides great methods for improving maps. Some examples are: .push() .all() .add()
Downloads
21
Maintainers
Readme
PushMap
Summary
Give power to any map. PushMap provides great methods for improving maps. Some examples are: .push() .all() .add().
Module developed by
tnfAngel
#8642.
Usage
let PushMap = require("pushmap")
let pmap = new PushMap()
Example
let PushMap = require("pushmap")
let pmap = new PushMap()
// Now we can use PushMap Methods on this map:
pmap.push('foo', 'bar')
// Normal Map get method
pmap.get('foo') // ['bar']
Documentation
PushMap Methods
Pushmap includes all normal Map methods and:
- Push |
.push(key, element)
-> updatedRow
Creates an array or push an element to original array of map.
pmap.push('foo', 'bar')
pmap.get('foo') // ['bar']
pmap.push('foo', 'baz')
pmap.get('foo') // ['bar', 'baz']
- RemoveIndex |
.removeIndex(key, index)
-> updatedRow || false
Remove an element from an array by its index or return false if something went wrong.
pmap.push('foo', 'bar') // ['bar']
pmap.push('foo', 'baz') // ['bar', 'baz']
pmap.removeIndex('foo', 0) // Remove the element 0 of the array obtained by his key.
pmap.get('foo') // ['baz']
- FilterElement |
.filterElement(key, callbackFn, [thisArg])
-> updatedRow || false
Same functionality as Array.prototype.filter(), but sets the result to the array. return false if something went wrong.
pmap.push('foo', 'bar') // ['bar']
pmap.push('foo', 'baz') // ['bar', 'baz']
pmap.filterElement('foo', x => x != 'baz') // Remove the element that has a value of baz.
// Also can be used with objects
pmap.push('qux', 'foo') // ['foo']
pmap.push('qux', {'bar': 'baz'}) // ['foo', {'bar': 'baz'}]
pmap.filterElement('qux', x => !x.bar)
pmap.get('qux') // ['foo']
- Shift |
.shift()
-> updatedRow
Same functionality as Array.prototype.shift(), but sets the result to the array.
pmap.push('foo', 'bar') // ['bar']
pmap.push('foo', 'baz') // ['bar', 'baz']
pmap.shift() // bar
pmap.get('foo') // ['baz']
- All |
.all()
-> array
Returns all the elements of the map in an array of objects.
pmap.set('foo', 'bar')
pmap.set('baz', 'qux')
pmap.all() // [{key: 'foo', value: 'bar'}, {key: 'baz', value: 'qux'}]
// Also you can iterate the all method:
pmap.all().forEach(element => {
console.log(element) // {key: x, value: x}
console.log(element.key) // foo & baz
console.log(element.value) // bar & qux
})
- ToObject |
.toObject()
-> object
Same as all method but returns an object.
pmap.set('foo', 'bar')
pmap.set('baz', 'qux')
pmap.toObject() // {
// 'foo': 'bar'
// 'baz': 'qux'
// {
- Add |
.add(key, number)
-> updatedRow
Adds a number to a key in the map. (If no existing number, it will add to 0)
pmap.add('corge', 1)
pmap.get('corge') // 1
pmap.add('corge', 2)
pmap.get('corge') // 3
// You can't add to a value that is not a number
pmap.set('someString', 'someString')
pmap.add('someString', 1) // PushMap error
- Subtract |
.subtract(key, number)
-> updatedRow
Subtracts a number to a key in the map. (If no existing number, it will subtract from 0)
pmap.subtract('grault', 1)
pmap.get('grault') // -1
pmap.subtract('grault', 2)
pmap.get('grault') // -3
// You can't subtract to a value that is not a number
pmap.set('someString', 'someString')
pmap.subtract('someString', 1) // PushMap error
- Type |
.type(key)
-> typeof
Returns the type of a value in the map.
pmap.set('someString', 'someString')
pmap.type('someString') // "string"
pmap.set('someObject', {foo: 'bar'})
pmap.type('someObject') // "object"
Normal Map Methods
Pushmap includes all normal Map methods.
- Clear |
.clear()
Removes all key-value pairs from the Map object.
pmap.clear()
- Delete |
.delete(key)
Returns true if an element in the Map object existed and has been removed, or false if the element does not exist.
pmap.delete('foo')
- Get |
.get(key)
Returns the value associated to the key, or undefined if there is none.
pmap.get('foo')
- Has |
.has(key)
Returns a boolean asserting whether a value has been associated to the key in the Map object or not.
pmap.has('foo')
- Set |
.set(key, value)
Sets the value for the key in the Map object. Returns the Map object.
pmap.set('foo', 'bar')
- Keys |
.keys()
Returns a new Iterator object that contains the keys for each element in the Map object in insertion order.
pmap.keys()
- Values |
.values()
Returns a new Iterator object that contains the values for each element in the Map object in insertion order.
pmap.values()
- Entries |
.entries()
Returns a new Iterator object that contains an array of [key, value] for each element in the Map object in insertion order.
pmap.entries()
- ForEach |
.forEach(callbackFn[, thisArg])
Calls callbackFn once for each key-value pair present in the Map object, in insertion order. If a thisArg parameter is provided to forEach, it will be used as the this value for each callback.
pmap.forEach(x => {
console.log(x)
})
Instalation
If you have instalation issues, join our support server.
Linux & Windows
- Open: CMD
- Put:
npm i pushmap@latest
Mac
- Install: XCode
- Put:
npm i pushmap@latest