@@ -58,20 +58,42 @@ function getLastIndex(
5858/**
5959 * Normalizes the options for cursor methods.
6060 */
61- function normalizeOptions ( options : CursorWithSkipOptions | undefined ) : {
62- includeComments : boolean ;
63- filter : FilterPredicate | null ;
61+ function normalizeSkipOptions ( options : CursorWithSkipOptions | undefined ) : {
62+ filter : FilterPredicate ;
6463 skip : number ;
6564} {
6665 if ( typeof options === "number" ) {
67- return { includeComments : false , filter : null , skip : options } ;
66+ return { filter : isNotComment , skip : options } ;
6867 }
6968 if ( typeof options === "function" ) {
70- return { includeComments : false , filter : options , skip : 0 } ;
69+ return {
70+ filter : ( n ) => {
71+ if ( isComment ( n ) ) {
72+ return false ;
73+ }
74+ return options ( n ) ;
75+ } ,
76+ skip : 0 ,
77+ } ;
78+ }
79+ let filter : FilterPredicate ;
80+ if ( options ?. includeComments ) {
81+ filter = options ?. filter ?? ( ( ) => true ) ;
82+ } else {
83+ if ( options ?. filter ) {
84+ const baseFilter = options ?. filter ;
85+ filter = ( token ) => {
86+ if ( isComment ( token ) ) {
87+ return false ;
88+ }
89+ return baseFilter ( token ) ;
90+ } ;
91+ } else {
92+ filter = isNotComment ;
93+ }
7194 }
7295 return {
73- includeComments : options ?. includeComments ?? false ,
74- filter : options ?. filter ?? null ,
96+ filter,
7597 skip : options ?. skip ?? 0 ,
7698 } ;
7799}
@@ -94,7 +116,7 @@ function isNotComment(token: YAMLToken): boolean {
94116 * Normalizes the options for cursor methods with count.
95117 */
96118function normalizeCountOptions ( options : CursorWithCountOptions | undefined ) : {
97- filter : FilterPredicate | null ;
119+ filter : FilterPredicate ;
98120 count : number ;
99121} {
100122 if ( typeof options === "number" ) {
@@ -111,11 +133,13 @@ function normalizeCountOptions(options: CursorWithCountOptions | undefined): {
111133 count : 0 ,
112134 } ;
113135 }
114- let filter = options ?. filter ;
115- if ( ! options ?. includeComments ) {
116- if ( filter ) {
117- const baseFilter = filter ;
118- filter = ( token : YAMLToken ) => {
136+ let filter : FilterPredicate ;
137+ if ( options ?. includeComments ) {
138+ filter = options ?. filter ?? ( ( ) => true ) ;
139+ } else {
140+ if ( options ?. filter ) {
141+ const baseFilter = options ?. filter ;
142+ filter = ( token ) => {
119143 if ( isComment ( token ) ) {
120144 return false ;
121145 }
@@ -126,7 +150,7 @@ function normalizeCountOptions(options: CursorWithCountOptions | undefined): {
126150 }
127151 }
128152 return {
129- filter : filter ?? null ,
153+ filter,
130154 count : options ?. count ?? 0 ,
131155 } ;
132156}
@@ -165,7 +189,7 @@ export class TokenStore {
165189 node : YAMLSyntaxElement ,
166190 options ?: CursorWithSkipOptions ,
167191 ) : YAMLToken | null {
168- const { filter, skip } = normalizeOptions ( options ) ;
192+ const { filter, skip } = normalizeSkipOptions ( options ) ;
169193 const startIndex = getFirstIndex (
170194 this . allTokens ,
171195 this . tokenStartToIndex ,
@@ -199,7 +223,7 @@ export class TokenStore {
199223 node : YAMLSyntaxElement ,
200224 options ?: CursorWithSkipOptions ,
201225 ) : YAMLToken | null {
202- const { filter, skip } = normalizeOptions ( options ) ;
226+ const { filter, skip } = normalizeSkipOptions ( options ) ;
203227 const startIndex = getFirstIndex (
204228 this . allTokens ,
205229 this . tokenStartToIndex ,
@@ -234,7 +258,7 @@ export class TokenStore {
234258 right : YAMLSyntaxElement ,
235259 options ?: CursorWithSkipOptions ,
236260 ) : YAMLToken | null {
237- const { filter, skip } = normalizeOptions ( options ) ;
261+ const { filter, skip } = normalizeSkipOptions ( options ) ;
238262 const startIndex = getFirstIndex (
239263 this . allTokens ,
240264 this . tokenStartToIndex ,
@@ -268,7 +292,7 @@ export class TokenStore {
268292 node : YAMLSyntaxElement ,
269293 options ?: CursorWithSkipOptions ,
270294 ) : YAMLToken | null {
271- const { filter, skip } = normalizeOptions ( options ) ;
295+ const { filter, skip } = normalizeSkipOptions ( options ) ;
272296 const endIndex = getLastIndex (
273297 this . allTokens ,
274298 this . tokenStartToIndex ,
@@ -297,7 +321,7 @@ export class TokenStore {
297321 node : YAMLSyntaxElement ,
298322 options ?: CursorWithSkipOptions ,
299323 ) : YAMLToken | null {
300- const { filter, skip } = normalizeOptions ( options ) ;
324+ const { filter, skip } = normalizeSkipOptions ( options ) ;
301325 const startIndex = getFirstIndex (
302326 this . allTokens ,
303327 this . tokenStartToIndex ,
0 commit comments