Skip to content

Commit

Permalink
update function for stringToJsRegex (#69)
Browse files Browse the repository at this point in the history
* update function for stringToJsRegex
* fix dependency
* prettier fix
  • Loading branch information
briangann authored Sep 7, 2019
1 parent feaac71 commit 758d84b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All notable changes to this project will be documented in this file.

## [0.0.9] - 2019-09-07

- Fix stringToJsRegex reference error

## [0.0.8] - 2019-09-07

- update packages

## [0.0.7] - 2019-07-26

New features/bugfixes by contributor jmp0x00, thanks!!
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "briangann-datatable-panel",
"private": false,
"license": "MIT",
"version": "0.0.8",
"version": "0.0.9",
"description": "Grafana Datatable Panel",
"homepage": "https://github.com/briangann/grafana-datatable-panel",
"author": {
Expand Down Expand Up @@ -60,6 +60,7 @@
"@types/lodash": "^4.14.138",
"@types/systemjs": "^0.20.6",
"angular": "^1.6.6",
"jquery": "1.9.1",
"lodash": "^4.17.15",
"popper.js": "^1.15.0",
"systemjs": "^6.0.0"
Expand Down
53 changes: 39 additions & 14 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,6 @@ export class DatatableRenderer {
this.sanitize = sanitize;
}

// taken from @grafana/data
stringToJsRegex(str: string): RegExp {
if (str[0] !== '/') {
return new RegExp('^' + str + '$');
}
const match = str.match(new RegExp('^/(.*?)/(g?i?m?y?)$'));
if (!match) {
throw new Error(`'${str}' is not a valid regular expression.`);
}
return new RegExp(match[1], match[2]);
}
/**
* Given a value, return the color corresponding to the threshold set
* @param {[Float]} value [Value to be evaluated]
Expand Down Expand Up @@ -78,6 +67,18 @@ export class DatatableRenderer {
* @return {[type]} [description]
*/
defaultCellFormatter(v: any, style: any, column: any) {
// taken from @grafana/data
function stringToJsRegex(str: string): RegExp {
if (str[0] !== '/') {
return new RegExp('^' + str + '$');
}
const match = str.match(new RegExp('^/(.*?)/(g?i?m?y?)$'));
if (!match) {
throw new Error(`'${str}' is not a valid regular expression.`);
}
return new RegExp(match[1], match[2]);
}

if (v === null || v === void 0 || v === undefined || column === null) {
return '';
}
Expand All @@ -96,7 +97,7 @@ export class DatatableRenderer {
style.splitPattern = '/ /';
}

const regex = this.stringToJsRegex(String(style.splitPattern));
const regex = stringToJsRegex(String(style.splitPattern));
const values = v.split(regex);
if (typeof cellTemplate !== 'undefined') {
// Replace $__cell with this cell's content.
Expand Down Expand Up @@ -224,11 +225,23 @@ export class DatatableRenderer {
* @return {[type]} [description]
*/
formatColumnValue(colIndex: any, rowIndex: any, value: any) {
// taken from @grafana/data
function stringToJsRegex(str: string): RegExp {
if (str[0] !== '/') {
return new RegExp('^' + str + '$');
}
const match = str.match(new RegExp('^/(.*?)/(g?i?m?y?)$'));
if (!match) {
throw new Error(`'${str}' is not a valid regular expression.`);
}
return new RegExp(match[1], match[2]);
}

if (!this.formatters[colIndex]) {
for (let i = 0; i < this.panel.styles.length; i++) {
const style = this.panel.styles[i];
const column = this.table.columns[colIndex];
const regex = this.stringToJsRegex(style.pattern);
const regex = stringToJsRegex(style.pattern);
if (column.text.match(regex)) {
this.formatters[colIndex] = this.createColumnFormatter(style, column);
}
Expand Down Expand Up @@ -277,14 +290,26 @@ export class DatatableRenderer {
}

getStyleForColumn(columnNumber: any) {
// taken from @grafana/data
function stringToJsRegex(str: string): RegExp {
if (str[0] !== '/') {
return new RegExp('^' + str + '$');
}
const match = str.match(new RegExp('^/(.*?)/(g?i?m?y?)$'));
if (!match) {
throw new Error(`'${str}' is not a valid regular expression.`);
}
return new RegExp(match[1], match[2]);
}

let colStyle = null;
for (let i = 0; i < this.panel.styles.length; i++) {
const style = this.panel.styles[i];
const column = this.table.columns[columnNumber];
if (column === undefined) {
break;
}
const regex = this.stringToJsRegex(style.pattern);
const regex = stringToJsRegex(style.pattern);
if (column.text.match(regex)) {
colStyle = style;
break;
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6005,6 +6005,11 @@ jmespath@0.15.0:
resolved "https://registry.yarnpkg.com/jmespath/-/jmespath-0.15.0.tgz#a3f222a9aae9f966f5d27c796510e28091764217"
integrity sha1-o/Iiqarp+Wb10nx5ZRDigJF2Qhc=

jquery@1.9.1:
version "1.9.1"
resolved "https://registry.yarnpkg.com/jquery/-/jquery-1.9.1.tgz#e4cd4835faaefbade535857613c0fc3ff2adaf34"
integrity sha1-5M1INfqu+63lNYV2E8D8P/KtrzQ=

jquery@3.4.1, jquery@>=1.7:
version "3.4.1"
resolved "https://registry.yarnpkg.com/jquery/-/jquery-3.4.1.tgz#714f1f8d9dde4bdfa55764ba37ef214630d80ef2"
Expand Down

0 comments on commit 758d84b

Please sign in to comment.