Skip to content

Commit 8242672

Browse files
Merge pull request #4 from sandrohaiden/master
[fix] Making the argument optional in the entity adapter builder
2 parents f8c6012 + 89462df commit 8242672

File tree

9 files changed

+31
-24
lines changed

9 files changed

+31
-24
lines changed

README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# Note
2+
This project is a fork of the project of [Manu Bhardwaj](https://github.com/imanubhardwaj),
3+
You can access the original project [here](https://github.com/imanubhardwaj/vue-entity-adapter).
4+
5+
6+
The difference from this project to the original project, are just a few changes in the definitions for typescript and a small improvement in the ordering logic in the getAll method and in logic in addOne method.
7+
18
# Introduction
29

310
Entity State adapter for managing record collections.
@@ -13,11 +20,7 @@ Extensible type-safe adapters for selecting entity information.
1320
## Installation
1421
Installing with *npm*
1522

16-
``npm i vue-entity-adapter``
17-
18-
Installing with *yarn*
19-
20-
``yarn add vue-entity-adapter``
23+
``npm i vue-entity-adapter-plus``
2124

2225
## Getting Started
2326

example/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"core-js": "^2.6.5",
1212
"vue": "^2.6.10",
1313
"vue-class-component": "^7.0.2",
14-
"vue-entity-adapter": "^1.0.2",
14+
"vue-entity-adapter-plus": "^1.0.0",
1515
"vue-property-decorator": "^8.1.0",
1616
"vuex": "^3.0.1"
1717
},

example/src/models/state.model.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {EntityState} from "vue-entity-adapter";
1+
import {EntityState} from "vue-entity-adapter-plus";
22
import {Lesson} from "@/models/lessons.model";
33

44
export interface State {

example/src/store.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Vue from 'vue'
22
import Vuex from 'vuex'
33
import {Lesson} from "@/models/lessons.model"
4-
import {EntityAdapter} from 'vue-entity-adapter'
4+
import {EntityAdapter} from 'vue-entity-adapter-plus'
55
import actions from '@/store/actions';
66
import mutations from '@/store/mutations';
77
import getters from '@/store/getters';

example/src/store/actions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
UPDATE_LESSONS_MUTATION, UPSERT_LESSON_ACTION, UPSERT_LESSON_MUTATION, UPSERT_LESSONS_ACTION, UPSERT_LESSONS_MUTATION
1717
} from "@/store/types";
1818
import {Lesson} from "@/models/lessons.model";
19-
import {Update} from "vue-entity-adapter";
19+
import {Update} from "vue-entity-adapter-plus";
2020

2121
export default {
2222
[ADD_LESSON_ACTION]: ({commit}: any, payload: Lesson) => {

example/src/store/mutations.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
import {State} from "@/models/state.model";
1515
import {Lesson} from "@/models/lessons.model";
1616
import {lessonsAdapter} from "@/store";
17-
import {Update} from "vue-entity-adapter";
17+
import {Update} from "vue-entity-adapter-plus";
1818

1919
export default {
2020
[ADD_LESSON_MUTATION]: (state: State, payload: Lesson) => {

index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference types="node" />
22

3-
declare module 'vue-entity-adapter' {
3+
declare module 'vue-entity-adapter-plus' {
44
export interface EntityState<T> {
55
ids: string[];
66
entities: T[];
@@ -18,7 +18,7 @@ declare module 'vue-entity-adapter' {
1818
export type Update<T> = UpdateStr<T> | UpdateNum<T>;
1919

2020
export class EntityAdapter<T> {
21-
constructor(sortFn: Function);
21+
constructor(sortFn?: Function);
2222

2323
public getInitialState(): EntityState<T>;
2424
public getOne(id: string, state: EntityState<T>): T;

index.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { cloneDeep } from 'lodash';
1+
import { cloneDeep } from "lodash";
22

33
/**
44
* Class to manipulate entities
@@ -17,8 +17,8 @@ export class EntityAdapter {
1717
getInitialState() {
1818
return {
1919
ids: this.ids,
20-
entities: this.entities
21-
}
20+
entities: this.entities,
21+
};
2222
}
2323

2424
/**
@@ -37,7 +37,9 @@ export class EntityAdapter {
3737
* @returns {(*)[]}
3838
*/
3939
getAll(state) {
40-
return Object.keys(state.entities).map(key => state.entities[key]).sort(this.sortFn);
40+
return this.sortFn
41+
? state.ids.map((id) => (state.entities)[id]).sort(this.sortFn)
42+
: state.ids.map((id) => (state.entities)[id]);
4143
}
4244

4345
/**
@@ -67,7 +69,9 @@ export class EntityAdapter {
6769
addOne(element, state) {
6870
const newState = cloneDeep(state);
6971

70-
newState.ids.push(element.id);
72+
if(!newState.ids.includes(element.id)) {
73+
newState.ids.push(element.id);
74+
}
7175
newState.entities[element.id] = element;
7276

7377
return newState;
@@ -116,7 +120,7 @@ export class EntityAdapter {
116120
const newState = cloneDeep(state);
117121

118122
delete newState.entities[id];
119-
newState.ids = newState.ids.filter(_id => _id !== id);
123+
newState.ids = newState.ids.filter((_id) => _id !== id);
120124

121125
return newState;
122126
}
@@ -193,7 +197,7 @@ export class EntityAdapter {
193197
let newState = cloneDeep(state);
194198

195199
if (element.id in newState.entities) {
196-
newState = this.updateOne({id: element.id, changes: element}, newState);
200+
newState = this.updateOne({ id: element.id, changes: element }, newState);
197201
} else {
198202
newState = this.addOne(element, newState);
199203
}

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "vue-entity-adapter",
3-
"version": "1.0.5",
2+
"name": "vue-entity-adapter-plus",
3+
"version": "1.0.1",
44
"description": "Package to maintain entities for vuex.",
55
"private": false,
66
"keywords": ["Vue", "Vuex", "State Management", "Entity Adapter", "Flux"],
@@ -11,14 +11,14 @@
1111
},
1212
"repository": {
1313
"type": "git",
14-
"url": "git+https://github.com/imanubhardwaj/vue-entity-adapter.git"
14+
"url": "git+https://github.com/sandrohaiden/vue-entity-adapter-plus.git"
1515
},
1616
"author": "Manu Bhardwaj",
1717
"license": "MIT",
1818
"bugs": {
19-
"url": "https://github.com/imanubhardwaj/vue-entity-adapter/issues"
19+
"url": "https://github.com/sandrohaiden/vue-entity-adapter-plus/issues"
2020
},
21-
"homepage": "https://github.com/imanubhardwaj/vue-entity-adapter#readme",
21+
"homepage": "https://github.com/sandrohaiden/vue-entity-adapter-plus#readme",
2222
"devDependencies": {
2323
"@babel/core": "^7.4.0",
2424
"@babel/preset-env": "^7.4.2",

0 commit comments

Comments
 (0)