-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from eemeli/unique-symbols
Add unique symbols
- Loading branch information
Showing
4 changed files
with
65 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { regexp } from './regexp.js' | ||
export { symbol } from './symbol.js' | ||
export { sharedSymbol, symbol } from './symbol.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,56 @@ | ||
import { test } from 'tap' | ||
import { parse, stringify } from 'yaml' | ||
import { Document, type Scalar, parse, stringify } from 'yaml' | ||
|
||
import { symbol } from '.' | ||
import { sharedSymbol, symbol } from '.' | ||
|
||
test('parse', t => { | ||
const res: symbol = parse(`!symbol foo`, { customTags: [symbol] }) | ||
test('parse shared', t => { | ||
const res: symbol = parse(`!symbol/shared foo`, { | ||
customTags: [sharedSymbol, symbol] | ||
}) | ||
t.type(res, 'symbol') | ||
t.same(Symbol.keyFor(res), 'foo') | ||
t.end() | ||
}) | ||
|
||
test('stringify shared', t => { | ||
const res = stringify(Symbol.for('some\nsymbol'), { customTags: [symbol] }) | ||
t.equal(res, '!symbol |-\nsome\nsymbol\n') | ||
const doc = new Document<Scalar, false>(Symbol.for('some\nsymbol'), { | ||
customTags: [sharedSymbol, symbol] | ||
}) | ||
t.equal(doc.toString(), '!symbol/shared |-\nsome\nsymbol\n') | ||
|
||
doc.contents.value = Symbol('foo') | ||
t.throws(() => doc.toString(), { name: 'TypeError' }) | ||
|
||
doc.contents.value = 42 | ||
t.throws(() => doc.toString(), { name: 'TypeError' }) | ||
|
||
t.throws(() => | ||
stringify(Symbol('some\nsymbol'), { customTags: [sharedSymbol] }) | ||
) | ||
|
||
t.end() | ||
}) | ||
|
||
test('parse private', t => { | ||
const res: symbol = parse(`!symbol foo`, { | ||
customTags: [sharedSymbol, symbol] | ||
}) | ||
t.type(res, 'symbol') | ||
t.notOk(Symbol.keyFor(res)) | ||
t.end() | ||
}) | ||
|
||
test('stringify private', t => { | ||
t.throws(() => stringify(Symbol('some\nsymbol'), { customTags: [symbol] })) | ||
const doc = new Document<Scalar, false>(Symbol('some\nsymbol'), { | ||
customTags: [sharedSymbol, symbol] | ||
}) | ||
t.equal(doc.toString(), '!symbol |-\nsome\nsymbol\n') | ||
|
||
doc.contents.value = Symbol.for('foo') | ||
t.equal(doc.toString(), '!symbol foo\n') | ||
|
||
doc.contents.value = 'foo' | ||
t.throws(() => doc.toString(), { name: 'TypeError' }) | ||
|
||
t.end() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,29 @@ | ||
import type { Scalar, ScalarTag } from 'yaml' | ||
import { StringifyContext, stringifyString } from 'yaml/util' | ||
|
||
export const symbol = { | ||
identify: value => value?.constructor === Symbol, | ||
tag: '!symbol', | ||
export const sharedSymbol = { | ||
identify: value => | ||
value?.constructor === Symbol && typeof Symbol.keyFor(value) === 'string', | ||
tag: '!symbol/shared', | ||
resolve: str => Symbol.for(str), | ||
stringify(item: Scalar, ctx: StringifyContext, onComment, onChompKeep) { | ||
const key = Symbol.keyFor(item.value as symbol) | ||
if (key === undefined) throw new Error('Only shared symbols are supported') | ||
if (key === undefined) { | ||
throw new TypeError('Only shared symbols are supported') | ||
} | ||
return stringifyString({ value: key }, ctx, onComment, onChompKeep) | ||
} | ||
} satisfies ScalarTag | ||
|
||
export const symbol = { | ||
identify: value => | ||
value?.constructor === Symbol && Symbol.keyFor(value) === undefined, | ||
tag: '!symbol', | ||
resolve: str => Symbol(str), | ||
stringify(item: Scalar, ctx: StringifyContext, onComment, onChompKeep) { | ||
const sym = item.value | ||
if (typeof sym !== 'symbol') throw new TypeError(`${sym} is not a symbol`) | ||
const value = String(sym).replace(/^Symbol\(|\)$/g, '') | ||
return stringifyString({ value }, ctx, onComment, onChompKeep) | ||
} | ||
} satisfies ScalarTag |