Skip to content

Commit

Permalink
fix(datetime): improve parseTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed May 17, 2016
1 parent 0bd736d commit 55ec80a
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 50 deletions.
2 changes: 1 addition & 1 deletion ionic/components/datetime/test/datetime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ describe('DateTime', () => {

// pt-br
var customLocale: datetime.LocaleData = {
dayShort: [
dayNames: [
'domingo',
'segunda-feira',
'ter\u00e7a-feira',
Expand Down
47 changes: 26 additions & 21 deletions ionic/util/datetime-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,33 +266,38 @@ export function updateDate(existingData: DateTimeData, newData: any) {


export function parseTemplate(template: string): string[] {
var formats: string[] = [];
let formats: string[] = [];

template = template.replace(/[^\w\s]/gi, ' ');

var foundFormats: {index: number; format: string}[] = [];
FORMAT_KEYS.forEach(format => {
var index = template.indexOf(format.f);
if (index > -1) {
template = template.replace(format.f, replacer(format.f));
foundFormats.push({
index: index,
format: format.f,
});
if (format.f.length > 1 && template.indexOf(format.f) > -1 && template.indexOf(format.f + format.f.charAt(0)) < 0) {
template = template.replace(format.f, ' ' + format.f + ' ');
}
});

// sort the found formats back to their original order
foundFormats.sort((a, b) => (a.index > b.index) ? 1 : (a.index < b.index) ? -1 : 0);

return foundFormats.map(val => val.format);
}

let words = template.split(' ').filter(w => w.length > 0);
words.forEach((word, i) => {
if (word.length) {
FORMAT_KEYS.forEach(format => {
if (word === format.f) {
if (word === FORMAT_A || word === FORMAT_a) {
// this format is an am/pm format, so it's an "a" or "A"
if ((formats.indexOf(FORMAT_h) < 0 && formats.indexOf(FORMAT_hh) < 0) ||
(words[i - 1] !== FORMAT_m && words[i - 1] !== FORMAT_mm)) {
// template does not already have a 12-hour format
// or this am/pm format doesn't have a minute format immediately before it
// so do not treat this word "a" or "A" as an am/pm format
return;
}
}
formats.push(word);
}
});
}
});

function replacer(originalStr: string): string {
let r = '';
for (var i = 0; i < originalStr.length; i++) {
r += '^';
}
return r;
return formats;
}


Expand Down
107 changes: 79 additions & 28 deletions ionic/util/test/datetime-util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,36 +249,87 @@ describe('getValueFromFormat', () => {

describe('parseTemplate', () => {

it('should get formats from template "a A m mm h hh H HH D DD DDD DDDD M MM MMM MMMM YY YYYY"', () => {
var formats = datetime.parseTemplate('a A m mm h hh H HH D DD DDD DDDD M MM MMM MMMM YY YYYY');
expect(formats[0]).toEqual('a');
expect(formats[1]).toEqual('A');
expect(formats[2]).toEqual('m');
expect(formats[3]).toEqual('mm');
expect(formats[4]).toEqual('h');
expect(formats[5]).toEqual('hh');
expect(formats[6]).toEqual('H');
expect(formats[7]).toEqual('HH');
expect(formats[8]).toEqual('D');
expect(formats[9]).toEqual('DD');
expect(formats[10]).toEqual('DDD');
expect(formats[11]).toEqual('DDDD');
expect(formats[12]).toEqual('M');
expect(formats[13]).toEqual('MM');
expect(formats[14]).toEqual('MMM');
expect(formats[15]).toEqual('MMMM');
expect(formats[16]).toEqual('YY');
expect(formats[17]).toEqual('YYYY');
});

it('should get formats from template YYMMMMDDHma', () => {
var formats = datetime.parseTemplate('YYMMMMDDHma');
it('should not parse D thats apart of a word', () => {
var formats = datetime.parseTemplate('D Days');
expect(formats.length).toEqual(1);
expect(formats[0]).toEqual('D');
});

it('should not parse D thats apart of a word', () => {
var formats = datetime.parseTemplate('DD Days');
expect(formats.length).toEqual(1);
expect(formats[0]).toEqual('DD');
});

it('should not parse m thats apart of a word', () => {
var formats = datetime.parseTemplate('m mins');
expect(formats.length).toEqual(1);
expect(formats[0]).toEqual('m');
});

it('should not parse M thats apart of a word', () => {
var formats = datetime.parseTemplate('mm Minutes');
expect(formats.length).toEqual(1);
expect(formats[0]).toEqual('mm');
});

it('should not pickup "a" within 12-hour, but its not the am/pm', () => {
var formats = datetime.parseTemplate('hh:mm is a time');
expect(formats.length).toEqual(2);
expect(formats[0]).toEqual('hh');
expect(formats[1]).toEqual('mm');
});

it('should allow am/pm when using 12-hour and no spaces', () => {
var formats = datetime.parseTemplate('hh:mma');
expect(formats.length).toEqual(3);
expect(formats[0]).toEqual('hh');
expect(formats[1]).toEqual('mm');
expect(formats[2]).toEqual('a');
});

it('should allow am/pm when using 12-hour', () => {
var formats = datetime.parseTemplate('hh:mm a');
expect(formats.length).toEqual(3);
expect(formats[0]).toEqual('hh');
expect(formats[1]).toEqual('mm');
expect(formats[2]).toEqual('a');
});

it('should not add am/pm when not using 24-hour', () => {
var formats = datetime.parseTemplate('HH:mm a');
expect(formats.length).toEqual(2);
expect(formats[0]).toEqual('HH');
expect(formats[1]).toEqual('mm');
});

it('should get formats from template "m mm h hh H HH D DD DDD DDDD M MM MMM MMMM YY YYYY"', () => {
var formats = datetime.parseTemplate('m mm h hh H HH D DD DDD DDDD M MM MMM MMMM YY YYYY');
expect(formats[0]).toEqual('m');
expect(formats[1]).toEqual('mm');
expect(formats[2]).toEqual('h');
expect(formats[3]).toEqual('hh');
expect(formats[4]).toEqual('H');
expect(formats[5]).toEqual('HH');
expect(formats[6]).toEqual('D');
expect(formats[7]).toEqual('DD');
expect(formats[8]).toEqual('DDD');
expect(formats[9]).toEqual('DDDD');
expect(formats[10]).toEqual('M');
expect(formats[11]).toEqual('MM');
expect(formats[12]).toEqual('MMM');
expect(formats[13]).toEqual('MMMM');
expect(formats[14]).toEqual('YY');
expect(formats[15]).toEqual('YYYY');
});

it('should get formats from template YYMMMMDDHHmm', () => {
var formats = datetime.parseTemplate('YYMMMMDDHHmm');
expect(formats[0]).toEqual('YY');
expect(formats[1]).toEqual('MMMM');
expect(formats[2]).toEqual('DD');
expect(formats[3]).toEqual('H');
expect(formats[4]).toEqual('m');
expect(formats[5]).toEqual('a');
expect(formats[3]).toEqual('HH');
expect(formats[4]).toEqual('mm');
});

it('should get formats from template MM/DD/YYYY', () => {
Expand Down Expand Up @@ -741,7 +792,7 @@ describe('parseISODate', () => {

// pt-br
var customLocale: datetime.LocaleData = {
dayShort: [
dayNames: [
'domingo',
'segunda-feira',
'ter\u00e7a-feira',
Expand Down

0 comments on commit 55ec80a

Please sign in to comment.