Completed
Push — master ( 45650c...c08a5d )
by Vitaly
26s
created

printable-characters.js   A

Complexity

Total Complexity 8
Complexity/F 1.6

Size

Lines of Code 44
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 0
wmc 8
c 4
b 0
f 0
nc 4
mnd 1
bc 4
fnc 5
dl 0
loc 44
rs 10
bpm 0.8
cpm 1.6
noi 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A module.exports.first 0 12 2
A module.exports.partition 0 5 3
A ➔ ??? 0 2 1
1
"use strict";
2
3
const ansiEscapeCode                   = '[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-PRZcf-nqry=><]'
4
    , zeroWidthCharacterExceptNewline  = '\u0000-\u0008\u000B-\u0019\u001b\u009b\u00ad\u200b\u2028\u2029\ufeff'
5
    , zeroWidthCharacter               = '\n' + zeroWidthCharacterExceptNewline
6
    , zeroWidthCharactersExceptNewline = new RegExp ('(?:' + ansiEscapeCode + ')|[' + zeroWidthCharacterExceptNewline + ']', 'g')
7
    , zeroWidthCharacters              = new RegExp ('(?:' + ansiEscapeCode + ')|[' + zeroWidthCharacter + ']', 'g')
8
    , partition                        = new RegExp ('((?:' + ansiEscapeCode + ')|[\t' + zeroWidthCharacter + '])?([^\t' + zeroWidthCharacter + ']*)', 'g')
9
10
module.exports = {
11
12
    zeroWidthCharacters,
13
14
    ansiEscapeCodes: new RegExp (ansiEscapeCode, 'g'),
15
16
    strlen: s => s.replace (zeroWidthCharacters, '')
17
                  .length,
18
19
    isBlank: s => s.replace (zeroWidthCharacters, '')
20
                   .replace (/\s/g, '')
21
                   .length === 0,
22
23
    blank: s => s.replace (zeroWidthCharactersExceptNewline, '')
24
                 .replace (/[^\t\n]/g, ' '),
25
26
    partition (s) {
27
        for (var m, spans = []; (partition.lastIndex !== s.length) && (m = partition.exec (s));) { spans.push ([m[1] || '', m[2]]) }
28
        partition.lastIndex = 0 // reset
29
        return spans
30
    },
31
32
    first (s, n) {
33
34
        let result = '', length = 0
35
36
        for (const [nonPrintable, printable] of module.exports.partition (s)) {
37
            const text = printable.substr (0, n - length)
38
            result += nonPrintable + text
39
            length += text.length
40
        }
41
42
        return result
43
    }
44
}