Skip to content

Commit aba4e75

Browse files
add R.matchUntil
1 parent 821a06b commit aba4e75

File tree

1 file changed

+36
-49
lines changed

1 file changed

+36
-49
lines changed

scriptum.js

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5693,7 +5693,7 @@ Parser.nat = ({inclZero, _throw = false}) => s => {
56935693
original: s,
56945694
});
56955695

5696-
else if (_throw) {debugger;throw new Err(`malformed natural number "${s}"`);}
5696+
else if (_throw) throw new Err(`malformed natural number "${s}"`);
56975697

56985698
else return Parser.Invalid({
56995699
value: s,
@@ -6626,21 +6626,21 @@ R.searchLastWith = p => rx => s => {
66266626

66276627
// negative indices are processed relative to the end
66286628

6629-
R.searchNth = (rx, i) => s => {
6629+
R.searchNth = (rx, n) => s => {
66306630
const xs = s.matchAll(rx);
6631-
if (i - 1 >= xs.length) return [];
6632-
else if (i >= 0) return [xs[i - 1].index];
6633-
else return [xs.slice(i) [0].index];
6631+
if (n - 1 >= xs.length) return [];
6632+
else if (n >= 0) return [xs[n - 1].index];
6633+
else return [xs.slice(n) [0].index];
66346634
};
66356635

66366636

66376637
// negative indices are processed relative to the end
66386638

6639-
R.searchNthWith = p => (rx, i) => s => {
6639+
R.searchNthWith = p => (rx, n) => s => {
66406640
const xs = R.matchAllWith({p, rx}) (s);
6641-
if (i - 1 >= xs.length) return [];
6642-
else if (i >= 0) return [xs[i - 1].index];
6643-
else return [xs.slice(i) [0].index];
6641+
if (n - 1 >= xs.length) return [];
6642+
else if (n >= 0) return [xs[n - 1].index];
6643+
else return [xs.slice(n) [0].index];
66446644
};
66456645

66466646

@@ -6694,23 +6694,29 @@ R.matchLastWith = ({p, rx}) => s =>
66946694

66956695
// negative indices are processed relative to the end
66966696

6697-
R.matchNth = ({i, rx}) => s => {
6697+
R.matchNth = ({n, rx}) => s => {
66986698
const xs = Array.from(s.matchAll(rx));
6699-
if (i - 1 >= xs.length) return [];
6700-
else if (i >= 0) return [xs[i - 1]];
6701-
else return [xs.slice(i) [0]];
6699+
if (n - 1 >= xs.length) return [];
6700+
else if (n >= 0) return [xs[n - 1]];
6701+
else return [xs.slice(n) [0]];
67026702
};
67036703

67046704

67056705
// negative indices are processed relative to the end
67066706

6707-
R.matchNthWith = ({p, i, rx}) => s => {
6707+
R.matchNthWith = ({p, n, rx}) => s => {
67086708
const xs = R.matchAllWith({p, rx}) (s),
6709-
o = xs[i];
6709+
if (n - 1 >= xs.length) return [];
6710+
else if (n >= 0) return [xs[n - 1]];
6711+
else return [xs.slice(n) [0]];
6712+
};
6713+
67106714

6711-
if (i - 1 >= xs.length) return [];
6712-
else if (i >= 0) return [xs[i - 1]];
6713-
else return [xs.slice(i) [0]];
6715+
// negative indices are processed relative to the end
6716+
6717+
R.matchUntil = ({n, rx}) => s => {
6718+
const xs = Array.from(s.matchAll(rx));
6719+
return xs.slice(0, n)
67146720
};
67156721

67166722

@@ -7067,57 +7073,38 @@ R.replaceLastBy = ({p, f, rx}) => s => {
70677073

70687074
// considers negative indices like native slice does
70697075

7070-
R.replaceNth = ({i, sub, rx}) => s => {
7076+
R.replaceNth = ({n, sub, rx}) => s => {
70717077
if (rx.flags.search("g") === notFound)
70727078
throw new Err("missing global flag");
70737079

70747080
const xs = Array.from(s.matchAll(rx));
70757081

7076-
if (i - 1 >= xs.length) return s;
7082+
if (n - 1 >= xs.length) return s;
70777083

70787084
else {
7079-
const match = i < 0 ? xs.slice(i) [0] : xs[i - 1],
7080-
i2 = match.index;
7085+
const match = n < 0 ? xs.slice(n) [0] : xs[n - 1],
7086+
n2 = match.index;
70817087

7082-
return s.slice(0, i2) + sub + s.slice(i2 + match.length);
7088+
return s.slice(0, n2) + sub + s.slice(n2 + match.length);
70837089
}
70847090
};
70857091

70867092

70877093
// considers negative indices like native slice does
70887094

7089-
R.replaceNthWith = ({i, f, rx}) => s => {
7095+
R.replaceNthWith = ({n, f, rx}) => s => {
70907096
const xs = Array.from(s.matchAll(rx));
70917097

7092-
if (i - 1 >= xs.length) return s;
7098+
if (n - 1 >= xs.length) return s;
70937099

70947100
else {
7095-
const r = i < 0 ? xs.slice(i) [0] : xs[i - 1],
7101+
const r = n < 0 ? xs.slice(n) [0] : xs[n - 1],
70967102
[match, ...ys] = r,
70977103
o = r.groups,
7098-
i2 = r.index;
7104+
n2 = r.index;
70997105

7100-
const sub = f({match, xs: ys, i: i2, o, s});
7101-
return s.slice(0, i2) + sub + s.slice(i2 + match.length);
7102-
}
7103-
};
7104-
7105-
7106-
// considers negative indices like native slice does
7107-
7108-
R.replaceNthBy = ({i, f, rx}) => s => {
7109-
const xs = R.matchAllWith({p, rx}) (s);
7110-
7111-
if (i - 1 >= xs.length) return s;
7112-
7113-
else {
7114-
const r = i < 0 ? xs.slice(i) [0] : xs[i - 1],
7115-
[match, ...ys] = r,
7116-
o = r.groups,
7117-
i2 = r.index;
7118-
7119-
const sub = f({match, xs: ys, i: i2, o, s});
7120-
return s.slice(0, i2) + sub + s.slice(i2 + match.length);
7106+
const sub = f({match, xs: ys, i: n2, o, s});
7107+
return s.slice(0, n2) + sub + s.slice(n2 + match.length);
71217108
}
71227109
};
71237110

@@ -8630,7 +8617,7 @@ R.Context.properName = ({locale, inclNoble = false}) => tokens => {
86308617
offset = [0, 0];
86318618

86328619
let valid = true;
8633-
debugger;
8620+
86348621
if (affixes.pre) {
86358622
if (tokens[i - 1] === " "
86368623
&& tokens[i - 2] === affixes.pre) {

0 commit comments

Comments
 (0)