Gathering detailed insights and metrics for rdf-literal
Gathering detailed insights and metrics for rdf-literal
Gathering detailed insights and metrics for rdf-literal
Gathering detailed insights and metrics for rdf-literal
npm install rdf-literal
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
6 Stars
45 Commits
1 Forks
3 Watching
5 Branches
4 Contributors
Updated on 20 Sept 2024
Minified
Minified + Gzipped
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
-62.6%
1,522
Compared to previous day
Last week
5.6%
17,386
Compared to previous week
Last month
41.4%
62,276
Compared to previous month
Last year
62%
482,201
Compared to previous year
RDF Literal makes it easier to translate between RDF literals and JavaScript primitives.
This library accepts RDFJS-compliant terms.
1$ yarn add rdf-literal
This package also works out-of-the-box in browsers via tools such as webpack and browserify.
1import { 2 fromRdf, 3 toRdf, 4 getSupportedJavaScriptPrimitives, 5 getSupportedRdfDatatypes, 6 getTermRaw, 7} from "rdf-literal";
or
1const { 2 fromRdf, 3 toRdf, 4 getSupportedJavaScriptPrimitives, 5 getSupportedRdfDatatypes, 6 getTermRaw, 7} = require("rdf-literal");
This library offers the following functions:
fromRdf
: Converts an RDF literal to a JavaScript primitive.toRdf
: Converts a JavaScript primitive to an RDF literal.Next to that, the following helper functions are provided:
getSupportedJavaScriptPrimitives
: An array of all JavaScript primitive types that can be converted.getSupportedRdfDatatypes
: An array of all RDF datatypes (as NamedNodes) that can be converted.getTermRaw
: Converts any RDF term to a JavaScript primitive. If the term is a literal, fromRdf
will be called on it. Otherwise, the .value
string will be returned.fromRdf(literal, validate?)
converts an RDF literal to a JavaScript value.
Optionally, the validate
argument can be passed as true
to force an error to be thrown if an invalid value for the given datatype is detected.
Explicit string datatypes are converted into JS strings.
1fromRdf(literal('abc'); // Returns 'abc' 2fromRdf(literal('abc', 'en-us'); // Returns 'abc' 3fromRdf(literal('abc', 4 namedNode('http://www.w3.org/2001/XMLSchema#normalizedString')); // Returns 'abc'
Integer-like and double-like literals are converted into JS numbers.
1// Integers 2fromRdf(literal('123', 3 namedNode('http://www.w3.org/2001/XMLSchema#integer')); // Returns 123 4fromRdf(literal('123', 5 namedNode('http://www.w3.org/2001/XMLSchema#long')); // Returns 123 6 7// Doubles 8fromRdf(literal('123.456', 9 namedNode('http://www.w3.org/2001/XMLSchema#double')); // Returns 123.456 10fromRdf(literal('123.456', 11 namedNode('http://www.w3.org/2001/XMLSchema#float')); // Returns 123.456 12 13// Invalid integers 14fromRdf(literal('123.456', 15 namedNode('http://www.w3.org/2001/XMLSchema#integer')); // Returns 123 16fromRdf(literal('123.456', 17 namedNode('http://www.w3.org/2001/XMLSchema#integer'), true); // Throws error
Boolean literals are converted into JS booleans.
1fromRdf(literal('true', 2 namedNode('http://www.w3.org/2001/XMLSchema#boolean')); // Returns true 3fromRdf(literal('0', 4 namedNode('http://www.w3.org/2001/XMLSchema#boolean')); // Returns false
Date(time) literals are converted into JS Dates.
1fromRdf(literal('2012-03-17T23:00:00.000Z', 2 namedNode('http://www.w3.org/2001/XMLSchema#dateTime'))); // Returns a Date 3fromRdf(literal('2012-03-17', 4 namedNode('http://www.w3.org/2001/XMLSchema#date'))); // Returns a Date 5fromRdf(literal('2012-03', 6 namedNode('http://www.w3.org/2001/XMLSchema#gYearMonth'))); // Returns a Date
Unknown datatypes are considered JS strings.
1fromRdf(literal('abc', 2 namedNode('http://example.org/unknown')); // Returns 'abc'
toRdf(value, options?)
converts a JavaScript value to an RDF literal term.
The optional options object can contain the following optional fields:
Name | Description |
---|---|
datatype | A custom NamedNode datatype that can be forced upon the literal value, which may influence the format of literal values. |
dataFactory | DataFactory for creating RDF terms. |
JS strings are converted to plain RDF literals.
1toRdf('abc'); // Returns literal('abc')
JS numbers are converted to RDF integers or doubles.
1toRdf(123); // Returns literal('123', 2 // namedNode('http://www.w3.org/2001/XMLSchema#integer') 3 4toRdf(123.456); // Returns literal('123.456', 5 // namedNode('http://www.w3.org/2001/XMLSchema#double')
JS booleans are converted to RDF booleans.
1toRdf(true); // Returns literal('true', 2 // namedNode('http://www.w3.org/2001/XMLSchema#boolean')
JS Dates are converted to RDF date times.
1toRdf(new Date('2012-03-17')); 2// Returns literal('2012-03-17T00:00:00.000Z', 3// namedNode('http://www.w3.org/2001/XMLSchema#dateTime')) 4 5toRdf(new Date('2012-03-17T23:00:00.000Z')); 6// Returns literal('2012-03-17T23:00:00.000Z', 7// namedNode('http://www.w3.org/2001/XMLSchema#dateTime')) 8 9toRdf(new Date('2012-03-17'), 10 { datatype: namedNode('http://www.w3.org/2001/XMLSchema#date') }); 11// Returns literal('2012-03-17', 12// namedNode('http://www.w3.org/2001/XMLSchema#date')) 13 14toRdf(2012, 15 { datatype: namedNode('http://www.w3.org/2001/XMLSchema#gYear') }); 16// Returns literal('2012', 17// namedNode('http://www.w3.org/2001/XMLSchema#gYear'))
The following table shows how RDF datatypes and JavaScript primitives are mapped:
JavaScript primitive | RDF Datatype |
---|---|
string | xsd:string |
string | xsd:normalizedString |
string | xsd:anyURI |
string | xsd:base64Binary |
string | xsd:language |
string | xsd:Name |
string | xsd:NCName |
string | xsd:NMTOKEN |
string | xsd:token |
string | xsd:hexBinary |
string | rdf:langString |
boolean | xsd:boolean |
number | xsd:integer |
number | xsd:long |
number | xsd:int |
number | xsd:byte |
number | xsd:short |
number | xsd:negativeInteger |
number | xsd:nonNegativeInteger |
number | xsd:nonPositiveInteger |
number | xsd:positiveInteger |
number | xsd:unsignedByte |
number | xsd:unsignedInt |
number | xsd:unsignedLong |
number | xsd:unsignedShort |
number | xsd:double |
number | xsd:decimal |
number | xsd:float |
Date | xsd:dateTime |
Date | xsd:date |
Date | xsd:gDay |
Date | xsd:gMonthDay |
Date | xsd:gYear |
Date | xsd:gYearMonth |
Used prefixes:
xsd: <http://www.w3.org/2001/XMLSchema#>
rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
The following XSD datatypes that are standardized in RDF are not supported, and will therefore be interpreted as plain strings:
xsd:duration
xsd:time
Any other unknown datatypes will also be interpreted as plain strings.
This software is written by Ruben Taelman.
This code is released under the MIT license.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 2/23 approved changesets -- score normalized to 0
Reason
1 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
22 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More