Skip to content

Commit 4465b15

Browse files
authored
Fix strict validation of numbers with leading zeros in neo4j.int (#988)
1 parent 3447791 commit 4465b15

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

packages/core/src/integer.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,20 @@ class Integer {
956956
}
957957
}
958958

959+
/**
960+
* @private
961+
* @param num
962+
* @param radix
963+
* @param minSize
964+
* @returns {string}
965+
*/
966+
function _convertNumberToString (num: number, radix: number, minSize: number): string {
967+
const theNumberString = num.toString(radix)
968+
const paddingLength = Math.max(minSize - theNumberString.length, 0)
969+
const padding = '0'.repeat(paddingLength)
970+
return `${padding}${theNumberString}`
971+
}
972+
959973
/**
960974
*
961975
* @private
@@ -967,7 +981,7 @@ class Integer {
967981
function _isValidNumberFromString (theString: string, theNumber: number, radix: number): boolean {
968982
return !Number.isNaN(theString) &&
969983
!Number.isNaN(theNumber) &&
970-
theNumber.toString(radix).toLocaleLowerCase() === theString.toLocaleLowerCase()
984+
_convertNumberToString(theNumber, radix, theString.length) === theString.toLowerCase()
971985
}
972986

973987
type Integerable =

packages/core/test/integer.test.ts

+79
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,12 @@ describe('Integer', () => {
309309
test('int("7891123456789876a") not toThrow invalid character', () =>
310310
expect(() => int('7891123456789876a')).not.toThrow())
311311

312+
test.each(malformedNumbers())('int("%s", { strictStringValidation: true }) toThrow invalid character', (theNumberString) =>
313+
expect(() => int(theNumberString, { strictStringValidation: true })).toThrow())
314+
315+
test.each(wellFormedNumbersAndRadix())('Integer.fromString("%s", %n, { strictStringValidation: true }) not toThrown', (theNumberString, radix) =>
316+
expect(() => Integer.fromString(theNumberString, radix, { strictStringValidation: true })).not.toThrow())
317+
312318
forEachStaticToNumberScenarios(({ input, expectedOutput }) =>
313319
test(`Integer.toNumber(${mayIntegerToString(input)}) toEqual ${expectedOutput}`, () =>
314320
expect(Integer.toNumber(input)).toEqual(expectedOutput))
@@ -1109,6 +1115,79 @@ function forEachStaticInSafeRangeScenarios (
11091115
].forEach(func)
11101116
}
11111117

1118+
function malformedNumbers (): string[] {
1119+
return [
1120+
'7a',
1121+
'7891123a',
1122+
'78911234a',
1123+
'789112345a',
1124+
'7891123456a',
1125+
'7891123456789876a',
1126+
'78911234567898765a',
1127+
'789112345678987654a',
1128+
'78911234567898765a2',
1129+
'7891123456789876a25',
1130+
'789112345678987a256',
1131+
'78911234567898a2567',
1132+
'7891123456789a25678',
1133+
'789112345678a256789',
1134+
'78911234567a2567898',
1135+
'7891123456a25678987',
1136+
'789112345a256789876',
1137+
'78911234a2567898765',
1138+
'7891123a25678987654',
1139+
'7891123ab2567898765',
1140+
'78911234ab256789876',
1141+
'789112345ab25678987',
1142+
'7891123456ab2567898',
1143+
'78911234567ab256789',
1144+
'78911234567abc25678',
1145+
'78911234567abcd2567',
1146+
'78911234567abcde256',
1147+
'78911234567abcdef25',
1148+
'78911234567abcdefg2',
1149+
'7891123456abcdefgh1',
1150+
'789112345abcdefgh12',
1151+
'78911234abcdefgh123',
1152+
'7891123abcdefgh1234',
1153+
'789112abcdefghij123',
1154+
'7kkkkabcdefghijklmn',
1155+
'7kkkkabcdefg12345mn',
1156+
'7kkkkabcdefg123456n',
1157+
'7kkkkab22efg123456n',
1158+
'7kkkkab22efg12345mn',
1159+
'7kkkkab223fg12345mn',
1160+
'kkkkk11223fg12345mn',
1161+
'kkkkk11223fg123456n',
1162+
'kkkkk11223fg1234567',
1163+
'kkkkk11223451234567',
1164+
'kkk111gkk3451234567',
1165+
'kkk111gkkkk51234567',
1166+
'kkk111gkkkkk123kk67',
1167+
'kkkk234',
1168+
'kkkk2345',
1169+
'kkkk23456',
1170+
'kkkk234567',
1171+
'kkkk2345679kk',
1172+
'kkkk2345679kkkkkk',
1173+
'kkk234567',
1174+
'kkk2345679',
1175+
'kk2345679',
1176+
'kkkkkkkkkkkkkkkkkkk'
1177+
]
1178+
}
1179+
1180+
function wellFormedNumbersAndRadix (): Array<[string, number]> {
1181+
return [
1182+
['01', 2],
1183+
['012', 3],
1184+
['0123', 4],
1185+
['0123456789', 10],
1186+
['0123456789ab', 12],
1187+
['0123456789abCde', 16]
1188+
]
1189+
}
1190+
11121191
interface AssertionPair<I, O> {
11131192
input: I
11141193
expectedOutput: O

0 commit comments

Comments
 (0)