-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix current time indicator related issues.
- Loading branch information
Ilya Vinogradov
committed
Dec 8, 2023
1 parent
450cdaa
commit bdca72a
Showing
19 changed files
with
573 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { shiftIntegerByModule } from '@ts/core/utils/math'; | ||
import each from 'jest-each'; | ||
|
||
describe('Math utils tests', () => { | ||
describe('shiftIntegerByModule', () => { | ||
each` | ||
value | module | expectedResult | ||
${0} | ${2} | ${0} | ||
${2} | ${2} | ${0} | ||
${2} | ${4} | ${2} | ||
${2} | ${1000} | ${2} | ||
${4} | ${2} | ${0} | ||
${5} | ${2} | ${1} | ||
${6} | ${2} | ${0} | ||
${1e10} | ${10} | ${0} | ||
${1e10 + 3} | ${10} | ${3} | ||
${-9} | ${3} | ${0} | ||
${-1} | ${6} | ${5} | ||
${-3} | ${9} | ${6} | ||
${-5} | ${9} | ${4} | ||
${-1e10} | ${10} | ${0} | ||
${-1e10 + 3} | ${10} | ${3} | ||
` | ||
.it('should return correct result', ({ | ||
value, | ||
module, | ||
expectedResult, | ||
}) => { | ||
const result = shiftIntegerByModule(value, module); | ||
|
||
expect(result).toEqual(expectedResult); | ||
}); | ||
|
||
it('should throw error if value isn\'t integer', () => { | ||
expect(() => shiftIntegerByModule(1.5, 3)).toThrow(); | ||
}); | ||
|
||
it('should throw error if module value isn\'t integer', () => { | ||
expect(() => shiftIntegerByModule(2, 2.5)).toThrow(); | ||
}); | ||
|
||
it('should throw error if module value equals zero', () => { | ||
expect(() => shiftIntegerByModule(2, 0)).toThrow(); | ||
}); | ||
|
||
it('should throw error if module value less than zero', () => { | ||
expect(() => shiftIntegerByModule(2, -2)).toThrow(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export const shiftIntegerByModule = ( | ||
integerValue: number, | ||
moduleValue: number, | ||
): number => { | ||
if (!Number.isInteger(integerValue)) { | ||
throw Error(`Passed integer value ${integerValue} is not an integer.`); | ||
} | ||
|
||
if (!Number.isInteger(moduleValue)) { | ||
throw Error(`Passed module value ${moduleValue} is not an integer.`); | ||
} | ||
|
||
if (moduleValue <= 0) { | ||
throw Error(`Passed module value ${moduleValue} must be > 0.`); | ||
} | ||
|
||
const normalizedInteger = integerValue % moduleValue; | ||
|
||
switch (true) { | ||
// NOTE: In some cases we can have -0 or +0 values. | ||
// So this is why we handle zero as separate case here. | ||
case normalizedInteger === 0: | ||
return 0; | ||
case normalizedInteger > 0: | ||
return normalizedInteger; | ||
case normalizedInteger < 0: | ||
return moduleValue + normalizedInteger; | ||
default: | ||
throw Error(`Unexpected division (${integerValue} % ${moduleValue}) occurred.`); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.