add missing predecessor function

closes #352
pull/353/head
Christoph Hermann 9 years ago
parent a84332cba6
commit baeeb549d4

@ -258,7 +258,19 @@ endsWith('image.old.gif', 'old', 9)
// => true
```
#### succ(str) => string
#### pred(string) => string
Returns the predecessor to str.
```javascript
pred('b')
// => 'a'
pred('B')
// => 'A'
```
#### succ(string) => string
Returns the successor to str.

@ -0,0 +1,6 @@
var makeString = require('./makeString');
module.exports = function adjacent(str, direction) {
str = makeString(str);
return str.slice(0, -1) + String.fromCharCode(str.charCodeAt(str.length - 1) + direction);
};

@ -36,6 +36,7 @@ s.lines = require('./lines');
s.reverse = require('./reverse');
s.startsWith = require('./startsWith');
s.endsWith = require('./endsWith');
s.pred = require('./pred');
s.succ = require('./succ');
s.titleize = require('./titleize');
s.camelize = require('./camelize');

@ -0,0 +1,5 @@
var adjacent = require('./helper/adjacent');
module.exports = function succ(str) {
return adjacent(str, -1);
};

@ -1,6 +1,5 @@
var makeString = require('./helper/makeString');
var adjacent = require('./helper/adjacent');
module.exports = function succ(str) {
str = makeString(str);
return str.slice(0, -1) + String.fromCharCode(str.charCodeAt(str.length - 1) + 1);
return adjacent(str, 1);
};

@ -335,6 +335,13 @@ $(document).ready(function() {
equal(_.splice(12345, 1, 2, 321), '132145', 'Non strings');
});
test('String: pred', function(){
equal(_('b').pred(), 'a');
equal(_('B').pred(), 'A');
equal(_(',').pred(), '+');
equal(_(2).pred(), '1');
});
test('String: succ', function(){
equal(_('a').succ(), 'b');
equal(_('A').succ(), 'B');

Loading…
Cancel
Save