2 var stringWidth = require('string-width')
4 module.exports = TemplateItem
6 function isPercent (num) {
7 if (typeof num !== 'string') return false
8 return num.slice(-1) === '%'
11 function percent (num) {
12 return Number(num.slice(0, -1)) / 100
15 function TemplateItem (values, outputLength) {
16 this.overallOutputLength = outputLength
30 if (typeof values === 'string') {
33 for (var prop in values) this[prop] = values[prop]
36 if (isPercent(this.length)) {
37 this.length = Math.round(this.overallOutputLength * percent(this.length))
39 if (isPercent(this.minLength)) {
40 this.minLength = Math.round(this.overallOutputLength * percent(this.minLength))
42 if (isPercent(this.maxLength)) {
43 this.maxLength = Math.round(this.overallOutputLength * percent(this.maxLength))
48 TemplateItem.prototype = {}
50 TemplateItem.prototype.getBaseLength = function () {
51 var length = this.length
52 if (length == null && typeof this.value === 'string' && this.maxLength == null && this.minLength == null) {
53 length = stringWidth(this.value)
58 TemplateItem.prototype.getLength = function () {
59 var length = this.getBaseLength()
60 if (length == null) return null
61 return length + this.padLeft + this.padRight
64 TemplateItem.prototype.getMaxLength = function () {
65 if (this.maxLength == null) return null
66 return this.maxLength + this.padLeft + this.padRight
69 TemplateItem.prototype.getMinLength = function () {
70 if (this.minLength == null) return null
71 return this.minLength + this.padLeft + this.padRight