-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#28: Compacting IRIs in storage (WIP)
- Loading branch information
1 parent
d400c5b
commit 2fc73d8
Showing
14 changed files
with
183 additions
and
46 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
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
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Quad } from 'rdf-js'; | ||
import { fromRDF, toRDF, Options, processContext } from 'jsonld'; | ||
import { cloneQuad } from './quads'; | ||
import { Context, Iri } from 'jsonld/jsonld-spec'; | ||
import { getInitialContext, expandIri, ActiveContext } from 'jsonld/lib/context'; | ||
import { compactIri as _compactIri } from 'jsonld/lib/compact'; | ||
export { Options } from 'jsonld'; | ||
export { ActiveContext } from 'jsonld/lib/context'; | ||
|
||
export function rdfToJson(quads: Quad[]): Promise<any> { | ||
// Using native types to avoid unexpected value objects | ||
return fromRDF(quads, { useNativeTypes: true }); | ||
} | ||
|
||
export async function jsonToRdf(json: any): Promise<Quad[]> { | ||
const quads = await toRDF(json) as Quad[]; | ||
// jsonld produces quad members without equals | ||
return quads.map(cloneQuad); | ||
} | ||
|
||
export function expandTerm(value: string, ctx: ActiveContext, options?: Options.Expand): Iri { | ||
return expandIri(ctx, value, { base: true }, options ?? {}); | ||
} | ||
|
||
export function compactIri(iri: Iri, ctx: ActiveContext, options?: Options.CompactIri): string { | ||
return _compactIri({ activeCtx: ctx, iri, ...options }); | ||
} | ||
|
||
export async function activeCtx(ctx: Context, options?: Options.DocLoader): Promise<ActiveContext> { | ||
return await processContext(getInitialContext({}), ctx, options ?? {}); | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
declare module 'jsonld/lib/compact' { | ||
import { Iri, Url } from 'jsonld/jsonld-spec'; | ||
import { ActiveContext } from 'jsonld/lib/context'; | ||
import { Options } from 'jsonld'; | ||
|
||
function compactIri(opts: { | ||
activeCtx: ActiveContext, | ||
iri: Iri | ||
} & Options.CompactIri): string; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
declare module 'jsonld/lib/context' { | ||
import { Context, Iri } from 'jsonld/jsonld-spec'; | ||
import { Options } from 'jsonld'; | ||
|
||
// Using the 'protected' field to prevent type mistakes | ||
type ActiveContext = Context & { protected: {} }; | ||
|
||
function getInitialContext(options: { processingMode?: boolean }): ActiveContext; | ||
function expandIri( | ||
activeCtx: Context, | ||
value: string, | ||
relativeTo: { vocab?: boolean, base?: boolean }, | ||
options: Options.Common): Iri; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Context, Url } from 'jsonld/jsonld-spec'; | ||
import { ActiveContext } from 'jsonld/lib/context'; | ||
|
||
declare module 'jsonld' { | ||
namespace Options { | ||
export interface CompactIri { | ||
value?: any, // TODO | ||
relativeTo?: { vocab: boolean; }, | ||
reverse?: boolean, | ||
base?: Url | ||
} | ||
} | ||
|
||
function processContext( | ||
activeCtx: ActiveContext, | ||
localCtx: Context, | ||
options: Options.DocLoader): | ||
Promise<ActiveContext>; | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { expandTerm, compactIri, ActiveContext, activeCtx } from '../src/engine/jsonld'; | ||
|
||
describe('JSON-LD', () => { | ||
const context = { | ||
'@base': 'http://example.org/', | ||
'@vocab': 'http://example.org/#', | ||
'mld': 'http://m-ld.org/' | ||
}; | ||
let ctx: ActiveContext; | ||
|
||
beforeAll(async () => { | ||
ctx = await activeCtx(context); | ||
}); | ||
|
||
describe('expand term', () => { | ||
test('expand prefix', () => { | ||
expect(expandTerm('mld:hello', ctx)).toBe('http://m-ld.org/hello'); | ||
}); | ||
|
||
test('ignores undefined prefix', () => { | ||
expect(expandTerm('foo:hello', ctx)).toBe('foo:hello'); | ||
}); | ||
|
||
test('leaves an IRI', () => { | ||
expect(expandTerm('http://hello.com/', ctx)).toBe('http://hello.com/'); | ||
}); | ||
|
||
test('expands against base', () => { | ||
expect(expandTerm('hello', ctx)).toBe('http://example.org/hello'); | ||
}); | ||
}); | ||
|
||
describe('compact IRI', () => { | ||
test('compacts with prefix', () => { | ||
expect(compactIri('http://m-ld.org/hello', ctx)).toBe('mld:hello'); | ||
}); | ||
|
||
test('ignores prefix', () => { | ||
expect(compactIri('foo:hello', ctx)).toBe('foo:hello'); | ||
}); | ||
|
||
test('leaves an unmatched IRI', () => { | ||
expect(compactIri('http://hello.com/', ctx)).toBe('http://hello.com/'); | ||
}); | ||
|
||
test('compacts using base', () => { | ||
expect(compactIri('http://example.org/hello', ctx)).toBe('hello'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.