Skip to content

andersontr15/ramda-lite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 

Repository files navigation

ramdaLite

Minimalist version of Ramda.js under 10kb

To include in project:

var ramdaLite = require("./src/ramdaLite");
var multiplyByTwo = function (v) {
  return v * 2;
};
var addTwo = function (v) {
  return v + 2;
};
var addThree = function (v) {
  return v + 3;
};
var composition = ramdaLite.compose(multiplyByTwo, addTwo, addThree);

composition(2)-- > 9;

API

  1. addTwo
  2. adjust
  3. all
  4. always
  5. and
  6. ap
  7. apply
  8. both
  9. binary
  10. bind
  11. complement
  12. compose
  13. contains
  14. defaultTo
  15. divide
  16. either
  17. empty
  18. false
  19. filter
  20. flip
  21. forEach
  22. gt
  23. gte
  24. has
  25. hasIn
  26. inc
  27. it
  28. juxt
  29. keys
  30. length
  31. last
  32. lte
  33. Maybe
  34. map

add-two

add two numbers together

var x = 5;
var y = 10;
ramdaLite.addTwo(x, y)-- > 15;

adjust

apply a function to a specific index in a list

var numbers = [4, 5, 6];
ramdaLite.adjust(5, 1, numbers)-- > [4, 10, 6];

all

a function that will return true if predicate is true for all values

var numbers = [4, 5, 6];
var isEven = function (val) {
  return val % 2 === 0;
};
ramdaLite.all(isEven, list)-- > false;

always

a function that always returns true

ramdaLite.always()-- > true;

and

a function that returns true if both arguments are true

ramdaLite.and(true, false) --> false

ap

a function that applies multiple methods to a list

var multiply = function (x) {
  return x * 2;
};
var add = function (x) {
  return x + 2;
};
ramdaLite.ap(multiply, add, [1, 2, 3]);

apply

a function that applies a given context to a function to be invoked

ramdaLite.apply(console.log, 5) -> 5 

both

a function that returns true if both functions passed to it with the argument are truthy

var isEven = function (value) {
  return value % 2 === 0;
};
var greaterThanTwenty = function (value) {
  return value > 20;
};
ramdaLite.both(isEven, greaterThanTwenty, 21);

binary

a function that returns a function which only utilizes two arguments

var threeArgs = function (a, b, c) {
  return [a, b, c];
};
var binaryArgsOnly = ramdaLite.binary(threeArgs);
binaryArgsOnly(1, 2, 3)-- > [1, 2, undefined];

bind

a function that binds the this context with the specified function and returns a bound function

var greet = function () {
  return "hello " + this.name;
};
var obj = {
  name: "theo",
};
var bound = ramdaLite.bind(greet, obj);
bound();

complement

a function that returns a function which returns true if argument is identical to first function

var complement = ramdaLite.complement(null);
complement(null) -> true
complement(undefined) -> false 

compose

a function that can take an infinite amount of functions and call them sequentially on a value

var addTwo = function (v) {
  return v + 2;
};
var multiplyByTwo = function (v) {
  return v * 2;
};
var composition = ramdaLite.compose(addTwo, multiplyByTwo);
composition(2);

contains

a function that returns true if value is in the supplied list

var numbers = [1,2,3,4];
var hasFour = ramdaLite.contains(4, numbers);
hasFour --> true 

defaultTo

divide

a function that divides two numbers

var x = 10;
var y = 5;
var quotient = ramdaLite.divide(x,y);
quotient --> 2

either

a function that returns true if value is truthy for one of two predicate functions

	var value = 2;
	var isOdd = function(v) {
		return v % 3 === 0
	}
	var isEven = function(v) {
		return v % 2 === 0
	}
	var either = ramdaLite.either(isEven, isOdd, value);
	either --> true 

empty

a function that returns the JavaScript class representation of the supplied type as an empty type

var numbers = [1,2,3,4];
var value = ramdaLite.empty(numbers);
value --> []

false

a function that always returns false

var f = ramdaLite.false();
f --> false; 

filter

