Skip to content

Commit 74f9fb4

Browse files
authored
add trunc as rounding method (#583)
1 parent e2a0cc6 commit 74f9fb4

File tree

6 files changed

+28
-3
lines changed

6 files changed

+28
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ By default the library will handle automatically [BigInt](https://developer.mozi
548548
<a name="integer"></a>
549549
#### Integers
550550
The `type: integer` property will be truncated if a floating point is provided.
551-
You can customize this behaviour with the `rounding` option that will accept [`round`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round), [`ceil`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil) or [`floor`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor):
551+
You can customize this behaviour with the `rounding` option that will accept [`round`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round), [`ceil`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil), [`floor`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor) or [`trunc`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc). Default is `trunc`:
552552

553553
```js
554554
const stringify = fastJson(schema, { rounding: 'ceil' })

index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ const Location = require('./lib/location')
1414

1515
let largeArraySize = 2e4
1616
let largeArrayMechanism = 'default'
17+
18+
const validRoundingMethods = [
19+
'floor',
20+
'ceil',
21+
'round',
22+
'trunc'
23+
]
24+
1725
const validLargeArrayMechanisms = [
1826
'default',
1927
'json-stringify'
@@ -93,7 +101,7 @@ function build (schema, options) {
93101
}
94102

95103
if (options.rounding) {
96-
if (!['floor', 'ceil', 'round'].includes(options.rounding)) {
104+
if (!validRoundingMethods.includes(options.rounding)) {
97105
throw new Error(`Unsupported integer rounding method ${options.rounding}`)
98106
}
99107
}

lib/serializer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = class Serializer {
1515
case 'round':
1616
this.parseInteger = Math.round
1717
break
18+
case 'trunc':
1819
default:
1920
this.parseInteger = Math.trunc
2021
break

test/integer.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ test('render a float as an integer', (t) => {
4545
{ input: 42, output: '42' },
4646
{ input: 1.99999, output: '1' },
4747
{ input: -45.05, output: '-45' },
48+
{ input: Math.PI, output: '3', rounding: 'trunc' },
49+
{ input: 5.0, output: '5', rounding: 'trunc' },
50+
{ input: null, output: '0', rounding: 'trunc' },
51+
{ input: 0, output: '0', rounding: 'trunc' },
52+
{ input: 0.0, output: '0', rounding: 'trunc' },
53+
{ input: 42, output: '42', rounding: 'trunc' },
54+
{ input: 1.99999, output: '1', rounding: 'trunc' },
55+
{ input: -45.05, output: '-45', rounding: 'trunc' },
4856
{ input: 0.95, output: '1', rounding: 'ceil' },
4957
{ input: 0.2, output: '1', rounding: 'ceil' },
5058
{ input: 45.95, output: '45', rounding: 'floor' },

types/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,10 @@ declare namespace build {
158158
ajv?: AjvOptions
159159
/**
160160
* Optionally configure how the integer will be rounded
161+
*
162+
* @default 'trunc'
161163
*/
162-
rounding?: 'ceil' | 'floor' | 'round'
164+
rounding?: 'ceil' | 'floor' | 'round' | 'trunc'
163165
/**
164166
* @deprecated
165167
* Enable debug mode. Please use `mode: "debug"` instead

types/index.test-d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ const schema2: Schema = {
1313
build(schema1)(25)
1414
build(schema2)(-5)
1515

16+
build(schema2, { rounding: 'ceil' })
17+
build(schema2, { rounding: 'floor' })
18+
build(schema2, { rounding: 'round' })
19+
build(schema2, { rounding: 'trunc' })
20+
expectError(build(schema2, { rounding: 'invalid' }))
21+
1622
// String schema
1723
const schema3: Schema = {
1824
type: 'string'

0 commit comments

Comments
 (0)