{"id":16784176,"url":"https://github.com/omgimalexis/upcast","last_synced_at":"2025-12-12T04:22:47.166Z","repository":{"id":6182547,"uuid":"7412834","full_name":"OmgImAlexis/upcast","owner":"OmgImAlexis","description":"A JavaScript type checking/casting library","archived":false,"fork":false,"pushed_at":"2020-10-29T23:47:53.000Z","size":439,"stargazers_count":74,"open_issues_count":12,"forks_count":7,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-08-09T18:21:45.187Z","etag":null,"topics":["casting-library","converts","type","type-checking","types","upcast"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/OmgImAlexis.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-01-02T20:32:02.000Z","updated_at":"2024-11-06T09:57:21.000Z","dependencies_parsed_at":"2022-08-31T01:00:10.488Z","dependency_job_id":null,"html_url":"https://github.com/OmgImAlexis/upcast","commit_stats":null,"previous_names":["rowanmanning/upcast"],"tags_count":17,"template":false,"template_full_name":null,"purl":"pkg:github/OmgImAlexis/upcast","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OmgImAlexis%2Fupcast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OmgImAlexis%2Fupcast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OmgImAlexis%2Fupcast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OmgImAlexis%2Fupcast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/OmgImAlexis","download_url":"https://codeload.github.com/OmgImAlexis/upcast/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OmgImAlexis%2Fupcast/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270016564,"owners_count":24512963,"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-12T02:00:09.011Z","response_time":80,"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":["casting-library","converts","type","type-checking","types","upcast"],"created_at":"2024-10-13T07:53:53.756Z","updated_at":"2025-12-12T04:22:47.122Z","avatar_url":"https://github.com/OmgImAlexis.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Upcast [![Build Status](https://travis-ci.org/OmgImAlexis/upcast.svg?branch=master)](https://travis-ci.org/OmgImAlexis/upcast) [![Coverage Status](https://codecov.io/gh/OmgImAlexis/upcast/branch/master/graph/badge.svg)](https://codecov.io/gh/OmgImAlexis/upcast) [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FOmgImAlexis%2Fupcast.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2FOmgImAlexis%2Fupcast?ref=badge_shield)\n\n\u003e Upcast is a low-level JavaScript type checking and casting library. Upcast simplifies type-checking and converts between types in a more sensible and predictable way than using plain ol' JavaScript.\n\nGetting Started\n---------------\n\nYou can use Upcast on the server side with [Node.js][node] and yarn/npm:\n\n```console\n$ yarn add upcast\n$ npm install upcast\n```\n\n\nUsage\n-----\n\nUpcast exposes three simple functions:\n\n- **[type](#upcasttype)**: get the type of an object\n- **[is](#upcastis)**: check whether an object is of a given type\n- **[to](#upcastto)**: convert an object to a specific type\n\n\n### upcast.type\n\nGet the type of an object. This accepts a single argument:\n**val:** *(mixed)* The object to get the type of.\n\nTypes in Upcast are different to `typeof` in what is reported for arrays and `null`. See the example below:\n\n```js\nupcast.type([]); // 'array'\nupcast.type(true); // 'boolean'\nupcast.type(function () {}); // 'function'\nupcast.type(null); // 'null'\nupcast.type(123); // 'number'\nupcast.type({}); // 'object'\nupcast.type('foo'); // 'string'\nupcast.type(undefined); // 'undefined'\n```\n\n\n### upcast.is\n\nCheck whether an object is of a given type. This accepts two arguments:\n**val:** *(mixed)* The object to check the type of.\n**type:** *(string)* The type to check for. One of `array`, `boolean`, `function`, `null`, `number`, `object`, `string` or `undefined`.\n\nThis function follows the same rules outlined in [`upcast.type`](#upcasttype) and allows you to use [type aliases](#type-aliases).\n\n```js\nupcast.is('foo', 'string'); // true\nupcast.is(123, 'string'); // false\n\nupcast.is([], 'array'); // true\nupcast.is([], 'object'); // false\n\nupcast.is(null, 'null'); // true\nupcast.is(null, 'object'); // false\n```\n\n\n### upcast.to\n\nConvert an object to a specific type. This accepts two arguments:\n**val:** *(mixed)* The object to convert.\n**type:** *(string)* The type to convert to. One of `array`, `boolean`, `function`, `null`, `number`, `object`, `string` or `undefined`.\n\nThe way types are converted aims to be sensible and allow easy switching back-and-forth of common types. For example, switching between strings and arrays is quite fluid:\n\n```js\nupcast.to('foo', 'array'); // ['f', 'o', 'o']\nupcast.to(['f', 'o', 'o'], 'string'); // 'foo'\n```\n\nYou can use [type aliases](#type-aliases) with this function. The examples below illustrate the way types are converted.\n\n#### Converting to an array\n\nConverting to an array from a boolean, function, number or object simply wraps the value in an array:\n\n```js\nupcast.to(123, 'array'); // [123]\n```\n\nStrings are handled differently, an array is returned with each character in the string as an item:\n\n```js\nupcast.to('foo', 'array'); // ['f', 'o', 'o']\n```\n\nNull and undefined are converted to an empty array:\n\n```js\nupcast.to(null, 'array'); // []\n```\n\n#### Converting to a boolean\n\nBoolean conversion simply converts to `true` or `false` based on whether the value is truthy or not. The only case where this doesn't follow JavaScript's standard behaviour is with empty arrays which are converted to `false`:\n\n```js\nupcast.to([1, 2, 3], 'boolean') // true\nupcast.to([], 'boolean') // false\n```\n\n#### Converting to a function\n\nWhen converting to a function, the original value is simply wrapped in a new function. This function returns the original value:\n\n```js\nupcast.to('foo', 'function'); // function () { return 'foo'; }\n```\n\n#### Converting to null\n\nAs expected, converting to null will always return `null`:\n\n```js\nupcast.to('foo', 'null'); // null\n```\n\n#### Converting to a number\n\nConverting to a number from a boolean, function, null or object simply calls `Number` with the original value as an argument, returning the expected value:\n\n```js\nupcast.to('true', 'number'); // 1\n```\n\nArrays and strings are handled differently, an array is joined to create a string, then evaluated with `parseInt`; strings are simply evaluated with `parseInt`:\n\n```js\nupcast.to([1, 2, 3], 'number'); // 123\nupcast.to('123', 'number'); // 123\nupcast.to('foo', 'number'); // 0\n```\n\nUndefined is converted to `0` rather than `NaN`:\n\n```js\nupcast.to(undefined, 'number'); // 0\n```\n\n#### Converting to an object\n\nConverting to an object simply calls `Object` with the value as a first argument. The following are equivalent:\n\n```js\nupcast.to('foo', 'object');\nObject('foo');\n```\n\n#### Converting to a string\n\nConverting to a string from a boolean, function, number or object simply returns the value added to an empty string, using JavaScript's default type conversion:\n\n```js\nupcast.to(true, 'string'); // 'true'\nupcast.to(123, 'string'); // '123'\n```\n\nArrays are handled differently, they are joined with an empty string:\n\n```js\nupcast.to(['f', 'o', 'o'], 'string'); // 'foo'\n```\n\nNull and undefined are converted to an empty string rather than `'null'` and `'undefined'`:\n\n```js\nupcast.to(null, 'string'); // ''\n```\n\n#### Converting to undefined\n\nAs expected, converting to undefined will always return `undefined`:\n\n```js\nupcast.to('foo', 'undefined'); // undefined\n```\n\n\n### Type aliases\n\nThe [`is`](#upcastis) and [`to`](#upcastto) functions allow you to use aliases to certain core types. The following are equivalent:\n\n```js\nupcast.is([], 'array');\nupcast.is([], 'arr');\nupcast.is([], 'a');\n```\n\nThe aliases available by default are:\n\n* **array:** `arr`, `a`\n* **boolean:** `bool`, `b`\n* **function:** `fn`, `f`\n* **number:** `num`, `n`\n* **object:** `obj`, `o`\n* **string:** `str`, `s`\n\n\nDevelopment\n-----------\n\nTo work on Upcast you'll need to clone the repo and it's install dependencies with `git clone https://github.com/OmgImalexis/upcast \u0026\u0026 cd upcast \u0026\u0026 yarn install`.\n\nOnce you're set up, you can run the following commands:\n\n```console\n$ yarn lint           # Run xo to lint all js files\n$ yarn test           # Run unit tests with ava\n$ yarn test-coverage  # Run unit tests and coverage report with ava + nyc\n```\n\n\nLicense\n-------\n\nUpcast is licensed under the [MIT][mit] license.\n\n\n\n[mit]: http://opensource.org/licenses/mit-license.php\n[lts]: https://github.com/nodejs/Release\n[node]: http://nodejs.org/\n[travis]: https://travis-ci.org/OmgImAlexis/upcast\n[travis-status]: https://travis-ci.org/OmgImAlexis/upcast.svg?branch=master\n\n\n## License\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FOmgImAlexis%2Fupcast.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2FOmgImAlexis%2Fupcast?ref=badge_large)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomgimalexis%2Fupcast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fomgimalexis%2Fupcast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomgimalexis%2Fupcast/lists"}