forked from getredash/redash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
127b45a
commit 8fc3af7
Showing
10 changed files
with
240 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import moment from 'moment/moment'; | ||
import numeral from 'numeral'; | ||
import _ from 'underscore'; | ||
|
||
// eslint-disable-next-line | ||
const urlPattern = /(^|[\s\n]|<br\/?>)((?:https?|ftp):\/\/[\-A-Z0-9+\u0026\u2019@#\/%?=()~_|!:,.;]*[\-A-Z0-9+\u0026@#\/%=~()_|])/gi; | ||
|
||
function createDefaultFormatter(highlightLinks) { | ||
if (highlightLinks) { | ||
return (value) => { | ||
if (_.isString(value)) { | ||
value = value.replace(urlPattern, '$1<a href="$2" target="_blank">$2</a>'); | ||
} | ||
return value; | ||
}; | ||
} | ||
return value => value; | ||
} | ||
|
||
function createDateTimeFormatter(format) { | ||
if (_.isString(format) && (format !== '')) { | ||
return (value) => { | ||
if (value && moment.isMoment(value)) { | ||
return value.format(format); | ||
} | ||
return value; | ||
}; | ||
} | ||
return value => value; | ||
} | ||
|
||
function createBooleanFormatter(values) { | ||
if (_.isArray(values)) { | ||
if (values.length >= 2) { | ||
// Both `true` and `false` specified | ||
return value => '' + values[value ? 1 : 0]; | ||
} else if (values.length === 1) { | ||
// Only `true` | ||
return value => (value ? values[0] : ''); | ||
} | ||
} | ||
return value => (value ? 'true' : 'false'); | ||
} | ||
|
||
function createNumberFormatter(format) { | ||
if (_.isString(format) && (format !== '')) { | ||
const n = numeral(0); // cache `numeral` instance | ||
return value => n.set(value).format(format); | ||
} | ||
return value => value; | ||
} | ||
|
||
export default function createFormatter(column) { | ||
switch (column.displayAs) { | ||
case 'number': return createNumberFormatter(column.numberFormat); | ||
case 'boolean': return createBooleanFormatter(column.booleanValues); | ||
case 'datetime': return createDateTimeFormatter(column.dateTimeFormat); | ||
default: return createDefaultFormatter(column.allowHTML && column.highlightLinks); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.