All files / plugins trim.js

15.62% Statements 5/32
0% Branches 0/24
0% Functions 0/5
20% Lines 5/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51      1x 1x   1x   1x                                           1x                                        
import {toStr} from "../helpers/string/to_string";
import { REGEXP_TRIM_LEFT, REGEXP_TRIM_RIGHT } from "../helpers/regexp/regexp";
 
const reduce = Array.prototype.reduce;
const reduceRight = Array.prototype.reduceRight;
 
export const trim = (s, ws) => ltrim(rtrim(s, ws), ws)
 
export const ltrim = (s, ws) => {
    let _s = toStr(s)
 
    if (!ws) {return _s.replace(REGEXP_TRIM_LEFT, '')}
    if (ws === '' || _s === '') {return _s}
    if (typeof ws !== "string") {ws = ''}
 
    let match = true
 
    return reduce.call(
        _s,
        (trimmed, char) => {
            if (match && ws.includes(char)) {
                return trimmed;
            }
            match = false
            return trimmed + char
        },
        ''
    );
}
 
export const rtrim = (s, ws) => {
    let _s = toStr(s)
 
    if (!ws) {return _s.replace(REGEXP_TRIM_RIGHT, '')}
    if (ws === '' || _s === '') {return _s}
    if (typeof ws !== "string") {ws = ''}
 
    let match = true
 
    return reduceRight.call(
        _s,
        (trimmed, char) => {
            if (match && ws.includes(char)) {
                return trimmed
            }
            match = false
            return char + trimmed
        },
        ''
    );
}