|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright (c) 2020 Julien Gonzalez |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in all |
| 13 | + * copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | + * SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +const tag_function = require('./utils/tag-function'); |
| 25 | + |
| 26 | +const |
| 27 | + { compose |
| 28 | + , constant |
| 29 | + , fallback |
| 30 | + , join |
| 31 | + , map |
| 32 | + , match |
| 33 | + , split |
| 34 | + , tail |
| 35 | + , trim |
| 36 | + } = require('./utils/fp'); |
| 37 | + |
| 38 | +const INVALID_DATE = String(new Date('All those moments will be lost in time, like tears in the rain.')); |
| 39 | +const RE = /^\s*@\S+/; |
| 40 | + |
| 41 | +/** @enum {string} */ |
| 42 | +const formatOptions = |
| 43 | + { DAY_NUM_LEADING_ZEROS: 'd' |
| 44 | + , DAY_NUM: 'j' |
| 45 | + , MONTH_NUM_LEADING_ZEROS: 'm' |
| 46 | + , MONTH_NUM: 'n' |
| 47 | + , YEAR_NUM_LONG: 'Y' |
| 48 | + , YEAR_NUM: 'y' |
| 49 | + }; |
| 50 | + |
| 51 | +/** |
| 52 | + * @type {Object<string, function(Date): string>} |
| 53 | + */ |
| 54 | +const formatFunctions = |
| 55 | + { [formatOptions.DAY_NUM_LEADING_ZEROS]: |
| 56 | + d => String(d.getDate()).padStart(2, '0') |
| 57 | + |
| 58 | + , [formatOptions.DAY_NUM]: |
| 59 | + d => String(d.getDate()) |
| 60 | + |
| 61 | + , [formatOptions.MONTH_NUM_LEADING_ZEROS]: |
| 62 | + d => String(d.getMonth() + 1).padStart(2, '0') |
| 63 | + |
| 64 | + , [formatOptions.MONTH_NUM]: |
| 65 | + d => String(d.getMonth() + 1) |
| 66 | + |
| 67 | + , [formatOptions.YEAR_NUM_LONG]: |
| 68 | + d => String(d.getFullYear()) |
| 69 | + |
| 70 | + , [formatOptions.YEAR_NUM]: |
| 71 | + d => String(d.getFullYear()).slice(-2) |
| 72 | + }; |
| 73 | + |
| 74 | +const is_date = |
| 75 | + x => |
| 76 | + x instanceof Date |
| 77 | + && String(x) !== INVALID_DATE; |
| 78 | + |
| 79 | +/** |
| 80 | + * Extract a date format from a string |
| 81 | + * |
| 82 | + * @example |
| 83 | + * get_format('@Y-m-d some extraneous text'); |
| 84 | + * //=> "Y-m-d" |
| 85 | + * |
| 86 | + * @function |
| 87 | + * @param {string} str |
| 88 | + * @return {string} |
| 89 | + */ |
| 90 | +const get_format = |
| 91 | + compose |
| 92 | + ( tail |
| 93 | + , trim |
| 94 | + , fallback('') |
| 95 | + , match(RE) |
| 96 | + ); |
| 97 | + |
| 98 | +const clear_format = str => str.replace(RE, ''); |
| 99 | + |
| 100 | +const formatter = |
| 101 | + d => |
| 102 | + f => |
| 103 | + (formatFunctions[f] || constant(f)) |
| 104 | + (d); |
| 105 | + |
| 106 | +const format = |
| 107 | + (f, d) => |
| 108 | + compose |
| 109 | + ( join('') |
| 110 | + , map(formatter(d)) |
| 111 | + , split(/([^Yymndj]+)/) |
| 112 | + )(f); |
| 113 | + |
| 114 | +/** |
| 115 | + * Format dates within a string. |
| 116 | + * |
| 117 | + * The `time` tag formats all interpolated dates according to a given format. |
| 118 | + * |
| 119 | + * ```javascript |
| 120 | + * import {time} from '@customcommander/tagtical'; |
| 121 | + * |
| 122 | + * time`Last login on ${date}@Y-m-d`; |
| 123 | + * //=> "Last login on 2020-01-09" |
| 124 | + * ``` |
| 125 | + * |
| 126 | + * The format is attached to the date as follow `${date}@Y-m-d`. |
| 127 | + * |
| 128 | + * The `@` sign links a date with a format and the format is made of formatting characters |
| 129 | + * as seen in [PHP's date function](https://www.php.net/manual/en/function.date.php). |
| 130 | + * The format is removed from the string after processing. |
| 131 | + * |
| 132 | + * Only a subset of these options is supported at the moment: |
| 133 | + * |
| 134 | + * | Character | Description | |
| 135 | + * |:----------|:------------------------------------------------| |
| 136 | + * | Y | Year. Four digits | |
| 137 | + * | y | Year. Two digits | |
| 138 | + * | m | Month. Two digits with leading zeros. e.g. "01" | |
| 139 | + * | n | Month. No leading zeros. e.g. "1" | |
| 140 | + * | d | Day. Two digits; leading zeros. e.g. "01" | |
| 141 | + * | j | Day. No leading zeros. e.g. "1" | |
| 142 | + * |
| 143 | + * Anything that isn't a formatting character is rendered as is. |
| 144 | + * |
| 145 | + * When an interpolated value isn't a date, the value is rendered as is and the date format is removed. |
| 146 | + * |
| 147 | + * ```javascript |
| 148 | + * time`Last login on ${0/0}@Y-m-d`; |
| 149 | + * //=> Last login on NaN |
| 150 | + * ``` |
| 151 | + */ |
| 152 | +module.exports = |
| 153 | + tag_function |
| 154 | + ( (l, x, r) => |
| 155 | + [ l |
| 156 | + , !is_date(x) || !get_format(r) |
| 157 | + ? x |
| 158 | + : format(get_format(r), x) |
| 159 | + , clear_format(r) |
| 160 | + ] |
| 161 | + ); |
0 commit comments