skip to Main Content

We have UI editor from which user can select any number of days.
If the user selects continuous days, those are represented by dashes or hyphens, but if non-continuous days are selected then those are represented by commas..
eg - String - M,W,F – Non Continuous days.
eg - String - M-F – User selected continuous 5 days.

From backend point of view, if we are getting some string like M-Tu,Th,Sa-Su or M-W,F-Su etc combination, how to find the valid days from this ?.

I have created a constant array to either loop or check the value

DAYS: ['M', 'Tu', 'W', 'Th', 'F', 'Sa', 'Su']
let userInput = 'M-Tu,Th,Sa-Su' or 'M-W,F-Su'

SomeMethod(userInput) {
  let validDays = [];
  const commaSeperatedDays = userInput?.split(',');
  const contDaysFromCommaSeparatedDays = commaSeperatedDays.map(x => 
  x.split('-'));
  let indDays = validDays.push(contDaysFromCommaSeparatedDays.map(x => 
  findIndividualDays(x)));

  //When only continuous days are there
  const ContinuousDays = days?.split('-');
}

function findIndividualDays(days) {
//getting confused here how to handle cases - if there is any inbuilt library ?
        if (days.length === 1) {
            return days[0];
        }
        let indDays = [];
        const startIndex = constants.DAYS.indexOf(days[0]);
        const endIndex = constants.DAYS.indexOf(days[1]);
        for (let index = startIndex; index <= endIndex; index++) {
            const element = constants.DAYS[index];
            indDays.push(element);
        }
        return { ...indDays };
    }

3

Answers


  1. Chosen as BEST ANSWER

    Modified my solution little bit and this is working for me.

    const DAYS = ['M', 'Tu', 'W', 'Th', 'F', 'Sa', 'Su'];
    const inputSample1 = 'M-Tu,Th,Sa-Su';
    const inputSample2 = 'M-W,F-Su';
    const inputSample3 = 'M-F';
    const inputSample4 = 'W,F-Su';
    
    function findIndividualDays(days, validDays) {
       if (days.length === 1) {
          validDays.push(days[0]);
       }
       const startIndex = DAYS.indexOf(days[0]);
       const endIndex = DAYS.indexOf(days[1]);
       for (let index = startIndex; index <= endIndex; index++) {
          const element = DAYS[index];
          validDays.push(element);
       }
    }
    
    const parseDays = (inputString) => {
        let validDays = [];
        const commaSeperatedDays = inputString?.split(',');
        const contDaysFromCommaSeparatedDays = commaSeperatedDays.map(x => x.split('-'));
        contDaysFromCommaSeparatedDays.map(x => findIndividualDays(x, validDays));
        return validDays;
    }
    
    console.log(parseDays(inputSample1));
    console.log(parseDays(inputSample2));
    console.log(parseDays(inputSample3));
    console.log(parseDays(inputSample4));


  2. const DAYS = ['M', 'Tu', 'W', 'Th', 'F', 'Sa', 'Su'];
    const DAY_DELIMITER = ',';
    const DAY_INTERVAL_DELIMITER = '-';
    
    const inputSample1 = 'M-Tu,Th,Sa-Su';
    const inputSample2 = 'M-W,F-Su';
    
    const intervalToDays = (interval) => {
        if (interval.includes(DAY_INTERVAL_DELIMITER)) {
            // if it is an interval, break it down into days
            const [startDay, endDay] = interval.split(DAY_INTERVAL_DELIMITER);
            const continuousDays = [];
            for (i = DAYS.indexOf(startDay); i <= DAYS.indexOf(endDay); i++) {
                continuousDays.push(DAYS[i]);
            }
            return continuousDays.join(DAY_DELIMITER);
        } else {
            // if it is just one day, return it as is
            return interval;
        }
    }
    
    const parseDays = (inputString) => {
        const intervals = inputString.split(DAY_DELIMITER);
        return intervals.map(interval => intervalToDays(interval)).join(DAY_DELIMITER);
    }
    
    console.log(parseDays(inputSample1));
    console.log(parseDays(inputSample2));
    Login or Signup to reply.
  3. Just to add an option for fun, I like using generator functions

    const DAYS = ['M', 'Tu', 'W', 'Th', 'F', 'Sa', 'Su'];
    
    function* enumerateDays(inputString){
        for(const value of inputString.split(',')){
        const [start,end] = value.split('-');
        if(end)
            for(index = DAYS.indexOf(start), endIndex = DAYS.indexOf(end) ;index <= endIndex;index++)
                yield DAYS[index];
        else
            yield start;
      }
    }
    
    
    
    //test cases
    const test = (inputString) => console.log(...enumerateDays(inputString));
    test( 'M-Tu,Th,Sa-Su');
    test('M-W,F-Su');
    test('M-Su');
    test('M,Tu,Th-Sa');
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search