September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
let s = " \t String with spaces \t ";
|
||||
// a future version of ECMAScript will have trimStart(). Some current
|
||||
// implementations have trimLeft().
|
||||
console.log("original: '" + s + "'");
|
||||
console.log("trimmed left: '" + s.replace(/^\s+/,'') + "'");
|
||||
// a future version of ECMAScript will have trimEnd(). Some current
|
||||
// implementations have trimRight().
|
||||
console.log("trimmed right: '" + s.replace(/\s+$/,'') + "'");
|
||||
console.log("trimmed both: '" + s.trim() + "'");
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// stripStart :: Text -> Text
|
||||
let stripStart = s => dropWhile(isSpace, s);
|
||||
|
||||
// stripEnd :: Text -> Text
|
||||
let stripEnd = s => dropWhileEnd(isSpace, s);
|
||||
|
||||
// strip :: Text -> Text
|
||||
let strip = s => dropAround(isSpace, s);
|
||||
// OR: let strip = s => s.trim();
|
||||
|
||||
|
||||
|
||||
// GENERIC FUNCTIONS
|
||||
|
||||
// dropAround :: (Char -> Bool) -> Text -> Text
|
||||
let dropAround = (p, s) => dropWhile(p, dropWhileEnd(p, s));
|
||||
|
||||
// dropWhile :: (a -> Bool) -> [a] -> [a]
|
||||
let dropWhile = (p, xs) => {
|
||||
for (var i = 0, lng = xs.length;
|
||||
(i < lng) && p(xs[i]); i++) {}
|
||||
return xs.slice(i);
|
||||
}
|
||||
|
||||
// dropWhileEnd :: (Char -> Bool) -> Text -> Text
|
||||
let dropWhileEnd = (p, s) => {
|
||||
for (var i = s.length; i-- && p(s[i]);) {}
|
||||
return s.slice(0, i + 1);
|
||||
}
|
||||
|
||||
// isSpace :: Char -> Bool
|
||||
let isSpace = c => /\s/.test(c);
|
||||
|
||||
// show :: a -> String
|
||||
let show = x => JSON.stringify(x, null, 2);
|
||||
|
||||
|
||||
// TEST
|
||||
|
||||
let strText = " \t\t \n \r Much Ado About Nothing \t \n \r ";
|
||||
|
||||
return show([stripStart, stripEnd, strip]
|
||||
.map(f => '-->' + f(strText) + '<--'));
|
||||
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue