first commit

This commit is contained in:
2026-09-08 20:28:54 +08:00
commit 2ada8d3d5a
37380 changed files with 4886169 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
/* eslint-disable no-fallthrough */
/** Represent a date as a short/default/long/full string
*
* The input value needs to be in a form that the
* {@link https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date Date object}
* can process using its single-argument form, `new Date(value)`.
*
* @memberof Formatters
* @param {number|string} value - Either a Unix epoch time in milliseconds, or a string value representing a date
* @param {string} [type='default'] - One of `'short'`, `'default'`, `'long'` , or `full`
*
* @example
* var mf = new MessageFormat(['en', 'fi']);
*
* mf.compile('Today is {T, date}')({ T: Date.now() })
* // 'Today is Feb 21, 2016'
*
* mf.compile('Tänään on {T, date}', 'fi')({ T: Date.now() })
* // 'Tänään on 21. helmikuuta 2016'
*
* mf.compile('Unix time started on {T, date, full}')({ T: 0 })
* // 'Unix time started on Thursday, January 1, 1970'
*
* var cf = mf.compile('{sys} became operational on {d0, date, short}');
* cf({ sys: 'HAL 9000', d0: '12 January 1999' })
* // 'HAL 9000 became operational on 1/12/1999'
*/
function date(v, lc, p) {
var o = { day: 'numeric', month: 'short', year: 'numeric' };
switch (p) {
case 'full':
o.weekday = 'long';
case 'long':
o.month = 'long';
break;
case 'short':
o.month = 'numeric';
}
return new Date(v).toLocaleDateString(lc, o);
}
module.exports = function() {
return date;
};
+56
View File
@@ -0,0 +1,56 @@
/**
* Represent a duration in seconds as a string
*
* Input should be a finite number; output will include one or two `:`
* separators, and match the pattern `hhhh:mm:ss`, possibly with a leading `-`
* for negative values and a trailing `.sss` part for non-integer input
*
* @memberof Formatters
* @param {number|string} value - A finite number, or its string representation
*
* @example
* var mf = new MessageFormat();
*
* mf.compile('It has been {D, duration}')({ D: 123 })
* // 'It has been 2:03'
*
* mf.compile('Countdown: {D, duration}')({ D: -151200.42 })
* // 'Countdown: -42:00:00.420'
*/
function duration(value) {
if (!isFinite(value)) return String(value);
var sign = '';
if (value < 0) {
sign = '-';
value = Math.abs(value);
} else {
value = Number(value);
}
var sec = value % 60;
var parts = [Math.round(sec) === sec ? sec : sec.toFixed(3)];
if (value < 60) {
parts.unshift(0); // at least one : is required
} else {
value = Math.round((value - parts[0]) / 60);
parts.unshift(value % 60); // minutes
if (value >= 60) {
value = Math.round((value - parts[0]) / 60);
parts.unshift(value); // hours
}
}
var first = parts.shift();
return (
sign +
first +
':' +
parts
.map(function(n) {
return n < 10 ? '0' + String(n) : String(n);
})
.join(':')
);
}
module.exports = function() {
return duration;
};
+52
View File
@@ -0,0 +1,52 @@
/* global CURRENCY, Intl */
/** Represent a number as an integer, percent or currency value
*
* Available in MessageFormat strings as `{VAR, number, integer|percent|currency}`.
* Internally, calls Intl.NumberFormat with appropriate parameters. `currency` will
* default to USD; to change, set `MessageFormat#currency` to the appropriate
* three-letter currency code, or use the `currency:EUR` form of the argument.
*
* @memberof Formatters
* @param {number} value - The value to operate on
* @param {string} type - One of `'integer'`, `'percent'` , `'currency'`, or `/currency:[A-Z]{3}/`
*
* @example
* var mf = new MessageFormat('en');
* mf.currency = 'EUR'; // needs to be set before first compile() call
*
* mf.compile('{N} is almost {N, number, integer}')({ N: 3.14 })
* // '3.14 is almost 3'
*
* mf.compile('{P, number, percent} complete')({ P: 0.99 })
* // '99% complete'
*
* mf.compile('The total is {V, number, currency}.')({ V: 5.5 })
* // 'The total is €5.50.'
*
* mf.compile('The total is {V, number, currency:GBP}.')({ V: 5.5 })
* // 'The total is £5.50.'
*/
function number(value, lc, arg) {
var a = (arg && arg.split(':')) || [];
var opt = {
integer: { maximumFractionDigits: 0 },
percent: { style: 'percent' },
currency: {
style: 'currency',
currency: (a[1] && a[1].trim()) || CURRENCY,
minimumFractionDigits: 2,
maximumFractionDigits: 2
}
};
return new Intl.NumberFormat(lc, opt[a[0]] || {}).format(value);
}
module.exports = function(mf) {
var parts = number
.toString()
.replace('CURRENCY', JSON.stringify(mf.currency || 'USD'))
.match(/\(([^)]*)\)[^{]*{([\s\S]*)}/);
return new Function(parts[1], parts[2]);
};
+39
View File
@@ -0,0 +1,39 @@
/** Represent a time as a short/default/long string
*
* The input value needs to be in a form that the
* {@link https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date Date object}
* can process using its single-argument form, `new Date(value)`.
*
* @memberof Formatters
* @param {number|string} value - Either a Unix epoch time in milliseconds, or a string value representing a date
* @param {string} [type='default'] - One of `'short'`, `'default'`, `'long'` , or `full`
*
* @example
* var mf = new MessageFormat(['en', 'fi']);
*
* mf.compile('The time is now {T, time}')({ T: Date.now() })
* // 'The time is now 11:26:35 PM'
*
* mf.compile('Kello on nyt {T, time}', 'fi')({ T: Date.now() })
* // 'Kello on nyt 23.26.35'
*
* var cf = mf.compile('The Eagle landed at {T, time, full} on {T, date, full}');
* cf({ T: '1969-07-20 20:17:40 UTC' })
* // 'The Eagle landed at 10:17:40 PM GMT+2 on Sunday, July 20, 1969'
*/
function time(v, lc, p) {
var o = { second: 'numeric', minute: 'numeric', hour: 'numeric' };
switch (p) {
case 'full':
case 'long':
o.timeZoneName = 'short';
break;
case 'short':
delete o.second;
}
return new Date(v).toLocaleTimeString(lc, o);
}
module.exports = function() {
return time;
};