Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 66 additions & 26 deletions src/nodes/YAMLMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,26 @@ import { Scalar } from './Scalar.ts'
import { ToJSContext } from './toJS.ts'
import type { CollectionBase, Node, NodeOf, Primitive, Range } from './types.ts'
import { cloneMapOrSet } from './util-clone-map-or-set.ts'
import {
indexNodeKey,
lookupNodeKey,
NOT_FOUND
} from './util-node-key-index.ts'

export type MapLike = Map<any, any> | Record<string | number | symbol, any>

export type KeyArg<K extends Primitive | Node, V extends Primitive | Node> =
K | NodeOf<K> | (K extends Scalar ? K['value'] : never) | Pair<K, V>

const getPairKey = (pair: { key: unknown }): unknown => pair.key

function nodeOf<K extends Primitive | Node, V extends Primitive | Node>(
key: KeyArg<K, V>
): Node | null {
if (isNode(key)) return key
return key instanceof Pair && isNode(key.key) ? key.key : null
}

export class YAMLMap<
K extends Primitive | Node = Primitive | Node,
V extends Primitive | Node = Primitive | Node
Expand All @@ -28,6 +42,7 @@ export class YAMLMap<
declare schema: Schema

values: Map<unknown, Pair<K, V>> = new Map()
#nodeKeys: WeakMap<Node, unknown> = new WeakMap()

/** An optional anchor on this map. Used by alias nodes. */
declare anchor?: string
Expand Down Expand Up @@ -76,6 +91,7 @@ export class YAMLMap<
const mk = nc.schema.mapKey(key)
const pair = nc.createPair(key, value)
map.values.set(mk, pair)
indexNodeKey(map.#nodeKeys, pair.key, mk)
}
}
}
Expand All @@ -89,9 +105,18 @@ export class YAMLMap<
enumerable: false,
writable: true
})
this.values = new Map(
elements?.map(pair => [this.schema.mapKey(pair), pair])
)
if (elements) {
for (const pair of elements) {
const mk = this.schema.mapKey(pair)
this.values.set(mk, pair)
indexNodeKey(this.#nodeKeys, pair.key, mk)
}
}
}

/** @internal */
_indexNode(node: unknown, mk: unknown): void {
indexNodeKey(this.#nodeKeys, node, mk)
}

get size(): number {
Expand All @@ -112,12 +137,23 @@ export class YAMLMap<
* @returns `true` if the item was found and removed.
*/
delete(key: KeyArg<K, V>): boolean {
let mk = this.schema.mapKey(key)
const mk = this.schema.mapKey(key)
const prev = this.values.get(mk)
if (prev) {
if (isNode(prev.key)) this.#nodeKeys.delete(prev.key)
return this.values.delete(mk)
}
if (this.values.delete(mk)) return true
mk = key instanceof Pair ? key.key : isNode(key) ? key : null
if (mk) {
for (const [k, p] of this.values)
if (p.key === mk) return this.values.delete(k)
const nk = lookupNodeKey(
this.values,
this.#nodeKeys,
nodeOf(key),
getPairKey
)
if (nk !== NOT_FOUND) {
const pair = this.values.get(nk)
if (pair && isNode(pair.key)) this.#nodeKeys.delete(pair.key)
return this.values.delete(nk)
}
return false
}
Expand All @@ -129,25 +165,26 @@ export class YAMLMap<

/** Return pair at `key`, or `undefined` if not found. */
getPair(key: KeyArg<K, V>): Pair<K, V> | undefined {
let mk = this.schema.mapKey(key)
const mk = this.schema.mapKey(key)
const pair = this.values.get(mk)
if (pair) return pair
mk = key instanceof Pair ? key.key : isNode(key) ? key : null
if (mk) {
for (const p of this.values.values()) if (p.key === mk) return p
}
return undefined
const nk = lookupNodeKey(
this.values,
this.#nodeKeys,
nodeOf(key),
getPairKey
)
return nk === NOT_FOUND ? undefined : this.values.get(nk)
}

/** Check if the mapping includes a value with the key `key`. */
has(key: KeyArg<K, V>): boolean {
let mk = this.schema.mapKey(key)
const mk = this.schema.mapKey(key)
if (this.values.has(mk)) return true
mk = key instanceof Pair ? key.key : isNode(key) ? key : null
if (mk) {
for (const p of this.values.values()) if (p.key === mk) return true
}
return false
return (
lookupNodeKey(this.values, this.#nodeKeys, nodeOf(key), getPairKey) !==
NOT_FOUND
)
}

/**
Expand All @@ -157,13 +194,15 @@ export class YAMLMap<
* even if the key is not in the map.
*/
keyOf(key: KeyArg<K, V>, allowMissing = false): unknown {
let mk = this.schema.mapKey(key)
const mk = this.schema.mapKey(key)
if (allowMissing || this.values.has(mk)) return mk
mk = key instanceof Pair ? key.key : isNode(key) ? key : null
if (mk) {
for (const [k, v] of this.values) if (v.key === mk) return k
}
return undefined
const nk = lookupNodeKey(
this.values,
this.#nodeKeys,
nodeOf(key),
getPairKey
)
return nk === NOT_FOUND ? undefined : nk
}

pairs(): Iterable<Pair<K, V>> {
Expand Down Expand Up @@ -208,6 +247,7 @@ export class YAMLMap<
}
}
this.values.set(mk, pair)
indexNodeKey(this.#nodeKeys, pair.key, mk)
return this
}

Expand Down
66 changes: 50 additions & 16 deletions src/nodes/YAMLSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import { Pair } from './Pair.ts'
import { ToJSContext } from './toJS.ts'
import type { CollectionBase, Node, NodeOf, Primitive, Range } from './types.ts'
import { cloneMapOrSet } from './util-clone-map-or-set.ts'
import {
indexNodeKey,
lookupNodeKey,
NOT_FOUND
} from './util-node-key-index.ts'

const getSetNode = <T>(node: T) => node

export class YAMLSet<
T extends Primitive | Node = Primitive | Node
Expand All @@ -22,6 +29,7 @@ export class YAMLSet<
tag = 'tag:yaml.org,2002:set'

values: Map<unknown, NodeOf<T>> = new Map()
#nodeKeys: WeakMap<Node, unknown> = new WeakMap()

/** An optional anchor on this set. Used by alias nodes. */
declare anchor?: string
Expand Down Expand Up @@ -57,6 +65,11 @@ export class YAMLSet<
})
}

