@amaui/utils
Version:
23 lines (21 loc) • 743 B
JavaScript
import is from './is';
import castParam from './castParam';
const optionsDefault = {
leadingZeros: 1
};
export const getLeadingZerosNumber = function (value_) {
let options_ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
const options = { ...optionsDefault,
...options_
};
const value = castParam(value_);
if (is('number', value) && value >= 0) {
let leadingZeros = '';
const string = String(value);
const leadingZerosToAdd = options.leadingZeros + 1 - string.length;
if (leadingZerosToAdd > 0) for (const _ of new Array(leadingZerosToAdd)) leadingZeros += '0';
return "".concat(leadingZeros).concat(string);
}
return String(value_);
};
export default getLeadingZerosNumber;