Skip to content

Commit

Permalink
Change memory search to match by parts and case insensitive
Browse files Browse the repository at this point in the history
The dev and production deployment uses AzureSearch, where search for
pattern "npmjs/red" will return results including npm/npmjs/-/redis/0.1.0.
In addition, the search is case insensitive.

In comparison, memory search (typically used during local development)
searches strictly for coordinates string containing "npmjs/red".
Change to match by parts and case insensitive.  This is to make
memory search more consistent with AzureSearch.
  • Loading branch information
qtomlinson committed Jul 12, 2022
1 parent 882a55e commit 1736292
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
7 changes: 6 additions & 1 deletion providers/search/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ class MemorySearch extends AbstractSearch {
* @returns {String[]} The list of suggested coordinates found
*/
async suggestCoordinates(pattern) {
const patternElements = pattern?.split('/').map(e => e.toLowerCase())
return values(this.index)
.filter(definition => definition.coordinates.includes(pattern))
.filter(definition => this._isMatch(patternElements, definition.coordinates.toLowerCase()))
.map(entry => entry.coordinates)
}

_isMatch(requiredParts = [], coordinates) {
return requiredParts.every(part => coordinates.includes(part))
}

store(definitions) {
const entries = this._getEntries(Array.isArray(definitions) ? definitions : [definitions])
entries.forEach(entry => (this.index[entry.coordinates] = entry))
Expand Down
38 changes: 38 additions & 0 deletions test/providers/search/memoryTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// (c) Copyright 2022, SAP SE and ClearlyDefined contributors. Licensed under the MIT license.
// SPDX-License-Identifier: MIT

const { expect } = require('chai')
const EntityCoordinates = require('../../../lib/entityCoordinates')
const MemorySearch = require('../../../providers/search/memory')

describe('memory search tests', () => {
const searches = {
'npmjs/red': 'npm/npmjs/-/redis/0.1.0',
'github/bit': 'git/github/bitflags/bitflags/518aaf91494e94f41651a40f1b38d6ab522b0235',
'mavencentral/org.apache': 'maven/mavencentral/org.apache.httpcomponents/httpcore/4.1',
'nuget/xunit': 'nuget/nuget/-/xunit.core/2.4.1',
'pypi/back': 'pypi/pypi/-/backports.ssl_match_hostname/3.7.0.1',
'rubygems/sma': 'gem/rubygems/-/small/0.4',
'cratesio/bit': 'crate/cratesio/-/bitflags/1.0.4',
'debian/0a': 'deb/debian/-/0ad/0.0.17-1_amd64',
'packagist/sym': 'composer/packagist/symfony/polyfill-mbstring/1.11.0',
'cocoapods/soft': 'pod/cocoapods/-/SoftButton/0.1.0'
}

let memorySearch
before(() =>{
const definitions = Object.values(searches)
.map(EntityCoordinates.fromString)
.map(coordinates => ({ coordinates }))

memorySearch = MemorySearch({})
memorySearch.store(definitions)
})

it('should search successfully', async () => {
Object.entries(searches).forEach(async ([key, value]) => {
const result = await memorySearch.suggestCoordinates(key)
expect(result[0]).to.be.equal(value)
})
})
})

0 comments on commit 1736292

Please sign in to comment.