Last active
September 25, 2023 16:17
-
-
Save jeffjohnson9046/7e0a6110d6052df2bd96d4d01a478cc8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/****************************************************************************************************************************** | |
I was watching a video of a guy reading an article (man... what fucking strange times we live in...) about developers missing | |
basic programming skills because they rely on npm packages to do the work for them. There are packages like left-pad, is-array, | |
is-positive-integer, etc. Apparently some change in the left-pad package broke a bunch of big-time repos (including React). | |
This is a link to said video: https://www.youtube.com/watch?v=NmHUjxKpD90 | |
My main take-aways from the article/video were: | |
* just because it's a package that gets downloaded a bunch of times doesn't mean it's good, quality code | |
* don't introduce a dependency if you don't need to | |
* an over-reliance on external dependencies: | |
* can make your software unintentionally brittle and/or difficult to debug and fix | |
* can add extra bloat to your application | |
So that's the background. The article basically said "if you can't write something that adds left padding to a string or | |
check if the thingy is an array in a few miuntes, you have a problem." So I took the challenge, and this is the result. The | |
leftPad function didn't take too long (5-ish minutes or so). The testHarness took a bit longer, 'cause it's been approximately | |
forever since I had to use `.apply()`. I had to go look it up on MDN and futz with it for a bit. | |
NOTE: For adding left-padding to a string in JavaScript, writing your own method to do so is not ideal. Using padStart would | |
be much better - at this point there's no actual need for a function like what's written below. The point of this was more for | |
the exercise than any sort of practicality. | |
******************************************************************************************************************************/ | |
/** | |
* Add left-padding to a string. If no padding character is provided, a space will be used. | |
* | |
* @param str the `string` to pad | |
* @param len the total length of the resulting string, including the padding | |
* @param ch the optional character to use for padding | |
* @returns `string` with left-padding | |
* @seealso https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart | |
*/ | |
function leftPad(str, len, ch) { | |
if (ch == null && ch !== 0) { | |
ch = ' ' | |
} | |
const padding = len - str.length; | |
return `${ch.repeat(padding)}${str}`; | |
} | |
/** | |
* A test harness to test our function. | |
* | |
* @param fn the `function` we want to test | |
* @param args an array-like of the arguments we want to pass to our function | |
*/ | |
function testHarness(fn, ...args) { | |
// Test various lengths of padding - a good refactor would be to pass this in as an argument instead of | |
// hard-coding it here. | |
[10, 100, 1000, 10000, 100000].forEach(x => { | |
const start = performance.now(); | |
fn.apply(null, [args, x]) | |
const end = performance.now() - start; | |
console.log(`${fn.name} ${x}: ${end}`); | |
}); | |
} | |
// Run our dopey test and see the output. | |
testHarness(leftPad, 'foo'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment