{"size": 2102, "ext": "js", "max_stars_repo_path": "src/index.js", "max_stars_repo_name": "taystack/use-leet", "max_stars_repo_stars_event_min_datetime": "2020-02-16T01:31:14.000Z", "max_stars_repo_stars_event_max_datetime": "2020-11-22T08:09:34.000Z", "max_issues_repo_path": "src/index.js", "max_issues_repo_name": "taystack/use-leet", "max_issues_repo_issues_event_min_datetime": "2021-03-10T08:00:09.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T23:46:13.000Z", "max_forks_repo_path": "src/index.js", "max_forks_repo_name": "taystack/use-leet", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": 8, "max_forks_count": null, "avg_line_length": 27.2987012987, "max_line_length": 107, "alphanum_fraction": 0.6921979068}
import { useState, useMemo } from "react";
import { defaultLeetMap } from "./config";
import useGeneratedLeet from "./useGeneratedLeet";


export default (global.UseLeet || (global.UseLeet = new UseLeetGlobal()));

function UseLeetGlobal() {
  this.map = { ...defaultLeetMap };
  this.generator = false;
}

export class UseLeetError extends Error {
  constructor(msg, arg) {
    const str = `${msg} - you provided`;
    try { super(`${str} ${arg.toString()}`);
    } catch(e) { super(`${str} something... something unprintable.`); }
  }
}

export class UseLeetCustomMapError extends UseLeetError {
  constructor(arg) { super("UseLeet expects customMap to be of a shape { CHAR_A: CHAR_A_RETURNS }", arg); }
}

export class UseLeetCustomGeneratorError extends UseLeetError {
  constructor(arg) {
    super("UseLeet expects customGenerator to be of type FUNC", arg);
  }
}

UseLeetGlobal.prototype.setMap = function(customLeetMap) {
  if (customLeetMap.constructor !== Object) throw new UseLeetCustomMapError(customLeetMap);
  Object.keys(customLeetMap).forEach(k => {
    if (typeof customLeetMap[k] !== "string") throw new UseLeetCustomMapError(customLeetMap[k]);
  });
  this.map = {
    ...defaultLeetMap,
    ...customLeetMap,
  };
}

UseLeetGlobal.prototype.setGenerator = function(generator) {
  if (typeof generator !== "function") throw new UseLeetCustomGeneratorError(generator);
  this.generator = generator;
}

UseLeetGlobal.prototype.resetGenerator = function() {
  this.generator = false;
}

UseLeetGlobal.prototype.getMap = function() {
  return this.map;
}

UseLeetGlobal.prototype.resetMap = function() {
  this.map = { ...defaultLeetMap };
}

export function useLeet( defaultValue = "", custom = {} ) {

  // Store the plain text stuff
  const [ value, setValue ] = useState(defaultValue);

  // Memoize the map used for leet-ifying
  const leetMap = useMemo(() => {
    return {
      ...UseLeet.getMap(),
      ...custom,
    };
  }, [custom]);

  // Memoize the generated leet
  const leet = useGeneratedLeet(value, leetMap, UseLeet.generator);

  return [ value, setValue, leet ];
}
