Home Reference Source

src/utils/FormatterUtils.js

/**
 * Utils function for formatting of data before displaying them.
 */
import {ColorPalette} from './ColorConstants';

/**
 * Convert a date in ISO format to an array of strings containing weekDay, day,
 * month and year.
 * @param {string} dateString - A string with the date (YYY-MM-DD)
 * @returns {string[]} words array of strings [weekDay, day, month, year]
 */
function convertDate(dateString) {
    const date = new Date(dateString);
    const locDate = date.toLocaleDateString('no-NO', {dateStyle: 'full'});

    // Extract day, month year to string
    const words = locDate.split(/\.\s|\s/);
    return words;
}

/**
 * Return the title of the diary with the specified name as part of the title.
 * @param {string} name - The name to be formated
 * @returns {string} - Title with name
 */
function formatTitleAndName(name) {
    let title = name;
    title += name.slice(-1) == 's' ? "'" : 's';
    title += ' dagbok';
    return title;
}

/**
 * Set the color of the border and add it to the specified style
 * @param {string} paperColor - The color of the paper
 * @param {Object} styles - The styles
 * @returns {Object[]} array of styles containting the border color
 */
function setBorderStyle(paperColor, styles) {
    const borderColor =
        paperColor === ColorPalette.ALMOST_WHITE
            ? ColorPalette.ATOMIC_TANGERINE
            : ColorPalette.ALMOST_WHITE;
    return [styles, {borderColor: borderColor}];
}

export {convertDate, formatTitleAndName, setBorderStyle};