stdlib-js / stdlib-js/stdlib

[RFC]: support for non-standard specifiers for string formatting

Đang mở
#845 4 bình luận 0 reaction 0 người được giao Xem trên GitHub
Feature Needs Discussion RFC status: Blocked
Ngôn ngữ chính
JavaScript
Star
6k
Fork
1.3k
Merge trung bình
1 ngày 3 giờ
Pull request đã merge (30 ngày)
611

Mô tả

### Description

This RFC proposes adding support for non-standard specifiers for string formatting. Currently, `@stdlib/string/format` supports the standard `printf` specifiers as found in C. Would be convenient to be able to support additional serialization formats, such as

- complex numbers (`Complex128` and `Complex64` with precision and exponential notation formatting).
- JSON
- generic objects
- times
- percentages

Based on the current list of supported specifiers, the following could be potential extensions (although, we'd want to confirm that none of these are already present in common (non-standard) `printf` implementations):

- `Complex128`: `Z`
- `Complex64`: `z`
- `JSON`: `J`
- `object`: `O` (capital o)
- `times`: `D` or `T`
- `percentage`: `p`

The tricky extension would be times, as would be nice to support something like `YYYY-MM-DD HH:MM:SS`, but not clear how this would be done, as this extends far beyond the relative complexity of `printf` specifier modifiers. Perhaps better would be to have a dedicated time formatter similar to C's [`strptime`](https://pubs.opengroup.org/onlinepubs/009695399/functions/strptime.html) and [`strftime`](https://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html).

My recommendation is that support for non-standard specifiers be included as a package separate from `@stdlib/string/format` which, IMO, should remain a high fidelity analog of C's `printf`. Such a package can reuse `@stdlib/string/base/format-tokenize`, but implement/extend `@stdlib/string/base/format-interpolate`. The name of this extended package is TBD.

### Related Issues

None.

### Questions

No.

### Other

Prior art:

- [d3-format](https://github.com/d3/d3-format)
- [d3-time-format](https://github.com/d3/d3-time-format)
- [PEP3101](https://www.python.org/dev/peps/pep-3101/) and [Python's format mini-language](https://docs.python.org/3/library/string.html#format-specification-mini-language)

### Checklist

- [X] I have read and understood the [Code of Conduct](https://github.com/stdlib-js/stdlib/blob/develop/CODE_OF_CONDUCT.md).
- [X] Searched for existing issues and pull requests.
- [X] The issue name begins with `RFC:`.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.