mirror of
https://github.com/pezkuwichain/pezkuwi-ui.git
synced 2026-10-20 18:56:22 +00:00
The header rule in @pezkuwi/dev requires the current year. Year range only; the third-party beachball LICENSE is left as it is.
31 lines
676 B
TypeScript
31 lines
676 B
TypeScript
// Copyright 2017-2026 @pezkuwi/ui-shared authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type { Seeder } from './types.js';
|
|
|
|
import { isU8a, stringToU8a } from '@pezkuwi/util';
|
|
|
|
const DIVISOR = 256 * 256;
|
|
|
|
export function seeder (_seed: string | Uint8Array = new Uint8Array(32)): Seeder {
|
|
const seed: Uint8Array = isU8a(_seed)
|
|
? _seed
|
|
: stringToU8a(_seed);
|
|
|
|
let index = (seed[Math.floor(seed.length / 2)] % seed.length) - 1;
|
|
|
|
const next = (): number => {
|
|
index += 1;
|
|
|
|
if (index === seed.length) {
|
|
index = 0;
|
|
}
|
|
|
|
return seed[index];
|
|
};
|
|
|
|
return (): number => {
|
|
return ((next() * 256) + next()) / DIVISOR;
|
|
};
|
|
}
|