-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
parseGuid: fix support for lexem forms and senses guids
addressing #181
- Loading branch information
Showing
2 changed files
with
62 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
// Normalize prefixed statements GUIDs as returned by SPARQL queries: | ||
// to the format used by Wikibase API | ||
// - from: wds:Q1827902-6706334E-D27E-4F4B-B6DA-ABE7544DF11C | ||
// - to: Q1827902$6706334E-D27E-4F4B-B6DA-ABE7544DF11C | ||
// Also accept Q1827902-6706334E-D27E-4F4B-B6DA-ABE7544DF11C | ||
export const parseGuid = guid => { | ||
guid = guid | ||
// Normalize prefixed statements GUIDs as returned by SPARQL queries: | ||
// to the format used by Wikibase API | ||
// - from: wds:Q1827902-6706334E-D27E-4F4B-B6DA-ABE7544DF11C | ||
// - to: Q1827902$6706334E-D27E-4F4B-B6DA-ABE7544DF11C | ||
// Also accept Q1827902-6706334E-D27E-4F4B-B6DA-ABE7544DF11C | ||
.replace(/^([a-z]+:)?([QPLM]\d+)-(.*)/i, '$2$$$3') | ||
// Often required when getting guids from the command line, as passing a value with a $ | ||
// can be challenging, and require different quoting strategies | ||
.replace(/("|')/g, '') | ||
.replace('\\$', '$') | ||
.trim() | ||
// Drop prefix | ||
.replace(/^[a-z]+:/, '') | ||
.replace(/^([QPLM]\d+(-[FS]\d+)?)-([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/i, '$1$$$3') | ||
// Often required when getting guids from the command line, as passing a value with a $ | ||
// can be challenging, and require different quoting strategies | ||
.replace(/("|')/g, '') | ||
.replace('\\$', '$') | ||
.trim() | ||
return guid | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { parseGuid } from '#lib/parse_command_utils' | ||
import 'should' | ||
|
||
describe('parseGuid', () => { | ||
it('should support a valid item guid', async () => { | ||
const guid = 'Q235116$6E538A76-DDE7-4ED4-AE4F-6BCCFD6B74A5' | ||
parseGuid(guid).should.equal(guid) | ||
}) | ||
|
||
it('should support shell-friendly hyphenated item guid', async () => { | ||
const guid = 'Q235116-6E538A76-DDE7-4ED4-AE4F-6BCCFD6B74A5' | ||
parseGuid(guid).should.equal('Q235116$6E538A76-DDE7-4ED4-AE4F-6BCCFD6B74A5') | ||
}) | ||
|
||
it('should support prefixed hyphenated item guid', async () => { | ||
const guid = 'wds:Q235116-6E538A76-DDE7-4ED4-AE4F-6BCCFD6B74A5' | ||
parseGuid(guid).should.equal('Q235116$6E538A76-DDE7-4ED4-AE4F-6BCCFD6B74A5') | ||
}) | ||
|
||
it('should support an over-escaped item guid', async () => { | ||
const guid = 'Q235116\\$6E538A76-DDE7-4ED4-AE4F-6BCCFD6B74A5' | ||
parseGuid(guid).should.equal('Q235116$6E538A76-DDE7-4ED4-AE4F-6BCCFD6B74A5') | ||
}) | ||
|
||
it('should support a double quoted item guid', async () => { | ||
const guid = '"Q235116$6E538A76-DDE7-4ED4-AE4F-6BCCFD6B74A5"' | ||
parseGuid(guid).should.equal('Q235116$6E538A76-DDE7-4ED4-AE4F-6BCCFD6B74A5') | ||
}) | ||
|
||
it('should support a simple quoted item guid', async () => { | ||
const guid = "'Q235116$6E538A76-DDE7-4ED4-AE4F-6BCCFD6B74A5'" | ||
parseGuid(guid).should.equal('Q235116$6E538A76-DDE7-4ED4-AE4F-6BCCFD6B74A5') | ||
}) | ||
|
||
it('should support a valid lexem form guid', async () => { | ||
const guid = 'L235116-F1$6E538A76-DDE7-4ED4-AE4F-6BCCFD6B74A5' | ||
parseGuid(guid).should.equal(guid) | ||
}) | ||
|
||
it('should support shell-friendly hyphenated lexem form guid', async () => { | ||
const guid = 'L235116-F1-6E538A76-DDE7-4ED4-AE4F-6BCCFD6B74A5' | ||
parseGuid(guid).should.equal('L235116-F1$6E538A76-DDE7-4ED4-AE4F-6BCCFD6B74A5') | ||
}) | ||
|
||
it('should support prefixed hyphenated lexem form guid', async () => { | ||
const guid = 'wds:L235116-F1-6E538A76-DDE7-4ED4-AE4F-6BCCFD6B74A5' | ||
parseGuid(guid).should.equal('L235116-F1$6E538A76-DDE7-4ED4-AE4F-6BCCFD6B74A5') | ||
}) | ||
}) |