@@ -6543,37 +6543,116 @@ R.searchFirstWith = p => rx => s => {
65436543R . searchLast = rx => s => {
65446544 const xs = s . matchAll ( rx ) ;
65456545 if ( xs . length === 0 ) return [ ] ;
6546- else return [ xs [ xs . length - 1 ] ] ;
6546+ else return [ xs [ xs . length - 1 ] . index ] ;
65476547} ;
65486548
65496549
65506550R . searchLastWith = p => rx => s => {
65516551 const xs = R . matchAllWith ( { p, rx} ) ( s ) ;
65526552 if ( xs . length === 0 ) return [ ] ;
6553- else return [ xs [ xs . length - 1 ] ] ;
6553+ else return [ xs [ xs . length - 1 ] . index ] ;
65546554} ;
65556555
65566556
6557+ // negative indices are processed relative to the end
6558+
65576559R . searchNth = ( rx , i ) => s => {
65586560 const xs = s . matchAll ( rx ) ;
65596561 if ( i - 1 >= xs . length ) return [ ] ;
6560- else return [ xs [ i - 1 ] ] ;
6562+ else if ( i >= 0 ) return [ xs [ i - 1 ] . index ] ;
6563+ else return [ xs . slice ( i ) [ 0 ] . index ] ;
65616564} ;
65626565
65636566
6567+ // negative indices are processed relative to the end
6568+
65646569R . searchNthWith = p => ( rx , i ) => s => {
65656570 const xs = R . matchAllWith ( { p, rx} ) ( s ) ;
65666571 if ( i - 1 >= xs . length ) return [ ] ;
6567- else return [ xs [ i - 1 ] ] ;
6572+ else if ( i >= 0 ) return [ xs [ i - 1 ] . index ] ;
6573+ else return [ xs . slice ( i ) [ 0 ] . index ] ;
6574+ } ;
6575+
6576+
6577+ //█████ Matching ██████████████████████████████████████████████████████████████
6578+
6579+
6580+ // strict variant
6581+
6582+ R . matchAll = rx => s => Array . from ( s . matchAll ( rx ) ) ;
6583+
6584+
6585+ R . matchAllWith = ( { p, rx} ) => s => Array . from ( s . matchAll ( rx ) ) . filter ( r => {
6586+ const [ match , ...xs ] = r ,
6587+ o = r . groups ,
6588+ i = r . index ;
6589+
6590+ return p ( { match, xs, i, o, s} ) ;
6591+ } ) ;
6592+
6593+
6594+
6595+ R . matchFirst = rx => s => {
6596+ if ( rx . flags . search ( "g" ) !== notFound )
6597+ throw new Err ( "unexpected global flag" ) ;
6598+
6599+ const r = s . match ( rx ) ;
6600+ if ( r === null ) return [ ] ;
6601+ else return [ r ] ;
6602+ } ;
6603+
6604+
6605+ R . matchFirstWith = ( { p, rx} ) => s => {
6606+ for ( const r of s . matchAll ( rx ) ) {
6607+ const [ match , ...xs ] = r ,
6608+ o = r . groups ,
6609+ i = r . index ;
6610+
6611+ if ( p ( { match, xs, i, o, s} ) ) return [ r ] ;
6612+ }
6613+
6614+ return [ ] ;
6615+ } ;
6616+
6617+
6618+ R . matchLast = rx => s => Array . from ( s . matchAll ( rx ) ) . slice ( - 1 ) ;
6619+
6620+
6621+ R . matchLastWith = ( { p, rx} ) => s =>
6622+ R . matchAllWith ( { p, rx} ) ( s ) . slice ( - 1 ) ;
6623+
6624+
6625+ // negative indices are processed relative to the end
6626+
6627+ R . matchNth = ( { i, rx} ) => s => {
6628+ const xs = Array . from ( s . matchAll ( rx ) ) ;
6629+ if ( i - 1 >= xs . length ) return [ ] ;
6630+ else if ( i >= 0 ) return [ xs [ i - 1 ] ] ;
6631+ else return [ xs . slice ( i ) [ 0 ] ] ;
6632+ } ;
6633+
6634+
6635+ // negative indices are processed relative to the end
6636+
6637+ R . matchNthWith = ( { p, i, rx} ) => s => {
6638+ const xs = R . matchAllWith ( { p, rx} ) ( s ) ,
6639+ o = xs [ i ] ;
6640+
6641+ if ( i - 1 >= xs . length ) return [ ] ;
6642+ else if ( i >= 0 ) return [ xs [ i - 1 ] ] ;
6643+ else return [ xs . slice ( i ) [ 0 ] ] ;
65686644} ;
65696645
65706646
65716647//█████ Splitting █████████████████████████████████████████████████████████████
65726648
65736649
6574- /* Split a string at the specified indices. If the supplied argument is an array
6575- of matches, the separators themselves are excluded from the result. If it is an
6576- array of indices, no substrings are removed during splitting. */
6650+ /* Split a string at the specified positions. If the supplied positions are
6651+ encoded as an array of matches created by a matching function, the separators
6652+ themselves are excluded from the result. If it is encoded as an array of indices
6653+ created by a searching function, separators are included. You can create a split
6654+ first or split last semantics simply by creating the positional argument using
6655+ the corresponding matching or searching function. */
65776656
65786657R . split = xs => s => {
65796658 if ( typeof xs [ 0 ] === "number" ) {
@@ -6616,7 +6695,7 @@ R.split = xs => s => {
66166695} ;
66176696
66186697
6619- // variant that additionally passes each split to a function
6698+ // variant that additionally passes each splitted substring to a function
66206699
66216700R . splitWith = ( { f, xs} ) => s => {
66226701 if ( typeof xs [ 0 ] === "number" ) {
@@ -6659,20 +6738,6 @@ R.splitWith = ({f, xs}) => s => {
66596738} ;
66606739
66616740
6662- // variant that splits only once
6663-
6664- R . split1 = x => s => {
6665- if ( typeof x === "number" ) {
6666- acc . push ( s . slice ( 0 , i ) ) ;
6667- acc . push ( s . slice ( i ) ) ;
6668- }
6669-
6670- else {
6671- acc . push ( s . slice ( 0 , o . index ) ) ;
6672- acc . push ( s . slice ( o . index + o [ 0 ] . length ) ) ;
6673- }
6674- } ;
6675-
66766741
66776742/* Split a string depending on character class transitions defined by regular
66786743expressions in the following form:
@@ -6739,76 +6804,6 @@ R.splitAllTrans = R.splitTrans("v") (
67396804 "(?<=\\p{Ll})(?=\\p{Lu})|(?<=\\p{Lu})(?=\\p{Ll})" ) ; // "fooBar" -> ["foo", "B", "ar"]
67406805
67416806
6742- //█████ Matching ██████████████████████████████████████████████████████████████
6743-
6744-
6745- // strict variant
6746-
6747- R . matchAll = rx => s => Array . from ( s . matchAll ( rx ) ) ;
6748-
6749-
6750- R . matchAllWith = ( { p, rx} ) => s => Array . from ( s . matchAll ( rx ) ) . filter ( r => {
6751- const [ match , ...xs ] = r ,
6752- o = r . groups ,
6753- i = r . index ;
6754-
6755- return p ( { match, xs, i, o, s} ) ;
6756- } ) ;
6757-
6758-
6759-
6760- R . matchFirst = rx => s => {
6761- if ( rx . flags . search ( "g" ) !== notFound )
6762- throw new Err ( "unexpected global flag" ) ;
6763-
6764- const r = s . match ( rx ) ;
6765- if ( r === null ) return [ ] ;
6766- else return [ r ] ;
6767- } ;
6768-
6769-
6770- R . matchFirstWith = ( { p, rx} ) => s => {
6771- for ( const r of s . matchAll ( rx ) ) {
6772- const [ match , ...xs ] = r ,
6773- o = r . groups ,
6774- i = r . index ;
6775-
6776- if ( p ( { match, xs, i, o, s} ) ) return [ r ] ;
6777- }
6778-
6779- return [ ] ;
6780- } ;
6781-
6782-
6783- R . matchLast = rx => s => Array . from ( s . matchAll ( rx ) ) . slice ( - 1 ) ;
6784-
6785-
6786- R . matchLastWith = ( { p, rx} ) => s =>
6787- R . matchAllWith ( { p, rx} ) ( s ) . slice ( - 1 ) ;
6788-
6789-
6790- // considers negative indices like native slice does
6791-
6792- R . matchNth = ( { i, rx} ) => s => {
6793- const xs = Array . from ( s . matchAll ( rx ) ) ;
6794- if ( i - 1 >= xs . length ) return [ ] ;
6795- else if ( i >= 0 ) return [ xs [ i - 1 ] ] ;
6796- else return [ xs . slice ( i ) [ 0 ] ] ;
6797- } ;
6798-
6799-
6800- // considers negative indices like native slice does
6801-
6802- R . matchNthWith = ( { p, i, rx} ) => s => {
6803- const xs = R . matchAllWith ( { p, rx} ) ( s ) ,
6804- o = xs [ i ] ;
6805-
6806- if ( i - 1 >= xs . length ) return [ ] ;
6807- else if ( i >= 0 ) return [ xs [ i - 1 ] ] ;
6808- else return [ xs . slice ( i ) [ 0 ] ] ;
6809- } ;
6810-
6811-
68126807//█████ Replacing █████████████████████████████████████████████████████████████
68136808
68146809
0 commit comments