Skip to content

Commit

Permalink
type: Fix types errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Sep 8, 2021
1 parent 0985e20 commit b4be484
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
30 changes: 0 additions & 30 deletions src/utils/getSurroundingWord.ts

This file was deleted.

30 changes: 29 additions & 1 deletion src/utils/markdownUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { TextRange } from '../commands';
import { getSurroundingWord } from './getSurroundingWord';

export interface TextSection {
text: string;
Expand Down Expand Up @@ -72,3 +71,32 @@ export function getBreaksNeededForEmptyLineAfter(text = '', startPosition: numbe
}
return isInLastLine ? 0 : neededBreaks;
}

export function getSurroundingWord(text: string, position: number): TextRange {
if (!text) throw Error("Argument 'text' should be truthy");

const isWordDelimiter = (c: string) => c === ' ' || c.charCodeAt(0) === 10;

// leftIndex is initialized to 0 because if selection is 0, it won't even enter the iteration
let start = 0;
// rightIndex is initialized to text.length because if selection is equal to text.length it won't even enter the interation
let end = text.length;

// iterate to the left
for (let i = position; i - 1 > -1; i--) {
if (isWordDelimiter(text[i - 1])) {
start = i;
break;
}
}

// iterate to the right
for (let i = position; i < text.length; i++) {
if (isWordDelimiter(text[i])) {
end = i;
break;
}
}

return { start, end };
}
2 changes: 1 addition & 1 deletion test/utils/getSurroundingWord.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
/* eslint-disable jest/no-conditional-expect */
import '@testing-library/jest-dom';
import { getSurroundingWord } from '../../src/utils';
import { getSurroundingWord } from '../../src/utils/markdownUtils';

it('getSurroundingWord', () => {
expect(getSurroundingWord('hello world', 0)).toMatchObject({
Expand Down

1 comment on commit b4be484

@vercel
Copy link

@vercel vercel bot commented on b4be484 Sep 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.