andrewplummer/Sugar — pull request #127 by nadinengland (2012-04-02 16:31:07+00:00)
Title: String#shorten & Array#punctuate

String#truncate didn't provide the functionality I needed, as such String#shorten has been added. The use case for the method is primarily URLs, where the beginning and end are more important than the middle. As such shorten defaults to truncating the string in the middle and including the length of the splitter in the length required.

```JavaScript
"https://www.google.co.uk/search?sourceid=chrome&ie=UTF-8&q=sugar+js".shorten(45)
-> "https://www.google.co...e&ie=UTF-8&q=sugar+js"
```

Performs similar to String#truncate in that custom splitters can be included, as well as allowing the resultant string's length to omit the length of the splitter. The splitter can also be positioned at the left, right or middle of the string.

Array#punctuate builds a grammatical list from the array, taking a fn handler to deal with deep arrays. I've been working with long calling patterns for train services and this addition to Sugar help me deal with the headache. The trailing only on single length lists can be omitted too.

```JavaScript
['Apples', 'Grapes', 'Bears'].punctuate()	-> 'Apples, Grapes and Bears';
[{type:'Apple', n:2}, {type:'Grape', n:1}, {type:'Bears', n:10}].punctuate(function(item) {
var name = (item.n == 1) ? item.type.singularize() : item.type.pluralize();
return item.n.toString() + " " + name;
})	-> '2 Apples, 1 Grape and 10 Bears';
```

Extensive unit testing added for both methods. Tested in Node.js v0.6.14 and Safari 5.1.5 (Snow Leopard). Unable to use unit_test/../sugar.html in Chrome 18.x as XMLHttpRequest won't load local files because of Access-Control-Allow-Origin. Unsure if this is a know issue. The actual unit tests were ran independently and succeeded in Chrome.