forked from twentyhq/twenty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to properly cast a string, number, null to an integer (tw…
- Loading branch information
1 parent
fc6768c
commit 70390ed
Showing
4 changed files
with
161 additions
and
38 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { | ||
canBeCastAsIntegerOrNull, | ||
castAsIntegerOrNull, | ||
} from '../cast-as-integer-or-null'; | ||
|
||
describe('canBeCastAsIntegerOrNull', () => { | ||
it(`should return true if null`, () => { | ||
expect(canBeCastAsIntegerOrNull(null)).toBeTruthy(); | ||
}); | ||
|
||
it(`should return true if number`, () => { | ||
expect(canBeCastAsIntegerOrNull(9)).toBeTruthy(); | ||
}); | ||
|
||
it(`should return true if empty string`, () => { | ||
expect(canBeCastAsIntegerOrNull('')).toBeTruthy(); | ||
}); | ||
|
||
it(`should return true if integer string`, () => { | ||
expect(canBeCastAsIntegerOrNull('9')).toBeTruthy(); | ||
}); | ||
|
||
it(`should return false if undefined`, () => { | ||
expect(canBeCastAsIntegerOrNull(undefined)).toBeFalsy(); | ||
}); | ||
|
||
it(`should return false if non numeric string`, () => { | ||
expect(canBeCastAsIntegerOrNull('9a')).toBeFalsy(); | ||
}); | ||
|
||
it(`should return false if non numeric string #2`, () => { | ||
expect(canBeCastAsIntegerOrNull('a9a')).toBeFalsy(); | ||
}); | ||
|
||
it(`should return false if float`, () => { | ||
expect(canBeCastAsIntegerOrNull(0.9)).toBeFalsy(); | ||
}); | ||
|
||
it(`should return false if float string`, () => { | ||
expect(canBeCastAsIntegerOrNull('0.9')).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe('castAsIntegerOrNull', () => { | ||
it(`should cast null to null`, () => { | ||
expect(castAsIntegerOrNull(null)).toBe(null); | ||
}); | ||
|
||
it(`should cast empty string to null`, () => { | ||
expect(castAsIntegerOrNull('')).toBe(null); | ||
}); | ||
|
||
it(`should cast an integer to an integer`, () => { | ||
expect(castAsIntegerOrNull(9)).toBe(9); | ||
}); | ||
|
||
it(`should cast an integer string to an integer`, () => { | ||
expect(castAsIntegerOrNull('9')).toBe(9); | ||
}); | ||
|
||
it(`should throw if trying to cast a float string to an integer`, () => { | ||
expect(() => castAsIntegerOrNull('9.9')).toThrow(Error); | ||
}); | ||
|
||
it(`should throw if trying to cast a non numeric string to an integer`, () => { | ||
expect(() => castAsIntegerOrNull('9.9a')).toThrow(Error); | ||
}); | ||
|
||
it(`should throw if trying to cast an undefined to an integer`, () => { | ||
expect(() => castAsIntegerOrNull(undefined)).toThrow(Error); | ||
}); | ||
}); |
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,58 @@ | ||
export function canBeCastAsIntegerOrNull( | ||
probableNumberOrNull: string | undefined | number | null, | ||
): probableNumberOrNull is number | null { | ||
if (probableNumberOrNull === undefined) { | ||
return false; | ||
} | ||
|
||
if (typeof probableNumberOrNull === 'number') { | ||
return Number.isInteger(probableNumberOrNull); | ||
} | ||
|
||
if (probableNumberOrNull === null) { | ||
return true; | ||
} | ||
|
||
if (probableNumberOrNull === '') { | ||
return true; | ||
} | ||
|
||
if (typeof probableNumberOrNull === 'string') { | ||
const stringAsNumber = +probableNumberOrNull; | ||
|
||
if (isNaN(stringAsNumber)) { | ||
return false; | ||
} | ||
if (Number.isInteger(stringAsNumber)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export function castAsIntegerOrNull( | ||
probableNumberOrNull: string | undefined | number | null, | ||
): number | null { | ||
if (canBeCastAsIntegerOrNull(probableNumberOrNull) === false) { | ||
throw new Error('Cannot cast to number or null'); | ||
} | ||
|
||
if (probableNumberOrNull === null) { | ||
return null; | ||
} | ||
|
||
if (probableNumberOrNull === '') { | ||
return null; | ||
} | ||
|
||
if (typeof probableNumberOrNull === 'number') { | ||
return probableNumberOrNull; | ||
} | ||
|
||
if (typeof probableNumberOrNull === 'string') { | ||
return +probableNumberOrNull; | ||
} | ||
|
||
return null; | ||
} |