Skip to content

Commit

Permalink
parseGuid: fix support for lexem forms and senses guids
Browse files Browse the repository at this point in the history
addressing #181
  • Loading branch information
maxlath committed Dec 5, 2023
1 parent 102d0cd commit 93cdbea
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
24 changes: 13 additions & 11 deletions lib/parse_command_utils.js
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
}
49 changes: 49 additions & 0 deletions test/parse_command_utils.js
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')
})
})

0 comments on commit 93cdbea

Please sign in to comment.