forked from flutter/samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.dart
More file actions
68 lines (59 loc) · 1.86 KB
/
search.dart
File metadata and controls
68 lines (59 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2020 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file
bool matchesQuery(String query, String sampleAttributes) {
if (query == null || query.isEmpty) {
return true;
}
var queryWords = query.split(' ')..removeWhere((s) => s.isEmpty);
var attributes = sampleAttributes.split(' ')..removeWhere((s) => s.isEmpty);
// Test for exact matches
if (attributes.contains(query)) {
return true;
}
// Test for exact matches for keywords
var matches = 0;
for (var word in queryWords) {
if (attributes.contains(word)) {
matches++;
}
if (matches == queryWords.length) {
return true;
}
}
// Test for queries whose keywords are a substring of any attribute
// e.g. searching "kitten tag:cats" is a match for a sample with the
// attributes "kittens tag:cats"
matches = 0;
for (var attribute in attributes) {
for (var queryWord in queryWords) {
if (attribute.startsWith(queryWord)) {
matches++;
}
}
// Only return true if each search term was matched
if (matches == queryWords.length) {
return true;
}
}
return false;
}
Map<String, String> parseHash(String hash) =>
Uri.parse(hash.substring(hash.indexOf('#') + 1)).queryParameters;
String formatHash(Map<String, String> parameters) =>
Uri().replace(queryParameters: parameters).toString();
String searchQueryFromParams(Map<String, String> params) {
var buf = StringBuffer();
if (params.containsKey('search')) {
buf.write(params['search']);
}
if (params.containsKey('type')) {
if (buf.isNotEmpty) buf.write(' ');
buf.write('type:' + params['type']);
}
if (params.containsKey('platform')) {
if (buf.isNotEmpty) buf.write(' ');
buf.write('platform:' + params['platform']);
}
return buf.toString();
}