Skip to content

Commit b4be484

Browse files
committedSep 8, 2021
type: Fix types errors.
1 parent 0985e20 commit b4be484

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed
 

‎src/utils/getSurroundingWord.ts

-30
This file was deleted.

‎src/utils/markdownUtils.ts

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { TextRange } from '../commands';
2-
import { getSurroundingWord } from './getSurroundingWord';
32

43
export interface TextSection {
54
text: string;
@@ -72,3 +71,32 @@ export function getBreaksNeededForEmptyLineAfter(text = '', startPosition: numbe
7271
}
7372
return isInLastLine ? 0 : neededBreaks;
7473
}
74+
75+
export function getSurroundingWord(text: string, position: number): TextRange {
76+
if (!text) throw Error("Argument 'text' should be truthy");
77+
78+
const isWordDelimiter = (c: string) => c === ' ' || c.charCodeAt(0) === 10;
79+
80+
// leftIndex is initialized to 0 because if selection is 0, it won't even enter the iteration
81+
let start = 0;
82+
// rightIndex is initialized to text.length because if selection is equal to text.length it won't even enter the interation
83+
let end = text.length;
84+
85+
// iterate to the left
86+
for (let i = position; i - 1 > -1; i--) {
87+
if (isWordDelimiter(text[i - 1])) {
88+
start = i;
89+
break;
90+
}
91+
}
92+
93+
// iterate to the right
94+
for (let i = position; i < text.length; i++) {
95+
if (isWordDelimiter(text[i])) {
96+
end = i;
97+
break;
98+
}
99+
}
100+
101+
return { start, end };
102+
}

‎test/utils/getSurroundingWord.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
/* eslint-disable jest/no-conditional-expect */
55
import '@testing-library/jest-dom';
6-
import { getSurroundingWord } from '../../src/utils';
6+
import { getSurroundingWord } from '../../src/utils/markdownUtils';
77

88
it('getSurroundingWord', () => {
99
expect(getSurroundingWord('hello world', 0)).toMatchObject({

1 commit comments

Comments
 (1)

vercel[bot] commented on Sep 8, 2021

@vercel[bot]
Please sign in to comment.