rightpad.js 806 B

12345678910111213141516171819202122232425262728
  1. // Coding standard for this project defined @ https://github.com/MatthewSH/standards/blob/master/JavaScript.md
  2. 'use strict';
  3. exports = module.exports = function rightPad (_string, _length, _char) {
  4. if (typeof _string !== 'string') {
  5. throw new Error('The string parameter must be a string.');
  6. }
  7. if (_string.length < 1) {
  8. throw new Error('The string parameter must be 1 character or longer.');
  9. }
  10. if (typeof _length !== 'number') {
  11. throw new Error('The length parameter must be a number.');
  12. }
  13. if(typeof _char !== 'string' && _char) {
  14. throw new Error('The character parameter must be a string.');
  15. }
  16. var i = -1;
  17. _length = _length - _string.length;
  18. if (!_char && _char !== 0) {
  19. _char = ' ';
  20. }
  21. while (++i < _length) {
  22. _string += _char;
  23. }
  24. return _string;
  25. }