Skip to content

Commit 4272438

Browse files
authored
Merge pull request #148 from bindable-ui/dd/search3
Fix highlight phrase
2 parents 5907448 + c8809ac commit 4272438

File tree

6 files changed

+92
-6
lines changed

6 files changed

+92
-6
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,19 @@ Run `yarn start`, then open `http://localhost:9000`
101101

102102
### Unit tests
103103

104-
Run `yarn test`
104+
Run `yarn test` to run all unit tests.
105+
106+
To test one file and watch for changes, run:
107+
108+
```shell
109+
yarn test:1 <relative_path_to_test_file.test.ts>
110+
```
105111

106112
### Release
107113

108114
This is used when updating Bindable.
109115

110-
Run `yarn update-version`. This will update the version in `package.json` and the title attribe in `dev-app/app.html` to the new version you specify.
116+
Run `yarn update-version`. This will update the version in `package.json` and the title attribute in `dev-app/app.html` to the new version you specify.
111117

112118
Once you merge your changes to master, [create a new release on Github](https://github.com/bindable-ui/bindable/releases).
113119

dev-app/app.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<c-nav-horizontal-item
5353
position="right"
5454
href="https://github.com/bindable-ui/bindable"
55-
title="v1.11.7"
55+
title="v1.11.8"
5656
></c-nav-horizontal-item>
5757
</c-nav-horizontal>
5858
</l-box>

jest1.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#! /usr/bin/env node
2+
/* eslint-disable no-console */
3+
const path = require('path');
4+
const chalk = require('chalk');
5+
const fs = require('fs');
6+
const childProcess = require('child_process');
7+
const opn = require('opn');
8+
9+
const args = process.argv.slice(2);
10+
11+
const showUsage = () => {
12+
console.log(chalk.yellow('Usage:'));
13+
console.log(chalk.yellow('--------------------------------------------------------------'));
14+
console.log(chalk.yellow('[**/*.test.js] # test file with coverage report'));
15+
console.log(chalk.yellow('--nowatch # turn off watcher'));
16+
console.log(chalk.yellow('open # opens the coverage report'));
17+
console.log(chalk.yellow('--help # display this message'));
18+
console.log(chalk.yellow('--------------------------------------------------------------'));
19+
};
20+
21+
let command = 'node_modules/jest/bin/jest.js --watch';
22+
let openCoverageReport = false;
23+
if (args.length === 0) {
24+
showUsage();
25+
return;
26+
}
27+
args.forEach(entry => {
28+
if (entry === '-h' || entry === '--help') {
29+
showUsage();
30+
} else if (entry === '--nowatch') {
31+
command = command.replace('--watch', '');
32+
} else if (entry === 'open') {
33+
openCoverageReport = true;
34+
command = command.replace('--watch', '');
35+
} else {
36+
if (!fs.existsSync(entry)) {
37+
console.log(chalk.red(`Error!! Test file: ${entry} doesn't exist.`));
38+
process.exit();
39+
return;
40+
}
41+
command += ` ${entry}`;
42+
43+
const {dir, name, ext} = path.parse(entry);
44+
const filename = name.replace('.test', '') + ext;
45+
const searchPath = `${dir}/${filename}`;
46+
const exists = fs.existsSync(searchPath);
47+
if (!exists) {
48+
console.log(chalk.red(`Error!! Original file: ${searchPath} doesn't exist.`));
49+
} else {
50+
command += ` --coverage --collectCoverageFrom=${searchPath}`;
51+
}
52+
}
53+
});
54+
55+
command += ' --color always';
56+
try {
57+
console.log(command);
58+
childProcess.execSync(command, {stdio: 'inherit'});
59+
if (openCoverageReport) {
60+
opn('./test/coverage-jest/index.html', {wait: false});
61+
}
62+
} catch (e) {} // eslint-disable-line no-empty

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@bindable-ui/bindable",
33
"description": "An Aurelia component library",
4-
"version": "1.11.7",
4+
"version": "1.11.8",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/bindable-ui/bindable"
@@ -99,6 +99,7 @@
9999
"preversion": "au test",
100100
"test-coverage": "jest --coverage=true",
101101
"test": "jest --coverage=true",
102+
"test:1": "./jest1.js",
102103
"test:debug": "jest --coverage=false --runInBand --detectOpenHandles --forceExit",
103104
"test:ci": "jest --coverage=true --ci --detectOpenHandles --forceExit",
104105
"update-version": "node ./update-version.js"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {highlightSearchPhrases} from './highlight-phrases';
2+
3+
describe('Highlight Search Phrases Helper', () => {
4+
it('should highlight selected search phrases', () => {
5+
const searchPhrases = ['dumb', 'and'];
6+
const desc = 'Dumb and Dumber';
7+
const highlighted = highlightSearchPhrases(searchPhrases, desc);
8+
expect(highlighted).toEqual(
9+
'<span style="background-color: #226684;">Dumb</span> ' +
10+
'<span style="background-color: #226684;">and</span> ' +
11+
'<span style="background-color: #226684;">Dumb</span>er',
12+
);
13+
const empty = highlightSearchPhrases(searchPhrases);
14+
expect(empty).toEqual('');
15+
});
16+
});

src/helpers/highlight-phrases.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ export const highlightSearchPhrases = (searchPhrases: string[], matchAgainst?: s
55
.replace('<', '&lt;')
66
.replace('>', '&gt;');
77
if (searchPhrases && searchPhrases.length > 0) {
8-
searchPhrases.forEach(orig => {
9-
title = title.replace(orig, `<span style="background-color: #226684;">${orig}</span>`);
8+
searchPhrases.forEach(sp => {
9+
const regEx = new RegExp(sp, 'gi');
10+
title = title.replace(regEx, '<span style="background-color: #226684;">$&</span>');
1011
});
1112
}
1213
return title;

0 commit comments

Comments
 (0)