{"id":16231513,"url":"https://github.com/hustcc/variable-type","last_synced_at":"2025-03-19T14:30:38.577Z","repository":{"id":57390850,"uuid":"98947293","full_name":"hustcc/variable-type","owner":"hustcc","description":"👏  ~ 1 kb. Schema validation. 一个只有 1 kb 的用于变量结构校验的库。","archived":false,"fork":false,"pushed_at":"2020-01-13T01:52:06.000Z","size":105,"stargazers_count":24,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-17T07:51:31.356Z","etag":null,"topics":["checking","prop-types","type-check","variable","variable-type"],"latest_commit_sha":null,"homepage":"http://git.hust.cc/variable-type","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/hustcc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":null,"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":["https://paypal.me/hustcc","https://atool.vip"]}},"created_at":"2017-08-01T01:50:59.000Z","updated_at":"2024-12-25T09:41:51.000Z","dependencies_parsed_at":"2022-09-26T16:51:05.189Z","dependency_job_id":null,"html_url":"https://github.com/hustcc/variable-type","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hustcc%2Fvariable-type","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hustcc%2Fvariable-type/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hustcc%2Fvariable-type/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hustcc%2Fvariable-type/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hustcc","download_url":"https://codeload.github.com/hustcc/variable-type/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244444438,"owners_count":20453724,"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","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":["checking","prop-types","type-check","variable","variable-type"],"created_at":"2024-10-10T13:05:54.198Z","updated_at":"2025-03-19T14:30:38.293Z","avatar_url":"https://github.com/hustcc.png","language":"TypeScript","readme":"# variable-type\n\n\u003e A high-performance javascript(`less then 1 kb`) library, runtime **type checking** for variable and similar objects.\n\u003e \n\u003e 一个非常简单的（仅 1 kb）高性能的用于做变量结构校验的 JavaScript 模块。\n\u003e \n\u003e Inspired by [prop-types](https://github.com/facebook/prop-types).\n\n[![npm Version](https://img.shields.io/npm/v/variable-type.svg)](https://www.npmjs.com/package/variable-type)\n[![Build Status](https://github.com/hustcc/variable-type/workflows/build/badge.svg)](https://github.com/hustcc/variable-type/actions)\n[![Coverage Status](https://coveralls.io/repos/github/hustcc/variable-type/badge.svg?branch=master)](https://coveralls.io/github/hustcc/variable-type?branch=master)\n[![npm download](https://img.shields.io/npm/dm/variable-type.svg)](https://www.npmjs.com/package/variable-type)\n[![npm License](https://img.shields.io/npm/l/variable-type.svg)](https://www.npmjs.com/package/variable-type)\n\n\n\n## 1. Install\n\n\u003e **npm i --save variable-type**\n\nThen import it.\n\n```ts\nimport VT from 'variable-type';\n```\n\n\n## 2. API \u0026 Types\n\nBefore use it to check variable, you should make your Types.\n \nAnd the library contains `Types` below:\n\n - **VT.bool**\n - **VT.func**\n - **VT.number**\n - **VT.string**\n - **VT.object**\n - **VT.array**\n - **VT.any**\n - **VT.null**\n - **VT.undefined**\n - **VT.instanceOf(Class)**\n - **VT.typeOf(String)**\n - **VT.in(Array)**\n - **VT.arrayOf(Type)**\n - **VT.shape(TypeObject)**\n - **VT.and(TypeArray)**\n - **VT.or(TypeArray)**\n - **VT.not(Type)**\n - **VT.apply(Function)**\n\nThe `Type` has 2 API:\n\n - **check(value)**\n - **optional()**: convent the type into optional.\n\nYou can see all the usage in the [test cases file](tests/test.ts).\n\nIf more Types are needed, welcome to `send a pull request`, or put an issue to me.\n\n\n## 3. Usage examples\n\nHere is some examples. More you can see in [test.ts](tests/test.ts) file.\n\n - Simple usage\n\n```js\nVT.number.check(1992);\nVT.string.check('hustcc');\nVT.func.check(Math.min);\nVT.bool.check(true);\nVT.object.check({});\nVT.array.check([1, 2, 3]);\nVT.null.check(null);\nVT.undefined.check(undefined);\nVT.instanceOf(Date).check(new Date());\nVT.in(['hustcc', 'hust', 'cc']).check('hustcc');\n```\n\n - And / Or / Not\n\n ```js\nVT.not(VT.in(['hustcc', 'cc'])).check('hustcc');\n\nVT.and([\n\tVT.string\n\tVT.in(['hustcc', 1992]),\n]).check('hustcc');\n\nVT.or([\n\tVT.number,\n\tVT.string,\n]).check('hustcc');\n ```\n\n - `Array` type.\n\n```js\nconst arr = ['hello', 'world', 25, new Date(1992, 8, 1)];\n \nconst types = VT.arrayOf(\n  VT.or([\n    VT.number,\n    VT.string,\n    VT.instanceOf(Date)\n  ])\n);\n\ntypes.check(arr); // will get true. \n```\n\n - `Object` type.\n\n```js\nconst obj = {\n  name: 'hustcc',\n  boy: true,\n  birthday: new Date(1992, 8, 1)\n};\n \nconst types = VT.shape({\n  name: VT.string,\n  boy: VT.bool,\n  birthday: VT.instanceOf(Date)\n});\n\ntypes.check(obj); // will get true. \n```\n\n - `Complex` example.\n\n```js\n\n// The only API `check`.\nVT.shape({\n  a: VT.bool,\n  b: VT.number,\n  c: VT.string,\n  d: VT.func,\n  e: VT.instanceOf(Date),\n  f: VT.in([1, '1']),\n  g: VT.shape({\n    h: VT.or([\n      VT.shape({\n        i: VT.arrayOf(\n          VT.or([\n            VT.number,\n            VT.string,\n            VT.bool,\n            VT.shape({\n              j: VT.func\n            })\n          ])\n        )\n      })\n    ])\n  })\n}).check({\n  a: true,\n  b: 1,\n  c: 'str',\n  d: function() {},\n  e: new Date(),\n  f: '1',\n  g: {\n    h: {\n      i: [\n        '1',\n        2,\n        true,\n        {\n          j: function() {}\n        }\n      ]\n    }\n  }\n}); // Then will get true.\n```\n\n - Optional type\n\n```js\nVT.shape({\n  name: VT.string,\n  birthday: VT.string,\n  sex: VT.string.optional()\n}).check({\n  name: 'hustcc',\n  birthday: '1992-08-01'\n}); // Then will get true.\n```\n\n\n## 4. Test \u0026 Perf\n\n```\n# install dependence\n$ npm i\n\n# run unit test\n$ npm run test\n\n# run performance test\n$ npm run perf\n```\n\n\u003e [OPS] variable-type / prop-types = 5.033\n\n\n## License\n\nMIT@[hustcc](https://github.com/hustcc).\n\n\n","funding_links":["https://paypal.me/hustcc","https://atool.vip"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhustcc%2Fvariable-type","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhustcc%2Fvariable-type","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhustcc%2Fvariable-type/lists"}