mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-03-22 03:55:19 +00:00
Implement helpers module with formatLongString()
This commit is contained in:
parent
c4bb275923
commit
5bbbc93d80
5
lib/helpers/helpers.coffee
Normal file
5
lib/helpers/helpers.coffee
Normal file
@ -0,0 +1,5 @@
|
||||
exports.formatLongString = (string, n) ->
|
||||
return string if not n?
|
||||
splitRegexp = new RegExp(".{1,#{n}}", 'g')
|
||||
splittedString = string.match(splitRegexp)
|
||||
return splittedString.join('\n')
|
35
lib/helpers/helpers.spec.coffee
Normal file
35
lib/helpers/helpers.spec.coffee
Normal file
@ -0,0 +1,35 @@
|
||||
expect = require('chai').expect
|
||||
_ = require('lodash')
|
||||
helpers = require('./helpers')
|
||||
|
||||
STRING =
|
||||
numbers: '1234567812345678'
|
||||
|
||||
describe 'Helpers:', ->
|
||||
|
||||
describe '#formatLongString()', ->
|
||||
|
||||
it 'should format a string', ->
|
||||
result = helpers.formatLongString(STRING.numbers, 4)
|
||||
expect(result).to.equal('1234\n5678\n1234\n5678')
|
||||
|
||||
it 'should return the same string if n is null/undefined', ->
|
||||
for value in [ undefined, null ]
|
||||
result = helpers.formatLongString(STRING.numbers, value)
|
||||
expect(result).to.equal(STRING.numbers)
|
||||
|
||||
it 'should throw an error if input is not a string', ->
|
||||
for value in [
|
||||
undefined
|
||||
null
|
||||
[]
|
||||
{}
|
||||
123
|
||||
]
|
||||
fn = _.partial(helpers.formatLongString, value, 4)
|
||||
expect(fn).to.throw
|
||||
|
||||
it 'should return the same string if n > string.length', ->
|
||||
stringLength = STRING.numbers.length
|
||||
result = helpers.formatLongString(STRING.numbers, stringLength + 1)
|
||||
expect(result).to.equal(STRING.numbers)
|
Loading…
x
Reference in New Issue
Block a user