test.js   A
last analyzed

Complexity

Total Complexity 7
Complexity/F 1

Size

Lines of Code 77
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 0 Features 1
Metric Value
cc 0
wmc 7
c 9
b 0
f 1
nc 1
mnd 0
bc 7
fnc 7
dl 0
loc 77
rs 10
bpm 1
cpm 1
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A ➔ ??? 0 63 1
1
"use strict";
2
3
const assert              = require ('assert')
4
const printableCharacters = require (process.env.PRINTABLE_CHARACTERS_TEST_FILE)
5
6
// cannot use spread operator in tests due to Node v4 compatibility requirements...
7
const strlen              = printableCharacters.strlen
8
    , isBlank             = printableCharacters.isBlank
9
    , blank               = printableCharacters.blank
10
    , ansiEscapeCodes     = printableCharacters.ansiEscapeCodes
11
    , zeroWidthCharacters = printableCharacters.zeroWidthCharacters
12
    , partition           = printableCharacters.partition
13
    , first               = printableCharacters.first
14
15
describe ('printable-characters', () => {
16
17
    it ('determines visible length', () => {
18
19
        assert.equal (strlen ('💩'), 1)
20
        //assert.equal (strlen ('👩‍❤️‍💋‍👩'), 1) // FAILING, see http://blog.jonnew.com/posts/poo-dot-length-equals-two for possible solution
21
        assert.equal (strlen ('❤️'), 1)
22
        assert.equal (strlen ('foo bar'), 7)
23
        assert.equal (strlen ('\u001b[106mfoo bar\u001b[49m'), 7)
24
    })
25
26
    it ('detects blank text', () => {
27
28
        assert (!isBlank ('💩'))
29
        assert (!isBlank ('foobar'))
30
        assert ( isBlank ('\u001b[106m  \t  \t   \n     \u001b[49m'))
31
    })
32
33
    it ('matches zero-width characters and ANSI escape codes', () => {
34
35
        let s = '\u001b[106m' + 'foo' + '\n\n' + 'bar' + '\u001b[49m'
36
37
        assert (s = s.replace (ansiEscapeCodes, ''), 'foo\n\nbar')
38
        assert (    s.replace (zeroWidthCharacters, ''), 'foobar')
39
    })
40
41
    it ('obtains blank string of the same width', () => {
42
43
        assert.equal (blank ('💩'), ' ')
44
        //assert.equal (blank ('👩‍❤️‍💋‍👩'), ' ') // FAILING, see http://blog.jonnew.com/posts/poo-dot-length-equals-two for possible solution
45
        assert.equal (blank ('❤️'), ' ')
46
        assert.equal (blank ('foo'), '   ')
47
        assert.equal (blank ('\n'), '\n')
48
        assert.equal (blank ('\t'), '\t')
49
        assert.equal (blank ('\tfoo \nfoo'), '\t    \n   ')
50
        assert.equal (blank ('\u001b[22m\u001b[1mfoo \t\u001b[39m\u001b[22m'), '    \t')
51
    })
52
53
    it ('extracts invisible parts followed by visible ones', () => {
54
55
        assert.deepEqual (partition (''),                        [                                                     ])
56
        assert.deepEqual (partition ('foo'),                     [['',          'foo']                                 ])
57
        assert.deepEqual (partition ('\u001b[1mfoo'),            [['\u001b[1m', 'foo']                                 ])
58
        assert.deepEqual (partition ('\u001b[1mfoo\u0000bar'),   [['\u001b[1m', 'foo'],   ['\u0000', 'bar']            ])
59
        assert.deepEqual (partition ('\u001b[1mfoo\u0000bar\n'), [['\u001b[1m', 'foo'],   ['\u0000', 'bar'], ['\n', '']])
60
    })
61
62
    it ('gets first N visible symbols (preserving invisible parts)', () => {
63
   
64
        assert.equal (first ('💩23456789', 0),   '')
65
        assert.equal (first ('💩23456789', 3),   '💩23')
66
        assert.equal (first ('💩23456789', 100), '💩23456789')
67
68
        const s = '\u001b[22m\u001b[1m' + '💩23' + '\u0000' + '45' + '\u001b[39m' + '67' + '\n' + '89' + '\u001b[39m\u001b[22m'
69
        
70
        assert.equal (first (s, 0),   '\u001b[22m\u001b[1m' + ''    + '\u0000' + ''   + '\u001b[39m' + ''   + '\n' + ''   + '\u001b[39m\u001b[22m')
71
        assert.equal (first (s, 3),   '\u001b[22m\u001b[1m' + '💩23' + '\u0000' + ''   + '\u001b[39m' + ''   + '\n' + ''   + '\u001b[39m\u001b[22m')
72
        assert.equal (first (s, 4),   '\u001b[22m\u001b[1m' + '💩23' + '\u0000' + '4'  + '\u001b[39m' + ''   + '\n' + ''   + '\u001b[39m\u001b[22m')
73
        assert.equal (first (s, 6),   '\u001b[22m\u001b[1m' + '💩23' + '\u0000' + '45' + '\u001b[39m' + '6'  + '\n' + ''   + '\u001b[39m\u001b[22m')
74
        assert.equal (first (s, 9),   '\u001b[22m\u001b[1m' + '💩23' + '\u0000' + '45' + '\u001b[39m' + '67' + '\n' + '89' + '\u001b[39m\u001b[22m')
75
        assert.equal (first (s, 100), '\u001b[22m\u001b[1m' + '💩23' + '\u0000' + '45' + '\u001b[39m' + '67' + '\n' + '89' + '\u001b[39m\u001b[22m')
76
    })
77
})