forked from editor-js/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.js
More file actions
35 lines (32 loc) · 905 Bytes
/
string.js
File metadata and controls
35 lines (32 loc) · 905 Bytes
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
/**
* Return the position of line starting from passed point
*
* ┌───────────────┐
* │1234\n │
* │2eda | dadd\n │ <-- returns 5
* └───────────────┘
*
* @param {string} string - string to process
* @param {number} position - search starting position
* @returns {number}
*/
export function getLineStartPosition(string, position) {
const charLength = 1;
let char = '';
/**
* Iterate through all the chars before the position till the
* - end of line (\n)
* - or start of string (position === 0)
*/
while (char !== '\n' && position > 0) {
position = position - charLength;
char = string.substr(position, charLength);
}
/**
* Do not count the linebreak symbol because it is related to the previous line
*/
if (char === '\n') {
position += 1;
}
return position;
}