Skip to content

Commit

Permalink
Add support for base directions in literals
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Nov 10, 2023
1 parent 4d3b481 commit 19e140b
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/handler/TypeHandlerBoolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export class TypeHandlerBoolean implements ITypeHandler {
}

public toRdf(value: any, { datatype, dataFactory }: IToRdfOptions): RDF.Literal {
return dataFactory.literal(value ? 'true' : 'false',
datatype || dataFactory.namedNode(TypeHandlerBoolean.TYPE));
return dataFactory!.literal(value ? 'true' : 'false',
datatype || dataFactory!.namedNode(TypeHandlerBoolean.TYPE));
}

}
6 changes: 3 additions & 3 deletions lib/handler/TypeHandlerDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ export class TypeHandlerDate implements ITypeHandler {
}

public toRdf(value: any, { datatype, dataFactory }: IToRdfOptions): RDF.Literal {
datatype = datatype || dataFactory.namedNode(TypeHandlerDate.TYPES[0]);
datatype = datatype || dataFactory!.namedNode(TypeHandlerDate.TYPES[0]);

// Assume date values
if (!(value instanceof Date)) {
return null;
return null!; // TODO: throw error in next breaking change
}
const date: Date = <Date> value;

Expand All @@ -73,7 +73,7 @@ export class TypeHandlerDate implements ITypeHandler {
default:
valueString = date.toISOString();
}
return dataFactory.literal(valueString, datatype);
return dataFactory!.literal(valueString, datatype);
}

}
10 changes: 5 additions & 5 deletions lib/handler/TypeHandlerNumberDouble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ export class TypeHandlerNumberDouble implements ITypeHandler {
}

public toRdf(value: any, { datatype, dataFactory }: IToRdfOptions): RDF.Literal {
datatype = datatype || dataFactory.namedNode(TypeHandlerNumberDouble.TYPES[0]);
datatype = datatype || dataFactory!.namedNode(TypeHandlerNumberDouble.TYPES[0]);
if (isNaN(value)) {
return dataFactory.literal('NaN', datatype);
return dataFactory!.literal('NaN', datatype);
}
if (!isFinite(value)) {
return dataFactory.literal(value > 0 ? 'INF' : '-INF', datatype);
return dataFactory!.literal(value > 0 ? 'INF' : '-INF', datatype);
}
if (value % 1 === 0) {
return null;
return null!; // TODO: throw error in next breaking change
}
return dataFactory.literal(value.toExponential(15).replace(/(\d)0*e\+?/, '$1E'), datatype);
return dataFactory!.literal(value.toExponential(15).replace(/(\d)0*e\+?/, '$1E'), datatype);
}

}
6 changes: 3 additions & 3 deletions lib/handler/TypeHandlerNumberInteger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ export class TypeHandlerNumberInteger implements ITypeHandler {
}

public toRdf(value: any, { datatype, dataFactory }: IToRdfOptions): RDF.Literal {
return dataFactory.literal(String(value), datatype
return dataFactory!.literal(String(value), datatype
|| (value <= TypeHandlerNumberInteger.MAX_INT && value >= TypeHandlerNumberInteger.MIN_INT
? dataFactory.namedNode(TypeHandlerNumberInteger.TYPES[0])
: dataFactory.namedNode(TypeHandlerNumberInteger.TYPES[1])));
? dataFactory!.namedNode(TypeHandlerNumberInteger.TYPES[0])
: dataFactory!.namedNode(TypeHandlerNumberInteger.TYPES[1])));
}

}
3 changes: 2 additions & 1 deletion lib/handler/TypeHandlerString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class TypeHandlerString implements ITypeHandler {
'http://www.w3.org/2001/XMLSchema#token',
'http://www.w3.org/2001/XMLSchema#hexBinary',
'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString',
'http://www.w3.org/1999/02/22-rdf-syntax-ns#dirLangString',

'http://www.w3.org/2001/XMLSchema#time',
'http://www.w3.org/2001/XMLSchema#duration',
Expand All @@ -28,7 +29,7 @@ export class TypeHandlerString implements ITypeHandler {
}

public toRdf(value: any, { datatype, dataFactory }: IToRdfOptions): RDF.Literal {
return dataFactory.literal(value, datatype);
return dataFactory!.literal(value, datatype);
}

}
6 changes: 6 additions & 0 deletions test/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ describe('fromRdf', () => {
.toEqual('abc');
});

it('should handle language tagged strings with direction', () => {
return expect(fromRdf(DF.literal('abc', { language: 'en-us', direction: 'ltr' })))
.toEqual('abc');
});

it('should handle normalized strings', () => {
return expect(fromRdf(DF.literal('abc',
DF.namedNode('http://www.w3.org/2001/XMLSchema#normalizedString'))))
Expand Down Expand Up @@ -655,6 +660,7 @@ describe('getSupportedRdfDatatypes', () => {
DF.namedNode('http://www.w3.org/2001/XMLSchema#token'),
DF.namedNode('http://www.w3.org/2001/XMLSchema#hexBinary'),
DF.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#langString'),
DF.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#dirLangString'),

DF.namedNode('http://www.w3.org/2001/XMLSchema#time'),
DF.namedNode('http://www.w3.org/2001/XMLSchema#duration'),
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"sourceMap": true,
"inlineSources": true,
"declaration": true,
"importHelpers": false
"importHelpers": false,
"strict": true
},
"include": [
"index.ts",
Expand Down

0 comments on commit 19e140b

Please sign in to comment.