Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getPickerCols #1906

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/time-picker/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export enum EPickerCols {
}

// RegExp
export const TIME_FORMAT = /(a\s+|A\s+)?(h+|H+)?:?(m+)?:?(s+)?:?(S+)?(\s+a|\s+A)?/;
export const TIME_FORMAT = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g;
// 上下午前置
export const PRE_MERIDIEM_FORMAT = /^(a|A)\s+?[h]{1,2}(:[m]{1,2}(:[s]{1,2})?)?$/;
// 上下午后置
Expand Down
37 changes: 37 additions & 0 deletions js/time-picker/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dayjs from 'dayjs';
import advancedFormat from 'dayjs/plugin/advancedFormat';
import customParseFormat from 'dayjs/plugin/customParseFormat';
import { EPickerCols, TIME_FORMAT } from './const';

dayjs.extend(advancedFormat);
dayjs.extend(customParseFormat);
Expand All @@ -26,3 +27,39 @@ export function closestLookup(
(a, b) => Math.abs(calcVal + 1 - a) - Math.abs(calcVal + 1 - b)
)[0];
}

export function getPickerCols(format:string) {
const renderCol: EPickerCols[] = [];
const {
meridiem, hour, minute, second, milliSecond,
} = EPickerCols;
const match = format.match(TIME_FORMAT);
match.forEach((m) => {
switch (m) {
case 'H':
case 'HH':
case 'h':
case 'hh':
renderCol.push(hour);
break;
case 'a':
case 'A':
renderCol.push(meridiem);
break;
case 'm':
case 'mm':
renderCol.push(minute);
break;
case 's':
case 'ss':
renderCol.push(second);
break;
case 'SSS':
renderCol.push(milliSecond);
break;
default:
break;
}
});
return renderCol;
}
78 changes: 78 additions & 0 deletions test/unit/time-picker/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { getPickerCols } from '../../../js/time-picker/utils';

describe('utils', () => {
describe(' getPickerCols', () => {
it('h', () => {
const res = getPickerCols('h');
expect(res.length).toBe(1);
expect(res[0]).toBe('hour');
});

it('m', () => {
const res = getPickerCols('m');
expect(res.length).toBe(1);
expect(res[0]).toBe('minute');
});

it('s', () => {
const res = getPickerCols('s');
expect(res.length).toBe(1);
expect(res[0]).toBe('second');
});

it('S', () => {
const res = getPickerCols('s');
expect(res.length).toBe(1);
expect(res[0]).toBe('second');
});

it('hh', () => {
const res = getPickerCols('hh');
expect(res.length).toBe(1);
expect(res[0]).toBe('hour');
});

it('HH', () => {
const res = getPickerCols('HH');
expect(res.length).toBe(1);
expect(res[0]).toBe('hour');
});
it('hh:mm', () => {
const res = getPickerCols('hh:mm');
expect(res.length).toBe(2);
expect(res[0]).toBe('hour');
expect(res[1]).toBe('minute');
});

it('HH:mm', () => {
const res = getPickerCols('HH:mm');
expect(res.length).toBe(2);
expect(res[0]).toBe('hour');
expect(res[1]).toBe('minute');
});

it('HH时mm分', () => {
const res = getPickerCols('HH时mm分');
expect(res.length).toBe(2);
expect(res[0]).toBe('hour');
expect(res[1]).toBe('minute');
});

it('hh:mm', () => {
const res = getPickerCols('hh:mm:ss');
expect(res.length).toBe(3);
expect(res[0]).toBe('hour');
expect(res[1]).toBe('minute');
expect(res[2]).toBe('second');
});

it('HH时mm分ss秒SSS毫秒', () => {
const res = getPickerCols('HH时mm分ss秒SSS毫秒');
expect(res.length).toBe(4);
expect(res[0]).toBe('hour');
expect(res[1]).toBe('minute');
expect(res[2]).toBe('second');
expect(res[3]).toBe('millisecond');
});
});
});
Loading