Skip to content

Commit fbc0a6b

Browse files
committed
init
0 parents  commit fbc0a6b

File tree

8 files changed

+164
-0
lines changed

8 files changed

+164
-0
lines changed

.babelrc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"presets": [
3+
"es2015", "stage-2"
4+
],
5+
"plugins": [
6+
"transform-runtime"
7+
]
8+
}
9+

.eslintrc

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"extends": "airbnb-base",
3+
"parser": "babel-eslint",
4+
"rules": {
5+
"arrow-parens": ["error", "as-needed"],
6+
"class-methods-use-this": 0,
7+
"comma-dangle": ["error", {
8+
"arrays": "always-multiline",
9+
"objects": "always-multiline",
10+
"imports": "always-multiline",
11+
"exports": "always-multiline",
12+
"functions": "never",
13+
}],
14+
"import/newline-after-import": 0,
15+
"import/no-extraneous-dependencies": 0,
16+
"import/prefer-default-export": 0,
17+
"import/no-dynamic-require": 0,
18+
"no-use-before-define": [2, "nofunc"],
19+
"space-before-function-paren": [2, "never"],
20+
"semi": ["error", "never"],
21+
"no-underscore-dangle": 0,
22+
"new-cap": 0,
23+
"prefer-template": 0,
24+
"no-path-concat": 0,
25+
"global-require": 0,
26+
"max-len": 0,
27+
"no-console": 0
28+
}
29+
}

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
.env
3+
dist
4+
lib

.npmignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
src
2+
test

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# github-api-isomorphic

package.json

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "array-fuzzy-match",
3+
"version": "0.1.0",
4+
"description": "babel 6 boilerplate",
5+
"main": "lib/index.js",
6+
"scripts": {
7+
"test": "./node_modules/.bin/ava --verbose test/* --watch",
8+
"lint": "./node_modules/.bin/eslint 'src/**/*.js' 'test/**/*.js'",
9+
"build": "./node_modules/babel-cli/bin/babel.js src --out-dir lib",
10+
"build:watch": "./node_modules/babel-cli/bin/babel.js src --out-dir lib --watch"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "https://github.com/ruanyl/array-fuzzy-match.git"
15+
},
16+
"keywords": [
17+
"fuzzy",
18+
"array",
19+
"match"
20+
],
21+
"author": "Ruan Yulong <[email protected]> (http://github.com/ruanyl)",
22+
"license": "MIT",
23+
"bugs": {
24+
"url": "https://github.com/ruanyl/array-fuzzy-match/issues"
25+
},
26+
"homepage": "https://github.com/ruanyl/array-fuzzy-match",
27+
"dependencies": {
28+
"babel-runtime": "^6.23.0",
29+
"es6-promise": "^4.0.5",
30+
"isomorphic-fetch": "^2.2.1",
31+
"pinyin": "^2.8.0"
32+
},
33+
"devDependencies": {
34+
"ava": "^0.18.2",
35+
"babel-cli": "^6.23.0",
36+
"babel-core": "^6.23.1",
37+
"babel-eslint": "^7.1.1",
38+
"babel-plugin-transform-runtime": "^6.23.0",
39+
"babel-preset-es2015": "^6.22.0",
40+
"babel-preset-stage-2": "^6.22.0",
41+
"eslint": "^3.16.0",
42+
"eslint-config-airbnb-base": "^11.1.0",
43+
"eslint-plugin-import": "^2.2.0"
44+
},
45+
"ava": {
46+
"require": ["babel-register"]
47+
}
48+
}

src/index.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export default (arr, str, customConfig) => {
2+
const config = { letterFromStart: true, wordFromStart: false, ...customConfig }
3+
4+
const allStr = arr.join('').toUpperCase()
5+
let wordIndexes = []
6+
let firstLetters = ''
7+
let isMatchLetter = false
8+
let isMatchWord = false
9+
10+
arr.forEach((word, i) => {
11+
wordIndexes = wordIndexes.concat(i === 0 ? 0 : arr[i - 1].length)
12+
firstLetters = `${firstLetters}${word.charAt(0).toUpperCase()}`
13+
})
14+
15+
if (config.letterFromStart) {
16+
isMatchLetter = firstLetters.indexOf(str.toUpperCase()) === 0
17+
} else {
18+
isMatchLetter = firstLetters.indexOf(str.toUpperCase()) >= 0
19+
}
20+
21+
if (config.wordFromStart) {
22+
isMatchWord = allStr.indexOf(str.toUpperCase()) === 0
23+
} else {
24+
isMatchWord = wordIndexes.includes(allStr.indexOf(str.toUpperCase()))
25+
}
26+
27+
return isMatchLetter || isMatchWord
28+
}

test/test.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import test from 'ava'
2+
3+
import isMatch from '../src'
4+
5+
test('should return true if matches words', t => {
6+
const arr = ['zhong', 'guo', 'zhong', 'che']
7+
t.true(isMatch(arr, 'zhong'))
8+
t.true(isMatch(arr, 'guo'))
9+
t.true(isMatch(arr, 'zhongg'))
10+
t.true(isMatch(arr, 'guozhong'))
11+
})
12+
13+
test('should return true if matches words case-insensitive', t => {
14+
const arr = ['zhong', 'guo', 'zhong', 'che']
15+
t.true(isMatch(arr, 'Zhong'))
16+
t.true(isMatch(arr, 'GUO'))
17+
t.true(isMatch(arr, 'ZHONGG'))
18+
t.true(isMatch(arr, 'guoZhong'))
19+
})
20+
21+
test('should matches first letter of each word from beginning', t => {
22+
const arr = ['zhong', 'guo', 'zhong', 'che']
23+
t.true(isMatch(arr, 'ZG'))
24+
t.true(isMatch(arr, 'ZGZC'))
25+
t.false(isMatch(arr, 'GZ'))
26+
})
27+
28+
test('should match from the beginning', t => {
29+
const arr = ['zhong', 'guo', 'zhong', 'che']
30+
const config = { wordFromStart: true }
31+
t.true(isMatch(arr, 'zhong', config))
32+
t.false(isMatch(arr, 'guo', config))
33+
t.true(isMatch(arr, 'zhongg', config))
34+
t.false(isMatch(arr, 'guozhong', config))
35+
})
36+
37+
test('should matches first letter of each word', t => {
38+
const arr = ['zhong', 'guo', 'zhong', 'che']
39+
const config = { letterFromStart: false }
40+
t.true(isMatch(arr, 'ZG', config))
41+
t.true(isMatch(arr, 'ZGZC', config))
42+
t.true(isMatch(arr, 'GZ', config))
43+
})

0 commit comments

Comments
 (0)