Allow locale.dow to accept integers?

Posted on
  • I'm developing an app which needs a list of the days of the week, but right now the require("locale").dow() function only accepts a Date object. So I've had to do this:

    let daysOfWeek = [];
    for (let i = 1; i <= 7; i++) {
    
        daysOfWeek.push(require('locale').dow(ne­w Date(1970, 1, i, 0, 0, 0, 0)); // 1/1/1970 was a Sunday
    }
    // Result: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    

    It seems to me it would be more efficient if the dow() function could accept an integer as well (0 = Sunday, 6 = Saturday) because right now it's just calling getDay() on the argument. That way, the code would be simpler:

    let daysOfWeek = [];
    for (let i = 0; i < 7; i++) {
        daysOfWeek.push(require('locale').dow(i)­;
    }
    

    Does this make sense?

  • P.S: I'm not saying the function should no longer accept Date objects: that would break previous implementations. Instead, it would accept both Date and integer arguments.

  • I just spotted your neat hack of require('locale').dow({getDay:()=>i}) that's a good solution for now.

    But yes, we could definitely extend it - the only issue is it needs extending in firmware and the locale app

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Allow locale.dow to accept integers?

Posted by Avatar for qucchia0 @qucchia0

Actions