Skip to content
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

Added some number formatting capability #1

Closed
wants to merge 13 commits into from

Conversation

deleted
Copy link

@deleted deleted commented Apr 18, 2013

Hi, davidchambers.

I added some functionality to string-format that allows you to specify number sign, float precision, and field padding.

Your tests pass and I added a few of my own. Any interest in merging this?

It now has the regexp-fu to parse everything in the python string format mini-language, although it doesn't implement absolutely everything yet. I implemented what I needed for the project I was working on. Perhaps someone else or a future me will be motivated to finish the job.

If you do decide to merge, you will want to change the part in the readme about this being a fork.

Cheers,

Ted Scharff

@davidchambers
Copy link
Owner

Thanks for the pull request, Ted. This functionality will make a nice addition. :)

I'll review the changes now for style issues; I probably won't have time to review the pull request in depth until the weekend.

@@ -7,6 +7,16 @@ When `format` is invoked on a string, placeholders within the string are
replaced with values determined by the arguments provided. A placeholder
is a sequence of characters beginning with `{` and ending with `}`.

## About this Fork
dchamber's original version implemented nested variable interpolation, and
Copy link
Owner

Choose a reason for hiding this comment

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

Should be davidchambers's (according to the Chicago Manual of Style). :D

@deleted
Copy link
Author

deleted commented Apr 18, 2013

David,

Thank you for reviewing my work, and bearing with my on style issues as this is my first adventure in Coffeescript.

I've pushed two commits to my master that address your comments and make a few other changes for tidiness, consistency, and style. Tests still pass.

@davidchambers
Copy link
Owner

Thanks, Ted. I look forward to familiarizing myself with this "mini-language" and reviewing these changes in earnest.


it "correctly formats floats", ->
'{:0}'.format(1.2345).should.equal '1.2345'
'{:03.2f}'.format(1.2345).should.equal '001.23'
Copy link
Owner

Choose a reason for hiding this comment

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

Python (2.7.2) behaves differently:

>>> '{:03.2f}'.format(1.2345)
'1.23'

Copy link
Author

Choose a reason for hiding this comment

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

You are correct. It helps to actually test against the python behavior. Fixed and added some additional tests.

@davidchambers
Copy link
Owner

Were you able to find comprehensive string formatting tests in Python's source code? I had a look, but the test cases I found in various files are not comprehensive, even collectively.

More comprehensive tests based on actual Python behavior
@deleted
Copy link
Author

deleted commented Apr 22, 2013

The tests in cpython relevant to the string.format() method seem to live here:
https://github.com/jonashaag/cpython/blob/master/Lib/test/test_string.py

They don't appear to be comprehensive enough to cover the numerical format spec "mini-language".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants