From 93cdbead4ef9c1c42a8b1b5547352c1f12f18cae Mon Sep 17 00:00:00 2001 From: maxlath Date: Tue, 5 Dec 2023 18:36:21 +0100 Subject: [PATCH] parseGuid: fix support for lexem forms and senses guids addressing https://github.com/maxlath/wikibase-cli/issues/181 --- lib/parse_command_utils.js | 24 +++++++++--------- test/parse_command_utils.js | 49 +++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 test/parse_command_utils.js diff --git a/lib/parse_command_utils.js b/lib/parse_command_utils.js index f196e99..6e96837 100644 --- a/lib/parse_command_utils.js +++ b/lib/parse_command_utils.js @@ -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 } diff --git a/test/parse_command_utils.js b/test/parse_command_utils.js new file mode 100644 index 0000000..6090c59 --- /dev/null +++ b/test/parse_command_utils.js @@ -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') + }) +})