Loading CHANGES.md +3 −0 Original line number Diff line number Diff line Loading @@ -8,6 +8,9 @@ Version 0.14.5 To be released. - Fixed a bug of `Object.toJsonLd()` method where it had not fall back to the proper compact form when the heuristic compact form was not available. Version 0.14.4 -------------- Loading src/codegen/__snapshots__/class.test.ts.snap +441 −309 File changed.Preview size limit exceeded, changes collapsed. Show changes src/codegen/codec.ts +17 −15 Original line number Diff line number Diff line Loading @@ -60,21 +60,7 @@ export async function* generateEncoder( `; if (isCompactableType(typeUri, types)) { yield ` if ( options.format == null `; for (const property of type.properties) { if (!property.range.every((r) => isCompactableType(r, types))) { yield ` && ( this.${await getFieldName(property.uri)} == null || this.${await getFieldName(property.uri)}.length < 1 ) `; } } yield ` ) { if (options.format == null && this.isCompactable()) { `; if (type.extends == null) { yield "const result: Record<string, unknown> = {};"; Loading Loading @@ -245,6 +231,22 @@ export async function* generateEncoder( } return compacted; } protected isCompactable(): boolean { `; for (const property of type.properties) { if (!property.range.every((r) => isCompactableType(r, types))) { yield ` if ( this.${await getFieldName(property.uri)} != null && this.${await getFieldName(property.uri)}.length > 0 ) return false; `; } } yield ` return ${type.extends == null ? "true" : "super.isCompactable()"}; } `; } Loading src/codegen/type.ts +3 −0 Original line number Diff line number Diff line Loading @@ -422,6 +422,9 @@ export function isCompactableType( } else if (typeUri in types) { const type = types[typeUri]; if (type.compactName == null) return false; else if (type.extends != null && !isCompactableType(type.extends, types)) { return false; } const defaultContext = type.defaultContext; return defaultContext != null && HEURISTICS_CONTEXTS.includes(defaultContext as string) || Loading src/vocab/vocab.test.ts +47 −0 Original line number Diff line number Diff line Loading @@ -30,6 +30,7 @@ import { CryptographicKey, type DataIntegrityProof, Follow, Hashtag, Note, Object, Person, Loading Loading @@ -235,6 +236,52 @@ test("Object.toJsonLd()", async () => { }); }); test("Note.toJsonLd()", async () => { const note = new Note({ tags: [ new Hashtag({ name: "#Fedify", href: new URL("https://fedify.dev/"), }), ], }); assertEquals(await note.toJsonLd({ contextLoader: mockDocumentLoader }), { "@context": [ "https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1", { Emoji: "toot:Emoji", Hashtag: "as:Hashtag", sensitive: "as:sensitive", toot: "http://joinmastodon.org/ns#", }, ], tag: { "@context": [ "https://www.w3.org/ns/activitystreams", { Hashtag: "as:Hashtag", }, ], href: "https://fedify.dev/", name: "#Fedify", type: "Hashtag", }, type: "Note", }); const noteWithName = note.clone({ name: "Test", }); assertEquals( await noteWithName.toJsonLd({ contextLoader: mockDocumentLoader }), await noteWithName.toJsonLd({ contextLoader: mockDocumentLoader, format: "compact", }), ); }); test("Activity.fromJsonLd()", async () => { const follow = await Activity.fromJsonLd( { Loading Loading
CHANGES.md +3 −0 Original line number Diff line number Diff line Loading @@ -8,6 +8,9 @@ Version 0.14.5 To be released. - Fixed a bug of `Object.toJsonLd()` method where it had not fall back to the proper compact form when the heuristic compact form was not available. Version 0.14.4 -------------- Loading
src/codegen/__snapshots__/class.test.ts.snap +441 −309 File changed.Preview size limit exceeded, changes collapsed. Show changes
src/codegen/codec.ts +17 −15 Original line number Diff line number Diff line Loading @@ -60,21 +60,7 @@ export async function* generateEncoder( `; if (isCompactableType(typeUri, types)) { yield ` if ( options.format == null `; for (const property of type.properties) { if (!property.range.every((r) => isCompactableType(r, types))) { yield ` && ( this.${await getFieldName(property.uri)} == null || this.${await getFieldName(property.uri)}.length < 1 ) `; } } yield ` ) { if (options.format == null && this.isCompactable()) { `; if (type.extends == null) { yield "const result: Record<string, unknown> = {};"; Loading Loading @@ -245,6 +231,22 @@ export async function* generateEncoder( } return compacted; } protected isCompactable(): boolean { `; for (const property of type.properties) { if (!property.range.every((r) => isCompactableType(r, types))) { yield ` if ( this.${await getFieldName(property.uri)} != null && this.${await getFieldName(property.uri)}.length > 0 ) return false; `; } } yield ` return ${type.extends == null ? "true" : "super.isCompactable()"}; } `; } Loading
src/codegen/type.ts +3 −0 Original line number Diff line number Diff line Loading @@ -422,6 +422,9 @@ export function isCompactableType( } else if (typeUri in types) { const type = types[typeUri]; if (type.compactName == null) return false; else if (type.extends != null && !isCompactableType(type.extends, types)) { return false; } const defaultContext = type.defaultContext; return defaultContext != null && HEURISTICS_CONTEXTS.includes(defaultContext as string) || Loading
src/vocab/vocab.test.ts +47 −0 Original line number Diff line number Diff line Loading @@ -30,6 +30,7 @@ import { CryptographicKey, type DataIntegrityProof, Follow, Hashtag, Note, Object, Person, Loading Loading @@ -235,6 +236,52 @@ test("Object.toJsonLd()", async () => { }); }); test("Note.toJsonLd()", async () => { const note = new Note({ tags: [ new Hashtag({ name: "#Fedify", href: new URL("https://fedify.dev/"), }), ], }); assertEquals(await note.toJsonLd({ contextLoader: mockDocumentLoader }), { "@context": [ "https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1", { Emoji: "toot:Emoji", Hashtag: "as:Hashtag", sensitive: "as:sensitive", toot: "http://joinmastodon.org/ns#", }, ], tag: { "@context": [ "https://www.w3.org/ns/activitystreams", { Hashtag: "as:Hashtag", }, ], href: "https://fedify.dev/", name: "#Fedify", type: "Hashtag", }, type: "Note", }); const noteWithName = note.clone({ name: "Test", }); assertEquals( await noteWithName.toJsonLd({ contextLoader: mockDocumentLoader }), await noteWithName.toJsonLd({ contextLoader: mockDocumentLoader, format: "compact", }), ); }); test("Activity.fromJsonLd()", async () => { const follow = await Activity.fromJsonLd( { Loading