{"id":23219140,"url":"https://github.com/fizzyelt/fakerator","last_synced_at":"2025-08-19T08:32:47.754Z","repository":{"id":229364366,"uuid":"775566118","full_name":"FizzyElt/fakerator","owner":"FizzyElt","description":"generate fake data fn","archived":false,"fork":false,"pushed_at":"2025-08-18T16:28:53.000Z","size":275,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-18T16:53:37.624Z","etag":null,"topics":["fake-data","fake-data-generator","javascript","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/struct-fakerator","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FizzyElt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-03-21T16:16:38.000Z","updated_at":"2025-08-18T16:28:56.000Z","dependencies_parsed_at":"2024-12-18T21:19:55.627Z","dependency_job_id":"e25534d7-b9f8-40db-8f4d-7912b42fd4e5","html_url":"https://github.com/FizzyElt/fakerator","commit_stats":null,"previous_names":["fizzyelt/fakerator"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/FizzyElt/fakerator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FizzyElt%2Ffakerator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FizzyElt%2Ffakerator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FizzyElt%2Ffakerator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FizzyElt%2Ffakerator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FizzyElt","download_url":"https://codeload.github.com/FizzyElt/fakerator/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FizzyElt%2Ffakerator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271121770,"owners_count":24702871,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-19T02:00:09.176Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fake-data","fake-data-generator","javascript","typescript"],"created_at":"2024-12-18T21:19:51.780Z","updated_at":"2025-08-19T08:32:47.743Z","avatar_url":"https://github.com/FizzyElt.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 假資料結構產生器\n\n- [假資料結構產生器](#假資料結構產生器)\n  - [Wrapping fakerjs Functions](#wrapping-fakerjs-functions)\n  - [用途](#用途)\n    - [Value](#value)\n    - [Selection](#selection)\n    - [Object](#object)\n    - [Array](#array)\n    - [Tuple](#tuple)\n    - [BoundedSeries](#boundedseries)\n  - [擴充](#擴充)\n\n## Wrapping fakerjs Functions\n\n- [X] Airline\n- [X] Animal\n- [X] Color\n- [X] Commerce\n- [X] Company\n- [X] Database\n- [X] Datatype\n- [X] Date(Datetime)\n- [X] Finance\n- [X] Food\n- [X] Git\n- [X] Hacker\n- [X] Image\n- [X] Internet\n- [X] Location\n- [X] Lorem\n- [X] Music\n- [X] Number\n- [X] Person\n- [X] Phone\n- [X] Science\n- [X] String\n- [X] System\n- [X] Vehicle\n\n## 用途\n\n使用 faker js 時是單一純值不能產生結構，需要自己手動組合結構，此專案利用撰寫設定檔的方式來產生一個特定的假資料函數，減少手動組合結構的麻煩。\n\n```typescript\nimport { StructConfig } from 'struct-fakerator'\n\nconst scheme = StructConfig.object({\n  name: StructConfig.value(() =\u003e 'hello'),\n  enum: StructConfig.select([1, 2, 3]),\n  list: StructConfig.array(StructConfig.value(() =\u003e 10), 5),\n  tuple: StructConfig.tuple([StructConfig.value(() =\u003e 'tuple1'), StructConfig.value(() =\u003e '100')])\n})\n\n// or\nimport {\n  createObjectConfig,\n  createArrayConfig,\n  createValueConfig,\n  createTupleConfig,\n  createSelectionConfig\n} from 'struct-fakerator';\n\n\nconst scheme = createObjectConfig({\n  name: createValueConfig(() =\u003e 'hello'),\n  enum: createSelectionConfig([1, 2, 3]),\n  list: createArrayConfig(\n    createValueConfig(() =\u003e 10),\n    5\n  ),\n  tuple: createTupleConfig([\n    createValueConfig(() =\u003e 'tuple1'),\n    createValueConfig(() =\u003e 100),\n    createValueConfig(() =\u003e false),\n  ])\n});\n\n// or\nconst scheme = {\n  type: 'obj',\n  content: {\n    name: {\n      type: 'value',\n      generateFn: () =\u003e 'hello',\n    },\n    enum: {\n      type: 'select',\n      items: [1, 2, 3],\n    },\n    list: {\n      type: 'arr',\n      item: {\n        type: 'value',\n        generateFn: () =\u003e 10,\n      },\n      len: 5,\n    },\n    tuple: {\n      type: 'tuple',\n      configItems: [\n        {\n          type: 'value',\n          generateFn: () =\u003e 'tuple1',\n        },\n        {\n          type: 'value',\n          generateFn: () =\u003e 100,\n        },\n        {\n          type: 'value',\n          generateFn: () =\u003e false,\n        },\n      ],\n    },\n  },\n};\n```\n\n```typescript\nimport { StructGenerator } from 'struct-fakerator';\n\nconst result = StructGenerator.genFn(scheme)()\n\nconsole.log(result);\n// {\n//   name: 'hello',\n//   enum: 1, // or 2 or 3\n//   list: [10, 10, 10, 10, 10],\n//   tuple: ['tuple1', 100, false]\n// }\n\n```\n\n\n### Value\n\n```typescript\nconst generateFn = StructGenerator.genFn(StructConfig.value(() =\u003e 10));\n\nconsole.log(generateFn());\n// 10\n```\n\n### Selection\n\n```typescript\nconst generateFn = StructGenerator.genFn(StructConfig.select([1, 2, 3, 4, 5]))\n\nconsole.log(generateFn());\n// 1 or 2 or 3 or 4 or 5\n```\n\n### Object\n\n```typescript\nconst generateFn = StructGenerator.genFn(StructConfig.object({\n  name: StructConfig.value(() =\u003e 'hello'),\n  list: StructConfig.array(StructConfig.value(() =\u003e 10), 5),\n}));\n\nconsole.log(generateFn());\n// {\n//   name: 'hello',\n//   list: [10, 10, 10, 10, 10]\n// }\n\n// with transformer function\nconst generateFn = StructGenerator.genFn(\n  StructConfig.object(\n    {\n      name: StructConfig.value(() =\u003e 'hello'),\n      list: StructConfig.array(\n        StructConfig.value(() =\u003e 10),\n        5\n      ),\n    },\n    ({ name, list }) =\u003e list.map((item) =\u003e `${name} ${item}`)\n  )\n);\n\nconsole.log(generateFn());\n// [\"hello 10\", \"hello 10\", \"hello 10\", \"hello 10\", \"hello 10\"]\n```\n\n### Array\n\n```typescript\nconst generateFn = StructGenerator.genFn(\n  StructConfig.array(\n    StructConfig.value(() =\u003e 10),\n    5\n  )\n);\n\nconsole.log(generateFn());\n// [10, 10, 10, 10, 10]\n\n// with next function\nconst generateFn = StructGenerator.genFn(\n  StructConfig.array(\n    StructConfig.value(() =\u003e 10),\n    5,\n    (prev, current) =\u003e prev + 1 + current\n  )\n);\n\nconsole.log(generateFn());\n// [21, 32, 43, 54, 65]\n```\n\n### Tuple\n\n```typescript\nconst generateFn = StructGenerator.genFn(\n  StructConfig.tuple([\n    StructConfig.value(() =\u003e 'tuple1'),\n    StructConfig.value(() =\u003e 100),\n    StructConfig.value(() =\u003e false),\n  ])\n);\n\nconsole.log(generateFn());\n// ['tuple1', 100, false]\n```\n\n### BoundedSeries\n\n```typescript\nconst generateFn = StructGenerator.genFn(\n  StructConfig.boundedSeries({\n    upperLimit: 1.1,\n    lowerLimit: 0.9,\n    createInitValue: () =\u003e 100,\n    count: 20,\n  })\n);\n\nconsole.log(generateFn());\n// [100 * 0.9 \u003c= num \u003c= 100 * 1.1, \n//  prev * 0.9 \u003c= num \u003c= prev * 1.1,\n//  prev * 0.9 \u003c= num \u003c= prev * 1.1,\n//  ...] \n```\n\n## 擴充\n\n一切的值皆由 `value` 產生，可以自己創建各種不同亂數函數\n\n```mermaid\nflowchart TB\n value --\u003e int\n value --\u003e float\n value --\u003e string\n value --\u003e email\n value --\u003e other[...]\n```\n\n但並不是所有人情況都能自己手動建立函數，有可能是開放給別人使用的服務，沒辦法在使用方建立函數，這時 `createGeneratorByType` 第二個可以讓製作服務的人帶入自己的擴充，這樣這個函數就能接受更多種型態。\n\n```typescript\nconst createIntValueConfig = (option) =\u003e createValueConfig(() =\u003e faker.number.int(option));\nconst createEmailValueConfig = (option) =\u003e createValueConfig(() =\u003e faker.internet.email(option));\n\nconst customTypeMatch = (config) =\u003e {\n  if (config.type === \"int\") {\n    return createIntValueConfig(config.option);\n  }\n  if (config.type === \"email\") {\n    return createEmailValueConfig(config.option);\n  }\n\n  throw Error(\"error\");\n};\n\nconst config = {\n  type: \"obj\",\n  content: {\n    name: { type: \"value\", generateFn: () =\u003e \"John\" },\n    age: { type: \"int\" },\n    email: { type: \"email\" },\n  },\n};\n\nconst result = createGeneratorByType(config, customTypeMatch)();\n\nconsole.log(result);\n\n/*\n  {\n    name: \"John\",\n    age: 50,\n    email: \"xxx@example.com\",\n  }\n*/\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffizzyelt%2Ffakerator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffizzyelt%2Ffakerator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffizzyelt%2Ffakerator/lists"}