Skip to content

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

Closed
wants to merge 2 commits into from

Conversation

ocombe
Copy link
Contributor

@ocombe ocombe commented Feb 22, 2018

PR Type

What kind of change does this PR introduce?

[x] Feature

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, and formatDate 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?

[x] No

@ocombe ocombe added feature Issue that requests a new feature action: review The PR is still awaiting reviews from at least one requested reviewer area: common Issues related to APIs in the @angular/common package target: major This PR is targeted for the next major release labels Feb 22, 2018
@ocombe ocombe added this to the v6-candidates milestone Feb 22, 2018
@ocombe ocombe requested a review from vicb February 22, 2018 16:07
@mary-poppins
Copy link

You can preview aee9a42 at https://pr22372-aee9a42.ngbuilds.io/.

@ocombe ocombe force-pushed the feat/#20536-export-pipe-fcts branch from aee9a42 to c4e518f Compare February 22, 2018 16:31
@mary-poppins
Copy link

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
Copy link
Contributor

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
Copy link
Contributor

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

Copy link
Contributor

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
Copy link
Contributor

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 ?

Copy link
Contributor Author

@ocombe ocombe Feb 23, 2018

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 {
Copy link
Contributor

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
*
Copy link
Contributor

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
*
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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();
Copy link
Contributor

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 ?

Copy link
Contributor

@vicb vicb left a 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).

@vicb vicb changed the title feat(common): export pipe utility functions feat(common): make date/number formatting functions public Feb 22, 2018
@ocombe ocombe force-pushed the feat/#20536-export-pipe-fcts branch 2 times, most recently from d4d8baa to a3fd774 Compare February 23, 2018 17:17
@mary-poppins
Copy link

You can preview 0fe525a at https://pr22372-0fe525a.ngbuilds.io/.

@mary-poppins
Copy link

You can preview d4d8baa at https://pr22372-d4d8baa.ngbuilds.io/.

@mary-poppins
Copy link

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
Copy link
Contributor

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

Copy link
Contributor

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
Copy link
Contributor

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)
Copy link
Contributor

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
Copy link
Contributor

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.
Copy link
Contributor

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.
Copy link
Contributor

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong way ! -> number

@ocombe ocombe force-pushed the feat/#20536-export-pipe-fcts branch from 68fe313 to da8586b Compare February 23, 2018 19:13
* 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>
Copy link
Contributor

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 ...})

@mary-poppins
Copy 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
Copy link
Contributor

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);
Copy link
Contributor

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?

Copy link
Contributor Author

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Date

@mary-poppins
Copy link

You can preview 6d02858 at https://pr22372-6d02858.ngbuilds.io/.

@vicb vicb added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Feb 23, 2018
@ngbot
Copy link

ngbot bot commented Feb 23, 2018

I see that you just added the PR action: merge label, but the following checks are still failing:
    failure status "ci/circleci: build" is failing
    failure status "ci/circleci: lint" is failing
    pending status "code-review/pullapprove" is pending
    pending status "google3" is pending
    pending status "continuous-integration/travis-ci/pr" is pending
    pending 1 pending code review

If you want your PR to be merged, it has to pass all the CI checks.

If you can't get the PR to a green state due to flakes or broken master, please try rebasing to master and/or restarting the CI job. If that fails and you believe that the issue is not due to your change, please contact the caretaker and ask for help.

…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
@ocombe ocombe force-pushed the feat/#20536-export-pipe-fcts branch from 6d02858 to dc77b4e Compare February 23, 2018 21:29
@mary-poppins
Copy link

You can preview dc77b4e at https://pr22372-dc77b4e.ngbuilds.io/.

Copy link
Contributor

@IgorMinar IgorMinar left a 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(
Copy link
Contributor

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.

@IgorMinar IgorMinar added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews and removed action: merge The PR is ready for merge by the caretaker labels Feb 23, 2018
@vicb vicb added action: merge The PR is ready for merge by the caretaker and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Feb 23, 2018
@ngbot
Copy link

ngbot bot commented Feb 23, 2018

I see that you just added the PR action: merge label, but the following checks are still failing:
    failure status "ci/circleci: lint" is failing
    pending status "google3" is pending
    pending status "continuous-integration/travis-ci/pr" is pending
    pending 1 pending code review

If you want your PR to be merged, it has to pass all the CI checks.

If you can't get the PR to a green state due to flakes or broken master, please try rebasing to master and/or restarting the CI job. If that fails and you believe that the issue is not due to your change, please contact the caretaker and ask for help.

@vicb
Copy link
Contributor

vicb commented Feb 23, 2018

presubmit

@mary-poppins
Copy link

You can preview 8ce1f81 at https://pr22372-8ce1f81.ngbuilds.io/.

@vicb vicb closed this Feb 23, 2018
@ocombe ocombe deleted the feat/#20536-export-pipe-fcts branch February 24, 2018 08:18
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: common Issues related to APIs in the @angular/common package cla: yes feature Issue that requests a new feature target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants