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 → "a" → "a"
charAt(1, "Hello world"); // → "e"
var thirdChar = charAt(2);
thirdChar("Hello world"); // → "l"
charCodeAt :: int → "a" → int
charCodeAt(1, "Hello world"); // → 101
concat :: a → a → a
concat("foo", "bar"); // → "barfoo"
concat("foo", [1, 2, 3] ); // → [1, 2, 3, "foo"]
concat([4, 5], [1, 2, 3] ); // → [1, 2, 3, 4, 5]
contains :: "a" → a → Boolean
contains("foo", "foobar"); // → true
contains("foo", [1, 2, "foo", 4] ); // → true
contains("foo", [1, 2, "bar", 4]; // → false
var hasBar = contains("bar");
hasBar("foobar"); // → true
endsWith :: "a" → a → Boolean
endsWith( 'world', 'hello world' ); // → true
endsWith('a', [1, 2, 3]); // → false
endsWith('a', [1, 2, 3, 'a'] ); // → true
exec :: RegExp → "a" → [a]
exec( /(hello \S+)/, 'This is a hello world!')[1]; // → "hello world!"
hasOwnProperty ::
// TODO - Have a look in the mdn documentation just above
indexOf :: a → int
indexOf( "o", "hello world!" ); // → 4 indexOf( 2, [1, 2, 3] ); // → 1
isPrototypeOf ::
// TODO - Have a look in the mdn documentation just above
join :: [a] → "a"
Currently the only function that takes an array but not a string.
join(" - ", [1, 2, 3]); // → "1 - 2 - 3"
var joinWithDash = join(" - ");
joinWithDash([1, 2, 3]); // → "1 - 2 - 3"
lastIndexOf :: a → a → int
lastIndexOf( "o", "hello world!" ); // → 7 lastIndexOf( 2, [1, 2, 3, 2, 4, 1, 0, 2, 4] ); // → 7
length :: a → int
Returns the length of a string or array
length( [1, 2, 3] ); // → 3 length( "hello" ); // → 5
localCompare ::
// TODO - Have a look in the mdn documentation just above
match ::
// TODO - Have a look in the mdn documentation just above
pop :: a → a
var a = [1, 2, 3];
pop(a); // → 3
a; // → [1, 2, 3] (a is unaltered)
// Works for strings too
pop("hello"); // → "o"
pop_ :: a → a
var a = [1, 2, 3];
pop_(a); // → [1, 2]
a; // → [1, 2, 3] (a is unaltered)
pop_("hello"); // → "hell"
push :: a → a → a
var a = [1, 2, 3];
push('z', a); // → [1, 2, 3, 'z']
a; // → [1, 2, 3] (a is unaltered)
push("oo", "hello"); // → "hellooo"
propertyIsEnumerable ::
// TODO - Have a look in the mdn documentation just above
replace ::
// TODO - Have a look in the mdn documentation just above
reverse :: a → a
reverse("hello"); // → "olleh"
var a = [1, 2, 3]
reverse(a); // → [3, 2, 1]
a; // → [1, 2, 3] (a is unaltered)
search ::
// TODO - Have a look in the mdn documentation just above
shift :: a → a
var a = [1, 2, 3];
shift(a); // → 1
a; // → [1, 2, 3] (a is unaltered)
shift("hello"); // → "h"
shift_ :: a → a
var a = [1, 2, 3];
shift_(a); // → [2, 3]
a; // → [1, 2, 3] (a is unaltered)
shift_("hello"); // → "ello"
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] slice( 1, 10, "hello world" ); // → "ello worl"
sort :: a → a
sort("hello"); // → "ehllo"
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!"
split ::
// TODO - Have a look in the mdn documentation just above
startsWith ::
// TODO - Have a look in the mdn documentation just above
substr ::
// TODO - Have a look in the mdn documentation just above
substring ::
// 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
toLocaleLowerCase ::
// TODO - Have a look in the mdn documentation just above
toLocaleUpperCase ::
// 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
toUpperCase ::
// TODO - Have a look in the mdn documentation just above
trim ::
// TODO - Have a look in the mdn documentation just above
trimLeft ::
// TODO - Have a look in the mdn documentation just above
trimRight ::
// 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