All files / src/utils/dayjs/plugin/advancedFormat index.js

11.53% Statements 3/26
0% Branches 0/33
33.33% Functions 1/3
11.53% Lines 3/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69        7x 7x 7x                                                                                                                            
import { FORMAT_DEFAULT } from 'dayjs/esm/constant';
 
export default (o, c) => {
    // locale needed later
    const proto = c.prototype;
    const oldFormat = proto.format;
    proto.format = function (formatStr) {
        const locale = this.$locale();
 
        if (!this.isValid()) {
            return oldFormat.bind(this)(formatStr);
        }
 
        const utils = this.$utils();
        const str = formatStr || FORMAT_DEFAULT;
        const result = str.replace(
            /\[([^\]]+)]|QQ|Q|wo|ww|w|WWWW|WWW|WW|W|zzz|z|gggg|GGGG|Do|X|x|k{1,2}|S/g,
            (match) => {
                switch (match) {
                    case 'Q':
                    case 'QQ':
                        return utils.s(
                            Math.ceil((this.$M + 1) / 3),
                            match === 'Q' ? 1 : 2,
                            'Q',
                        );
                    case 'Do':
                        return locale.ordinal(this.$D);
                    case 'gggg':
                        return this.weekYear();
                    case 'GGGG':
                        return this.isoWeekYear();
                    case 'wo':
                        return locale.ordinal(this.week(), 'W'); // W for week
                    case 'w':
                    case 'ww':
                        return utils.s(this.week(), match === 'w' ? 1 : 2, '0');
                    case 'W':
                    case 'WW':
                        return utils.s(this.isoWeek(), match === 'W' ? 1 : 2, '0');
                        // 为了支持W01这样的格式
                    case 'WWW':
                        return 'W' + this.isoWeek();
                    case 'WWWW':
                        return 'W' + utils.s(this.isoWeek(), 2, '0');
                    case 'k':
                    case 'kk':
                        return utils.s(
                            String(this.$H === 0 ? 24 : this.$H),
                            match === 'k' ? 1 : 2,
                            '0',
                        );
                    case 'X':
                        return Math.floor(this.$d.getTime() / 1000);
                    case 'x':
                        return this.$d.getTime();
                    case 'z':
                        return `[${this.offsetName()}]`;
                    case 'zzz':
                        return `[${this.offsetName('long')}]`;
                    default:
                        return match;
                }
            },
        );
        return oldFormat.bind(this)(result);
    };
};