Skip to content

Commit

Permalink
Export timeunit.format() for VgTooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
light-and-salt committed Mar 13, 2016
1 parent b5b8e7c commit 23fc2b9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/compile/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {contains} from '../util';

import {FacetModel} from './facet';
import {Model} from './model';
import {format as timeFormatExpr} from './time';
import {format as timeFormatExpr} from '../timeunit';
import {UnitModel} from './unit';


Expand Down
50 changes: 0 additions & 50 deletions src/compile/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,6 @@ import {contains, range} from '../util';
import {COLUMN, ROW, SHAPE, COLOR, Channel} from '../channel';
import {TimeUnit} from '../timeunit';

/** returns the template name used for axis labels for a time unit */
export function format(timeUnit: TimeUnit, abbreviated = false): string {
if (!timeUnit) {
return undefined;
}

let timeString = timeUnit.toString();

let dateComponents = [];

if (timeString.indexOf('year') > -1) {
dateComponents.push(abbreviated ? '%y' : '%Y');
}

if (timeString.indexOf('month') > -1) {
dateComponents.push(abbreviated ? '%b' : '%B');
}

if (timeString.indexOf('day') > -1) {
dateComponents.push(abbreviated ? '%a' : '%A');
} else if (timeString.indexOf('date') > -1) {
dateComponents.push('%d');
}

let timeComponents = [];

if (timeString.indexOf('hours') > -1) {
timeComponents.push('%H');
}
if (timeString.indexOf('minutes') > -1) {
timeComponents.push('%M');
}
if (timeString.indexOf('seconds') > -1) {
timeComponents.push('%S');
}
if (timeString.indexOf('milliseconds') > -1) {
timeComponents.push('%L');
}

let out = [];
if (dateComponents.length > 0) {
out.push(dateComponents.join('-'));
}
if (timeComponents.length > 0) {
out.push(timeComponents.join(':'));
}

return out.length > 0 ? out.join(' ') : undefined;
}

/** returns the smallest nice unit for scale.nice */
export function smallestUnit(timeUnit): string {
if (!timeUnit) {
Expand Down
50 changes: 50 additions & 0 deletions src/timeunit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,53 @@ export const TIMEUNITS = [
TimeUnit.MINUTESSECONDS,
TimeUnit.SECONDSMILLISECONDS,
];

/** returns the template name used for axis labels for a time unit */
export function format(timeUnit: TimeUnit, abbreviated = false): string {
if (!timeUnit) {
return undefined;
}

let timeString = timeUnit.toString();

let dateComponents = [];

if (timeString.indexOf('year') > -1) {
dateComponents.push(abbreviated ? '%y' : '%Y');
}

if (timeString.indexOf('month') > -1) {
dateComponents.push(abbreviated ? '%b' : '%B');
}

if (timeString.indexOf('day') > -1) {
dateComponents.push(abbreviated ? '%a' : '%A');
} else if (timeString.indexOf('date') > -1) {
dateComponents.push('%d');
}

let timeComponents = [];

if (timeString.indexOf('hours') > -1) {
timeComponents.push('%H');
}
if (timeString.indexOf('minutes') > -1) {
timeComponents.push('%M');
}
if (timeString.indexOf('seconds') > -1) {
timeComponents.push('%S');
}
if (timeString.indexOf('milliseconds') > -1) {
timeComponents.push('%L');
}

let out = [];
if (dateComponents.length > 0) {
out.push(dateComponents.join('-'));
}
if (timeComponents.length > 0) {
out.push(timeComponents.join(':'));
}

return out.length > 0 ? out.join(' ') : undefined;
}

0 comments on commit 23fc2b9

Please sign in to comment.