@@ -6,7 +6,12 @@ import type { Schema } from '../schema/Schema.ts'
66import type { StringifyContext } from '../stringify/stringify.ts'
77import { stringifyCollection } from '../stringify/stringifyCollection.ts'
88import { addPairToJSMap } from './addPairToJSMap.ts'
9- import type { CollectionBase , NodeOf , Primitive } from './Collection.ts'
9+ import {
10+ copyCollection ,
11+ type CollectionBase ,
12+ type NodeOf ,
13+ type Primitive
14+ } from './Collection.ts'
1015import { isNode } from './identity.ts'
1116import type { Node , Range } from './Node.ts'
1217import { Pair } from './Pair.ts'
@@ -33,9 +38,10 @@ export function findPair<
3338export class YAMLMap <
3439 K extends Primitive | Node = Primitive | Node ,
3540 V extends Primitive | Node = Primitive | Node
36- > implements CollectionBase {
37- items : Pair < K , V > [ ] = [ ]
38-
41+ >
42+ extends Array < Pair < K , V > >
43+ implements CollectionBase
44+ {
3945 schema : Schema | undefined
4046
4147 /** An optional anchor on this collection. Used by alias nodes. */
@@ -84,20 +90,21 @@ export class YAMLMap<
8490 if ( typeof replacer === 'function' ) value = replacer . call ( obj , key , value )
8591 else if ( Array . isArray ( replacer ) && ! replacer . includes ( key ) ) return
8692 if ( value !== undefined || nc . keepUndefined )
87- map . items . push ( nc . createPair ( key , value ) )
93+ map . push ( nc . createPair ( key , value ) )
8894 }
8995 if ( obj instanceof Map ) {
9096 for ( const [ key , value ] of obj ) add ( key , value )
9197 } else if ( obj && typeof obj === 'object' ) {
9298 for ( const key of Object . keys ( obj ) ) add ( key , ( obj as any ) [ key ] )
9399 }
94100 if ( typeof nc . schema . sortMapEntries === 'function' ) {
95- map . items . sort ( nc . schema . sortMapEntries )
101+ map . sort ( nc . schema . sortMapEntries )
96102 }
97103 return map
98104 }
99105
100- constructor ( schema ?: Schema ) {
106+ constructor ( schema ?: Schema , elements : Array < Pair < K , V > > = [ ] ) {
107+ super ( ...elements )
101108 Object . defineProperty ( this , 'schema' , {
102109 value : schema ,
103110 configurable : true ,
@@ -112,14 +119,7 @@ export class YAMLMap<
112119 * @param schema - If defined, overwrites the original's schema
113120 */
114121 clone ( schema ?: Schema ) : this {
115- const copy : this = Object . create (
116- Object . getPrototypeOf ( this ) ,
117- Object . getOwnPropertyDescriptors ( this )
118- )
119- if ( schema ) copy . schema = schema
120- copy . items = copy . items . map ( it => it . clone ( schema ) )
121- if ( this . range ) copy . range = [ ...this . range ]
122- return copy
122+ return copyCollection ( this , schema )
123123 }
124124
125125 /**
@@ -130,36 +130,36 @@ export class YAMLMap<
130130 add ( pair : Pair < K , V > ) : void {
131131 if ( ! ( pair instanceof Pair ) ) throw new TypeError ( 'Expected a Pair' )
132132
133- const prev = findPair ( this . items , pair . key )
133+ const prev = findPair ( this , pair . key )
134134 const sortEntries = this . schema ?. sortMapEntries
135135 if ( prev ) {
136136 // For scalars, keep the old node & its comments and anchors
137137 if ( prev . value instanceof Scalar && pair . value instanceof Scalar )
138138 prev . value . value = pair . value . value
139139 else prev . value = pair . value
140140 } else if ( sortEntries ) {
141- const i = this . items . findIndex ( item => sortEntries ( pair , item ) < 0 )
142- if ( i === - 1 ) this . items . push ( pair )
143- else this . items . splice ( i , 0 , pair )
141+ const i = this . findIndex ( item => sortEntries ( pair , item ) < 0 )
142+ if ( i === - 1 ) this . push ( pair )
143+ else this . splice ( i , 0 , pair )
144144 } else {
145- this . items . push ( pair )
145+ this . push ( pair )
146146 }
147147 }
148148
149149 delete ( key : unknown ) : boolean {
150- const it = findPair ( this . items , key )
150+ const it = findPair ( this , key )
151151 if ( ! it ) return false
152- const del = this . items . splice ( this . items . indexOf ( it ) , 1 )
152+ const del = this . splice ( this . indexOf ( it ) , 1 )
153153 return del . length > 0
154154 }
155155
156156 get ( key : unknown ) : NodeOf < V > | undefined {
157- const it = findPair ( this . items , key )
157+ const it = findPair ( this , key )
158158 return it ?. value ?? undefined
159159 }
160160
161161 has ( key : unknown ) : boolean {
162- return ! ! findPair ( this . items , key )
162+ return ! ! findPair ( this , key )
163163 }
164164
165165 set (
@@ -203,7 +203,7 @@ export class YAMLMap<
203203 ctx ??= new ToJSContext ( )
204204 const map = Type ? new Type ( ) : ctx ?. mapAsMap ? new Map ( ) : { }
205205 if ( this . anchor ) ctx . setAnchor ( this , map )
206- for ( const item of this . items ) addPairToJSMap ( doc , ctx , map , item )
206+ for ( const item of this ) addPairToJSMap ( doc , ctx , map , item )
207207 return map
208208 }
209209
@@ -213,7 +213,7 @@ export class YAMLMap<
213213 onChompKeep ?: ( ) => void
214214 ) : string {
215215 if ( ! ctx ) return JSON . stringify ( this )
216- for ( const item of this . items ) {
216+ for ( const item of this ) {
217217 if ( ! ( item instanceof Pair ) )
218218 throw new Error (
219219 `Map items must all be pairs; found ${ JSON . stringify ( item ) } instead`
0 commit comments