Skip to content

If class name in class expression equals to variable name Consulo displays "Duplicate declaration" error #47

@unv-unv

Description

@unv-unv

"Duplicate declaration" for class name which equals to variable name. This is normal for class expression
изображение

/**
 * Форматирование даты в стиле Java SimpleDateFormat.
 */
class DateFormatter
{
    constructor()
    {
        const DateMacro = class DateMacro
        {
            constructor(macro, leadingChars, dateFunc)
            {
                this.macro = macro;
                this.leadingChars = leadingChars;
                this.dateFunc = dateFunc;
            }

            pad(value)
            {
                const len = value.length, minLen = this.leadingChars.length;
                return (len >= minLen) ? value : this.leadingChars.substring(len) + value;
            }

            replace(pattern, date)
            {
                if (pattern.indexOf(this.macro) < 0) return pattern;
                const value = this.pad("" + this.dateFunc(date));
                return pattern.replace(this.macro, value);
            }
        };

        this.macros = [
            new DateMacro("yyyy", "0000", date => date.getFullYear()),
            new DateMacro("yy", "00", date => date.getFullYear() % 100),
            new DateMacro("MM", "00", date => date.getMonth() + 1),
            new DateMacro("M", "0", date => date.getMonth() + 1),
            new DateMacro("dd", "00", date => date.getDate()),
            new DateMacro("d", "0", date => date.getDate()),
            new DateMacro("HH", "00", date => date.getHours()),
            new DateMacro("H", "0", date => date.getHours()),
            new DateMacro("mm", "00", date => date.getMinutes()),
            new DateMacro("m", "0", date => date.getMinutes()),
            new DateMacro("ss", "00", date => date.getSeconds()),
            new DateMacro("s", "0", date => date.getSeconds()),
            new DateMacro("SSS", "000", date => date.getMilliseconds()),
            new DateMacro("S", "0", date => date.getMilliseconds())
        ];
    }

    format(date, format)
    {
        let result = format;
        for (const macro of this.macros) result = macro.replace(result, date);
        return result;
    }
}

DateFormatter.instance = new DateFormatter();

Date.prototype.format = function (format) { return DateFormatter.instance.format(this, format); };

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions