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
+22
View File
@@ -0,0 +1,22 @@
# Change Log
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [2.0.1](https://github.com/messageformat/messageformat/compare/messageformat-formatters@2.0.0...messageformat-formatters@2.0.1) (2019-07-17)
### Bug Fixes
* Update dependencies ([b4907b5](https://github.com/messageformat/messageformat/commit/b4907b5))
# 2.0.0 (2019-05-02)
### Features
* **formatters:** Split into its own package from messageformat ([ed36829](https://github.com/messageformat/messageformat/commit/ed36829))
+20
View File
@@ -0,0 +1,20 @@
Copyright 2012-2018 Alex Sexton, Eemeli Aro, and Contributors
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+35
View File
@@ -0,0 +1,35 @@
/**
* @classdesc
* Default number formatting functions in the style of ICU's
* {@link http://icu-project.org/apiref/icu4j/com/ibm/icu/text/MessageFormat.html simpleArg syntax}
* implemented using the
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl Intl}
* object defined by ECMA-402.
*
* In MessageFormat source, a formatter function is called with the syntax
* `{var, name, arg}`, where `var` is a variable, `name` is the formatter name
* (by default, either `date`, `duration`, `number` or `time`; `spellout` and
* `ordinal` are not supported by default), and `arg` is an optional string
* argument.
*
* In JavaScript, a formatter is a function called with three parameters:
* - The **`value`** of the variable; this can be of any user-defined type
* - The current **`locale`** code
* - The trimmed **`arg`** string value, or `null` if not set
*
* As formatter functions may be used in a precompiled context, they should not
* refer to any variables that are not defined by the function parameters or
* within the function body. To add your own formatter, either add it to the
* static `MessageFormat.formatters` object, or use
* {@link MessageFormat#addFormatters} to add it to a MessageFormat instance.
*
* @class Formatters
* @hideconstructor
*/
module.exports = {
date: require('./lib/date'),
duration: require('./lib/duration'),
number: require('./lib/number'),
time: require('./lib/time')
};
+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;
};
+24
View File
@@ -0,0 +1,24 @@
{
"name": "messageformat-formatters",
"version": "2.0.1",
"description": "Formatters for messageformat",
"keywords": [
"icu",
"messageformat",
"formatter"
],
"author": "Eemeli Aro <eemeli@gmail.com>",
"license": "MIT",
"homepage": "https://messageformat.github.io/",
"repository": {
"type": "git",
"url": "https://github.com/messageformat/messageformat.git",
"directory": "packages/formatters"
},
"main": "index.js",
"files": [
"index.js",
"lib/"
],
"gitHead": "cd47ed7db2b7a4f5e21df4cd6284ae3f7dad05a1"
}