-
Notifications
You must be signed in to change notification settings - Fork 26.2k
feat(common): make date/number formatting functions public #22372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
You can preview aee9a42 at https://pr22372-aee9a42.ngbuilds.io/. |
aee9a42
to
c4e518f
Compare
You can preview c4e518f at https://pr22372-c4e518f.ngbuilds.io/. |
@@ -40,25 +43,26 @@ enum TranslationType { | |||
/** | |||
* Transforms a date to a locale string based on a pattern and a timezone | |||
* | |||
* @internal | |||
* @stable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs more doc !
@@ -607,3 +613,66 @@ function convertTimezoneToLocal(date: Date, timezone: string, reverse: boolean): | |||
const timezoneOffset = timezoneToOffset(timezone, dateTimezoneOffset); | |||
return addDateMinutes(date, reverseValue * (timezoneOffset - dateTimezoneOffset)); | |||
} | |||
|
|||
/** | |||
* Transforms a value into a date |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment here do no say anything more than toDate()
.
Pls improve
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
refine type also
return new Date(parseFloat(value)); | ||
} else if (typeof value === 'string' && /^(\d{4}-\d{1,2}-\d{1,2})$/.test(value)) { | ||
/** | ||
* For ISO Strings without time the day, month and year must be extracted from the ISO String |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move to API doc of the function ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's there to explain the method used to convert the ISO string to a date, but it has no value for people who use the function (also keep in mind that the function is internal only for now).
return date; | ||
} | ||
|
||
export function isoStringToDate(match: RegExpMatchArray): Date { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a comment here pls
@@ -140,7 +140,7 @@ function formatNumber( | |||
/** | |||
* Formats a currency to a locale string | |||
* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add docs
@@ -162,12 +162,12 @@ export function formatCurrency( | |||
/** | |||
* Formats a percentage to a locale string | |||
* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add docs
@@ -120,6 +116,9 @@ export const ISO8601_DATE_REGEX = | |||
* pipe needs to re-run (this is to avoid reformatting the date on every change detection run | |||
* which would be an expensive operation). | |||
* | |||
* If you want to use this pipe outside of a template, you can use the function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no you don't want to use the pipe outside of a template. Remove ?
@@ -31,6 +31,9 @@ import {invalidPipeArgumentError} from './invalid_pipe_argument_error'; | |||
* For more information on the acceptable range for each of these numbers and other | |||
* details see your native internationalization library. | |||
* | |||
* If you want to use this pipe outside of a template, you can use the function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment as other non-pipes
@@ -47,28 +48,44 @@ import localeAr from '@angular/common/locales/ar'; | |||
}); | |||
|
|||
describe('supports', () => { | |||
it('should support date', () => { expect(() => pipe.transform(date)).not.toThrow(); }); | |||
it('should support date', () => { | |||
expect(isDate(toDate(date))).toBeTruthy(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the intent here ?
Does this belong to date_pipe_spec.ts
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please
- Add or expand the API docs. Especially important for API docs. Internal functions should also have a short comment,
- refine date type from "any" to a|b|c,
- I was expected to see more tests moved to the new format spec. It doesn't make sense any more to have them in the pipes if pipes only forward to those functions (only a sanity check is ok for the pipes).
d4d8baa
to
a3fd774
Compare
You can preview 0fe525a at https://pr22372-0fe525a.ngbuilds.io/. |
You can preview d4d8baa at https://pr22372-d4d8baa.ngbuilds.io/. |
You can preview a3fd774 at https://pr22372-a3fd774.ngbuilds.io/. |
* | ||
* @internal | ||
* Where: | ||
* - `value` is a date object or a number (milliseconds since UTC epoch) or an ISO string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please update the type accordingly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"is a Date
or"
* (https://www.w3.org/TR/NOTE-datetime). | ||
* - `format` indicates which date/time components to include. See {@link DatePipe} for a detailed | ||
* description. | ||
* - `locale` is a `string` defining the locale to use (uses the current {@link LOCALE_ID} by |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no default
* default). | ||
* - `timezone` to be used for formatting. It understands UTC/GMT and the continental US time zone | ||
* abbreviations, but for general use, use a time zone offset, for example, | ||
* `'+0430'` (4 hours, 30 minutes east of the Greenwich meridian) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove "(4 hours, 30 minutes east of the Greenwich meridian)" - no added value and technically wrong GMT vs UTC
@@ -607,3 +628,90 @@ function convertTimezoneToLocal(date: Date, timezone: string, reverse: boolean): | |||
const timezoneOffset = timezoneToOffset(timezone, dateTimezoneOffset); | |||
return addDateMinutes(date, reverseValue * (timezoneOffset - dateTimezoneOffset)); | |||
} | |||
|
|||
/** | |||
* Converts a value to date |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing final .
} | ||
|
||
/** | ||
* Convert a date in ISO8601 to a Date. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Converts
|
||
/** | ||
* Convert a date in ISO8601 to a Date. | ||
* Used instead of Date.parse because of browser discrepancies. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quote Date.parse
* Use `currency` to format a number as currency. | ||
* | ||
* Where: | ||
* - `value` is a number or numeric string. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong way ! -> number
68fe313
to
da8586b
Compare
* Where: | ||
* - `value` is a number or numeric string. | ||
* - `locale` is a `string` defining the locale to use. | ||
* - `digitInfo` is a `string` which has a following format: <br> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add to all pipes or no pipe (other have {@link ...}
)
You can preview da8586b at https://pr22372-da8586b.ngbuilds.io/. |
* (https://www.w3.org/TR/NOTE-datetime). | ||
* - `format` indicates which date/time components to include. See {@link DatePipe} for more | ||
* details. | ||
* - `locale` is a `string` defining the locale to use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing final .
let strNum = String(num); | ||
while (strNum.length < digits) { | ||
strNum = '0' + strNum; | ||
} | ||
if (trim) { | ||
strNum = strNum.substr(strNum.length - digits); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strNum.length - digits
isn't always equal to 0
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not always, we can have additional zeros
* Converts a value to date | ||
* | ||
* Supported input formats: | ||
* - `Date`s |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Date
You can preview 6d02858 at https://pr22372-6d02858.ngbuilds.io/. |
…es & dates The utility functions `formatNumber`, `formatPercent`, `formatCurrency`, and `formatDate` used by the number, percent, currency and date pipes are now available for developers who want to use them outside of templates. Fixes angular#20536
6d02858
to
dc77b4e
Compare
You can preview dc77b4e at https://pr22372-dc77b4e.ngbuilds.io/. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the rest looks good. thanks
/** | ||
* Transforms a number to a locale string based on a style and a format | ||
*/ | ||
function formatNumber( | ||
value: number | string, pattern: ParsedNumberFormat, locale: string, groupSymbol: NumberSymbol, | ||
function formatNbToLocaleString( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please avoid using custom abbreviations in non-local variables. formatNbToLocaleString -> formatNumberToLocaleString
anyone not familiar with our codebase that sees "Nb" will have to question what it means. This makes our code base harder to read and change by people not familiar with every change we make - e.g. our community, but also team members working on other parts of the code base that come across this code.
You can preview 8ce1f81 at https://pr22372-8ce1f81.ngbuilds.io/. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
PR Type
What kind of change does this PR introduce?
What is the current behavior?
If you want to use the number, percent, currency and date pipes outside of your templates, you need to instantiate those pipes and manually transform the values
Issue Number: #20536
What is the new behavior?
The utility functions
formatNumber
,formatPercent
,formatCurrency
, andformatDate
used by the number, percent, currency and date pipes are now available for developers who want to use them outside of templates.Does this PR introduce a breaking change?