Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve padWithZeroes implementation and add tests #180

Merged
merged 9 commits into from
Aug 16, 2021

Conversation

rekmarks
Copy link
Member

@rekmarks rekmarks commented Aug 15, 2021

The padWithZeroes implementation was inefficient, but most of all, ugly. This PR makes it more efficient, adds some basic input validation, and unit tests.

@rekmarks rekmarks requested a review from a team as a code owner August 15, 2021 19:48
@rekmarks rekmarks requested a review from Gudahtt August 15, 2021 19:55
function padWithZeroes(number: string, length: number): string {
let myString = `${number}`;
while (myString.length < length) {
myString = `0${myString}`;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This computes a new string and stores it in memory length - number.length times.

src/index.ts Outdated
* @returns The input string front-padded with zeroes, or the original string
* if it was already greater than or equal to to the target length.
*/
export function padWithZeroes(hexString: string, targetLength: number): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotta say, I'm not too keen on adding more stuff to this API 🤔 We've already exposed an annoying amount of what would otherwise be internal functions.

I think it would be fine to test this via concatSig, remove the tests, and keep this private.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either that or delete this altogether and use String.prototype.padStart

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL padStart is a thing! I'm definitely going to use that.

@rekmarks rekmarks requested a review from Gudahtt August 16, 2021 17:39
@rekmarks
Copy link
Member Author

@Gudahtt I refactored it to use padStart as you suggested, and moved the file to a new file, utils.ts that has its own tests.

Gudahtt
Gudahtt previously approved these changes Aug 16, 2021
Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Left a couple of minor non-blocking suggestions

src/utils.ts Outdated Show resolved Hide resolved
export function padWithZeroes(hexString: string, targetLength: number): string {
if (hexString !== '' && !/^[a-f0-9]+$/iu.test(hexString)) {
throw new Error(
`Expected an unprefixed hex string. Received: ${hexString}`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old function didn't include this check. It would coerce anything it was given into a string. I'm pretty confident it was only ever called with a hex string though, because in the one place it's called, it's given the result of a Buffer.prototype.toString('hex') call. So I'd guess this doesn't change behaviour.

Copy link
Member Author

@rekmarks rekmarks Aug 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, my thinking was exactly that it's non-breaking for current usage, and we now also won't accidentally shoot ourselves in the foot in the future.

expect(padWithZeroes(input, 4)).toStrictEqual(`0000`);
});

it('returns a string longer than or equal to the target length without modifying it', function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: might be nice to test the "longer than" case, which isn't covered here.

Co-authored-by: Mark Stacey <[email protected]>
@rekmarks
Copy link
Member Author

Okay, I added a couple more test cases, including the one you suggested. I also added input validation for the target length.

Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@rekmarks rekmarks merged commit 0ec5ce5 into main Aug 16, 2021
@rekmarks rekmarks deleted the improve-padWithZeroes branch August 16, 2021 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants