Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random function is not good for tree-shaking #1144

Closed
ryoppippi opened this issue Jul 3, 2024 · 1 comment
Closed

random function is not good for tree-shaking #1144

ryoppippi opened this issue Jul 3, 2024 · 1 comment

Comments

@ryoppippi
Copy link
Contributor

ryoppippi commented Jul 3, 2024

Feature Request

Related to: #1069
After several PRs including #1133, the bundle size is decreasing !!

So basic functions like is or assert is well tree-shaked.

However, random generator is little bit hard to tree-shaking.

Here is why.

suppose you create a random function like this

import typia, { type tags } from "typia";

export const random = typia.createRandom<IMember>();

interface IMember {
  email: string & tags.Format<"email">;
}

this is compiled like this.

const RandomGenerator = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  array,
  bigint,
  boolean,
  byte,
  date,
  datetime,
  duration,
  email,
  hostname,
  idnEmail,
  idnHostname,
  integer,
  ipv4,
  ipv6,
  iri,
  iriReference,
  jsonPointer,
  length,
  number,
  password,
  pattern,
  pick,
  regex,
  relativeJsonPointer,
  string,
  time,
  uri,
  uriReference,
  uriTemplate,
  url,
  uuid
}, Symbol.toStringTag, { value: "Module" }));
const random$2 = () => ({
  generator: RandomGenerator,
  pick
});
function random$1() {
  halt("random");
}
const randomPure = /* @__PURE__ */ Object.assign(
  random$1,
  /* @__PURE__ */ random$2()
);
function createRandom() {
  halt("createRandom");
}
const createRandomPure = /* @__PURE__ */ Object.assign(createRandom, randomPure);
function halt(name) {
  throw new Error(`Error on typia.${name}(): no transform has been configured. Read and follow https://typia.io/docs/setup please.`);
}
const random = (generator) => {
  const $generator = createRandomPure.generator;
  const $ro0 = (_recursive = false, _depth = 0) => {
    var _a, _b;
    return {
      email: ((_b = (_a = $generator.customs) == null ? void 0 : _a.string) == null ? void 0 : _b.call(_a, [
        {
          name: 'Format<"email">',
          kind: "format",
          value: "email"
        }
      ])) ?? (0, $generator.email)()
    };
  };
  return $ro0();
};

The implementation is here.
https://github.com/ryoppippi/typia/blob/b09a027d958fff0e384d4b6cae16f362fa4d6887/src/functional/Namespace/index.ts?plain=1#L73-L76

If this random function will be an exports like this

export { generator, pick }

and generater call method should be
random$2().generator.email

random$2.generator.email

It is really great for tree-shaking.

I think this needs change APIs a little bit, and I don't know if I can tackle this change as I don't fully understand the API yet.

I also think that this should be included in v7 because this change is API level and users need re-compile

@samchon
Copy link
Owner

samchon commented Nov 15, 2024

This problem would be cleared fixed in the next v7 update, and it is coming. Only LLm schema features are left.

@samchon samchon closed this as completed Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants