Skip to content

Commit

Permalink
Fixed #341
Browse files Browse the repository at this point in the history
  • Loading branch information
krisk committed Mar 9, 2020
1 parent be759d5 commit af4e659
Show file tree
Hide file tree
Showing 14 changed files with 334 additions and 199 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ node_modules/
package-lock.json

# Filter test runner
runner.js
runner.js

perf/
1 change: 1 addition & 0 deletions dist/fuse.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ declare class Fuse<T, O extends Fuse.FuseOptions<T>> {
)

setCollection(list: ReadonlyArray<T>): ReadonlyArray<T>;
list: ReadonlyArray<T>
}

declare namespace Fuse {
Expand Down
2 changes: 1 addition & 1 deletion dist/fuse.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"homepage": "http://fusejs.io",
"keywords": [
"fuzzy",
"search",
"bitap"
],
"main": "dist/fuse.js",
Expand Down Expand Up @@ -48,5 +49,6 @@
},
"files": [
"dist/"
]
],
"dependencies": {}
}
92 changes: 92 additions & 0 deletions release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env bash

version='';
re="\"(version)\": \"([^\"]*)\"";

while read -r l; do
if [[ $l =~ $re ]]; then
value="${BASH_REMATCH[2]}";
version="$value";
fi
done < package.json;

echo $version;

file_has_changed () {
if [ ! -f $1 ]; then
return 1
fi

for f in `git ls-files --modified`; do
[[ "$f" == "$1" ]] && return 0
done

return 1
}

version_is_unique () {
for v in `git tag -l`; do
[[ "$v" == "v$1" ]] && return 1
done

return 0
}

on_master_branch () {
[[ $(git symbolic-ref --short -q HEAD) == "master" ]] && return 0
return 1
}

# version=$(cat VERSION)
previous_version=$(git describe --abbrev=0)

if ! on_master_branch; then
echo -e "\033[0;31mRefusing to release from non master branch.\033[0m"
exit 1
fi

if ! file_has_changed "VERSION"; then
echo -e "\033[0;31mRefusing to release because VERSION has not changed.\033[0m"
exit 1
fi

if ! file_has_changed "CHANGELOG.md"; then
echo -e "\033[0;31mRefusing to release because CHANGELOG.md has not been updated.\033[0m"
exit 1
fi

if ! file_has_changed "package.json"; then
echo -e "\033[0;31mRefusing to release because package.json has not been updated.\033[0m"
exit 1
fi

if ! version_is_unique $version; then
echo -e "\033[0;31mRefusing to release because VERSION is not unique.\033[0m"
exit 1
fi

echo -e "\033[1mAbout to release v$version with the following changes:\033[0m"
git log --date=short --pretty=format:"%ad %h%x09%an%x09%s" $previous_version..HEAD

echo

echo -e "\033[1mThe following files will be part of the release commit:\033[0m"
git ls-files --modified

echo

read -e -p "Are you sure you want to release? " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo -e "\033[0;32mReleasing...\033[0m"
echo
# yarn build
# git commit -a -m "Build version $version"
# git tag -a v$version -m "Version $version"
# git push origin master
# git push --tags

# npm publish
else
echo -e "\033[0;31mCancelling...\033[0m"
fi
15 changes: 8 additions & 7 deletions src/bitap/bitap_search.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const bitapScore = require('./bitap_score')
const matchedIndices = require('./bitap_matched_indices')

module.exports = (text, pattern, patternAlphabet, { location = 0, distance = 100, threshold = 0.6, findAllMatches = false, minMatchCharLength = 1 }) => {
module.exports = (text, pattern, patternAlphabet, { location = 0, distance = 100, threshold = 0.6, findAllMatches = false, minMatchCharLength = 1, includeMatches = false }) => {
const expectedLocation = location
// Set starting location at beginning text and initialize the alphabet.
const textLen = text.length
Expand Down Expand Up @@ -135,21 +135,22 @@ module.exports = (text, pattern, patternAlphabet, { location = 0, distance = 100
distance
})

//console.log('score', score, finalScore)

if (score > currentThreshold) {
break
}

lastBitArr = bitArr
}

//console.log('FINAL SCORE', finalScore)

// Count exact matches (those with a score of 0) to be "almost" exact
return {
let _res = {
isMatch: bestLocation >= 0,
score: finalScore === 0 ? 0.001 : finalScore,
matchedIndices: matchedIndices(matchMask, minMatchCharLength)
}

if (includeMatches) {
_res.matchedIndices = matchedIndices(matchMask, minMatchCharLength)
}

return _res
}
31 changes: 19 additions & 12 deletions src/bitap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class Bitap {
// match is found before the end of the same input.
findAllMatches = false,
// Minimum number of characters that must be matched before a result is considered a match
minMatchCharLength = 1
minMatchCharLength = 1,

includeMatches = false
}) {
this.options = {
location,
Expand All @@ -35,31 +37,39 @@ class Bitap {
isCaseSensitive,
tokenSeparator,
findAllMatches,
includeMatches,
minMatchCharLength
}

this.pattern = this.options.isCaseSensitive ? pattern : pattern.toLowerCase()
this.pattern = isCaseSensitive ? pattern : pattern.toLowerCase()

if (this.pattern.length <= maxPatternLength) {
this.patternAlphabet = patternAlphabet(this.pattern)
}
}

search (text) {
if (!this.options.isCaseSensitive) {
const { isCaseSensitive, includeMatches } = this.options

if (!isCaseSensitive) {
text = text.toLowerCase()
}

// Exact match
if (this.pattern === text) {
return {
let result = {
isMatch: true,
score: 0,
matchedIndices: [[0, text.length - 1]]
score: 0
}

if (includeMatches) {
result.matchedIndices = [[0, text.length - 1]]
}

return result
}

// When pattern length is greater than the machine word length, just do a a regex comparison
// When pattern length is greater than the machine word length, just do a regex comparison
const { maxPatternLength, tokenSeparator } = this.options
if (this.pattern.length > maxPatternLength) {
return bitapRegexSearch(text, this.pattern, tokenSeparator)
Expand All @@ -72,13 +82,10 @@ class Bitap {
distance,
threshold,
findAllMatches,
minMatchCharLength
minMatchCharLength,
includeMatches
})
}
}

// let x = new Bitap("od mn war", {})
// let result = x.search("Old Man's War")
// console.log(result)

module.exports = Bitap
39 changes: 0 additions & 39 deletions src/helpers/deep_value.js

This file was deleted.

1 change: 0 additions & 1 deletion src/helpers/is_array.js

This file was deleted.

0 comments on commit af4e659

Please sign in to comment.