finki-main
main
|
Last change
on this file since 81bc7da was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago |
|
Initial commit
|
-
Property mode
set to
100644
|
|
File size:
1.0 KB
|
| Line | |
|---|
| 1 | 'use strict'
|
|---|
| 2 | var validate = require('aproba')
|
|---|
| 3 | var renderTemplate = require('./render-template.js')
|
|---|
| 4 | var wideTruncate = require('./wide-truncate')
|
|---|
| 5 | var stringWidth = require('string-width')
|
|---|
| 6 |
|
|---|
| 7 | module.exports = function (theme, width, completed) {
|
|---|
| 8 | validate('ONN', [theme, width, completed])
|
|---|
| 9 | if (completed < 0) {
|
|---|
| 10 | completed = 0
|
|---|
| 11 | }
|
|---|
| 12 | if (completed > 1) {
|
|---|
| 13 | completed = 1
|
|---|
| 14 | }
|
|---|
| 15 | if (width <= 0) {
|
|---|
| 16 | return ''
|
|---|
| 17 | }
|
|---|
| 18 | var sofar = Math.round(width * completed)
|
|---|
| 19 | var rest = width - sofar
|
|---|
| 20 | var template = [
|
|---|
| 21 | { type: 'complete', value: repeat(theme.complete, sofar), length: sofar },
|
|---|
| 22 | { type: 'remaining', value: repeat(theme.remaining, rest), length: rest },
|
|---|
| 23 | ]
|
|---|
| 24 | return renderTemplate(width, template, theme)
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | // lodash's way of repeating
|
|---|
| 28 | function repeat (string, width) {
|
|---|
| 29 | var result = ''
|
|---|
| 30 | var n = width
|
|---|
| 31 | do {
|
|---|
| 32 | if (n % 2) {
|
|---|
| 33 | result += string
|
|---|
| 34 | }
|
|---|
| 35 | n = Math.floor(n / 2)
|
|---|
| 36 | /* eslint no-self-assign: 0 */
|
|---|
| 37 | string += string
|
|---|
| 38 | } while (n && stringWidth(result) < width)
|
|---|
| 39 |
|
|---|
| 40 | return wideTruncate(result, width)
|
|---|
| 41 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.