/** @internal */
_indexNode(node: unknown, mk: unknown): void {
indexNodeKey(this.#nodeKeys, node, mk)
}

get size(): number {
return this.values.size
}
Expand All @@ -72,6 +85,7 @@ export class YAMLSet<
? value
: new NodeCreator(this.schema, options).create(value)
this.values.set(mk, node as NodeOf<T>)
indexNodeKey(this.#nodeKeys, node, mk)
return this
}

Expand All @@ -90,11 +104,21 @@ export class YAMLSet<
*/
delete(value: T | NodeOf<T>): boolean {
const mk = this.schema.mapKey(value)
const prev = this.values.get(mk)
if (prev) {
if (isNode(prev)) this.#nodeKeys.delete(prev)
return this.values.delete(mk)
}
if (this.values.delete(mk)) return true
if (isNode(value)) {
for (const [k, v] of this.values) {
if (v === value) return this.values.delete(k)
}
const nk = lookupNodeKey(
this.values,
this.#nodeKeys,
isNode(value) ? value : null,
getSetNode
)
if (nk !== NOT_FOUND) {
this.#nodeKeys.delete(value as Node)
return this.values.delete(nk)
}
return false
}
Expand All @@ -104,20 +128,27 @@ export class YAMLSet<
const mk = this.schema.mapKey(value)
const node = this.values.get(mk)
if (node) return node
if (isNode(value)) {
for (const v of this.values.values()) if (v === value) return v
}
return undefined
const nk = lookupNodeKey(
this.values,
this.#nodeKeys,
isNode(value) ? value : null,
getSetNode
)
return nk === NOT_FOUND ? undefined : this.values.get(nk)
}

/** Check if the set includes `value`. */
has(value: T | NodeOf<T>): boolean {
const mk = this.schema.mapKey(value)
if (this.values.has(mk)) return true
if (isNode(value)) {
for (const v of this.values.values()) if (v === value) return true
}
return false
return (
lookupNodeKey(
this.values,
this.#nodeKeys,
isNode(value) ? value : null,
getSetNode
) !== NOT_FOUND
)
}

/**
Expand All @@ -129,10 +160,13 @@ export class YAMLSet<
keyOf(value: T | NodeOf<T>, allowMissing = false): unknown {
const mk = this.schema.mapKey(value)
if (allowMissing || this.values.has(mk)) return mk
if (isNode(value)) {
for (const [k, v] of this.values) if (v === value) return k
}
return undefined
const nk = lookupNodeKey(
this.values,
this.#nodeKeys,
isNode(value) ? value : null,
getSetNode
)
return nk === NOT_FOUND ? undefined : nk
}

/** A plain JavaScript representation of this set. */
Expand Down
5 changes: 4 additions & 1 deletion src/nodes/util-clone-map-or-set.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Schema } from '../schema/Schema.ts'
import { Pair } from './Pair.ts'
import type { YAMLMap } from './YAMLMap.ts'
import type { YAMLSet } from './YAMLSet.ts'

Expand All @@ -9,7 +10,9 @@ export function cloneMapOrSet<T extends YAMLMap | YAMLSet>(
schema ??= coll.schema
const copy = new (coll.constructor as typeof YAMLMap)(schema) as T
for (const [key, value] of coll.values) {
copy.values.set(key, value.clone(schema) as any)
const cloned = value.clone(schema) as any
copy.values.set(key, cloned)
copy._indexNode(cloned instanceof Pair ? cloned.key : cloned, key)
}
if (coll.range) copy.range = [...coll.range]
const propDesc = Object.getOwnPropertyDescriptors(coll)
Expand Down
25 changes: 25 additions & 0 deletions src/nodes/util-node-key-index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { isNode } from './identity.ts'
import type { Node } from './types.ts'

export const NOT_FOUND: unique symbol = Symbol('NOT_FOUND')

export function lookupNodeKey<E>(
values: Map<unknown, E>,
nodeKeys: WeakMap<Node, unknown>,
node: Node | null,
getNode: (entry: E) => unknown
): unknown {
if (!node || values.size === 0) return NOT_FOUND
const mk = nodeKeys.get(node)
if (mk === undefined && !nodeKeys.has(node)) return NOT_FOUND
const entry = values.get(mk)
return entry && getNode(entry) === node ? mk : NOT_FOUND
}

export function indexNodeKey(
nodeKeys: WeakMap<Node, unknown>,
node: unknown,
mk: unknown
): void {
if (isNode(node)) nodeKeys.set(node, mk)
}
4 changes: 3 additions & 1 deletion src/schema/yaml-1.1/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export const set: CollectionTag = {
if (typeof nc.replacer === 'function')
value = nc.replacer.call(iterable, value, value)
const mk = nc.schema.mapKey(value)
set.values.set(mk, nc.create(value))
const node = nc.create(value)
set.values.set(mk, node)
set._indexNode(node, mk)
}
return set
},
Expand Down
Loading