Skip to content

Commit f54a093

Browse files
committed
better code
1 parent ecf3ad9 commit f54a093

File tree

2 files changed

+24
-35
lines changed

2 files changed

+24
-35
lines changed

dist/app.js

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
//var db = new PouchDB('teletext');
22

33
var justify = function(str, len) {
4-
54
var re = RegExp("(?:\\s|^)(.{1," + len + "})(?=\\s|$)", "g");
65
var res = [];
76
var finalResult = [];
8-
97
while ((m = re.exec(str)) !== null) {
108
res.push(m[1]);
119
}
12-
1310
for (var i = 0; i < res.length - 1; i++){
1411
if(res[i].indexOf(' ') != -1){
1512
while(res[i].length < len){
@@ -24,9 +21,7 @@ var justify = function(str, len) {
2421
}
2522
finalResult.push(res[i]);
2623
}
27-
2824
finalResult.push(res[res.length - 1]);
29-
3025
let first = true
3126
finalResult = finalResult.map((l) => {
3227
if (first) {
@@ -36,9 +31,7 @@ var justify = function(str, len) {
3631
return ' ' + l
3732
}
3833
})
39-
4034
return finalResult.join('\n');
41-
4235
}
4336

4437
var app = new Vue({
@@ -49,9 +42,7 @@ var app = new Vue({
4942
date: '',
5043
time: '',
5144
progress: 0,
52-
stories: {},
53-
subsetOfStories: {},
54-
story: {}
45+
stories: []
5546
},
5647
methods: {
5748
next: () => {
@@ -98,34 +89,28 @@ var app = new Vue({
9889
computed: {
9990
paddedPageStr: function() {
10091
return this.pageStr.padEnd(3, '_')
92+
},
93+
subsetOfStories: function() {
94+
if (this.page < 101 || this. page >= 200) {
95+
return []
96+
}
97+
const start = 10 * (this.page - 101)
98+
return this.stories.slice(start, start + 10)
99+
},
100+
story: function() {
101+
if (this.page.toString().length !== 3) {
102+
return null
103+
}
104+
if (this.page >= 200) {
105+
return this.stories[this.page - 200]
106+
}
107+
return null
101108
}
102109
},
103110
created() {
104111
window.addEventListener('keydown', async (e) => {
105112
app.key(e)
106113
});
107-
},
108-
watch: {
109-
page: function() {
110-
if (this.page.toString().length !== 3) {
111-
return
112-
}
113-
this.subsetOfStories = {}
114-
this.story = {}
115-
if (this.page == 100) {
116-
return
117-
}
118-
if (this.page >= 200) {
119-
this.story = this.stories[this.page]
120-
} else {
121-
const start = 200 + (this.page - 101) * 10
122-
for(var i = start; i < start + 10; i++) {
123-
this.subsetOfStories[i] = this.stories[i]
124-
this.subsetOfStories[i].justified = justify(this.subsetOfStories[i].description, 39)
125-
this.subsetOfStories[i].justifiedTitle = justify(this.subsetOfStories[i].title, 39)
126-
}
127-
}
128-
}
129114
}
130115
})
131116

@@ -160,7 +145,10 @@ const startup = async function() {
160145
story.url = story.link
161146
story.shorturl = story.link.substr(0, 20)
162147
story.time = story.pubDate
163-
app.stories[id] = story
148+
story.justified = justify(story.description, 39)
149+
story.justifiedTitle = justify(story.title, 39)
150+
story.id = id
151+
app.stories.push(story)
164152
app.progress++
165153
id++
166154
if (id >= 300) {

dist/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@
3737

3838
<span v-if="page>100 && page <= 110">
3939
&#xE2C0;
40-
<span v-for="story,id in subsetOfStories">
41-
<a v-bind:href="'JavaScript:app.goto(' + id + ')'" class="white greenbg">{{ id }}</a> <span class="yellow" v-if="story">{{ story.title.substr(0,35) }}</span>&#xE2C0;
40+
<span v-for="story in subsetOfStories">
41+
<a v-bind:href="'JavaScript:app.goto(' + story.id + ')'" class="white greenbg">{{ story.id }}</a> <span class="yellow" v-if="story">{{ story.title.substr(0,35) }}</span>&#xE2C0;
4242
</span>
4343
&#xE2C0;
4444
<span v-if="page > 101"><a href="JavaScript:app.previous()" class="white greenbg">{{page - 1}}</a> Previous</span>
4545
<span v-if="page < 110"><a href="JavaScript:app.next()" class="white greenbg">{{page + 1}}</a> Next</span>
4646
</span>
47+
4748

4849
<span v-if="page>=200 && page<300 && story">
4950
&#xE2C0;

0 commit comments

Comments
 (0)