The namegen5 website.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.3 KiB

3 years ago
  1. import wasm from "./Cargo.toml";
  2. let rust = null;
  3. const promise = wasm().then((res) => {
  4. console.log(res);
  5. rust = res;
  6. }).catch(err => {
  7. console.error("Rust failed to load.")
  8. console.error("Error:", err);
  9. return Promise.reject(err);
  10. })
  11. export function load() {
  12. return promise
  13. }
  14. export default class NameGenerator {
  15. constructor(data) {
  16. console.log(rust);
  17. if (data != null) {
  18. this._gen = rust.WasmNameGenerator.load(JSON.stringify(data));
  19. } else {
  20. this._gen = rust.WasmNameGenerator.new();
  21. }
  22. }
  23. get data() {
  24. return this._gen.data();
  25. }
  26. generate(formatName, seed) {
  27. return this._gen.generate_one(formatName, seed);
  28. }
  29. generateMany(formatName, amount, seed) {
  30. return this._gen.generate_many(formatName, amount, seed);
  31. }
  32. addPart(options) {
  33. return this._gen.add_part(options);
  34. }
  35. addFormat(formatName, formatStr) {
  36. return this._gen.add_format(formatName, formatStr);
  37. }
  38. learn(partName, sampleSet) {
  39. try {
  40. this._gen.learn(partName, sampleSet)
  41. } catch(err) {
  42. throw new Error(err);
  43. }
  44. }
  45. toJSON() {
  46. return this.data;
  47. }
  48. free() {
  49. this._gen.free();
  50. }
  51. }