Comparing version 2.4.1 to 2.4.3
{ | ||
"name": "enmap", | ||
"version": "2.4.1", | ||
"version": "2.4.3", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
203
README.md
@@ -56,3 +56,3 @@ # Enmap - Enhanced Maps | ||
* [Enmap-SQLite](https://www.npmjs.com/package/enmap-sqlite) *Note: Against all odds, this provider DOES support sharding!* | ||
* [Enmap-Rethink](https://www.npmjs.com/package/enmap-rethink) *Note: Obviously, supports sharding.* | ||
* [Enmap-Rethink](https://www.npmjs.com/package/enmap-rethink) *Note: THE best for sharding: the only one that receives database updates on all shards (enmap-rethink 2.1.0 and higher)* | ||
* [Enmap-PGSQL](https://www.npmjs.com/package/enmap-pgsql) *Note: That's shorthand for "Postgresql". Supports sharding of course.* | ||
@@ -167,18 +167,22 @@ * [Enmap-Mongo](https://www.npmjs.com/package/enmap-mongo) *Note: Yay, MongoDB! Supports sharding, duh.* | ||
* _instance_ | ||
* [.get(key)](#Enmap+get) ⇒ <code>\*</code> \| <code>Promise.<\*></code> | ||
* [.fetchEverything()](#Enmap+fetchEverything) ⇒ <code>Map</code> | ||
* [.fetch(keyOrKeys)](#Enmap+fetch) ⇒ <code>\*</code> \| <code>Map</code> | ||
* [.fetchEverything()](#Enmap+fetchEverything) ⇒ <code>Map</code> | ||
* [.autonum()](#Enmap+autonum) ⇒ <code>number</code> | ||
* [.set(key, val)](#Enmap+set) ⇒ <code>Map</code> | ||
* [.setAsync(key, val)](#Enmap+setAsync) ⇒ <code>Promise.<Map></code> | ||
* [.setProp(key, prop, val, save)](#Enmap+setProp) ⇒ <code>Map</code> | ||
* [.push(key, val, allowDupes)](#Enmap+push) ⇒ <code>Map</code> | ||
* [.pushIn(key, prop, val, allowDupes)](#Enmap+pushIn) ⇒ <code>Map</code> | ||
* [.get(key)](#Enmap+get) ⇒ <code>\*</code> \| <code>Promise.<\*></code> | ||
* [.getProp(key, prop)](#Enmap+getProp) ⇒ <code>\*</code> | ||
* [.setProp(key, prop, val, save)](#Enmap+setProp) ⇒ <code>Map</code> | ||
* [.getHighestAutonum(start)](#Enmap+getHighestAutonum) ⇒ <code>Integer</code> | ||
* [.has(key)](#Enmap+has) ⇒ <code>Promise.<boolean></code> | ||
* [.hasProp(key, prop)](#Enmap+hasProp) ⇒ <code>boolean</code> | ||
* [.deleteProp(key, prop)](#Enmap+deleteProp) ⇒ [<code>Promise.<Enmap></code>](#Enmap) \| [<code>Enmap</code>](#Enmap) | ||
* [.delete(key, bulk)](#Enmap+delete) | ||
* [.deleteAsync(key, bulk)](#Enmap+deleteAsync) | ||
* [.autonum()](#Enmap+autonum) ⇒ <code>number</code> | ||
* [.getHighestAutonum(start)](#Enmap+getHighestAutonum) ⇒ <code>Integer</code> | ||
* [.deleteAll(bulk)](#Enmap+deleteAll) | ||
* [.deleteAllAsync(bulk)](#Enmap+deleteAllAsync) ⇒ <code>Promise</code> | ||
* [.remove(key, val, allowDupes)](#Enmap+remove) ⇒ <code>Map</code> | ||
* [.removeFrom(key, prop, val)](#Enmap+removeFrom) ⇒ <code>Map</code> | ||
* [.deleteProp(key, prop)](#Enmap+deleteProp) ⇒ [<code>Promise.<Enmap></code>](#Enmap) \| [<code>Enmap</code>](#Enmap) | ||
* [.array()](#Enmap+array) ⇒ <code>Array</code> | ||
@@ -203,19 +207,9 @@ * [.keyArray()](#Enmap+keyArray) ⇒ <code>Array</code> | ||
<a name="Enmap+get"></a> | ||
<a name="Enmap+fetchEverything"></a> | ||
### enmap.get(key) ⇒ <code>\*</code> \| <code>Promise.<\*></code> | ||
Retrieves a key from the enmap. If fetchAll is false, returns a promise. | ||
### enmap.fetchEverything() ⇒ <code>Map</code> | ||
Fetches every key from the persistent enmap and loads them into the current enmap value. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
**Returns**: <code>\*</code> \| <code>Promise.<\*></code> - The value or a promise containing the value. | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| key | <code>string</code> \| <code>number</code> | The key to retrieve from the enmap. | | ||
**Example** | ||
```js | ||
const myKeyValue = enmap.get("myKey"); | ||
console.log(myKeyValue); | ||
``` | ||
**Returns**: <code>Map</code> - The enmap containing all values. | ||
<a name="Enmap+fetch"></a> | ||
@@ -233,9 +227,9 @@ | ||
<a name="Enmap+fetchEverything"></a> | ||
<a name="Enmap+autonum"></a> | ||
### enmap.fetchEverything() ⇒ <code>Map</code> | ||
Fetches every key from the persistent enmap and loads them into the current enmap value. | ||
### enmap.autonum() ⇒ <code>number</code> | ||
Generates an automatic numerical key for inserting a new value. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
**Returns**: <code>Map</code> - The enmap containing all values. | ||
**Returns**: <code>number</code> - The generated key number. | ||
<a name="Enmap+set"></a> | ||
@@ -275,2 +269,64 @@ | ||
<a name="Enmap+setProp"></a> | ||
### enmap.setProp(key, prop, val, save) ⇒ <code>Map</code> | ||
Modify the property of a value inside the enmap, if the value is an object or array. | ||
This is a shortcut to loading the key, changing the value, and setting it back. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
**Returns**: <code>Map</code> - The EnMap. | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| key | <code>string</code> \| <code>number</code> | Required. The key of the element to add to The Enmap or array. 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> | Optional. Whether to save to persistent DB (used as false in init) | | ||
<a name="Enmap+push"></a> | ||
### enmap.push(key, val, allowDupes) ⇒ <code>Map</code> | ||
Push to an array value in Enmap. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
**Returns**: <code>Map</code> - The EnMap. | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| key | <code>string</code> \| <code>number</code> | | Required. The key of the array element to push to in Enmap. This value MUST be a string or number. | | ||
| val | <code>\*</code> | | Required. The value to push to the array. | | ||
| allowDupes | <code>boolean</code> | <code>false</code> | Allow duplicate values in the array (default: false). | | ||
<a name="Enmap+pushIn"></a> | ||
### enmap.pushIn(key, prop, val, allowDupes) ⇒ <code>Map</code> | ||
Push to an array element inside an Object or Array element in Enmap. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
**Returns**: <code>Map</code> - The EnMap. | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| key | <code>string</code> \| <code>number</code> | | Required. The key of the element. This value MUST be a string or number. | | ||
| prop | <code>\*</code> | | Required. The name of the array property to push to. | | ||
| val | <code>\*</code> | | Required. The value push to the array property. | | ||
| allowDupes | <code>boolean</code> | <code>false</code> | Allow duplicate values in the array (default: false). | | ||
<a name="Enmap+get"></a> | ||
### enmap.get(key) ⇒ <code>\*</code> \| <code>Promise.<\*></code> | ||
Retrieves a key from the enmap. If fetchAll is false, returns a promise. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
**Returns**: <code>\*</code> \| <code>Promise.<\*></code> - The value or a promise containing the value. | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| key | <code>string</code> \| <code>number</code> | The key to retrieve from the enmap. | | ||
**Example** | ||
```js | ||
const myKeyValue = enmap.get("myKey"); | ||
console.log(myKeyValue); | ||
``` | ||
<a name="Enmap+getProp"></a> | ||
@@ -290,17 +346,15 @@ | ||
<a name="Enmap+setProp"></a> | ||
<a name="Enmap+getHighestAutonum"></a> | ||
### enmap.setProp(key, prop, val, save) ⇒ <code>Map</code> | ||
Modify the property of a value inside the enmap, if the value is an object or array. | ||
This is a shortcut to loading the key, changing the value, and setting it back. | ||
### enmap.getHighestAutonum(start) ⇒ <code>Integer</code> | ||
Internal method used by autonum(). | ||
Loops on incremental numerical values until it finds a free key | ||
of that value in the Enamp. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
**Returns**: <code>Map</code> - The EnMap. | ||
**Returns**: <code>Integer</code> - The first non-existant value found. | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| key | <code>string</code> \| <code>number</code> | Required. The key of the element to add to The Enmap or array. 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> | Optional. Whether to save to persistent DB (used as false in init) | | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| start | <code>Integer</code> | <code>0</code> | The starting value to look for. | | ||
@@ -331,15 +385,2 @@ <a name="Enmap+has"></a> | ||
<a name="Enmap+deleteProp"></a> | ||
### enmap.deleteProp(key, prop) ⇒ [<code>Promise.<Enmap></code>](#Enmap) \| [<code>Enmap</code>](#Enmap) | ||
Delete a property from an object or array value in Enmap. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
**Returns**: [<code>Promise.<Enmap></code>](#Enmap) \| [<code>Enmap</code>](#Enmap) - If fetchAll is true, return the Enmap. Otherwise return a promise containing the Enmap. | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| key | <code>string</code> \| <code>number</code> | Required. The key of the element to delete the property from in Enmap. | | ||
| prop | <code>\*</code> | Required. The name of the property to remove from the object. | | ||
<a name="Enmap+delete"></a> | ||
@@ -367,23 +408,2 @@ | ||
<a name="Enmap+autonum"></a> | ||
### enmap.autonum() ⇒ <code>number</code> | ||
Generates an automatic numerical key for inserting a new value. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
**Returns**: <code>number</code> - The generated key number. | ||
<a name="Enmap+getHighestAutonum"></a> | ||
### enmap.getHighestAutonum(start) ⇒ <code>Integer</code> | ||
Internal method used by autonum(). | ||
Loops on incremental numerical values until it finds a free key | ||
of that value in the Enamp. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
**Returns**: <code>Integer</code> - The first non-existant value found. | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| start | <code>Integer</code> | <code>0</code> | The starting value to look for. | | ||
<a name="Enmap+deleteAll"></a> | ||
@@ -412,2 +432,45 @@ | ||
<a name="Enmap+remove"></a> | ||
### enmap.remove(key, val, allowDupes) ⇒ <code>Map</code> | ||
Remove a value in an Array or Object element in Enmap. Note that this only works for | ||
values, not keys. Complex values such as objects and arrays will not be removed this way. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
**Returns**: <code>Map</code> - The EnMap. | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| key | <code>string</code> \| <code>number</code> | Required. The key of the element to remove from in Enmap. This value MUST be a string or number. | | ||
| val | <code>\*</code> | Required. The value to remove from the array or object. | | ||
| allowDupes | <code>boolean</code> | Allow duplicate values in the array (default: false). | | ||
<a name="Enmap+removeFrom"></a> | ||
### enmap.removeFrom(key, prop, val) ⇒ <code>Map</code> | ||
Remove a value from an Array or Object property inside an Array or Object element in Enmap. | ||
Confusing? Sure is. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
**Returns**: <code>Map</code> - The EnMap. | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| key | <code>string</code> \| <code>number</code> | Required. The key of the element. This value MUST be a string or number. | | ||
| prop | <code>\*</code> | Required. The name of the array property to remove from. | | ||
| val | <code>\*</code> | Required. The value to remove from the array property. | | ||
<a name="Enmap+deleteProp"></a> | ||
### enmap.deleteProp(key, prop) ⇒ [<code>Promise.<Enmap></code>](#Enmap) \| [<code>Enmap</code>](#Enmap) | ||
Delete a property from an object or array value in Enmap. | ||
**Kind**: instance method of [<code>Enmap</code>](#Enmap) | ||
**Returns**: [<code>Promise.<Enmap></code>](#Enmap) \| [<code>Enmap</code>](#Enmap) - If fetchAll is true, return the Enmap. Otherwise return a promise containing the Enmap. | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| key | <code>string</code> \| <code>number</code> | Required. The key of the element to delete the property from in Enmap. | | ||
| prop | <code>\*</code> | Required. The name of the property to remove from the object. | | ||
<a name="Enmap+array"></a> | ||
@@ -414,0 +477,0 @@ |
319
src/index.js
@@ -26,2 +26,4 @@ /** | ||
/* GENERAL-USE METHODS & HELPERS */ | ||
/** | ||
@@ -62,17 +64,7 @@ * Initialize multiple Enmaps easily. | ||
/** | ||
* Retrieves a key from the enmap. If fetchAll is false, returns a promise. | ||
* @param {string|number} key The key to retrieve from the enmap. | ||
* @example | ||
* const myKeyValue = enmap.get("myKey"); | ||
* console.log(myKeyValue); | ||
* @return {*|Promise<*>} The value or a promise containing the value. | ||
* Fetches every key from the persistent enmap and loads them into the current enmap value. | ||
* @return {Map} The enmap containing all values. | ||
*/ | ||
get(key) { | ||
if (this.has(key)) { | ||
return super.get(key); | ||
} | ||
if (this.fetchAll) { | ||
return null; | ||
} | ||
return this.fetch(key); | ||
fetchEverything() { | ||
return this.db.fetchEverything(); | ||
} | ||
@@ -97,9 +89,14 @@ | ||
/** | ||
* Fetches every key from the persistent enmap and loads them into the current enmap value. | ||
* @return {Map} The enmap containing all values. | ||
* Generates an automatic numerical key for inserting a new value. | ||
* @return {number} The generated key number. | ||
*/ | ||
fetchEverything() { | ||
return this.db.fetchEverything(); | ||
autonum() { | ||
const start = this.get('internal::autonum') || 0; | ||
let highest = this.getHighestAutonum(start); | ||
this.set('internal::autonum', ++highest); | ||
return highest; | ||
} | ||
/* METHODS THAT SET THINGS IN ENMAP */ | ||
/** | ||
@@ -164,22 +161,21 @@ * Set the value in Enmap. | ||
/** | ||
* 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 {string|number} key Required. The key of the element to get from The Enmap. | ||
* @param {*} prop Required. The property to retrieve from the object or array. | ||
* @return {*} The value of the property obtained. | ||
* Modify the property of a value inside the enmap, if the value is an object or array. | ||
* This is a shortcut to loading the key, changing the value, and setting it back. | ||
* @param {string|number} key Required. The key of the element to add to The Enmap or array. | ||
* This value MUST be a string or number. | ||
* @param {*} prop Required. The property to modify inside the value object or array. | ||
* @param {*} val Required. The value to apply to the specified property. | ||
* @param {boolean} save Optional. Whether to save to persistent DB (used as false in init) | ||
* @return {Map} The EnMap. | ||
*/ | ||
getProp(key, prop) { | ||
if (this.fetchAll) { | ||
if (this.has(key)) { | ||
const data = super.get(key); | ||
return typeof data === 'object' ? data[prop] || null : data; | ||
} else { | ||
throw 'This key does not exist'; | ||
} | ||
} else { | ||
return this.fetch(key).then(data => { | ||
if (!data) throw 'This key does not exist'; | ||
return typeof data === 'object' ? data[prop] || null : data; | ||
}); | ||
setProp(key, prop, val) { | ||
if (!this.has(key)) { | ||
throw 'This key does not exist'; | ||
} | ||
const data = super.get(key); | ||
if (typeof data !== 'object') { | ||
throw 'Method can only be used when the value is an object'; | ||
} | ||
data[prop] = val; | ||
return this.set(key, data); | ||
} | ||
@@ -205,3 +201,3 @@ | ||
data.push(val); | ||
return super.set(key, data); | ||
return this.set(key, data); | ||
} | ||
@@ -231,57 +227,45 @@ | ||
data[prop].push(val); | ||
return super.set(key, data); | ||
return this.set(key, data); | ||
} | ||
/* METHODS THAT GETS THINGS FROM ENMAP */ | ||
/** | ||
* Remove a value in an Array or Object element in Enmap. Note that this only works for | ||
* values, not keys. Complex values such as objects and arrays will not be removed this way. | ||
* @param {string|number} key Required. The key of the element to remove from in Enmap. | ||
* This value MUST be a string or number. | ||
* @param {*} val Required. The value to remove from the array or object. | ||
* @param {boolean} allowDupes Allow duplicate values in the array (default: false). | ||
* @return {Map} The EnMap. | ||
* Retrieves a key from the enmap. If fetchAll is false, returns a promise. | ||
* @param {string|number} key The key to retrieve from the enmap. | ||
* @example | ||
* const myKeyValue = enmap.get("myKey"); | ||
* console.log(myKeyValue); | ||
* @return {*|Promise<*>} The value or a promise containing the value. | ||
*/ | ||
remove(key, val) { | ||
if (!this.has(key)) { | ||
throw 'This key does not exist'; | ||
get(key) { | ||
if (this.has(key)) { | ||
return super.get(key); | ||
} | ||
const data = super.get(key); | ||
if (typeof data !== 'object') { | ||
throw 'Method can only be used when the value is an object or array'; | ||
if (this.fetchAll) { | ||
return null; | ||
} | ||
if (data.constructor.name === 'Array') { | ||
const index = data.indexOf(val); | ||
return super.set(key, data.slice(index, 1)); | ||
} else { | ||
delete data[key]; | ||
return super.set(key, data); | ||
} | ||
return this.fetch(key); | ||
} | ||
/** | ||
* Remove a value from an Array or Object property inside an Array or Object element in Enmap. | ||
* Confusing? Sure is. | ||
* @param {string|number} key Required. The key of the element. | ||
* This value MUST be a string or number. | ||
* @param {*} prop Required. The name of the array property to remove from. | ||
* @param {*} val Required. The value to remove from the array property. | ||
* @return {Map} The EnMap. | ||
* 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 {string|number} key Required. The key of the element to get from The Enmap. | ||
* @param {*} prop Required. The property to retrieve from the object or array. | ||
* @return {*} The value of the property obtained. | ||
*/ | ||
removeFrom(key, prop, val) { | ||
if (!this.has(key)) { | ||
throw 'This key does not exist'; | ||
} | ||
const data = super.get(key); | ||
if (typeof data !== 'object') { | ||
throw 'Method can only be used when the value is an object or array'; | ||
} | ||
if (data[prop].constructor.name === 'Array') { | ||
let propdata = data[prop]; | ||
const index = propdata.indexOf(val); | ||
propdata = propdata.slice(index, 1); | ||
data[prop] = propdata; | ||
return super.set(key, data); | ||
getProp(key, prop) { | ||
if (this.fetchAll) { | ||
if (this.has(key)) { | ||
const data = super.get(key); | ||
return typeof data === 'object' ? data[prop] || null : data; | ||
} else { | ||
throw 'This key does not exist'; | ||
} | ||
} else { | ||
delete data[prop][key]; | ||
return super.set(key, data); | ||
return this.fetch(key).then(data => { | ||
if (!data) throw 'This key does not exist'; | ||
return typeof data === 'object' ? data[prop] || null : data; | ||
}); | ||
} | ||
@@ -291,26 +275,18 @@ } | ||
/** | ||
* Modify the property of a value inside the enmap, if the value is an object or array. | ||
* This is a shortcut to loading the key, changing the value, and setting it back. | ||
* @param {string|number} key Required. The key of the element to add to The Enmap or array. | ||
* This value MUST be a string or number. | ||
* @param {*} prop Required. The property to modify inside the value object or array. | ||
* @param {*} val Required. The value to apply to the specified property. | ||
* @param {boolean} save Optional. Whether to save to persistent DB (used as false in init) | ||
* @return {Map} The EnMap. | ||
* Internal method used by autonum(). | ||
* Loops on incremental numerical values until it finds a free key | ||
* of that value in the Enamp. | ||
* @param {Integer} start The starting value to look for. | ||
* @return {Integer} The first non-existant value found. | ||
*/ | ||
setProp(key, prop, val) { | ||
if (!this.has(key)) { | ||
throw 'This key does not exist'; | ||
getHighestAutonum(start = 0) { | ||
let highest = start; | ||
while (this.has(highest)) { | ||
highest++; | ||
} | ||
const data = super.get(key); | ||
if (typeof data !== 'object') { | ||
throw 'Method can only be used when the value is an object'; | ||
} | ||
data[prop] = val; | ||
if (this.persistent) { | ||
this.db.set(key, data); | ||
} | ||
return super.set(key, data); | ||
return highest; | ||
} | ||
/* BOOLEAN METHODS THAT CHECKS FOR THINGS IN ENMAP */ | ||
/** | ||
@@ -354,30 +330,3 @@ * Returns whether or not the key exists in the Enmap. | ||
/** | ||
* Delete a property from an object or array value in Enmap. | ||
* @param {string|number} key Required. The key of the element to delete the property from in Enmap. | ||
* @param {*} prop Required. The name of the property to remove from the object. | ||
* @returns {Promise<Enmap>|Enmap} If fetchAll is true, return the Enmap. Otherwise return a promise containing the Enmap. | ||
*/ | ||
deleteProp(key, prop) { | ||
if (this.fetchAll) { | ||
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.'; | ||
} | ||
delete data[prop]; | ||
return this.set(key, data); | ||
} else { | ||
return this.fetch(key).then(data => { | ||
if (!data) throw 'This key does not exist'; | ||
if (typeof data !== 'object') { | ||
throw 'The value of this key is not an object.'; | ||
} | ||
delete data[prop]; | ||
return this.set(key, data); | ||
}); | ||
} | ||
} | ||
/* METHODS THAT DELETE THINGS FROM ENMAP */ | ||
@@ -407,28 +356,2 @@ /** | ||
/** | ||
* Generates an automatic numerical key for inserting a new value. | ||
* @return {number} The generated key number. | ||
*/ | ||
autonum() { | ||
const start = this.get('internal::autonum') || 0; | ||
let highest = this.getHighestAutonum(start); | ||
this.set('internal::autonum', ++highest); | ||
return highest; | ||
} | ||
/** | ||
* Internal method used by autonum(). | ||
* Loops on incremental numerical values until it finds a free key | ||
* of that value in the Enamp. | ||
* @param {Integer} start The starting value to look for. | ||
* @return {Integer} The first non-existant value found. | ||
*/ | ||
getHighestAutonum(start = 0) { | ||
let highest = start; | ||
while (this.has(highest)) { | ||
highest++; | ||
} | ||
return highest; | ||
} | ||
/** | ||
* Calls the `delete()` method on all items that have it. | ||
@@ -470,2 +393,88 @@ * @param {boolean} bulk Optional. Defaults to True. whether to use the provider's "bulk" delete feature if it has one. | ||
/** | ||
* Remove a value in an Array or Object element in Enmap. Note that this only works for | ||
* values, not keys. Complex values such as objects and arrays will not be removed this way. | ||
* @param {string|number} key Required. The key of the element to remove from in Enmap. | ||
* This value MUST be a string or number. | ||
* @param {*} val Required. The value to remove from the array or object. | ||
* @param {boolean} allowDupes Allow duplicate values in the array (default: false). | ||
* @return {Map} The EnMap. | ||
*/ | ||
remove(key, val) { | ||
if (!this.has(key)) { | ||
throw 'This key does not exist'; | ||
} | ||
let data = super.get(key); | ||
if (typeof data !== 'object') { | ||
throw 'Method can only be used when the value is an object or array'; | ||
} | ||
if (data.constructor.name === 'Array') { | ||
const index = data.indexOf(val); | ||
data = data.slice(index, 1); | ||
} else { | ||
delete data[key]; | ||
} | ||
return this.set(key, data); | ||
} | ||
/** | ||
* Remove a value from an Array or Object property inside an Array or Object element in Enmap. | ||
* Confusing? Sure is. | ||
* @param {string|number} key Required. The key of the element. | ||
* This value MUST be a string or number. | ||
* @param {*} prop Required. The name of the array property to remove from. | ||
* @param {*} val Required. The value to remove from the array property. | ||
* @return {Map} The EnMap. | ||
*/ | ||
removeFrom(key, prop, val) { | ||
if (!this.has(key)) { | ||
throw 'This key does not exist'; | ||
} | ||
const data = super.get(key); | ||
if (typeof data !== 'object') { | ||
throw 'Method can only be used when the value is an object or array'; | ||
} | ||
if (!data[prop]) { | ||
throw 'Property does not exist'; | ||
} | ||
if (data[prop].constructor.name === 'Array') { | ||
let propdata = data[prop]; | ||
const index = propdata.indexOf(val); | ||
propdata = propdata.slice(index, 1); | ||
data[prop] = propdata; | ||
} else { | ||
delete data[prop][val]; | ||
} | ||
return this.set(key, data); | ||
} | ||
/** | ||
* Delete a property from an object or array value in Enmap. | ||
* @param {string|number} key Required. The key of the element to delete the property from in Enmap. | ||
* @param {*} prop Required. The name of the property to remove from the object. | ||
* @returns {Promise<Enmap>|Enmap} If fetchAll is true, return the Enmap. Otherwise return a promise containing the Enmap. | ||
*/ | ||
deleteProp(key, prop) { | ||
if (this.fetchAll) { | ||
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.'; | ||
} | ||
delete data[prop]; | ||
return this.set(key, data); | ||
} else { | ||
return this.fetch(key).then(data => { | ||
if (!data) throw 'This key does not exist'; | ||
if (typeof data !== 'object') { | ||
throw 'The value of this key is not an object.'; | ||
} | ||
delete data[prop]; | ||
return this.set(key, data); | ||
}); | ||
} | ||
} | ||
/* | ||
@@ -472,0 +481,0 @@ BELOW IS DISCORD.JS COLLECTION CODE |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
98907
1324
718