|
| 1 | +/** |
| 2 | + * |
| 3 | + * Copyright 2019 Rightech IoT. All Rights Reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +const { startOfDay, timeOf, getLocalTime, getOffset } = require('./time'); |
| 19 | + |
| 20 | +const DEFAULT_FALLBACK = '-'; |
| 21 | +const DEFAULT_LOCALE = 'en'; |
| 22 | +const DEFAULT_24H_LOCALE = 'en-GB'; |
| 23 | + |
| 24 | + |
| 25 | +let fallback = DEFAULT_FALLBACK; |
| 26 | +let currentLocale = DEFAULT_LOCALE; |
| 27 | + |
| 28 | +const intlCache = {}; |
| 29 | + |
| 30 | +function getIntl(type, key, locale, options) { |
| 31 | + if (!locale) { |
| 32 | + locale = currentLocale; |
| 33 | + } |
| 34 | + key = `${type}.${key}.${locale}`; |
| 35 | + if (key in intlCache) { |
| 36 | + return intlCache[key]; |
| 37 | + } |
| 38 | + return intlCache[key] = options |
| 39 | + ? new Intl[type](locale, options) |
| 40 | + : new Intl[type](locale); |
| 41 | +} |
| 42 | + |
| 43 | +function setLocale(locale) { |
| 44 | + currentLocale = locale; |
| 45 | + return { locale, currentLocale }; |
| 46 | +} |
| 47 | + |
| 48 | +function date(value, locale) { |
| 49 | + if (typeof value === 'string') { |
| 50 | + value = +value; |
| 51 | + } |
| 52 | + if (!value || isNaN(value)) { |
| 53 | + return fallback; |
| 54 | + } |
| 55 | + return getIntl('DateTimeFormat', 'date', locale) |
| 56 | + .format(getLocalTime(value)); |
| 57 | +} |
| 58 | + |
| 59 | +function time(value, locale) { |
| 60 | + if (typeof value === 'string') { |
| 61 | + value = +value; |
| 62 | + } |
| 63 | + if (!value || isNaN(value)) { |
| 64 | + return fallback; |
| 65 | + } |
| 66 | + const options = { |
| 67 | + hour: 'numeric', |
| 68 | + minute: 'numeric', |
| 69 | + second: 'numeric' |
| 70 | + }; |
| 71 | + return getIntl('DateTimeFormat', 'time', locale, options) |
| 72 | + .format(getLocalTime(value)); |
| 73 | +} |
| 74 | + |
| 75 | +function dateTime(value, locale) { |
| 76 | + if (!value || isNaN(value)) { |
| 77 | + return fallback; |
| 78 | + } |
| 79 | + return `${date(value, locale)} ${time(value, locale)}`; |
| 80 | +} |
| 81 | + |
| 82 | +function dateOrTime(value, from, locale) { |
| 83 | + if (!value || isNaN(value)) { |
| 84 | + return fallback; |
| 85 | + } |
| 86 | + let local = getLocalTime(value || Date.now()); |
| 87 | + return local >= startOfDay(from) |
| 88 | + ? time(value, locale) |
| 89 | + : date(value, locale); |
| 90 | +} |
| 91 | + |
| 92 | +function timeSpan(date) { |
| 93 | + if (typeof date === 'undefined' || date === 0) { // eslint-disable-line no-magic-numbers |
| 94 | + return fallback; |
| 95 | + } |
| 96 | + let days = Math.floor(+date / timeOf(1).days); // eslint-disable-line no-magic-numbers |
| 97 | + days = days > 0 ? (`${days}:`) : ''; // eslint-disable-line no-magic-numbers |
| 98 | + date = (+date + getOffset() * 60 * 1000); |
| 99 | + return days + time(date, DEFAULT_24H_LOCALE); |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +function number(value, fixed) { |
| 104 | + if (isNaN(value) || !isFinite(value)) { |
| 105 | + return value || fallback; |
| 106 | + } |
| 107 | + if (typeof fixed === 'undefined') { |
| 108 | + fixed = 2; |
| 109 | + } |
| 110 | + if (value && value.toFixed) { |
| 111 | + let [tmp, exp] = value.toFixed(fixed).split('e+'); |
| 112 | + if (exp) { |
| 113 | + tmp = parseFloat(tmp).toFixed(fixed) |
| 114 | + } |
| 115 | + value = parseFloat(tmp); |
| 116 | + } |
| 117 | + return value; |
| 118 | +} |
| 119 | + |
| 120 | +function currency(value, currency, locale) { |
| 121 | + if (isNaN(value) || !isFinite(value)) { |
| 122 | + return value || fallback; |
| 123 | + } |
| 124 | + const style = currency ? { style: 'currency', currency } : {}; |
| 125 | + return new Intl.NumberFormat(locale, style).format(value); |
| 126 | +} |
| 127 | + |
| 128 | +/* |
| 129 | +function normalize(term, separator) { |
| 130 | + term = (term || '').toString(); |
| 131 | + separator = typeof separator === 'string' ? separator : '_'; |
| 132 | + let normalized = inflected.parameterize(term, { separator }) |
| 133 | + .replace(/-/g, separator); |
| 134 | +
|
| 135 | + normalized = inflected.humanize(normalized).trim(); |
| 136 | + normalized = inflected.parameterize(normalized, { separator }) |
| 137 | + .replace(/-/g, separator); |
| 138 | + return normalized; |
| 139 | +} |
| 140 | +*/ |
| 141 | + |
| 142 | +function percent(value, total, format) { |
| 143 | + format = format || ''; |
| 144 | + if (!total || !value) { |
| 145 | + return `${0} ${format}`; |
| 146 | + } |
| 147 | + let percent = (value * 100) / total; |
| 148 | + if (format) { |
| 149 | + percent = (+percent).toFixed(0); |
| 150 | + } |
| 151 | + return `${percent} ${format}`; |
| 152 | +} |
| 153 | + |
| 154 | +module.exports = { |
| 155 | + setLocale, |
| 156 | + |
| 157 | + date, |
| 158 | + time, |
| 159 | + dateTime, |
| 160 | + dateOrTime, |
| 161 | + timeSpan, |
| 162 | + number, |
| 163 | + currency, |
| 164 | + percent |
| 165 | +}; |
0 commit comments