Skip to content

Commit f290627

Browse files
authored
Merge pull request #2 from lucasmafra/fix-empty-list
Fix search crash when empty list
2 parents cb4d8e8 + fc48cc7 commit f290627

File tree

6 files changed

+49
-10
lines changed

6 files changed

+49
-10
lines changed

.github/workflows/actions.yml

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
on: push
1+
on: [push, pull_request]
22

33
name: CI
44

@@ -10,31 +10,29 @@ jobs:
1010
uses: actions/checkout@master
1111
- name: Run tests
1212
uses: comigor/actions/dart-test@master
13+
env:
14+
DTA_EXCLUDE_REGEX: example
1315
check-version-and-changelog:
1416
needs: test
1517
runs-on: ubuntu-latest
1618
container: golang
1719
steps:
1820
- uses: actions/checkout@master
1921
- run: |
20-
go get -u github.com/itchyny/gojq/cmd/gojq
22+
GO111MODULE=on go get -u github.com/itchyny/gojq/cmd/gojq@d24ecb5d89a9eee8b4cd2071bdff7585a8b44f0e
2123
cd "$GITHUB_WORKSPACE"
2224
- name: Check if version on pubspec.yaml was changed and if there's an entry for this new version on CHANGELOG
2325
run: |
2426
git fetch --prune --unshallow
25-
2627
if test "${{ github.ref }}" = "refs/heads/master"; then
2728
where=HEAD~$(gojq '.commits | length' "${GITHUB_EVENT_PATH}")
2829
else
2930
where=origin/master
3031
fi
31-
3232
diff=$(git diff $where pubspec.yaml)
3333
echo "$diff" | grep -E '\+.*version'
34-
3534
mkdir -p artifacts
3635
git diff -U0 $where CHANGELOG.md | grep '^\+' | grep -Ev '^(--- a/|\+\+\+ b/)' | sed -E 's/^\+(.*)/\1/g' > artifacts/changelog
37-
3836
package_version=$(cat pubspec.yaml | gojq --yaml-input -r '.version')
3937
cat CHANGELOG.md | grep "$package_version"
4038
echo "$package_version" > artifacts/version
@@ -77,4 +75,4 @@ jobs:
7775
- name: Publish to pub.dev
7876
uses: comigor/actions/pub-publish@master
7977
env:
80-
PUB_CREDENTIALS: ${{ secrets.PUB_CREDENTIALS }}
78+
PUB_CREDENTIALS: ${{ secrets.PUB_CREDENTIALS }}

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# CHANGELOG
2+
## 0.2.1
3+
- Fix bug for search empty list
24

35
## 0.2.0+8
46
- Testing GitHub actions

example/pubspec.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: fuzzy_example
2+
version: 0.0.1
3+
4+
authors:
5+
- Igor Borges <[email protected]>
6+
7+
environment:
8+
sdk: '>=2.0.0 <3.0.0'
9+
10+
dependencies:
11+
fuzzy:
12+
path: ../

lib/fuzzy.dart

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ export 'data/fuzzy_options.dart';
1818
class Fuzzy<T> {
1919
/// Instantiates it given a list of strings to look into, and options
2020
Fuzzy(
21-
this.list, {
21+
List<T> list, {
2222
FuzzyOptions<T> options,
23-
}) : options = options ?? FuzzyOptions<T>();
23+
}) : list = list ?? [],
24+
options = options ?? FuzzyOptions<T>();
2425

2526
/// The original list of string
2627
final List<T> list;
@@ -30,6 +31,8 @@ class Fuzzy<T> {
3031

3132
/// Search for a given [pattern] on the [list], optionally [limit]ing the result length
3233
List<Result<T>> search(String pattern, [int limit = -1]) {
34+
if (list.isEmpty) return <Result<T>>[];
35+
3336
final searchers = _prepareSearchers(pattern);
3437

3538
final resultsAndWeights =

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: fuzzy
2-
version: 0.2.0+8
2+
version: 0.2.1
33

44
authors:
55
- Igor Borges <[email protected]>

test/fuzzy_test.dart

+24
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@ Fuzzy setup({
3131
}
3232

3333
void main() {
34+
group('Empty list of strings', () {
35+
Fuzzy fuse;
36+
setUp(() {
37+
fuse = setup(itemList: <String>[]);
38+
});
39+
test('empty result is returned', () {
40+
final result = fuse.search('Bla');
41+
expect(result.isEmpty, true);
42+
});
43+
});
44+
45+
group('Null list', () {
46+
Fuzzy fuse;
47+
List<String> items;
48+
setUp(() {
49+
fuse = Fuzzy(items, options: defaultOptions);
50+
});
51+
test('empty result is returned', () {
52+
final result = fuse.search('Bla');
53+
print(result);
54+
expect(result.isEmpty, true);
55+
});
56+
});
57+
3458
group('Flat list of strings: ["Apple", "Orange", "Banana"]', () {
3559
Fuzzy fuse;
3660
setUp(() {

0 commit comments

Comments
 (0)