This library takes all the methods on instances of strings, arrays, objects, numbers, and regexp's and turns them into functions that all:
Example:
// Normal way: var a = [1, 2, 3]; a.push(4); // → 4 a; // → [1, 2, 3, 4] // lib1 way: var b = [1, 2, 3]; push(4, b); // → [1, 2, 3, 4] b; // → [1, 2, 3] (b is unaltered) // We can also curry pop() var c = [1, 2, 3]; var addFour = pop(4); addFour(c); // → [1, 2, 3, 4] // Functions that operate on arrays can (usually) also take strings // Just think of "hello" as ['h','e','l','l','o'] and it should make sense push(" world", "hello"); // → "hello world"
Together with a compose()
we can do nice things like:
// Currying some functions var fetchFirstSix = substr(0,6); var addName = push("Bill"); // concat() will work too // Combining them var greet = compose(addName, fetchFirstSix); greet("Hello world"); // → "Hello Bill"
charAt :: Int → String → String
charAt(1, "Hello world"); // → "e" var thirdChar = charAt(2); thirdChar("Hello world"); // → "l"
charCodeAt :: Int → String → Int
charCodeAt(1, "Hello world"); // → 101
concat :: String -> String... -> String
concat("foo", "bar"); // → "foobar"
var fooSomething = concat("foo");
fooSomething("bar", "baz"); // → "foobarbaz"
indexOf :: String → String → Int
indexOf( "o", "hello world!" ); // → 4
indexOf_ :: String → Int → String → Int
indexOf_( "welcome", 14, "Hello world, welcome to the welcomed universe."); // → 28
lastIndexOf :: String → String → Int
lastIndexOf( "o", "hello world!" ); // → 7
match :: Regexp|String → String → [String]
var str = "The rain in SPAIN stays mainly in the plain";
match(/ain/g, str); // → ['ain','ain','ain']
replace :: Regexp|String → String → String → String
var str = "Visit Microsoft!";
replace("Microsoft","W3Schools", str); // → 'Visit W3Schools!'
search :: Regexp|String → String → Int
var str="Visit W3Schools!";
search("W3Schools", str); / /6
slice :: Int → String → String
slice( 1, "hello world!" ); // → "ello world!"
slice_ :: Int → Int → String → String
slice_( 1, 10, "hello world!" ); // → "ello worl"
split :: String → String → [String]
var str = "How are you doing today?";
split(" ", str)); // → ['How','are','you','doing','today?']
split_ :: String → Int → String → [String]
var str = "How are you doing today?";
split_(" ", 2, str); // → ['How','are']
substring :: Int → String → String
var str = "Hello world!";
substring(3, str); // → 'lo world!'
substring_ :: Int → Int → String → String
var str = "Hello world!";
substring(3, 5, str); // → 'lo'
toLocaleLowerCase :: String → String
var str = "Hello World!";
toLocaleLowerCase(str); // → 'hello world!'
toLocaleUpperCase :: String → String
var str = "Hello World!";
toLocaleUpperCase(str); // → 'HELLO WORLD!'
toLowerCase :: String → String
var str = "Hello World!";
toLowerCase(str); // → 'hello world!'
toUpperCase :: String → String
var str = "Hello World!";
toUpperCase(str); // → 'HELLO WORLD!'
trim :: String → String
var str = " Hello World! ";
trim(str); // → 'Hello world!'
concat :: [a] → [a] → [a]
concat([1, 2], [3, 4, 5] ); // → [1, 2, 3, 4, 5] var oneTwo = concat([1, 2]); oneTwo([3, 4], [5]); // → [1, 2, 3, 4, 5]
every :: (a → Boolean) → [a] → Boolean
every(lte(10), [12, 5, 8, 130, 44]); // → false
every(gte(4), [12, 5, 8, 130, 44]); // → true
filter :: (a → Boolean) → [a] → [a]
var isEven = function(n) { return !(n % 2); }
filter(isEven, [1,2,3,4]); // → [2,4];
forEach :: (a -> _) -> [a] -> undefined
var x = 1;
var addToX = function(y){ x += y }
forEach(addToX, [2, 5]);
x; // → 8
indexOf :: a → [a] → Int
indexOf(2, [1, 2, 3]); // → 1
indexOf_ :: a → Int → [a] → Int
indexOf_( 2, 2, [1, 2, 3, 2] ); // → 3
join :: [a] → String
join(" - ", [1, 2, 3]); // → "1 - 2 - 3" var joinWithDash = join(" - "); joinWithDash([1, 2, 3]); // → "1 - 2 - 3"
lastIndexOf :: a → [a] → Int
lastIndexOf( 2, [1, 2, 3, 2, 4, 1, 0, 2, 4] ); // → 7
map :: (a → b) → [a] → [b]
map(add(1), [1,2,3,4]); // → [2, 3, 4, 5]
pop :: [a] → [a]
var a = [1, 2, 3]; pop(a); // → [1, 2] a; // → [1, 2, 3] (a is unaltered)
push :: a → [a] → [a]
var a = [1, 2, 3]; push('z', a); // → [1, 2, 3, 'z'] a; // → [1, 2, 3] (a is unaltered)
reduce :: (b → a → b) → b → [a] → b
var words = ['foo', 'bar', 'baz']; reduce(add, "", words) // → 'foobarbaz'
reduceRight :: (b → a → b) → b → [a] → b
var words = ['foo', 'bar', 'baz'];
reduceRight(add, '', words); // → 'bazbarfoo'
reverse :: a → a
var a = [1, 2, 3]; reverse(a); // → [3, 2, 1] a; // → [1, 2, 3] (a is unaltered)
shift :: a → a
var a = [1, 2, 3]; shift(a); // → [2, 3] a; // → [1, 2, 3] (a is unaltered)
slice :: Int → a → a
var a = [1, 2, 3]; slice(1, a); // → [2, 3]
slice_ :: Int → Int → a → a
var a = [1, 2, 3]; slice_(1, 3, a); // → [2, 3] var getFirstTwo = slice_(0, 2); getFirstTwo(a); // → [1, 2]
sort :: [a] → [a]
sort(["Delta", "alpha", "CHARLIE", "bravo"]); // → ["CHARLIE", "Delta", "alpha", "bravo"]
splice :: Int → Int → [a] → [a]
splice(2, 3, "Hello world!"); → "He world!" splice(2, 3, ["zero", "one", "two", "three", "four", "five"]); // → ["zero", "one", "five"]
splice_ :: Int → Int → a → [a] → [a]
splice_(2, 3, "hi", ["zero", "one", "two", "three", "four", "five"]); // → ["zero", "one", "hi", "five"] splice_(0, 5, "What a wonderful", "Hello world!") // → "What a wonderful world!"
propertyIsEnumerable ::
// TODO - Have a look in the mdn documentation just above
exec :: RegExp → String → [String]
exec( /(hello \S+)/, 'This is a hello world!')[1]; // → "hello world!"
hasOwnProperty ::
// TODO - Have a look in the mdn documentation just above
isPrototypeOf ::
// TODO - Have a look in the mdn documentation just above
test ::
// TODO - Have a look in the mdn documentation just above
toExponential ::
// TODO - Have a look in the mdn documentation just above
toFixed ::
// TODO - Have a look in the mdn documentation just above
toPrecision ::
// TODO - Have a look in the mdn documentation just above
toString ::
// TODO - Have a look in the mdn documentation just above
unshift ::
// TODO - Have a look in the mdn documentation just above
valueOf ::
// TODO - Have a look in the mdn documentation just above