File tree Expand file tree Collapse file tree 2 files changed +14
-12
lines changed
Expand file tree Collapse file tree 2 files changed +14
-12
lines changed Original file line number Diff line number Diff line change @@ -290,30 +290,28 @@ function literal(value, languageOrDataType) {
290290 if ( typeof languageOrDataType === 'string' )
291291 return new Literal ( '"' + value + '"@' + languageOrDataType . toLowerCase ( ) ) ;
292292
293- // Create a datatyped literal
294- var datatype = languageOrDataType && languageOrDataType . value || '' ;
295- if ( ! datatype ) {
296- switch ( typeof value ) {
293+ // Automatically determine datatype for booleans and numbers
294+ let datatype = languageOrDataType ? languageOrDataType . value : '' ;
295+ if ( datatype === '' ) {
297296 // Convert a boolean
298- case 'boolean' :
297+ if ( typeof value === 'boolean' )
299298 datatype = xsd . boolean ;
300- break ;
301299 // Convert an integer or double
302- case 'number' :
300+ else if ( typeof value === 'number' ) {
303301 if ( Number . isFinite ( value ) )
304302 datatype = Number . isInteger ( value ) ? xsd . integer : xsd . double ;
305303 else {
306304 datatype = xsd . double ;
307305 if ( ! Number . isNaN ( value ) )
308306 value = value > 0 ? 'INF' : '-INF' ;
309307 }
310- break ;
311- // No datatype, so convert a plain string
312- default :
313- return new Literal ( '"' + value + '"' ) ;
314308 }
315309 }
316- return new Literal ( '"' + value + '"^^' + datatype ) ;
310+
311+ // Create a datatyped literal
312+ return ( datatype === '' || datatype === xsd . string ) ?
313+ new Literal ( '"' + value + '"' ) :
314+ new Literal ( '"' + value + '"^^' + datatype ) ;
317315}
318316
319317// ### Creates a variable
Original file line number Diff line number Diff line change @@ -49,6 +49,10 @@ describe('DataFactory', function () {
4949 DataFactory . literal ( 'abc' , new NamedNode ( 'http://ex.org/type' ) ) . should . deep . equal ( new Literal ( '"abc"^^http://ex.org/type' ) ) ;
5050 } ) ;
5151
52+ it ( 'converts a non-empty string with an xsd:string type' , function ( ) {
53+ DataFactory . literal ( 'abc' , new NamedNode ( 'http://www.w3.org/2001/XMLSchema#string' ) ) . should . deep . equal ( new Literal ( '"abc"' ) ) ;
54+ } ) ;
55+
5256 it ( 'converts an integer' , function ( ) {
5357 DataFactory . literal ( 123 ) . should . deep . equal ( new Literal ( '"123"^^http://www.w3.org/2001/XMLSchema#integer' ) ) ;
5458 } ) ;
You can’t perform that action at this time.
0 commit comments