Comparing version 0.6.1 to 0.6.2
{ | ||
"name": "enmap", | ||
"version": "0.6.1", | ||
"version": "0.6.2", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha" | ||
}, | ||
@@ -22,3 +22,4 @@ "repository": { | ||
"devDependencies": { | ||
"enmap-level": "*", | ||
"enmap-level": "^1.0.1", | ||
"enmap-sqlite": "^1.0.1", | ||
"eslint": "^4.7.0", | ||
@@ -25,0 +26,0 @@ "eslint-config-airbnb-base": "^11.3.2", |
136
README.md
@@ -155,7 +155,8 @@ # Enmap - Enhanced Maps | ||
### Enmap ⇐ <code>Map</code> | ||
<a name="docs"></a> | ||
Enhanced Map structure with additional utility methods. | ||
Can be made persistent with optional provider modules. | ||
## API Documentation | ||
### Enmap ⇒ <code>Map</code> | ||
A enhanced Map structure with additional utility methods. | ||
Can be made persistent | ||
**Kind**: global class | ||
@@ -165,10 +166,10 @@ **Extends**: <code>Map</code> | ||
* [Enmap](#Enmap) ⇐ <code>Map</code> | ||
* [.init()](#Enmap+init) ⇒ <code>Void</code> | ||
* [.validateName()](#Enmap+validateName) ⇒ <code>boolean</code> | ||
* [.close()](#Enmap+close) | ||
* [.set(key, val)](#Enmap+set) ⇒ <code>Map</code> | ||
* [.set(key, val, save)](#Enmap+set) ⇒ <code>Map</code> | ||
* [.getProp(key, prop)](#Enmap+getProp) ⇒ <code>\*</code> | ||
* [.setProp(key, prop, val, save)](#Enmap+setProp) ⇒ <code>Map</code> | ||
* [.hasProp(key, prop)](#Enmap+hasProp) ⇒ <code>boolean</code> | ||
* [.setAsync(key, val)](#Enmap+setAsync) ⇒ <code>Map</code> | ||
* [.delete(key, bulk)](#Enmap+delete) | ||
* [.deleteAsync(key, bulk)](#Enmap+deleteAsync) | ||
* [.purge()](#Enmap+purge) ⇒ <code>Promise</code> | ||
* [.array()](#Enmap+array) ⇒ <code>Array</code> | ||
@@ -189,34 +190,68 @@ * [.keyArray()](#Enmap+keyArray) ⇒ <code>Array</code> | ||
* [.concat(...enmaps)](#Enmap+concat) ⇒ [<code>Enmap</code>](#Enmap) | ||
* [.deleteAll()](#Enmap+deleteAll) ⇒ <code>Array.<Promise></code> | ||
* [.deleteAll(bulk)](#Enmap+deleteAll) | ||
* [.deleteAllAsync(bulk)](#Enmap+deleteAllAsync) | ||
* [.equals(enmap)](#Enmap+equals) ⇒ <code>boolean</code> | ||
<a name="Enmap+init"></a> | ||
<a name="Enmap+close"></a> | ||
### enmap.init() ⇒ <code>Void</code> | ||
Internal method called on persistent Enmaps to load data from the underlying database. | ||
### enmap.close() | ||
Shuts down the underlying persistent enmap database. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
<a name="Enmap+validateName"></a> | ||
<a name="Enmap+set"></a> | ||
### enmap.validateName() ⇒ <code>boolean</code> | ||
Internal method used to validate persistent enmap names (valid Windows filenames); | ||
### enmap.set(key, val, save) ⇒ <code>Map</code> | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
**Returns**: <code>Map</code> - The Enmap. | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| key | <code>\*</code> | | Required. The key of the element to add to The Enmap. If the EnMap is persistent this value MUST be a string or number. | | ||
| val | <code>\*</code> | | Required. The value of the element to add to The Enmap. If the EnMap is persistent this value MUST be stringifiable as JSON. | | ||
| save | <code>boolean</code> | <code>true</code> | Optional. Whether to save to persistent DB (used as false in init) | | ||
<a name="Enmap+getProp"></a> | ||
### enmap.getProp(key, prop) ⇒ <code>\*</code> | ||
Returns the specific property within a stored value. If the value isn't an object or array, returns the unchanged data | ||
If the key does not exist or the value is not an object, throws an error. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
**Returns**: <code>boolean</code> - Indicates whether the name is valid. | ||
<a name="Enmap+close"></a> | ||
**Returns**: <code>\*</code> - The value of the property obtained. | ||
### enmap.close() | ||
Shuts down the underlying persistent enmap database. | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| key | <code>\*</code> | Required. The key of the element to get from The Enmap. | | ||
| prop | <code>\*</code> | Required. The property to retrieve from the object or array. | | ||
<a name="Enmap+setProp"></a> | ||
### enmap.setProp(key, prop, val, save) ⇒ <code>Map</code> | ||
Modify the property of a value inside the enmap, assuming this value is an object or array. | ||
This is a shortcut to loading the key, changing the value, and setting it back. | ||
If the key does not exist or the value is not an object, throws an error. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
<a name="Enmap+set"></a> | ||
**Returns**: <code>Map</code> - The EnMap. | ||
### enmap.set(key, val) ⇒ <code>Map</code> | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| key | <code>\*</code> | | Required. The key of the element to add to The Enmap or array. If the EnMap is persistent this value MUST be a string or number. | | ||
| prop | <code>\*</code> | | Required. The property to modify inside the value object or array. | | ||
| val | <code>\*</code> | | Required. The value to apply to the specified property. | | ||
| save | <code>boolean</code> | <code>true</code> | Optional. Whether to save to persistent DB (used as false in init) | | ||
<a name="Enmap+hasProp"></a> | ||
### enmap.hasProp(key, prop) ⇒ <code>boolean</code> | ||
Returns whether or not the property exists within an object or array value in enmap. | ||
If the key does not exist or the value is not an object, throws an error. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
**Returns**: <code>Map</code> - The EnMap object. | ||
**Returns**: <code>boolean</code> - Whether the property exists. | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| key | <code>\*</code> | Required. The key of the element to add to the EnMap object. If the EnMap is persistent this value MUST be a string or number. | | ||
| val | <code>\*</code> | Required. The value of the element to add to the EnMap object. If the EnMap is persistent this value MUST be stringifiable as JSON. | | ||
| key | <code>\*</code> | Required. The key of the element to check in the Enmap or array. | | ||
| prop | <code>\*</code> | Required. The property to verify inside the value object or array. | | ||
@@ -227,8 +262,8 @@ <a name="Enmap+setAsync"></a> | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
**Returns**: <code>Map</code> - The EnMap object. | ||
**Returns**: <code>Map</code> - The Enmap. | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| key | <code>\*</code> | Required. The key of the element to add to the EnMap object. If the EnMap is persistent this value MUST be a string or number. | | ||
| val | <code>\*</code> | Required. The value of the element to add to the EnMap object. If the EnMap is persistent this value MUST be stringifiable as JSON. | | ||
| key | <code>\*</code> | Required. The key of the element to add to The Enmap. If the EnMap is persistent this value MUST be a string or number. | | ||
| val | <code>\*</code> | Required. The value of the element to add to The Enmap. If the EnMap is persistent this value MUST be stringifiable as JSON. | | ||
@@ -240,6 +275,6 @@ <a name="Enmap+delete"></a> | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| key | <code>\*</code> | | Required. The key of the element to delete from the EnMap object. | | ||
| bulk | <code>boolean</code> | <code>false</code> | Internal property used by the purge method. | | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| key | <code>\*</code> | Required. The key of the element to delete from The Enmap. | | ||
| bulk | <code>boolean</code> | Internal property used by the purge method. | | ||
@@ -251,13 +286,7 @@ <a name="Enmap+deleteAsync"></a> | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| key | <code>\*</code> | | Required. The key of the element to delete from the EnMap object. | | ||
| bulk | <code>boolean</code> | <code>false</code> | Internal property used by the purge method. | | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| key | <code>\*</code> | Required. The key of the element to delete from The Enmap. | | ||
| bulk | <code>boolean</code> | Internal property used by the purge method. | | ||
<a name="Enmap+purge"></a> | ||
### enmap.purge() ⇒ <code>Promise</code> | ||
Completely deletes all keys from an EnMap, including persistent data. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
<a name="Enmap+array"></a> | ||
@@ -267,3 +296,3 @@ | ||
Creates an ordered array of the values of this Enmap, and caches it internally. | ||
The array will only be reconstructed if an item is added to or removed from the Enmap, | ||
The array will only be reconstructed if an item is added to or removed from the Enmap, | ||
or if you change the length of the array itself. If you don't want this caching behaviour, | ||
@@ -276,3 +305,3 @@ use `Array.from(enmap.values())` instead. | ||
### enmap.keyArray() ⇒ <code>Array</code> | ||
Creates an ordered array of the keys of this Enmap, and caches it internally. | ||
Creates an ordered array of the keys of this Enmap, and caches it internally. | ||
The array will only be reconstructed if an item is added to or removed from the Enmap, | ||
@@ -283,3 +312,2 @@ or if you change the length of the array itself. If you don't want this caching behaviour, | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
<a name="Enmap+random"></a> | ||
@@ -292,3 +320,3 @@ | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
**Returns**: <code>\*</code> \| <code>Array.<\*></code> - The single value if `count` is undefined, | ||
**Returns**: <code>\*</code> \| <code>Array.<\*></code> - The single value if `count` is undefined, | ||
or an array of values of `count` length | ||
@@ -483,6 +511,22 @@ | ||
### enmap.deleteAll() ⇒ <code>Array.<Promise></code> | ||
### enmap.deleteAll(bulk) | ||
Calls the `delete()` method on all items that have it. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| bulk | <code>boolean</code> | <code>true</code> | Optional. Defaults to True. whether to use the provider's "bulk" delete feature if it has one. | | ||
<a name="Enmap+deleteAllAsync"></a> | ||
### enmap.deleteAllAsync(bulk) | ||
Calls the `delete()` method on all items that have it. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| bulk | <code>boolean</code> | <code>true</code> | Optional. Defaults to True. whether to use the provider's "bulk" delete feature if it has one. | | ||
<a name="Enmap+equals"></a> | ||
@@ -489,0 +533,0 @@ |
@@ -69,2 +69,3 @@ /** | ||
* Returns the specific property within a stored value. If the value isn't an object or array, returns the unchanged data | ||
* If the key does not exist or the value is not an object, throws an error. | ||
* @param {*} key Required. The key of the element to get from The Enmap. | ||
@@ -75,2 +76,5 @@ * @param {*} prop Required. The property to retrieve from the object or array. | ||
getProp(key, prop) { | ||
if (!this.has(key)) { | ||
throw 'This key does not exist'; | ||
} | ||
const data = super.get(key); | ||
@@ -80,3 +84,3 @@ if (typeof data !== 'object') { | ||
} | ||
return data[prop]; | ||
return data[prop] || null; | ||
} | ||
@@ -87,2 +91,3 @@ | ||
* This is a shortcut to loading the key, changing the value, and setting it back. | ||
* If the key does not exist or the value is not an object, throws an error. | ||
* @param {*} key Required. The key of the element to add to The Enmap or array. | ||
@@ -96,2 +101,5 @@ * If the EnMap is persistent this value MUST be a string or number. | ||
setProp(key, prop, val, save = true) { | ||
if (!this.has(key)) { | ||
throw 'This key does not exist'; | ||
} | ||
const data = super.get(key); | ||
@@ -109,2 +117,20 @@ if (typeof data !== 'object') { | ||
/** | ||
* Returns whether or not the property exists within an object or array value in enmap. | ||
* If the key does not exist or the value is not an object, throws an error. | ||
* @param {*} key Required. The key of the element to check in the Enmap or array. | ||
* @param {*} prop Required. The property to verify inside the value object or array. | ||
* @return {boolean} Whether the property exists. | ||
*/ | ||
hasProp(key, prop) { | ||
if (!this.has(key)) { | ||
throw 'This key does not exist'; | ||
} | ||
const data = super.get(key); | ||
if (typeof data !== 'object') { | ||
throw 'The value of this key is not an object.'; | ||
} | ||
return data.hasOwnProperty(prop); | ||
} | ||
/** | ||
* | ||
@@ -143,2 +169,3 @@ * @param {*} key Required. The key of the element to add to The Enmap. | ||
} | ||
/** | ||
@@ -145,0 +172,0 @@ * Creates an ordered array of the values of this Enmap, and caches it internally. |
/* global describe, it, before */ | ||
const assert = require('assert'); | ||
const Enmap = require('../'); | ||
const Provider = require('enmap-level'); | ||
const persistent = new Enmap({ provider: new Provider({ name: 'testing' }) }); | ||
@@ -63,6 +65,4 @@ describe('Standard Enmaps', () => { | ||
describe('Persistent Enmap', () => { | ||
let enmap; | ||
before(async () => { | ||
enmap = new Enmap({ name: 'testing', persistent: true }); | ||
return enmap.defer; | ||
await persistent.defer; | ||
}); | ||
@@ -72,3 +72,3 @@ | ||
it('can load existing data', () => { | ||
assert.ok(enmap.size); | ||
assert.ok(persistent.size); | ||
}); | ||
@@ -79,22 +79,56 @@ }); | ||
it('inserts string values', () => { | ||
enmap.set('simplevalue', 'this is a string'); | ||
persistent.set('simplevalue', 'this is a string'); | ||
}); | ||
it('inserts other primitives', () => { | ||
enmap.set('boolean', true); | ||
enmap.set('integer', 42); | ||
enmap.set('null', null); | ||
persistent.set('boolean', true); | ||
persistent.set('integer', 42); | ||
persistent.set('null', null); | ||
}); | ||
it('remembers values', () => { | ||
assert.equal(enmap.get('simplevalue'), 'this is a string'); | ||
assert.equal(enmap.get('integer'), 42); | ||
assert.equal(persistent.get('simplevalue'), 'this is a string'); | ||
assert.equal(persistent.get('integer'), 42); | ||
}); | ||
it('supports arrays', () => { | ||
enmap.set('array', [1, 2, 3]); | ||
assert.equal(enmap.get('array')[2], 3); | ||
persistent.set('array', [1, 2, 3]); | ||
assert.equal(persistent.get('array')[2], 3); | ||
}); | ||
it('also supports objects', () => { | ||
enmap.set('object', { color: 'black', action: 'paint', desire: true }); | ||
assert.equal(enmap.get('object').color, 'black'); | ||
persistent.set('object', { color: 'black', action: 'paint', desire: true }); | ||
assert.equal(persistent.get('object').color, 'black'); | ||
}); | ||
it('can get a specific object or array property', () => { | ||
assert.equal(persistent.getProp('object', 'action'), 'paint'); | ||
assert.equal(persistent.getProp('array', 0), 1); | ||
}); | ||
}); | ||
describe('Performance Tests', () => { | ||
it('can insert 10,000 records', async () => { | ||
const promises = []; | ||
for (let i = 0; i < 10000; i++) { | ||
promises.push(persistent.setAsync(`test${i}`, 'simple string')); | ||
} | ||
await Promise.all(promises); | ||
}); | ||
it('can delete them too', async () => { | ||
const promises = []; | ||
for (let i = 0; i < 10000; i++) { | ||
promises.push(persistent.deleteAsync(`test${i}`)); | ||
} | ||
await Promise.all(promises); | ||
}); | ||
it('can insert 100,000 records', async () => { | ||
const promises = []; | ||
for (let i = 0; i < 100000; i++) { | ||
promises.push(persistent.setAsync(`test${i}`, 'simple string')); | ||
} | ||
await Promise.all(promises); | ||
}).timeout(45000); | ||
it('can delete them too', async () => { | ||
const promises = []; | ||
for (let i = 0; i < 100000; i++) { | ||
promises.push(persistent.deleteAsync(`test${i}`)); | ||
} | ||
await Promise.all(promises); | ||
}).timeout(45000); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
95787
750
1
534
9