Pad Left - NPM, GitHub
Single Responsibility:
- “Left pad a string with zeros or a specified string. Fastest implementation.”
On Replication:
- This module would be very easy to implement myself, it might be best not to use this module. I could code this in under a half an hour then my project would not be dependent on third-party code. I could also replicate this module’s behavior without further dependencies. Given that their main selling point is being faster than left-pad, the pros don’t out-weigh the cons. My application does not need to pad large strings, or so many strings a minute. And anyway my app is so far from optimized that faster leftpadding wouldn’t make a noticeable difference in performance.
Principle Developer: jonschlinkert
Index
Analysis
This package is the best of it’s kind I was able to find. While it’s not too actively maintained, the code is also very simple and still passes it’s tests. The code is also well written and well documented so I would have no trouble hunting down any errors that came from this module.
Top Down
Community:
- Dependent Projects: 52
- Dependencies: 1
- Contributors: 4
- Forks: 6
- Stars: 31
- Curated Lists: Awesome Micro NPM Packages
- Version: 2.1.0
- Releases: 11
- Latest Commit: March 14, 2017
- Downloads Last Month: 124,085
- Open Issues: 0
- Open Pull Requests: 0
- License: MIT
Bottom Up
The Code:
- Ease of Use: high
- Documentation: good
- Differentiation: faster
- Maintenance: unmaintained, but not relevant for such a small package
- Project Scope: Clear, simple, well-stated
- Complaints: no open issues. Just the bigger questions of why not to write it myself
- Support: not relevant with no complaints
- Code: clear and well-written. I have no trouble understanding it or navigating it
Security
The source code in this module poses very little risk. While it’s not very actively maintained, and does not have a large group of contributors it’s also small and simple enough that this isn’t that big of an issue.
But it does have one dependency that could be a vulnerability. I haven’t assessed this one yet.
I can imagine this package posing a risk if someone were to get hold of the maintainer’s account and messed with the padding. They could either have the function pad with a dangerous regular expression, or simply make it pad by more than I want. Either way they could crash my system, or open dangerous vulnerabilities.
Competition
There is only one major competitor: The Infamous Left-Pad. Apparently this one is faster.
File Structure
pad-left
|
+-- /benchmark
| +-- /code
| | : Many ways to left pad a string
| +-- /fixtures
| | : Test cases
| +-- index.js
| | : Runs the benchmark tests and outputs the results
| +-- node-*.md
| : Outputs from the benchmark tests
|
+-- .*
| : A whole lot of configuration files
|
+-- LICENSE
|
+-- README.md
| : Title, stats
| : The module's behavior, and a link to a related module
| : Install instructions
| : Usage instructions
| : Benchmarking against the infamous left-pad
| : More related projects
| : Contributing Guidelines
| : How to build the docs with verb
| : How to run the tests
| : Author info
| : License
|
+-- index.js
| : Requires the repeat-string node module
| : Exports the pad-left function
| : Defines and exports the module's functionality
|
+-- package.json
| : Pretty basic package.json
|
+-- test.js
: Requires mocha, should, and pad-left
: Exports nothing
: Runs test cases for pad-left
Specs
pad-left: Function
ARGS: 3
str: String
* the string to be left-padded
num: Number
* the length of the final string
ch: String
* what to pad the string with
RETURN: String
BEHAVIOR:
* returns 'str' with 'ch' repeated to the
left until 'str' is 'num' characters long.
Use Cases
I don’t know of many use cases myself. But it must be a common operation since the left-pad incident is real.
It’s probably useful if you’re doing protocol compliance.
- CodeWars
- Entering data in a database
- Formatting URLs