Skip to content

Instantly share code, notes, and snippets.

@nuintun
Created March 23, 2017 09:35
Show Gist options
  • Select an option

  • Save nuintun/23777e297b67f4290692dd5c204a07cc to your computer and use it in GitHub Desktop.

Select an option

Save nuintun/23777e297b67f4290692dd5c204a07cc to your computer and use it in GitHub Desktop.
字符右补白
'use strict'

// 兼容模式,兼容老版本浏览器
function pad(value, length, placeholder) {
  length = length || 15;
  placeholder = placeholder || ' ';

  var diff = Math.max(0, length - value.length) + 1;

  return value + new Array(diff).join(placeholder);
}

// 流行模式,只兼容现代浏览器(es5, es6)
function pad(value, length, placeholder) {
  length = length || 15;
  placeholder = placeholder || ' ';
  
  var diff = Math.max(0, length - value.length);

  return value + placeholder.repeat(diff);
}

// 现代模式,只兼容现代浏览器(es6)
function pad(value, length = 15, placeholder = ' ') {
  var diff = Math.max(0, length - value.length);

  return value + placeholder.repeat(diff);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment