Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change memory search to match by parts and case insensitive #942

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})
})