bahtrext
Version: 
BahtText Stringify
33 lines (28 loc) • 824 B
JavaScript
import BF from "./BF";
import defaultBulkBahtTextPat from "../const/defaultConfig/BulkBahtTextPat";
import InvalidType from "../const/error/InvalidType";
import ed from "../const/defaultConfig/ed";
export default (
  str,
  options = {
    pat: defaultBulkBahtTextPat,
    ed,
  }
) => {
  if (typeof str !== "string") return InvalidType;
  if (!str) return "";
  const { pat, ed } = options;
  let result = str;
  let regex =
    pat instanceof RegExp && !pat.global
      ? new RegExp(pat.source, pat.flags + "g")
      : pat;
  let match;
  while ((match = regex.exec(str)) !== null) {
    let number = match[0];
    const numMatch = number.match(/[\d,]+(\.\d+)?/);
    if (numMatch) number = numMatch[0];
    result = result.replace(match[0], BF(number, ed));
  }
  return result;
};