node-rdf
node-rdf copied to clipboard
Improve PrefixMap.shrink() - shrink to longest-matching namespace
Addresses issue #9
I believe this is a better fix than the currently proposed patch.
I changed shrink() to match against a list of namespace IRIs, sorted by length. If a substring match is found, I then do a reverse-lookup from IRI to prefix.
The sorted list is only generated during shrink() if it does not already exist: it is cached between calls. Calls to set() or remove() nullify the cached list.
Two new properties have been added to PrefixMap for all this: _rev which holds a reverse-lookup from namespace IRI to prefix, and _nscache which holds the list of namespace IRIs, sorted by length.
Additionally, in set() I now silently ignore prefixes beginning with '_' (an underscore char - eaten by github), to prevent accidently clobbering of the new properties. Which, albeit a subtle behaviour change (which I'm not too keen on), shouldn't matter anyway because in Turtle, SPARQL, et al., underscore is not a valid start character for a prefix name.
Just noticed there are whitespace issues with this patch - happy to fix, please advise re: tabs/space/tabsize for the project