8000 Optimize code style · HowProgrammingWorks/Graph@4e9c7d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4e9c7d1

Browse files
committed
Optimize code style
1 parent 820b594 commit 4e9c7d1

File tree

2 files changed

+44
-29
lines changed

2 files changed

+44
-29
lines changed

JavaScript/1-graph.js

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ class Vertex {
88
}
99
link(...args) {
1010
const distinct = new Set(args);
11-
const links = this.links;
12-
const keyField = this.graph.keyField;
11+
const { links } = this;
12+
const { keyField } = this.graph;
1313
for (const item of distinct) {
14-
const key = item.data[keyField];
15-
links.set(key, null);
14+
const value = item.data[keyField];
15+
links.set(value, item);
1616
}
1717
return this;
1818
}
@@ -23,7 +23,7 @@ class Cursor {
2323
this.vertices = vertices;
2424
}
2525
linked(...names) {
26-
const vertices = this.vertices;
26+
const { vertices } = this;
2727
const result = new Set();
2828
for (const vertex of vertices) {
2929
let condition = true;
@@ -53,7 +53,7 @@ class Graph {
5353
const vertices = new Set();
5454
for (const vertex of this.vertices.values()) {
5555
let condition = true;
56-
const data = vertex.data;
56+
const { data } = vertex;
5757
if (data) {
5858
for (const field in query) {
5959
condition = condition && data[field] === query[field];
@@ -69,21 +69,36 @@ class Graph {
6969

7070
const graph = new Graph('name');
7171

72-
const marcus = graph.add(
73-
{ name: 'Marcus Aurelius', city: 'Rome', born: 121, dynasty: 'Antonine' }
74-
);
75-
const lucius = graph.add(
76-
{ name: 'Lucius Verus', city: 'Rome', born: 130, dynasty: 'Antonine' }
77-
);
78-
const pius = graph.add(
79-
{ name: 'Antoninus Pius', city: 'Lanuvium', born: 86, dynasty: 'Antonine' }
80-
);
81-
const hadrian = graph.add(
82-
{ name: 'Hadrian', city: 'Santiponce', born: 76, dynasty: 'Nerva–Trajan' }
83-
);
84-
const trajan = graph.add(
85-
{ name: 'Trajan', city: 'Sevilla', born: 98, dynasty: 'Nerva–Trajan' }
86-
);
72+
const marcus = graph.add({
73+
name: 'Marcus Aurelius',
74+
city: 'Rome',
75+
born: 121,
76+
dynasty: 'Antonine',
77+
});
78+
const lucius = graph.add({
79+
name: 'Lucius Verus',
80+
city: 'Rome',
81+
born: 130,
82+
dynasty: 'Antonine',
83+
});
84+
const pius = graph.add({
85+
name: 'Antoninus Pius',
86+
city: 'Lanuvium',
87+
born: 86,
88+
dynasty: 'Antonine',
89+
});
90+
const hadrian = graph.add({
91+
name: 'Hadrian',
92+
city: 'Santiponce',
93+
born: 76,
94+
dynasty: 'Nerva–Trajan',
95+
});
96+
const trajan = graph.add({
97+
name: 'Trajan',
98+
city: 'Sevilla',
99+
born: 98,
100+
dynasty: 'Nerva–Trajan',
101+
});
87102

88103
marcus.link(lucius);
89104
lucius.link(trajan, marcus, marcus, marcus);

JavaScript/2-insert-link-to.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ class Vertex {
88
}
99
link(...args) {
1010
const distinct = new Set(args);
11-
const links = this.links;
12-
const keyField = this.graph.keyField;
11+
const { links } = this;
12+
const { keyField } = this.graph;
1313
for (const item of distinct) {
1414
const key = item.data[keyField];
15-
links.set(key, null);
15+
links.set(key, item);
1616
}
1717
return this;
1818
}
@@ -23,7 +23,7 @@ class Cursor {
2323
this.vertices = vertices;
2424
}
2525
linked(...names) {
26-
const vertices = this.vertices;
26+
const { vertices } = this;
2727
const result = new Set();
2828
for (const vertex of vertices) {
2929
let condition = true;
@@ -53,7 +53,7 @@ class Graph {
5353
const vertices = new Set();
5454
for (const vertex of this.vertices.values()) {
5555
let condition = true;
56-
const data = vertex.data;
56+
const { data } = vertex;
5757
if (data) {
5858
for (const field in query) {
5959
condition = condition && data[field] === query[field];
@@ -64,10 +64,10 @@ class Graph {
6464
return new Cursor(vertices);
6565
}
6666
link(source) {
67-
const vertices = this.vertices;
67+
const { vertices } = this;
68+
const from = vertices.get(source);
6869
return {
6970
to(...destinations) {
70-
const from = vertices.get(source);
7171
if (from) {
7272
destinations.forEach(destination => {
7373
const target = vertices.get(destination);
@@ -93,7 +93,7 @@ graph.insert([
9393
{ name: 'Lucius Verus', city: 'Rome', born: 130, dynasty: 'Antonine' },
9494
{ name: 'Antoninus Pius', city: 'Lanuvium', born: 86, dynasty: 'Antonine' },
9595
{ name: 'Hadrian', city: 'Santiponce', born: 76, dynasty: 'Nerva–Trajan' },
96-
{ name: 'Trajan', city: 'Sevilla', born: 98, dynasty: 'Nerva–Trajan' }
96+
{ name: 'Trajan', city: 'Sevilla', born: 98, dynasty: 'Nerva–Trajan' },
9797
]);
9898

9999
graph.link('Marcus Aurelius').to('Lucius Verus');

0 commit comments

Comments
 (0)
0