|
| 1 | +export var iso8601 = { |
| 2 | + Period: {} |
| 3 | +}; |
| 4 | + |
| 5 | +// create sub packages |
| 6 | + |
| 7 | +//---- public properties |
| 8 | + |
| 9 | +/** |
| 10 | + * version of the ISO8601 version |
| 11 | + */ |
| 12 | +iso8601.version = '0.2'; |
| 13 | + |
| 14 | +//---- public methods |
| 15 | + |
| 16 | +/** |
| 17 | + * Returns an array of the duration per unit. The normalized sum of all array elements |
| 18 | + * represents the total duration. |
| 19 | + * |
| 20 | + * - array[0]: years |
| 21 | + * - array[1]: months |
| 22 | + * - array[2]: weeks |
| 23 | + * - array[3]: days |
| 24 | + * - array[4]: hours |
| 25 | + * - array[5]: minutes |
| 26 | + * - array[6]: seconds |
| 27 | + * |
| 28 | + * @param period iso8601 period string |
| 29 | + * @param distributeOverflow if 'true', the unit overflows are merge into the next higher units. Defaults to 'false'. |
| 30 | + */ |
| 31 | +iso8601.Period.parse = function (period, distributeOverflow) { |
| 32 | + return parsePeriodString(period, distributeOverflow); |
| 33 | +}; |
| 34 | + |
| 35 | +/** |
| 36 | + * Returns the total duration of the period in seconds. |
| 37 | + */ |
| 38 | +iso8601.Period.parseToTotalSeconds = function (period) { |
| 39 | + |
| 40 | + var multiplicators = [31104000 /* year (360*24*60*60) */, |
| 41 | + 2592000 /* month (30*24*60*60) */, |
| 42 | + 604800 /* week (24*60*60*7) */, |
| 43 | + 86400 /* day (24*60*60) */, |
| 44 | + 3600 /* hour (60*60) */, |
| 45 | + 60 /* minute (60) */, |
| 46 | + 1 /* second (1) */]; |
| 47 | + var durationPerUnit = parsePeriodString(period); |
| 48 | + var durationInSeconds = 0; |
| 49 | + |
| 50 | + for (var i = 0; i < durationPerUnit.length; i++) { |
| 51 | + durationInSeconds += durationPerUnit[i] * multiplicators[i]; |
| 52 | + } |
| 53 | + |
| 54 | + return durationInSeconds; |
| 55 | +}; |
| 56 | + |
| 57 | +/** |
| 58 | + * Return boolean based on validity of period |
| 59 | + * @param period |
| 60 | + * @return {Boolean} |
| 61 | + */ |
| 62 | +iso8601.Period.isValid = function (period) { |
| 63 | + try { |
| 64 | + parsePeriodString(period); |
| 65 | + return true; |
| 66 | + } catch (e) { |
| 67 | + return false; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Returns a more readable string representation of the ISO8601 period. |
| 73 | + * @param period the ISO8601 period string |
| 74 | + * @param unitName the names of the time units if there is only one (such as hour or minute). |
| 75 | + * Defaults to ['year', 'month', 'week', 'day', 'hour', 'minute', 'second']. |
| 76 | + * @param unitNamePlural thenames of the time units if there are several (such as hours or minutes). |
| 77 | + * Defaults to ['years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds']. |
| 78 | + * @param distributeOverflow if 'true', the unit overflows are merge into the next higher units. Defaults to 'false'. |
| 79 | + */ |
| 80 | +iso8601.Period.parseToString = function (period, unitNames, unitNamesPlural, distributeOverflow) { |
| 81 | + |
| 82 | + var result = ['', '', '', '', '', '', '']; |
| 83 | + var durationPerUnit = parsePeriodString(period, distributeOverflow); |
| 84 | + |
| 85 | + // input validation (use english as default) |
| 86 | + if (!unitNames) unitNames = ['year', 'month', 'week', 'day', 'hour', 'minute', 'second']; |
| 87 | + if (!unitNamesPlural) unitNamesPlural = ['years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds']; |
| 88 | + |
| 89 | + // assemble string per unit |
| 90 | + for (var i = 0; i < durationPerUnit.length; i++) { |
| 91 | + if (durationPerUnit[i] > 0) { |
| 92 | + if (durationPerUnit[i] == 1) result[i] = durationPerUnit[i] + " " + unitNames[i]; |
| 93 | + else result[i] = durationPerUnit[i] + " " + unitNamesPlural[i]; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // trim because of space at very end and because of join(" ") |
| 98 | + // replace double spaces because of join(" ") and empty strings |
| 99 | + // Its actually possible to get more than 2 spaces in a row, |
| 100 | + // so lets get 2+ spaces and remove them |
| 101 | + return result.join(' ').trim().replace(/[ ]{2,}/g, ' '); |
| 102 | +}; |
| 103 | + |
| 104 | +//---- private methods |
| 105 | + |
| 106 | +/** |
| 107 | + * Parses a ISO8601 period string. |
| 108 | + * @param period iso8601 period string |
| 109 | + * @param _distributeOverflow if 'true', the unit overflows are merge into the next higher units. |
| 110 | + */ |
| 111 | +function parsePeriodString(period, _distributeOverflow) { |
| 112 | + |
| 113 | + // regex splits as follows |
| 114 | + // grp0 omitted as it is equal to the sample |
| 115 | + // |
| 116 | + // | sample | grp1 | grp2 | grp3 | grp4 | grp5 | grp6 | grp7 | grp8 | grp9 | |
| 117 | + // -------------------------------------------------------------------------------------------- |
| 118 | + // | P1Y2M3W | 1Y2M3W | 1Y | 2M | 3W | 4D | T12H30M17S | 12H | 30M | 17S | |
| 119 | + // | P3Y6M4DT12H30M17S | 3Y6M4D | 3Y | 6M | | 4D | T12H30M17S | 12H | 30M | 17S | |
| 120 | + // | P1M | 1M | | 1M | | | | | | | |
| 121 | + // | PT1M | 3Y6M4D | | | | | T1M | | 1M | | |
| 122 | + // -------------------------------------------------------------------------------------------- |
| 123 | + |
| 124 | + var distributeOverflow = (_distributeOverflow) ? _distributeOverflow : false; |
| 125 | + var valueIndexes = [2, 3, 4, 5, 7, 8, 9]; |
| 126 | + var duration = [0, 0, 0, 0, 0, 0, 0]; |
| 127 | + var overflowLimits = [0, 12, 4, 7, 24, 60, 60]; |
| 128 | + var struct; |
| 129 | + |
| 130 | + // upcase the string just in case people don't follow the letter of the law |
| 131 | + period = period.toUpperCase(); |
| 132 | + |
| 133 | + // input validation |
| 134 | + if (!period) return duration; |
| 135 | + else if (typeof period !== "string") throw new Error("Invalid iso8601 period string '" + period + "'"); |
| 136 | + |
| 137 | + // parse the string |
| 138 | + if (struct = /^P((\d+Y)?(\d+M)?(\d+W)?(\d+D)?)?(T(\d+H)?(\d+M)?(\d+S)?)?$/.exec(period)) { |
| 139 | + |
| 140 | + // remove letters, replace by 0 if not defined |
| 141 | + for (var i = 0; i < valueIndexes.length; i++) { |
| 142 | + var structIndex = valueIndexes[i]; |
| 143 | + duration[i] = struct[structIndex] ? +struct[structIndex].replace(/[A-Za-z]+/g, '') : 0; |
| 144 | + } |
| 145 | + } |
| 146 | + else { |
| 147 | + throw new Error("String '" + period + "' is not a valid ISO8601 period."); |
| 148 | + } |
| 149 | + |
| 150 | + if (distributeOverflow) { |
| 151 | + // note: stop at 1 to ignore overflow of years |
| 152 | + for (var i = duration.length - 1; i > 0; i--) { |
| 153 | + if (duration[i] >= overflowLimits[i]) { |
| 154 | + duration[i - 1] = duration[i - 1] + Math.floor(duration[i] / overflowLimits[i]); |
| 155 | + duration[i] = duration[i] % overflowLimits[i]; |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return duration; |
| 161 | +}; |
0 commit comments