From a0d314bf5e707d7918afd6f6034f911f7712d3a0 Mon Sep 17 00:00:00 2001 From: Sashika Nawarathne Date: Tue, 17 Nov 2020 23:50:00 +0530 Subject: [PATCH] Add type param filter (#596) Format code Add match by type tests --- web/samples_index/lib/src/search.dart | 11 +++++++++++ web/samples_index/test/samples_index_test.dart | 8 +++++++- web/samples_index/tool/grind.dart | 5 ++++- web/samples_index/web/description.dart | 3 +-- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/web/samples_index/lib/src/search.dart b/web/samples_index/lib/src/search.dart index 3d2d6e834..3d2ad4fd7 100644 --- a/web/samples_index/lib/src/search.dart +++ b/web/samples_index/lib/src/search.dart @@ -9,6 +9,17 @@ bool matchesQuery(String query, String sampleAttributes) { var queryWords = query.split(' ')..removeWhere((s) => s.isEmpty); var attributes = sampleAttributes.split(' ')..removeWhere((s) => s.isEmpty); + // Test for type filter + // This will check whether a type parameter is present in the + // search query, and return false if the self type mismatches + // the query type + for (var word in queryWords) { + if ((word.contains('type:') && !attributes.contains(word)) || + (word.contains('platform:') && !attributes.contains('type:demo'))) { + return false; + } + } + // Test for exact matches if (attributes.contains(query)) { return true; diff --git a/web/samples_index/test/samples_index_test.dart b/web/samples_index/test/samples_index_test.dart index e08e2e9a7..b4d8510e8 100644 --- a/web/samples_index/test/samples_index_test.dart +++ b/web/samples_index/test/samples_index_test.dart @@ -107,7 +107,8 @@ void main() { 'widget:AnimatedBuilder ' 'widget:FutureBuilder ' 'package:json_serializable ' - 'package:path'; + 'package:path ' + 'type:sample'; // Test if various queries match these attributes expect(matchesQuery('foo', attributes), false); @@ -124,6 +125,11 @@ void main() { expect(matchesQuery('kitten tag:cats', attributes), true); expect(matchesQuery('tag:beginner dogs', attributes), false); expect(matchesQuery('asdf ', attributes), false); + + // Test if queries match by type + expect(matchesQuery('type:sample', attributes), true); + expect(matchesQuery('type:cookbook', attributes), false); + expect(matchesQuery('kittens type:cookbook', attributes), false); }); }); diff --git a/web/samples_index/tool/grind.dart b/web/samples_index/tool/grind.dart index e7220778c..5b385763f 100644 --- a/web/samples_index/tool/grind.dart +++ b/web/samples_index/tool/grind.dart @@ -33,7 +33,10 @@ void deploy() { @Depends(createThumbnails) Future buildRelease() async { var app = PubApp.local('build_runner'); - await app.runAsync('build --release --output web:public --delete-conflicting-outputs'.split(' ').toList()); + await app.runAsync( + 'build --release --output web:public --delete-conflicting-outputs' + .split(' ') + .toList()); } @DefaultTask('Build the project.') diff --git a/web/samples_index/web/description.dart b/web/samples_index/web/description.dart index 39783ae32..94f05a07a 100644 --- a/web/samples_index/web/description.dart +++ b/web/samples_index/web/description.dart @@ -5,6 +5,5 @@ import 'package:mdc_web/mdc_web.dart'; InputElement searchInput; void main() { - querySelectorAll('.mdc-card__primary-action') - .forEach((el) => MDCRipple(el)); + querySelectorAll('.mdc-card__primary-action').forEach((el) => MDCRipple(el)); }