Skip to content

Commit

Permalink
Merge pull request #3 from Lwdthe1/morehelp
Browse files Browse the repository at this point in the history
Adding more functions and tests
  • Loading branch information
Lwdthe1 authored Nov 15, 2019
2 parents ecb27e6 + 2bed520 commit 78b2142
Show file tree
Hide file tree
Showing 4 changed files with 285 additions and 37 deletions.
32 changes: 21 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,27 @@ You can also specify placeholders for displaying shimmers while awaiting data fr

# Usage

1. `constructor.instance(configObject)` Create a new instance of the store with an optional configuration object.
2. `addUniqueObject()` Add an unique object to the store.
3. `addUniqueObjects()` Add an array unique objects to the store.
4. `prependUniqueObject()` Add an unique object to front of the store.
5. `prependUniqueObjects()` Add an array unique objects to front of the store.
6. `replaceObject()` Replace an existing object or add to the store.
7. `getObjectById()` Get an object from the store by its id.
8. `deleteObjectById()` Delete an object from the store by its id.
9. `hasObjectById()` Check if the store has an object by its id.
10. `hasPlaceholders()` Checks if the store has placeholder objects.
11. `clearPlaceholders()` Remove the placeholder objects from the store.
- `constructor.instance(configObject)` Create a new instance of the store with an optional configuration object.
- `addUniqueObject()` Add an unique object to the store. Objects are unique by their `id` field, so ensure your objects have that.
- `addUniqueObjects()` Add an array unique objects to the store.
- `prependUniqueObject()` Add an unique object to front of the store.
- `prependUniqueObjects()` Add an array unique objects to front of the store.
- `setBeforeAddProcessor(callbackFunction)` Set a function that will be called before any time an object is added to the store.

- `hasObjectById()` Check if the store has an object by its id.
- `replaceObject()` Replace an existing object or add to the store.
- `deleteObjectById()` Delete an object from the store by its id.
- `reset()` Delete all objects from the store.

- `getObjects()` Get all the objects in the store.
- `getObjectIds()` Get all the object ids in the store.
- `getObjectById()` Get an object from the store by its id.
- `getObjectAtIndex()` Get an object from the store at a specific index.
- `getObjectByPredicate()`Get the first object matching the provided filter predicate.
- `getObjectIndexById()` Get the index of an object by its id.

- `hasPlaceholders()` Checks if the store has placeholder objects.
- `clearPlaceholders()` Remove the placeholder objects from the store.

See the tests (`tests/dataStore_test.js`) to see how these methods are used.

Expand Down
79 changes: 54 additions & 25 deletions lib/dataStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class ArrayDataStore {
this._objectIdMap = {};
this._objects = [];

this._beforeAddObjectProcessor;

let placeholdersData;
let numPlaceholders;
if (!config.placeholders) {
Expand All @@ -31,6 +33,9 @@ class ArrayDataStore {
if (this._numPlaceholders) {
this._preparePlaceholders();
}

// Whether or not to log console messages
this._debug = !!config.debug;
}

get id() {
Expand All @@ -44,6 +49,10 @@ class ArrayDataStore {
);
}

get contents() {
return this._objects;
}

hasContents() {
return this.size > 0;
}
Expand All @@ -52,10 +61,6 @@ class ArrayDataStore {
return !!this._numPlaceholders && !this._placeholdersCleared;
}

get contents() {
return this._objects;
}

reset() {
this._objects = [];
this._objectIdMap = {};
Expand Down Expand Up @@ -86,26 +91,46 @@ class ArrayDataStore {
}
}

beforeAdd(processor) {
/**
* Set a function that will be called before any time an object is added to the store.
* @param {cb} (object, {isPrepend: Boolean}) => {} processor
*/
setBeforeAddProcessor(processor) {
this._beforeAddObjectProcessor = processor;
}

setObjectUidGetter(cb) {
this._objectUidGetter = cb;
}

/**
* Add a unique object to the store. An object is unique by its `id` field, so ensure yours has one.
* @param {Object} object
*/
addUniqueObject(object) {
return this.addUniqueObjects([object]);
}

/**
* Add unique objects to the store. An object is unique by its `id` field, so ensure yours have it.
* @param {Array<Object>} objects
*/
addUniqueObjects(objects) {
return this._addUniqueObjects(objects, false);
}

/**
* Add unique objects to the front of the store. An object is unique by its `id` field, so ensure yours has one.
* @param {Array<Object>} objects
*/
prependUniqueObject(object) {
return this.prependUniqueObjects([object]);
}

/**
* Add unique objects to the front of the store. An object is unique by its `id` field, so ensure yours have it.
* @param {Array<Object>} objects
*/
prependUniqueObjects(objects) {
return this._addUniqueObjects(objects, true);
}
Expand All @@ -125,11 +150,15 @@ class ArrayDataStore {
return this._objectIdMap[id];
}

getObjectAtIndex(index) {
return this._objects[index];
}

hasObjectById(id) {
return !!this._objectIdMap[id] && this._objectIdMap[id].id == id;
}

getIndexOfObjectById(objId) {
getObjectIndexById(objId) {
return this._objects.findIndex(({ id }) => id == objId);
}

Expand Down Expand Up @@ -165,18 +194,20 @@ class ArrayDataStore {
}

/**
* Get the first object matching the provided filter predicate.
* @param {(object) => boolean} predicate
* @returns the first object that matches the predicate
*/
getObjectByPredicate(predicate) {
let foundObject;
this.getObjects().some(object => {
if (predicate(object)) {
foundObject = object;
return true;
}
});
return foundObject;
return this.getObjects().find(predicate);
}

getObjects() {
return this._objects;
}

getObjectIds() {
return Object.keys(this._objectIdMap);
}

/**
Expand All @@ -196,6 +227,12 @@ class ArrayDataStore {
this._placeholdersCleared = true;
}

_consoleWarn() {
if (this._debug) {
console.warn(...arguments);
}
}

_addUniqueObject(object, shouldPrepend, opt_atIndex) {
if (!object) return;

Expand All @@ -211,10 +248,10 @@ class ArrayDataStore {

try {
if (this._beforeAddObjectProcessor) {
this._beforeAddObjectProcessor(object);
this._beforeAddObjectProcessor(object, { isPrepend: !!shouldPrepend });
}
} catch (err) {
console.warn(
this._consoleWarn(
`ArrayDataStore #${this.id} failed to run the before-add processor for that object ${object.id} due to ${err.stack}`
);
}
Expand All @@ -239,7 +276,7 @@ class ArrayDataStore {
try {
uid = this._objectUidGetter(object);
} catch (err) {
console.warn(
this._consoleWarn(
`ArrayDataStore #${this.id} failed to get the object uid using the provided getter.`
);
}
Expand All @@ -249,14 +286,6 @@ class ArrayDataStore {
return uid != undefined ? uid : object.id;
}

getObjects() {
return this._objects;
}

getCount() {
return this.size;
}

static instance(config) {
return new ArrayDataStore(config);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "array-datastore",
"version": "1.0.8",
"version": "1.1.1",
"description": "A datastore to store unique objects in an array.",
"main": "index.js",
"repository": {
Expand Down
Loading

0 comments on commit 78b2142

Please sign in to comment.