-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistory.js
More file actions
executable file
·89 lines (74 loc) · 1.94 KB
/
History.js
File metadata and controls
executable file
·89 lines (74 loc) · 1.94 KB
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
#! /usr/bin/jsc
"use strict"
var console = {
log: function (...rest) { print(rest) }
}
function getline() { // readln gets
let l = readline();
if (l) return l;
quit();
}
// Emulate the window.localStorage HTML Web Storage API
//
let store = Object.create(null);
let localStorage = {
store,
setItem(k,v) {this.store[k] = v; return v},
getItem(k) {return this.store[k]}
};
// BEGIN
class History { // a wrapper around Web Storage API
constructor() {
let keystr = localStorage.getItem(History.key); // returns string of previously gen'd books
this.prev = keystr ? keystr.split('+') : []; // Split into an array (if truthy)
}
_strRep(c) { // STRing REPresentation, intended only for internal use
return c ? c.toString() : "void";
}
isPresent(c) {
c = this._strRep(c); // expecting a string, but make damn sure
return this.prev.indexOf(c) >= 0;
}
getHist() {
return localStorage.getItem(History.key);
}
add(c) {
c = this._strRep(c); // expecting a string, but make damn sure
if (this.isPresent(c))
return null;
this.prev.unshift(c);
this.prev.length = Math.min(History.limit, this.prev.length);
return localStorage.setItem(History.key, this.prev.join('+'));
}
}
History.limit = 600;
History.key = `dont-repeat-last-${History.limit}-random-gamebooks`;
// END
let HI_STANDALONE = 1;
if (HI_STANDALONE) {
var hh = new History();
let x,y,z;
hh.add("one");
hh.add("one");
hh.add("two");
hh.add("one");
hh.add("two");
hh.add("one");
hh.add("two");
hh.add("two");
//hh.add("three");
hh.add();
hh.add("four");
x = hh.getHist();
console.log(x);
hh.add("five");
x = hh.getHist();
console.log(x);
x = hh.isPresent();
y = hh.isPresent("nosuch");
z = hh.isPresent("four");
console.log(x, y, z);
for (let e in localStorage.store) {
console.log(e, localStorage.store[e]);
}
}