https://github.com/jcoreio/coerce
our company standard JS type coercions
https://github.com/jcoreio/coerce
Last synced: about 1 year ago
JSON representation
our company standard JS type coercions
- Host: GitHub
- URL: https://github.com/jcoreio/coerce
- Owner: jcoreio
- License: mit
- Created: 2020-06-01T22:06:42.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-11T08:24:39.000Z (over 3 years ago)
- Last Synced: 2025-02-01T01:41:51.023Z (over 1 year ago)
- Language: JavaScript
- Size: 2.69 MB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# @jcoreio/coerce
[](https://circleci.com/gh/jcoreio/coerce)
[](https://codecov.io/gh/jcoreio/coerce)
[](https://github.com/semantic-release/semantic-release)
[](http://commitizen.github.io/cz-cli/)
[](https://badge.fury.io/js/%40jcoreio%2Fcoerce)
Our company standard JS type coercions for time series values
# API
## `coerceToNumber(x: any): number | null`
Coerces the given value to a number.
```
const { coerceToNumber } = require('@jcoreio/coerce')
console.log(coerceToNumber(true)) // 1
```
### Rules:
- `null`, `undefined`, or string that isn't a valid number literal get coerced to `null`
- `Date` gets coerced to timestamp
- Any other object gets coerced to `null`
- Any other primitive value gets coerced with `Number(x)`
## `coerceToBigInt(x: any): bigint | null`
Coerces the given value to a bigint.
```
const { coerceToBigInt } = require('@jcoreio/coerce')
console.log(coerceToBigInt(true)) // 1n
```
### Rules:
- `null`, `undefined`, or string that isn't a valid number literal get coerced to `null`
- `Date` gets coerced to timestamp bigint
- Any other object gets coerced to `null`
- Any other primitive value gets coerced with `BigInt(x)`
- numbers get rounded first
- if `BigInt(string)` fails, uses `BigInt(coerceToNumber(string))`
## `coerceToNumberOrBigInt(x: any): bigint | null`
Coerces the given value to a number or bigint, whichever is more suitable.
```
const { coerceToNumberOrBigInt } = require('@jcoreio/coerce')
console.log(coerceToNumberOrBigInt('25e4')) // 250000
console.log(coerceToNumberOrBigInt('9007199254740992')) // 9007199254740992n
```
### Rules:
- `null`, `undefined`, or string that isn't a valid number literal get coerced to `null`
- integer string literals outside the safe integer range get coerced to bigint
- `Date` gets coerced to timestamp number
- Any other object gets coerced to `null`
- Any other primitive value gets coerced with `Number(x)`
## `coerceToString(x: any): string | null`
Coerces the given value to a string.
```
const { coerceToString } = require('@jcoreio/coerce')
console.log(coerceToString(1)) // '1'
```
### Rules:
- `null` and `undefined` get coerced to `null`
- `Date` gets coerced to ISO string
- Any other object gets `JSON.stringify`ed
- Any other primitive value gets coerced with `String(x)`
## `coerceToBoolean(x: any): boolean | null`
Coerces the given value to a boolean.
```
const { coerceToBoolean } = require('@jcoreio/coerce')
console.log(coerceToBoolean(1)) // true
```
### Rules:
- `'0'`, `'f'`, and `'false'` (in any case) get coerced to `false`
- `'1'`, `'t'`, and `'true'` (in any case) get coerced to `true`
- Any number besides `NaN` gets coerced with `Boolean(x)`
- Any other non-boolean value gets coerced to `null`
## `coerceTo['number' | 'bigint' | 'numberOrBigInt' | 'string' | 'boolean']`
This is just a map from the type name to the coercion function:
```
const { coerceTo } = require('@jcoreio/coerce')
console.log(coerceTo['string'](3)) // '3'
```