| Conditions | 1 |
| Paths | 1 |
| Total Lines | 68 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | const assert = require ('assert') |
||
| 3 | describe ('printable-characters', () => { |
||
| 4 | |||
| 5 | it ('determines visible length', () => { |
||
| 6 | |||
| 7 | const { strlen } = require ('./printable-characters') |
||
| 8 | |||
| 9 | assert (strlen ('foo bar'), 7) |
||
| 10 | assert (strlen ('\u001b[106mfoo bar\u001b[49m'), 7) |
||
| 11 | }) |
||
| 12 | |||
| 13 | it ('detects blank text', () => { |
||
| 14 | |||
| 15 | const { isBlank } = require ('./printable-characters') |
||
| 16 | |||
| 17 | assert (!isBlank ('foobar')) |
||
| 18 | assert ( isBlank ('\u001b[106m \t \t \n \u001b[49m')) |
||
| 19 | }) |
||
| 20 | |||
| 21 | it ('matches zero-width characters and ANSI escape codes', () => { |
||
| 22 | |||
| 23 | const { ansiEscapeCodes, zeroWidthCharacters } = require ('./printable-characters') |
||
| 24 | |||
| 25 | let s = '\u001b[106m' + 'foo' + '\n\n' + 'bar' + '\u001b[49m' |
||
| 26 | |||
| 27 | assert (s = s.replace (ansiEscapeCodes, ''), 'foo\n\nbar') |
||
| 28 | assert ( s.replace (zeroWidthCharacters, ''), 'foobar') |
||
| 29 | }) |
||
| 30 | |||
| 31 | it ('obtains blank string of the same width', () => { |
||
| 32 | |||
| 33 | const { blank } = require ('./printable-characters') |
||
| 34 | |||
| 35 | assert.equal (blank ('foo'), ' ') |
||
| 36 | assert.equal (blank ('\n'), '\n') |
||
| 37 | assert.equal (blank ('\t'), '\t') |
||
| 38 | assert.equal (blank ('\tfoo \nfoo'), '\t \n ') |
||
| 39 | assert.equal (blank ('\u001b[22m\u001b[1mfoo \t\u001b[39m\u001b[22m'), ' \t') |
||
| 40 | }) |
||
| 41 | |||
| 42 | it ('extracts invisible parts followed by visible ones', () => { |
||
| 43 | |||
| 44 | const { partition } = require ('./printable-characters') |
||
| 45 | |||
| 46 | assert.deepEqual (partition (''), [ ]) |
||
| 47 | assert.deepEqual (partition ('foo'), [['', 'foo'] ]) |
||
| 48 | assert.deepEqual (partition ('\u001b[1mfoo'), [['\u001b[1m', 'foo'] ]) |
||
| 49 | assert.deepEqual (partition ('\u001b[1mfoo\u0000bar'), [['\u001b[1m', 'foo'], ['\u0000', 'bar'] ]) |
||
| 50 | assert.deepEqual (partition ('\u001b[1mfoo\u0000bar\n'), [['\u001b[1m', 'foo'], ['\u0000', 'bar'], ['\n', '']]) |
||
| 51 | }) |
||
| 52 | |||
| 53 | it ('gets first N visible symbols (preserving invisible parts)', () => { |
||
| 54 | |||
| 55 | const { first } = require ('./printable-characters') |
||
| 56 | |||
| 57 | assert.equal (first ('123456789', 0), '') |
||
| 58 | assert.equal (first ('123456789', 3), '123') |
||
| 59 | assert.equal (first ('123456789', 100), '123456789') |
||
| 60 | |||
| 61 | const s = '\u001b[22m\u001b[1m' + '123' + '\u0000' + '45' + '\u001b[39m' + '67' + '\n' + '89' + '\u001b[39m\u001b[22m' |
||
| 62 | |||
| 63 | assert.equal (first (s, 0), '\u001b[22m\u001b[1m' + '' + '\u0000' + '' + '\u001b[39m' + '' + '\n' + '' + '\u001b[39m\u001b[22m') |
||
| 64 | assert.equal (first (s, 3), '\u001b[22m\u001b[1m' + '123' + '\u0000' + '' + '\u001b[39m' + '' + '\n' + '' + '\u001b[39m\u001b[22m') |
||
| 65 | assert.equal (first (s, 4), '\u001b[22m\u001b[1m' + '123' + '\u0000' + '4' + '\u001b[39m' + '' + '\n' + '' + '\u001b[39m\u001b[22m') |
||
| 66 | assert.equal (first (s, 6), '\u001b[22m\u001b[1m' + '123' + '\u0000' + '45' + '\u001b[39m' + '6' + '\n' + '' + '\u001b[39m\u001b[22m') |
||
| 67 | assert.equal (first (s, 9), '\u001b[22m\u001b[1m' + '123' + '\u0000' + '45' + '\u001b[39m' + '67' + '\n' + '89' + '\u001b[39m\u001b[22m') |
||
| 68 | assert.equal (first (s, 100), '\u001b[22m\u001b[1m' + '123' + '\u0000' + '45' + '\u001b[39m' + '67' + '\n' + '89' + '\u001b[39m\u001b[22m') |
||
| 69 | }) |
||
| 70 | }) |