From d175192586b82284df5d45769af4792eefe4cf37 Mon Sep 17 00:00:00 2001 From: David Chambers Date: Sun, 10 Apr 2016 22:46:14 -0700 Subject: [PATCH] string: add S.trim --- index.js | 14 ++++++++++++++ test/index.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/index.js b/index.js index e2d88d00..6a74cac0 100644 --- a/index.js +++ b/index.js @@ -2919,6 +2919,20 @@ [$.String, $.String], function(s) { return s.toLowerCase(); }); + //# trim :: String -> String + //. + //. Strips leading and trailing whitespace characters. + //. + //. ```javascript + //. > S.trim('\t\t foo bar \n') + //. 'foo bar' + //. ``` + S.trim = + def('trim', + {}, + [$.String, $.String], + R.trim); + //# words :: String -> [String] //. //. Takes a string and returns the list of words the string contains diff --git a/test/index.js b/test/index.js index fb19ee48..5927decd 100644 --- a/test/index.js +++ b/test/index.js @@ -5553,6 +5553,39 @@ describe('string', function() { }); + describe('trim', function() { + + it('is a unary function', function() { + eq(typeof S.trim, 'function'); + eq(S.trim.length, 1); + }); + + it('type checks its arguments', function() { + throws(function() { S.trim(/XXX/); }, + errorEq(TypeError, + 'Invalid value\n' + + '\n' + + 'trim :: String -> String\n' + + ' ^^^^^^\n' + + ' 1\n' + + '\n' + + '1) /XXX/ :: RegExp\n' + + '\n' + + 'The value at position 1 is not a member of ‘String’.\n')); + }); + + it('strips leading and trailing whitespace characters', function() { + eq(S.trim(''), ''); + eq(S.trim(' '), ''); + eq(S.trim('x'), 'x'); + eq(S.trim(' x'), 'x'); + eq(S.trim('x '), 'x'); + eq(S.trim(' x '), 'x'); + eq(S.trim('\n\r\t x \n\r\t x \n\r\t'), 'x \n\r\t x'); + }); + + }); + describe('words', function() { it('is a unary function', function() {