From 7f9e9df4221d27b0b41a8ca04ea2cf69e7065613 Mon Sep 17 00:00:00 2001 From: Matt Mayer <152770+matthewmayer@users.noreply.github.com> Date: Tue, 2 May 2023 04:39:20 +0700 Subject: fix(test): fix failing latitude test (#2116) --- test/location.spec.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/test/location.spec.ts b/test/location.spec.ts index e1b9e1ca..22b5b214 100644 --- a/test/location.spec.ts +++ b/test/location.spec.ts @@ -11,6 +11,18 @@ function kilometersToMiles(miles: number) { return miles * 0.621371; } +/** + * Returns the number of decimal places a number has + */ +function precision(num: number): number { + const decimalPart = num.toString().split('.')[1]; + if (decimalPart === undefined) { + return 0; + } + + return decimalPart.length; +} + // http://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html const EQUATORIAL_EARTH_RADIUS = 6378.137; @@ -239,7 +251,7 @@ describe('location', () => { const latitude = faker.location.latitude({ max: 5, min: -5 }); expect( - latitude.toString().split('.')[1].length, + precision(latitude), 'The precision of latitude should be 4 digits' ).lessThanOrEqual(4); @@ -251,7 +263,7 @@ describe('location', () => { const latitude = faker.location.latitude({ precision: 7 }); expect( - latitude.toString().split('.')[1].length, + precision(latitude), 'The precision of latitude should be 7 digits' ).lessThanOrEqual(7); @@ -278,7 +290,7 @@ describe('location', () => { const longitude = faker.location.longitude({ max: 100, min: -30 }); expect( - longitude.toString().split('.')[1].length, + precision(longitude), 'The precision of longitude should be 4 digits' ).lessThanOrEqual(4); @@ -290,7 +302,7 @@ describe('location', () => { const longitude = faker.location.longitude({ precision: 7 }); expect( - longitude.toString().split('.')[1].length, + precision(longitude), 'The precision of longitude should be 7 digits' ).lessThanOrEqual(7); -- cgit v1.2.3