Tag Info

Hot answers tagged

5

Objective-C: This should work on iOS 3.2, iOS 4.0 and Mac OS X 10.4 - 10.6 : I don't have multiple returns because I like a single exit point in my code. Hence I use my trusty friend returnable. - (NSString*) stringForReputationFormatted:(NSNumber*)reputation { NSString *returnable = nil; long rep = [reputation longValue]; NSString *returnable ...


5

Here you go, another JavaScript approach, originally posted on SO: function getRepString (rep) { rep = rep+''; // coerce to string if (rep < 1000) { // return the same number return rep; } if (rep < 10000) { // place a comma between return rep.charAt(0) + ',' + rep.substring(1); } // divide and format return ...


5

For all the jQuery developers, there is - as always - a decent plugin available: timeago... Bonus: comes with auto-refresh, supports a semantic approach and is kind of progressive enhancement-ish, since it will turn something like <abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr> into timestamps like about a year ago ...


4

c# Rounds up - same same SO. public string FormatReputation(int value) { var rep = System.Convert.ToDouble(value); if (rep < 10000) { return rep.ToString("N0"); } return (rep / 1000) .ToString(rep % 1000 == 0 ? "" : "F1") + 'k'; } 100 => "100" 1200 => "1,200" 9999 => "9,999" 10000 => "10k" 10200 => "10.2k" ...


4

Here's a Java implementation using NumberFormat. public static void main(String[] args) { int[] values = { 999, 1000, 9999, 10000, 10100 }; for( int rep : values ) { System.out.println( formatRep(rep) ); } } public static String formatRep(int rep) { if(rep < 1000) { return rep + ""; } if(rep < 10000) { ...


3

Code that does the same in StackWrap4J: List<User> allUsers = new UsersList(new StackWrapper("stackapps.com"), new UserQuery()); Collections.sort(allUsers, new Comparator<User>() { @Override public int compare(User o1, User o2) { return o2.getAnswerCount() - o1.getAnswerCount(); } }); for (User u : allUsers) { if ...


3

These are great feature ideas - thanks for the tips! I've more or less converted CMS' answer to Python: class FormattedReputation(int): def format(rep): """Formats the reputation score like it is formatted on the sites. Heavily based on CMS' JavaScript implementation at ...


3

Just for completeness, here is the C# version I am using ported from the VB version given by @rockinthesixstring: private string FormatReputation(int reputation) { string s = reputation.ToString(); if (reputation > 99999) return s.Remove(s.Length - 3) + "k"; else if (reputation > 9999) return ...


3

The slug seems to be limited to 81 characters. Here is a javascript version of slugify algorithm: function slugify(str) { var stripped = stripVowelAccent(str); return stripped.replace(/[^A-Za-z0-9 ]/g, "").replace(/[ ]+/g, "-").toLowerCase().substr(0,81); } /* (C)Stephen Chalmers * Strips grave, acute & circumflex accents from vowels * Adjusted ...


3

Shouldn't we drop the prefix 'Dev-Tip'? - Asap! Shouldn't we revert to the question/answer format? - Yes, i would like to see questions title like "How to.." Should we adopt the following tags from SO? Uhm, i would find cross-language useful Should we drop the 'dev-tip' tag? Nope, i like it. EDIT: what should i do with a question like this one ...


3

As I said, I like it. As usual, Steffen presents a well considered opinion and one that I agree with. The dev-tip is just something I started doing without much thought except 'hey, for those trying to do [insert common api related task], here is a way to do it'. Shouldn't we drop the prefix 'Dev-Tip'? - Yes Shouldn't we revert to the question/answer ...


3

the 'slug' is a cleaned title hyphen delimited and truncated to some length which is easily discoverable by measuring a few 'slugs' that are obviously truncated. and in reality, you can place any thing in that path position, or nothing at all if there are no following path positions by cleaned i mean that non ascii alpha numeric characters are removed and ...


3

Here's a quick and dirty way of how I did mine in .NET Visual Basic Public Shared Function GetReputation(ByVal input As Integer) As String Dim _input As String = input.ToString Select Case input Case Is > 99999 : Return _input.Remove(_input.Length - 3) & "k" Case Is > 9999 : Return ...


2

Here is a similar function in PHP: function Pluralize($amount, $unit) { if($amount == 1) return "$amount $unit ago"; else return "$amount {$unit}s ago"; } function RelativeTime($timestamp) { // Calculate the difference $current_time = time(); $difference = $current_time - $timestamp; if($difference == 0) ...


2

Here's a quick and dirty way of how I did mine in .NET Visual Basic ''# <summary> ''# Return how long in the past something happend ''# </summary> ''# <param name="input">The date of an event in UTC time</param> ''# <param name="append">append characters to the end of the string ''# example: ...


2

Here's my code from Py-StackExchange, in Python: def format_relative_date(date): """Takes a datetime object and returns the date formatted as a string e.g. "3 minutes ago", like the real site. This is based roughly on George Edison's code from StackApps: ...


2

C# and Javascript port of Bill's answer Works for me - adding it to both Soapi libraries. See the JS version in action here One thing to keep in mind is that dates coming from the API are UTC. In order to get a viable diff, you need to convert your Now to UTC. c# /// <summary> /// Formats the time elapsed between fromDate and now. /// ...


2

C# sliding window throttle implementation This is the throttle implementation used by Soapi.CS. It is compatible with all .net platforms (desktop/silverlight/phone/mono) It has proven to provide maximum practical API throughput while complying both with the letter of the law as well as compensating for incidental vagaries. I would go so far as to ...


2

I think we also need a consistent tagging style. I propose [dev-tip] + [support]. Of all the required tags, support is the best fit.


2

Thanks for wrapping that up (+1) - a couple of recent topics are missing though, so here are More Common Questions Why is there no stable sort order for /sites? See How to order /sites result in a user friendly way to use it in our apps. for comments, discussion and workarounds. Why is the site creation date not available? See Can we add a ...


2

Similar code for StackWrap4J: List<Users> allUsers = new UsersList( new StackWrapper("api.stackapps.com"), new UserQuery() ); You can then iterate through allUsers and see their lastAccessDate. The currently released version will plow through the allowed calls per second, but v1.0.1 will properly throttle the requests.


2

Here is my take on kevin's sort using Soapi.JS get a list of sites from stackauth /sites get the community user from each site to get creation date attach each meta to it's parent sort by date group by 'state' or type. render grouped or as a single list. Live Demo HERE p.s. - i will be tightening this code up and merging it into Soapi.JS for those that ...


2

For future reference: http://stackapps.com/questions/1/api-documentation-and-help To answer your question: To discover sites that are available in the Stack Exchange network, monitor http://stackauth.com/1.0/sites (http://stackauth.com/1.0/help/method?method=sites) { "api_sites": [ { "name": "Stack Overflow", "logo_url": ...


2

[thinking out loud] Well, the sites we want to pin should be a constant and, as you describe, are. I would have 3 arrays, head, body and tail. I would statically populate head and tail with the 'pinned' sites. I would sort the response from /sites, keyed by reversed api_endpoint in descending order e.g. moc.wolfrevokcats.ipa//:ptth ...


2

Good ole' PHP: // Any number larger than 1000 gets returned like '4.5k' function Numberify($num) { if($num > 1000) return (floor($num / 100) / 10) . 'k'; return $num } Do you like the name of the function? :)


2

Here's a Java implementation that was heavily influenced by the answers on the SO question How do I calculate relative time?, particularly @Jeff's and the accepted answer by @Vincent Robert. /** * Formats the time elapsed between fromDate and now. * See http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time/ * * @param fromDate the ...


2

C# / Soapi.CS using Soapi.CS Questions (from user in Context.Official.StackApps .Users.PageSize(100).PageCount(0) where user.QuestionCount > 0 orderby user.QuestionCount descending select user) .ToList().ForEach(user => Console.WriteLine("{0} - {1} [{2}]", user.QuestionCount, user.DisplayName, user.UserId)); Output: 95 - ...


2

C# / Soapi.CS using Soapi.CS // Context .Official .StackApps .Questions .PageCount(0) .PageSize(100) .GroupBy(question => question.OwnerId) .Select(group => new { User = group.First().Owner.DisplayName + "[" + group.First().Owner.UserId + "]", FavedCount = group.Sum(question => ...


2

JavaSript / Soapi.JS using Soapi.JS Soapi.RouteFactory("api.stackapps.com", apiKey) // get all users .Questions({ pagesize: 100 }).getPagedResponse(function(data) { var values = {}, keys = []; // add up the favorite for each user for (var i = 0; i < data.items.length; i++) { var q = data.items[i]; var u = q.owner; ...


2

JavaSript / Soapi.JS using Soapi.JS Questions Soapi.RouteFactory("api.stackapps.com", apiKey) // get all users .Users({ pagesize: 100 }).getPagedResponse(function(data) { var output = ""; data.items.sort(function(a, b) { return b.question_count - a.question_count }); for (var i = 0; i < data.items.length; i++) { output += ...



Only top voted, non community-wiki answers of a minimum length are eligible