printable-characters.js   A
last analyzed

Complexity

Total Complexity 11
Complexity/F 1.83

Size

Lines of Code 44
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 0
wmc 11
c 4
b 0
f 0
nc 16
mnd 1
bc 4
fnc 6
dl 0
loc 44
rs 10
bpm 0.6666
cpm 1.8333
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 1 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\ufe00-\ufe0f'
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 => Array.from (s.replace (zeroWidthCharacters, '')).length, // Array.from solves the emoji problem as described here: http://blog.jonnew.com/posts/poo-dot-length-equals-two
17
18
    isBlank: s => s.replace (zeroWidthCharacters, '')
19
                   .replace (/\s/g, '')
20
                   .length === 0,
21
22
    blank: s => Array.from (s.replace (zeroWidthCharactersExceptNewline, '')) // Array.from solves the emoji problem as described here: http://blog.jonnew.com/posts/poo-dot-length-equals-two
23
                     .map (x => ((x === '\t') || (x === '\n')) ? x : ' ')
24
                     .join (''),
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 = Array.from (printable).slice (0, n - length) // Array.from solves the emoji problem as described here: http://blog.jonnew.com/posts/poo-dot-length-equals-two
38
            result += nonPrintable + text.join ('')
39
            length += text.length
40
        }
41
42
        return result
43
    }
44
}