Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
daf68394
Commit
daf68394
authored
Apr 20, 2023
by
Alejandro Celaya
Committed by
Alejandro Celaya
Apr 20, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate normalize module to TS
parent
672324e4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
22 deletions
+25
-22
normalize.ts
src/annotator/util/normalize.ts
+25
-22
No files found.
src/annotator/util/normalize.
j
s
→
src/annotator/util/normalize.
t
s
View file @
daf68394
/**
* Find the smallest offset in `str` which contains at least `count` chars
* that match `filter` before it.
*
* @param {string} str
* @param {number} count
* @param {(char: string) => boolean} filter
* @param {number} [startPos]
*/
function
advance
(
str
,
count
,
filter
,
startPos
=
0
)
{
function
advance
(
str
:
string
,
count
:
number
,
filter
:
(
char
:
string
)
=>
boolean
,
startPos
=
0
):
number
{
let
pos
=
startPos
;
while
(
pos
<
str
.
length
&&
count
>
0
)
{
if
(
filter
(
str
[
pos
]))
{
...
...
@@ -20,13 +20,13 @@ function advance(str, count, filter, startPos = 0) {
/**
* Count characters which match `filter` in `str`.
*
* @param {string} str
* @param {(char: string) => boolean} filter
* @param {number} startPos
* @param {number} endPos
*/
function
countChars
(
str
,
filter
,
startPos
,
endPos
)
{
function
countChars
(
str
:
string
,
filter
:
(
char
:
string
)
=>
boolean
,
startPos
:
number
,
endPos
:
number
):
number
{
let
count
=
0
;
for
(
let
pos
=
startPos
;
pos
<
endPos
;
pos
++
)
{
if
(
filter
(
str
[
pos
]))
{
...
...
@@ -58,20 +58,23 @@ function countChars(str, filter, startPos, endPos) {
* // The returned offsets select the substring "b c" in the "output" argument.
* translateOffsets('abcd', ' a b c d ', 1, 3, char => char !== ' ')
*
* @param {string} input
* @param {string} output
* @param {number} start - Start offset in `input`
* @param {number} end - End offset in `input`
* @param {(ch: string) => boolean} filter - Filter function that returns true
* if a character should be counted when relating positions between `input`
* and `output`.
* @return {[number, number]} - Start and end offsets in `output`
* @param start - Start offset in `input`
* @param end - End offset in `input`
* @param filter - Filter function that returns true if a character should be
* counted when relating positions between `input` and `output`.
* @return Start and end offsets in `output`
*/
export
function
translateOffsets
(
input
,
output
,
start
,
end
,
filter
)
{
export
function
translateOffsets
(
input
:
string
,
output
:
string
,
start
:
number
,
end
:
number
,
filter
:
(
ch
:
string
)
=>
boolean
):
[
number
,
number
]
{
const
beforeStartCount
=
countChars
(
input
,
filter
,
0
,
start
);
const
startToEndCount
=
countChars
(
input
,
filter
,
start
,
end
);
// Find smallest offset in `output` with same number of non-ignored characters
// Find
the
smallest offset in `output` with same number of non-ignored characters
// before it as before `start` in the input. This offset might correspond to
// an ignored character.
let
outputStart
=
advance
(
output
,
beforeStartCount
,
filter
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment