all files / component/editor/ string.utils.ts

12.5% Statements 7/56
0% Branches 0/32
16.67% Functions 1/6
10.91% Lines 6/55
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143                                                                                                                                                                                                                                                                                 
/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
 
 export class StringUtils {
    public static getLastWord(from: string, cursorPosition: number) {
        if (!from) {
            return null;
        }
        if (from.length > cursorPosition) {
            from =
                from.substr(0, cursorPosition) +
                from.substr(cursorPosition).split(/[\s,;.:]/)[0];
        }
        if (!from) {
            return null;
        }
        if (from.match(/[\s,.!?;:]$/)) {
            return null;
        }
        const stringArray = from.split(/[\s,.!?;:]/);
        return stringArray[stringArray.length - 1];
    }
 
    public static insert(
        from: string,
        newValue: string,
        cursorStartPosition: number,
        cursorEndPosition: number
    ) {
        if (cursorStartPosition === cursorEndPosition) {
            const result = {
                value:
                    from.substring(
                        0,
                        cursorStartPosition === 0 ? 0 : cursorStartPosition + 1
                    ) + newValue,
                newStartPosition: 0,
                newEndPosition: 0
            };
            result.newStartPosition = result.value.length;
            result.newEndPosition = result.value.length;
            return result;
        } else {
            const result = {
                value: from.substring(0, cursorStartPosition),
                newStartPosition: 0,
                newEndPosition: 0
            };
            result.newStartPosition = result.value.length;
            result.value += newValue;
            result.newEndPosition = result.value.length;
            result.value = result.value + from.substring(cursorEndPosition);
            return result;
        }
    }
 
    public static replace(
        from: string,
        lastValue: string,
        newValue: string,
        cursorStartPosition: number,
        cursorEndPosition: number
    ): { newStartPosition: number; newEndPosition: number; value: string } {
        if (cursorStartPosition === cursorEndPosition) {
            let position = cursorStartPosition;
            if (from.charAt(position).match(/[\s,;.:]/)) {
                position--;
            }
            while (!from.charAt(position).match(/[\s,;.:]/) && position > 0) {
                position--;
            }
            const result = {
                value:
                    from.substring(0, position === 0 ? 0 : position + 1) +
                    newValue,
                newStartPosition: 0,
                newEndPosition: 0
            };
            result.newStartPosition = result.value.length;
            result.newEndPosition = result.value.length;
            result.value =
                result.value +
                from.substring(
                    (position === 0 ? 0 : position + 1) +
                        (lastValue ? lastValue.length : 0)
                );
 
            return result;
        } else {
            const result = {
                value: from.substring(0, cursorStartPosition),
                newStartPosition: 0,
                newEndPosition: 0
            };
            result.newStartPosition = result.value.length;
            result.value += newValue;
            result.newEndPosition = result.value.length;
            result.value = result.value + from.substring(cursorEndPosition);
            return result;
        }
    }
 
    public static removeLastWord(
        from: string,
        cursorStartPosition: number,
        cursorEndPosition: number
    ): { startValue: string; endValue: string } {
        if (cursorStartPosition === cursorEndPosition) {
            let position = cursorStartPosition;
            while (!from.charAt(position).match(/[\s,;.:]/) && position > 0) {
                position--;
            }
            let endPosition = cursorEndPosition + 1;
            while (
                !from.charAt(endPosition).match(/[\s,;.:]/) &&
                endPosition < from.length
            ) {
                endPosition++;
            }
            const result = {
                startValue: from.substring(
                    0,
                    position === 0 ? 0 : position + 1
                ),
                endValue: from.substring(endPosition)
            };
            return result;
        } else {
            const result = {
                startValue: from.substring(0, cursorStartPosition),
                endValue: from.substring(cursorEndPosition)
            };
            return result;
        }
    }
}