a function that filters a list based of the the predicate function

	var numbers = [1,2,3,4];
	var filtered = ramdaLite.filter(numbers, function(v) {
		return v % 2 === 0
	});
	filtered --> [2,4]

flip

a function that reverses argument order of functions

	var multiply = function(x) {
		return x * 2
	}
	var add = function(x) {
		return x + 2
	}
	var flipped = ramdaLite.flipped(multiply, add);
	flipped(4) --> add --> multiply --> 12 

forEach

a function that will iterate through a list and call a function on each value in the list

	var list = [1,2,3,4];
	var log = function(v) {
		console.log('value is ' + v)
	}
	ramdaLite.forEach(log, list) -> 'The value is 1', 'The value is 2', 'The value is 3', 'The value is 4'

gt

a function that returns true if the first argument is greater than the second argument

	var x = 5;
	var y = 10;
	var result = ramdaLite.gt(x,y);
	result --> false;

gte

a function that returns true if the first argument is greater than or equal to the second argument

	var x = 10;
	var y = 10;
	var result = ramdaLite.gte(x,y);
	result --> true 

identity

a function that takes in a value and return the value as its self.

	var value = 10;
	var result = ramdaLite.identity(10);
	result --> 10;

ifElse

inc

a function that will take in a value and increment it by 1

  var result = ramdaLite.inc(5);
  result --> 6 

is

a function that will return true if first argument is an instance of the second argumet (its constructor)

	var obj = {
		name: 'theo'
	};
	var result = ramdaLite.is(obj, {}) --> true 
	var falsy = ramdaLite.is(obj, []) --> false 

it

a function that returns true if the first argument is less than the second argument

	var x = 5;
	var y = 10;
	ramdaLite.it(x,y) --> true

isEmpty

a function that will return true if the value is empty

	var value = [1,2,3];
	var value2 = [];
	ramdaLite.isEmpty(value) -> false;
	ramdaLite.isEmpty(value2) -> true;

juxt

a function which is invokes multiple functions on a list of values

	var double = function(x) {
		return x * x 
	}
	var addThree  = function(x) {
		return x + 3
	}
	var list = [1,2,3,4];
	var result = ramdaLite.juxt(double,triple,list);
	result --> [4,7,12,19]

keys

a function that creates a new array from an Object with just the keys

	var obj = {
		name: 'theo',
		height: 69,
		age: 23
	};
	ramdaLite.keys(obj) -> ['name', 'height', 'age']

length

a function that returns the length of a list

	var numbers = [1,2,3,4];
	ramdaLite.length(numbers) --> 4

last

a function that returns the last element in an Array

	var numbers = [1,2,3,4];
	ramdaLite.last(numbers) --> 4;

lte

a function that returns true if the first argument is less than or equal to the second argument

	var x = 5;
	var y = 10;
	ramdaLite.lte(5,10) --> true;

Maybe

a function that takes a value and wraps it an an Array container with either an error or the value

	var good = 5;
	var bad = null;
	ramdaLite.Maybe(good) --> [5]
	ramdaLite.Maybe(bad) --> ['error']

map

a function that takes a list and a function to apply to each value in the list. returns a new Array

	var numbers = [1,2,3,4];
	var double = function(v) {
		return v * 2
	}
	ramdaLite.map(numbers, double) --> [2,4,6,8];

max

a function that takes a list and returns the maximum value

	var numbers = [2,34,100,9];
	ramdaLite.max(numbers) --> 100

memoize

a function that returns a cached value of the passed in function if it has already been stored

	var multiplyByTwo = function(v) {
		return v * 2 
	}
	var memoize = ramdaLite.memoize(multiplyByTwo);
	memoize(multiplyByTwo(2)) -> returns function from cache 
	memoize(console.log(2)) -> adds method to cache and returns console.log(2)

min

a function that takes a list and returns the minimum value

	var numbers = [1,2,3,4];
	ramdaLite.min(numbers) --> 1 

multiply

negate

none

nth

nthArg

of

or

pair

prepend

product

range

reduce

reject

reverse

subtract

tail

take

takeLast

times

toUpper

toLower

toPairs

tryCatch

true

unique

values

without

zip