-
Notifications
You must be signed in to change notification settings - Fork 943
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
What about filesize? #94
Comments
Although it's a very specific problem I think it fits in nicely. P.S. Give me a PR :) |
https://github.com/omar/ByteSize may have some helpful bits you could
|
Thanks. That's a pretty extensive lib and I really like how it works. The API is very easy to use but also inconsistent with the way Humanizer works! Maybe Humanizer should provide some non-extension API too! |
It can be made extension API like this (2.0).FromTeraBytes(FileSizeUnit.MB) => 2097152 MB you must tell what is input and what is output public static string FromTeraBytes(this double value, FileSizeUnit target)
{
// TODO
} This is enum as an example public enum FileSizeUnit
{
B,
KB,
MB,
GB,
TB,
PB,
EB,
ZB,
YB
} Or a general way to convert byte to the best size public static string BytesToString(this long byteCount)
{
string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; //Longs run out around EB
if (byteCount == 0)
return "0" + suf[0];
long bytes = Math.Abs(byteCount);
int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
double num = Math.Round(bytes / Math.Pow(1024, place), 1);
return (Math.Sign(byteCount) * num).ToString() + suf[place];
} |
What about
That gives an API that looks like this: |
Thanks for the input guys. Reading your great API suggestions made me think of a third variation: var byteSize = (2.0).Terabytes(); // which returns an instance of a class called, hmmmm, perhaps ByteSize Obviously there will be many extensions like this that know how to instantiate the ByteSize class. WRT the ByteSize class itself I think it could more or less look like the ByteSize library!! So basically you could write your code as What do you think? |
Oh or should I say Omar and George's code?! @GeorgeHahn - just noticed you're a contributor on ByteSize. It's so much easier to steal the code now that we have an insider ;) |
Ha, don't give me too much credit - all I did was write the parser; the API is all Omar's 😀 |
I like that API! Would it be possible to take a dependency on the ByteSize lib? It would have to be PCL first, but I've already submitted a PR to solve that. |
Hi folks,
My 2c... |
@GeorgeHahn maybe arguable, but for a small lib like ByteSize I prefer to port or even reimplement than to depend. Also Humanizer has relatively aggressive release cycles and I don't want to have to depend on another library to turn something over quickly. Just a personal quirk I guess; but it's worked well for me in other frameworks too. So I'd have to say no to nuget dependency. In BDDfy I took dependency on RazorEngine in the beginning and that came back and bite me bad - so we implemented our own tiny templating engine inside BDDfy, and I couldn't be happier with the decision and effort as that has been extended a lot since. @DoCode thanks a lot for sending the code. Making this a byte count extension we're kinda locked with byte input while the other suggested APIs allow us to take any scale in and also output it in the desired scale which is more liberating, and I'm sure a few days after releasing this someone is going to ask for support for other size scales (e.g. terabyte input and gigabyte output). We could obviously later extend it with taking a second optional enum like that suggested by @faisalr. It's just that I am quite sold on the ByteSize API, and when there's already such extensive and well designed lib out there, I think providing a limited API in Humanizer won't do justice for the users. |
I agree that Humanizer should never have any dependencies except for Xunit for unit testing. I wish we can make Humanizer trend in Github again like what happened in January 2014. |
@MehdiK |
@faisalr nitpicking: Humanizer doesn't depend on xUnit. It's only used in tests and internally; i.e. when you TBH I'm not quite sure how and why that happened in Jan: it might have been that it was blogged about on C9 MSDN, donno; but man I'd love to see it trend again. @DoCode the proposed APIs do that too: |
@MehdiK |
Needs: - [] Rest of tests - [] Dehumanize byte quantities - [] Refactor namespaces all around - [] Add to readme - [] Licensing
Needs work, but what does everyone think of 7e1bdc6? Here's what needs to be done:
API is |
Hey guys, Just wanted to say that with a great effort from @GeorgeHahn this has now been released to NuGet as v1.13.2. Special thanks goes to @omar for his awesome ByteSize library. Hopefully this will invite Omar to bring more of his awesomeness to Humanizer :) |
=> input as bytes(long) => output as readable string(eg. 1.2 MB)
The text was updated successfully, but these errors were encountered: