Skip to content

Commit 9809504

Browse files
committed
seeded
1 parent 9bb5ac3 commit 9809504

1 file changed

Lines changed: 44 additions & 37 deletions

File tree

demo.html

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -169,38 +169,20 @@ <h1>Demo of String_random.js</h1>
169169
<script type="module">
170170
import { String_random } from './lib/String_random.js';
171171

172-
(function(window) {
173-
174-
// exit if the browser implements that event
175-
if ( "onhashchange" in window.document.body ) { return; }
176-
177-
var location = window.location,
178-
oldURL = location.href,
179-
oldHash = location.hash;
180-
181-
// check the location hash on a 100ms interval
182-
setInterval(function() {
183-
var newURL = location.href,
184-
newHash = location.hash;
185-
186-
// if the hash has changed and a handler has been bound...
187-
if ( newHash != oldHash && typeof window.onhashchange === "function" ) {
188-
// execute the handler
189-
window.onhashchange({
190-
type: "hashchange",
191-
oldURL: oldURL,
192-
newURL: newURL
193-
});
194-
195-
oldURL = newURL;
196-
oldHash = newHash;
197-
}
198-
}, 100);
199-
200-
})(window);
172+
function seededRandom(seed) {
173+
let x = seed >>> 0;
174+
return function me() {
175+
// Xorshift32アルゴリズム
176+
x ^= x << 13;
177+
x ^= x >>> 17;
178+
x ^= x << 5;
179+
me.seed = x;
180+
return (x >>> 0) / 0x100000000;
181+
};
182+
}
201183

202184

203-
function generate (re) {
185+
function generate (re, seed) {
204186
var input = document.querySelector('input[name=regexp]');
205187
var output = document.getElementById('output');
206188
output.innerHTML = '';
@@ -210,23 +192,33 @@ <h1>Demo of String_random.js</h1>
210192
if (!re) return;
211193
}
212194

195+
if (typeof seed === "undefined") seed = new Date().getMilliseconds();
196+
213197
input.value = re.source || re;
214-
location.hash = '#' + encodeURIComponent(input.value);
198+
199+
const params = new URLSearchParams();
200+
params.set('seed', seed);
201+
params.set('regexp', input.value);
202+
203+
location.hash = '#' + params.toString();
215204
document.title = input.value;
216205

217-
for (var i = 0; i < 10; i++) {
218-
var generated = String_random(re);
206+
console.log('seed:', seed);
207+
const random = seededRandom(seed);
208+
for (let i = 0; i < 10; i++) {
209+
const generated = String_random(re, { random });
219210

220-
var p = document.createElement('p');
211+
const p = document.createElement('p');
221212
p.textContent = generated;
222213
const params = new URLSearchParams();
223214
params.set('url', location.href);
224215
params.set('text', `${generated} #string_random_js`);
225216

226-
var url = `https://twitter.com/intent/tweet?${params.toString()}`;
217+
const url = `https://twitter.com/intent/tweet?${params.toString()}`;
227218

228-
var a = document.createElement('a');
219+
const a = document.createElement('a');
229220
a.href = url;
221+
a.target = '_blank';
230222
a.className = 'btn btn-sm btn-info twitter-share-button';
231223
a.textContent = 'Tweet';
232224
p.appendChild(a);
@@ -249,7 +241,22 @@ <h1>Demo of String_random.js</h1>
249241
});
250242

251243
window.onhashchange = function (a, b) {
252-
generate(decodeURIComponent(location.hash.slice(1)));
244+
const hash = location.hash.replace(/^#/, '');
245+
if (hash.match(/regexp=/)) {
246+
const params = new URLSearchParams(hash);
247+
const seed = parseInt(params.get('seed'), 10);
248+
if (params.has('regexp')) {
249+
if (seed) {
250+
generate(new RegExp(params.get('regexp')), seed);
251+
} else {
252+
generate(new RegExp(params.get('regexp')));
253+
}
254+
return;
255+
}
256+
} else {
257+
// backward compatibility
258+
generate(new RegExp(decodeURIComponent(hash)));
259+
}
253260
};
254261

255262
window.onhashchange();

0 commit comments

Comments
 (0)