{"id":22382527,"url":"https://github.com/jcoreio/coerce","last_synced_at":"2025-03-26T19:41:08.102Z","repository":{"id":40782670,"uuid":"268642853","full_name":"jcoreio/coerce","owner":"jcoreio","description":"our company standard JS type coercions","archived":false,"fork":false,"pushed_at":"2022-12-11T08:24:39.000Z","size":2816,"stargazers_count":0,"open_issues_count":19,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-01T01:41:51.023Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/jcoreio.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2020-06-01T22:06:42.000Z","updated_at":"2020-06-12T21:20:56.000Z","dependencies_parsed_at":"2023-01-26T19:01:24.975Z","dependency_job_id":null,"html_url":"https://github.com/jcoreio/coerce","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fcoerce","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fcoerce/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fcoerce/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcoreio%2Fcoerce/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jcoreio","download_url":"https://codeload.github.com/jcoreio/coerce/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245726875,"owners_count":20662545,"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":[],"created_at":"2024-12-05T00:13:21.356Z","updated_at":"2025-03-26T19:41:08.080Z","avatar_url":"https://github.com/jcoreio.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @jcoreio/coerce\n\n[![CircleCI](https://circleci.com/gh/jcoreio/coerce.svg?style=svg)](https://circleci.com/gh/jcoreio/coerce)\n[![Coverage Status](https://codecov.io/gh/jcoreio/coerce/branch/master/graph/badge.svg)](https://codecov.io/gh/jcoreio/coerce)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)\n[![npm version](https://badge.fury.io/js/%40jcoreio%2Fcoerce.svg)](https://badge.fury.io/js/%40jcoreio%2Fcoerce)\n\nOur company standard JS type coercions for time series values\n\n# API\n\n## `coerceToNumber(x: any): number | null`\n\nCoerces the given value to a number.\n\n```\nconst { coerceToNumber } = require('@jcoreio/coerce')\nconsole.log(coerceToNumber(true)) // 1\n```\n\n### Rules:\n\n- `null`, `undefined`, or string that isn't a valid number literal get coerced to `null`\n- `Date` gets coerced to timestamp\n- Any other object gets coerced to `null`\n- Any other primitive value gets coerced with `Number(x)`\n\n## `coerceToBigInt(x: any): bigint | null`\n\nCoerces the given value to a bigint.\n\n```\nconst { coerceToBigInt } = require('@jcoreio/coerce')\nconsole.log(coerceToBigInt(true)) // 1n\n```\n\n### Rules:\n\n- `null`, `undefined`, or string that isn't a valid number literal get coerced to `null`\n- `Date` gets coerced to timestamp bigint\n- Any other object gets coerced to `null`\n- Any other primitive value gets coerced with `BigInt(x)`\n  - numbers get rounded first\n  - if `BigInt(string)` fails, uses `BigInt(coerceToNumber(string))`\n\n## `coerceToNumberOrBigInt(x: any): bigint | null`\n\nCoerces the given value to a number or bigint, whichever is more suitable.\n\n```\nconst { coerceToNumberOrBigInt } = require('@jcoreio/coerce')\nconsole.log(coerceToNumberOrBigInt('25e4')) // 250000\nconsole.log(coerceToNumberOrBigInt('9007199254740992')) // 9007199254740992n\n```\n\n### Rules:\n\n- `null`, `undefined`, or string that isn't a valid number literal get coerced to `null`\n- integer string literals outside the safe integer range get coerced to bigint\n- `Date` gets coerced to timestamp number\n- Any other object gets coerced to `null`\n- Any other primitive value gets coerced with `Number(x)`\n\n## `coerceToString(x: any): string | null`\n\nCoerces the given value to a string.\n\n```\nconst { coerceToString } = require('@jcoreio/coerce')\nconsole.log(coerceToString(1)) // '1'\n```\n\n### Rules:\n\n- `null` and `undefined` get coerced to `null`\n- `Date` gets coerced to ISO string\n- Any other object gets `JSON.stringify`ed\n- Any other primitive value gets coerced with `String(x)`\n\n## `coerceToBoolean(x: any): boolean | null`\n\nCoerces the given value to a boolean.\n\n```\nconst { coerceToBoolean } = require('@jcoreio/coerce')\nconsole.log(coerceToBoolean(1)) // true\n```\n\n### Rules:\n\n- `'0'`, `'f'`, and `'false'` (in any case) get coerced to `false`\n- `'1'`, `'t'`, and `'true'` (in any case) get coerced to `true`\n- Any number besides `NaN` gets coerced with `Boolean(x)`\n- Any other non-boolean value gets coerced to `null`\n\n## `coerceTo['number' | 'bigint' | 'numberOrBigInt' | 'string' | 'boolean']`\n\nThis is just a map from the type name to the coercion function:\n\n```\nconst { coerceTo } = require('@jcoreio/coerce')\nconsole.log(coerceTo['string'](3)) // '3'\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcoreio%2Fcoerce","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjcoreio%2Fcoerce","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcoreio%2Fcoerce/lists"}