This repository has been archived by the owner on Nov 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInMemDB.js
123 lines (107 loc) · 3.23 KB
/
InMemDB.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const DB = require('./DB.js')
class InMemDB extends DB {
constructor() {
super()
this.data = {
identities: [],
ideas: [],
connections: [],
identityId: 0,
focussedIdeaId: 0
}
this.generateRandomData()
}
generateRandomData(numberOfIdentities, ideasPerIdentity, connectionsPerIdentity) {
numberOfIdentities = numberOfIdentities || 3;
ideasPerIdentity = ideasPerIdentity || 40;
connectionsPerIdentity = connectionsPerIdentity || 60;
for(var identityCounter = 0; identityCounter < numberOfIdentities; identityCounter++) {
var identity = {
id: identityCounter,
name: 'Identity' + identityCounter
}
this.data.identities.push(identity)
for(var ideaCounter = 0; ideaCounter < ideasPerIdentity; ideaCounter++) {
var id = ideasPerIdentity * identityCounter + ideaCounter
var idea = {
id: id,
name: 'Idea' + id,
description: '',
importance: Math.random()
}
this.data.ideas.push(idea)
}
for(var connectionCounter = 0; connectionCounter < connectionsPerIdentity; connectionCounter++) {
var connection = {
id: connectionCounter + identityCounter * connectionsPerIdentity,
from: Math.floor(Math.random() * ideasPerIdentity * numberOfIdentities),
to: Math.floor(Math.random() * ideasPerIdentity * numberOfIdentities),
weight: Math.random()
}
this.data.connections.push(connection)
}
}
}
async getIdentity() {
return this.data.identities.find(i => i.id === this.data.identityId)
}
async getFocussedIdeaId() {
return this.data.ideas.find(i => i.id === this.data.focussedIdeaId).id
}
async focusIdea(ideaId) {
this.data.focussedIdeaId = ideaId
}
async searchIdea(searchString) {
this.data.ideas.filter(i => i.name.includes(searchString))
}
async getNearbyIdeasAndConnections(centerIdeaId, depth) {
var ideasIds = await this.getNearbyIdeasIds(centerIdeaId, depth)
var connections = await this.getRelevantConnections(ideasIds)
return {
ideas: (new Array(...ideasIds)).map(ideaId => this.data.ideas.find(i => i.id === ideaId)),
connections: connections
}
}
async getNearbyIdeasIds(centerIdeaId, depth) {
var nearbyIdeas = new Set([centerIdeaId])
for(var level = 0; level < depth; level++) {
console.log('logging nearby ideas level ' + level)
var previous = nearbyIdeas
nearbyIdeas = new Set(previous)
this.data.connections.forEach(
c => {
if(previous.has(c.from)) {
nearbyIdeas.add(c.to)
}
else if (previous.has(c.to)) {
nearbyIdeas.add(c.from)
}
}
)
}
return nearbyIdeas
}
async getRelevantConnections(ideasIds) {
return this.data.connections.filter(c => {
if(ideasIds.has(c.from) && ideasIds.has(c.to)){
console.log(c.from + ' - ' + c.to)
return true
} else {
return false
}
})
}
async addIdea() {
}
async addConnection() {
}
async updateIdea() {
}
async updateConnection() {
}
async rmIdea() {
}
async rmConnection() {
}
}
module.exports = InMemDB