Skip to content

Commit 6b55a7a

Browse files
committed
Update style and optimise
1 parent 94154cf commit 6b55a7a

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

JavaScript/1-graph.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class Vertex {
1212
const { links } = this;
1313
const { keyField } = this.graph;
1414
for (const item of distinct) {
15-
const value = item.data[keyField];
16-
links.set(value, item);
15+
const key = item.data[keyField];
16+
links.set(key, item);
1717
}
1818
return this;
1919
}
@@ -30,7 +30,10 @@ class Cursor {
3030
for (const vertex of vertices) {
3131
let condition = true;
3232
for (const name of names) {
33-
condition = condition && vertex.links.has(name);
33+
if (!vertex.links.has(name)) {
34+
condition = false;
35+
break;
36+
}
3437
}
3538
if (condition) result.add(vertex);
3639
}
@@ -45,9 +48,10 @@ class Graph {
4548
}
4649

4750
add(data) {
48-
const vertex = new Vertex(this, data);
4951
const key = data[this.keyField];
50-
if (this.vertices.get(key) === undefined) {
52+
let vertex = this.vertices.get(key);
53+
if (!vertex) {
54+
vertex = new Vertex(this, data);
5155
this.vertices.set(key, vertex);
5256
}
5357
return vertex;
@@ -60,7 +64,10 @@ class Graph {
6064
const { data } = vertex;
6165
if (data) {
6266
for (const field in query) {
63-
condition = condition && data[field] === query[field];
67+
if (data[field] !== query[field]) {
68+
condition = false;
69+
break;
70+
}
6471
}
6572
if (condition) vertices.add(vertex);
6673
}

JavaScript/2-insert-link-to.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ class Cursor {
3030
for (const vertex of vertices) {
3131
let condition = true;
3232
for (const name of names) {
33-
condition = condition && vertex.links.has(name);
33+
if (!vertex.links.has(name)) {
34+
condition = false;
35+
break;
36+
}
3437
}
3538
if (condition) result.add(vertex);
3639
}
@@ -45,9 +48,10 @@ class Graph {
4548
}
4649

4750
add(data) {
48-
const vertex = new Vertex(this, data);
4951
const key = data[this.keyField];
50-
if (this.vertices.get(key) === undefined) {
52+
let vertex = this.vertices.get(key);
53+
if (!vertex) {
54+
vertex = new Vertex(this, data);
5155
this.vertices.set(key, vertex);
5256
}
5357
return vertex;
@@ -60,7 +64,10 @@ class Graph {
6064
const { data } = vertex;
6165
if (data) {
6266
for (const field in query) {
63-
condition = condition && data[field] === query[field];
67+
if (data[field] !== query[field]) {
68+
condition = false;
69+
break;
70+
}
6471
}
6572
if (condition) vertices.add(vertex);
6673
}
@@ -73,13 +80,12 @@ class Graph {
7380
const from = vertices.get(source);
7481
return {
7582
to(...destinations) {
76-
if (from) {
77-
destinations.forEach((destination) => {
78-
const target = vertices.get(destination);
79-
if (target) from.link(target);
80-
});
83+
if (!from) return;
84+
for (const destination of destinations) {
85+
const target = vertices.get(destination);
86+
if (target) from.link(target);
8187
}
82-
}
88+
},
8389
};
8490
}
8591

JavaScript/3-index.js

+18-15
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ class Cursor {
3232
for (const vertex of vertices.values()) {
3333
let condition = true;
3434
for (const name of names) {
35-
condition = condition && vertex.links.has(name);
35+
if (!vertex.links.has(name)) {
36+
condition = false;
37+
break;
38+
}
3639
}
3740
if (condition) result.add(vertex);
3841
}
@@ -48,9 +51,10 @@ class Graph {
4851
}
4952

5053
add(data) {
51-
const vertex = new Vertex(this, data);
5254
const key = data[this.keyField];
53-
if (this.vertices.get(key) === undefined) {
55+
let vertex = this.vertices.get(key);
56+
if (!vertex) {
57+
vertex = new Vertex(this, data);
5458
this.vertices.set(key, vertex);
5559
}
5660
return vertex;
@@ -81,10 +85,10 @@ class Graph {
8185
link(from) {
8286
return {
8387
to(...destinations) {
84-
destinations.forEach((target) => {
85-
if (target) from.link(target);
86-
});
87-
}
88+
for (const destination of destinations) {
89+
from.link(destination);
90+
}
91+
},
8892
};
8993
}
9094

@@ -95,15 +99,14 @@ class Graph {
9599
vertices.push(vertex);
96100
const keys = Object.keys(record);
97101
for (const [key, idx] of this.indices) {
98-
if (keys.includes(key)) {
99-
const value = record[key];
100-
let records = idx.get(value);
101-
if (!records) {
102-
records = new Set();
103-
idx.set(value, records);
104-
}
105-
records.add(vertex);
102+
if (!keys.includes(key)) continue;
103+
const value = record[key];
104+
let records = idx.get(value);
105+
if (!records) {
106+
records = new Set();
107+
idx.set(value, records);
106108
}
109+
records.add(vertex);
107110
}
108111
}
109112
return vertices;

0 commit comments

Comments
 (0)