jskos-tools
Advanced tools
Comparing version 0.2.3 to 0.2.4
const regexChars = /[\\^$.*+?()[\]{}|]/g | ||
const regexEscape = string => string.replace(regexChars, "\\$&") | ||
// Special characters that uriPattern can be build with | ||
// Some other special characters such as [ and ] will not work! | ||
const notationEscapeChars = /[% ]/ | ||
/** | ||
@@ -24,6 +28,11 @@ * A [JSKOS Concept Scheme](http://gbv.github.io/jskos/jskos.html#concept-schemes). | ||
this.NOTATION_REGEX = RegExp("^" + this.notationPattern + "$") | ||
} | ||
if (!this.uriPattern && this.namespace) { | ||
this.uriPattern = "^" + regexEscape(this.namespace) | ||
+ "(" + this.notationPattern + ")$" | ||
if (!this.uriPattern && this.namespace) { | ||
this.uriPattern = "^" + regexEscape(this.namespace) | ||
if (this.notationPattern) { | ||
let escaped = this.notationPattern.replace(notationEscapeChars, encodeURI) | ||
this.uriPattern += "(" + escaped + ")$" | ||
} else { | ||
this.uriPattern += "(.+)$" | ||
} | ||
@@ -39,2 +48,3 @@ } | ||
* Check whether a string is a valid notation. | ||
* Requires scheme to have `notationPattern`. | ||
* @param {string} notation | ||
@@ -44,3 +54,3 @@ * @returns {array|null} | ||
isValidNotation(notation) { | ||
return this.NOTATION_REGEX ? this.NOTATION_REGEX.exec(notation) : undefined | ||
return this.NOTATION_REGEX ? this.NOTATION_REGEX.exec(notation) : null | ||
} | ||
@@ -50,3 +60,5 @@ | ||
* Check whether URI belongs to the scheme, return local notation on success. | ||
* Requires scheme to have `uriPattern` or `namespace`. | ||
* @param {string} uri | ||
* @returns {string|undefined} | ||
*/ | ||
@@ -64,2 +76,3 @@ notationFromUri(uri) { | ||
* Map local notation to URI. Does not check whether notation is valid! | ||
* Requires scheme to have `uriPattern` or `namespace`. | ||
* @param {string} notation | ||
@@ -78,2 +91,3 @@ * @example scheme.uriFromNotation("123") // http://example.org/123 | ||
* Check whether URI belongs to the scheme, return concept on success. | ||
* Requires scheme to have `uriPattern` or `namespace`. | ||
* @param {string} uri | ||
@@ -87,4 +101,15 @@ */ | ||
} | ||
/** | ||
* Map local notation to concept, if notation is valid. Requires scheme to | ||
* have `uriPattern` (or `namespace`) and `notationPattern`. | ||
* @param {string} notation | ||
*/ | ||
conceptFromNotation (notation) { | ||
if (this.isValidNotation(notation)) { | ||
return this.conceptFromUri(this.uriFromNotation(notation)) | ||
} | ||
} | ||
} | ||
module.exports = ConceptScheme |
{ | ||
"name": "jskos-tools", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "Tools for working with the JSKOS data format.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/gbv/jskos-tools", |
const assert = require("assert") | ||
const { ConceptScheme } = require("../index.js") | ||
const gnd = require("./gnd.scheme.json") | ||
describe("ConceptConceptScheme", () => { | ||
let scheme = new ConceptScheme(gnd) | ||
let uri = "http://d-nb.info/gnd/4021477-1" | ||
let notation = "4021477-1" | ||
let { uriPattern } = scheme | ||
describe("ConceptScheme", () => { | ||
const gnd = require("./gnd.scheme.json") | ||
const uri = "http://d-nb.info/gnd/4021477-1" | ||
const notation = "4021477-1" | ||
const scheme = new ConceptScheme(gnd) | ||
it("has copied some fields", () => { | ||
it("constructor", () => { | ||
assert.deepEqual(scheme.notation, ["GND"]) | ||
assert.equal(scheme.uriPattern, "^http://d-nb\\.info/gnd/([0-9X-]+)$") | ||
let small = new ConceptScheme({ namespace: scheme.namespace }) | ||
assert.equal(small.uriPattern, "^http://d-nb\\.info/gnd/(.+)$") | ||
}) | ||
it("isValidNotation", () => { | ||
assert.ok(scheme.isValidNotation(notation)) | ||
assert.ok(!scheme.isValidNotation(undefined)) | ||
assert.ok(!scheme.isValidNotation("x")) | ||
}) | ||
it("maps notation <=> uri", () => { | ||
@@ -28,12 +38,38 @@ assert.equal(notation, scheme.notationFromUri(uri)) | ||
it("conceptFromNotation", () => { | ||
let concept = scheme.conceptFromNotation(notation) | ||
assert.deepEqual({uri, notation: [notation]}, concept ) | ||
}) | ||
}) | ||
describe("ConceptScheme with spaces in notation", () => { | ||
const scheme = new ConceptScheme({ | ||
namespace: "http://example.org/", | ||
notationPattern: "[A-Z]( [A-Z])*" | ||
}) | ||
const notation = "A B" | ||
const uri = "http://example.org/A%20B" | ||
it("isValidNotation", () => { | ||
assert.ok(scheme.isValidNotation(notation)) | ||
assert.ok(!scheme.isValidNotation("x")) | ||
}) | ||
it("builds uriPattern", () => { | ||
delete scheme.uriPattern | ||
scheme = new ConceptScheme(scheme) | ||
assert.equal(uriPattern, scheme.uriPattern) | ||
it("uriFromNotation", () => { | ||
assert.equal(uri, scheme.uriFromNotation(notation)) | ||
}) | ||
it("notationFromUri", () => { | ||
assert.equal(notation, scheme.notationFromUri(uri)) | ||
}) | ||
it("conceptFromUri", () => { | ||
assert.deepEqual({uri, notation: [notation]}, scheme.conceptFromUri(uri)) | ||
}) | ||
it("conceptFromNotation", () => { | ||
let concept = scheme.conceptFromNotation(notation) | ||
assert.deepEqual({uri, notation: [notation]}, concept ) | ||
}) | ||
}) |
@@ -29,4 +29,3 @@ { | ||
], | ||
"uri" : "http://d-nb.info/gnd/7749153-1", | ||
"uriPattern" : "^http://d-nb\\.info/gnd/([0-9X-]+)$" | ||
"uri" : "http://d-nb.info/gnd/7749153-1" | ||
} |
@@ -13,2 +13,6 @@ const assert = require("assert") | ||
assert.equal(guessObjectType(name, true), shortName) | ||
let plural = shortName[-1] === "y" | ||
? shortName.slice(0,-1) + "ies" : shortName + "s" | ||
assert.equal(guessObjectType(plural, true), shortName) | ||
} | ||
@@ -15,0 +19,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75408
2012