rubydoobydoo v0.17.0
Rubydoobydoo!
Helping JS be more Ruby.
Unashamedly monkey patching JS numbers, strings, arrays and objects with Ruby methods.
Ruby has loads of really nice methods, now you can use them in JS as well!
Write code like:
[1,2,3].last // 3
[1,2,3].count // 3
(21).ordinalize // "21st"
"Rubydoobydoo".reverse //m "oodyboodybuR"
[1,2,3].sum.squared // 9
["A","A","C","A","B","A","B"].tally // {"A": 4, "C": 1, "B": 2}Usage
npm install rubydoobydooThen just add either require "rubydoobydoo" or import "rubydoobydoo" to the top of any JS file and suddenly coding in JS becomes a lot more ejoyable!
Array Methods
Property Methods
first
Returns the first element of the array.
[1, 2, 3].first; // 1
[].first; // undefinedsecond, third, fourth, fifth
Returns the second, third, fourth, or fifth element of the array.
[10, 20, 30].second; // 20
[10].third; // undefinedforty_two
Returns the 42nd element (index 41) of the array.
Array(50).fill(0).map((_, i) => i + 1).forty_two; // 42### third_to_last, second_to_last, last
Returns the third-to-last, second-to-last, or last element of the array.
[1, 2, 3, 4].second_to_last; // 3
[].last; // undefinedempty
Returns true if the array is empty, false otherwise.
[].empty; // true
[1].empty; // falseclear()
Clears all elements from the array.
let arr = [1, 2, 3];
arr.clear();
console.log(arr); // []size
Returns the length of the array.
[1, 2, 3].size; // 3min, max
Returns the smallest or largest number in the array.
[5, 3, 9].min; // 3
[5, 3, 9].max; // 9
[].min; // undefineduniq
Returns a new array with duplicate elements removed.
[1, 2, 2, 3].uniq; // [1, 2, 3]to_sentence
Converts the array into a human-readable sentence.
["a", "b", "c"].to_sentence; // "a, b and c"compact
Returns a new array with null and undefined values removed.
[1, null, 2, undefined, 3].compact; // [1, 2, 3]to_param
Converts the array into a string joined by /.
["users", 42, "edit"].to_param; // "users/42/edit"Functional Methods
any(func?)
Returns true if at least one element satisfies func, or if the array is not empty.
[1, 2, 3].any(x => x > 2); // true
[].any(); // falseone(func?)
Returns true if exactly one element satisfies func.
[1, 2, 3].one(x => x > 2); // true
[1, 2, 3, 4].one(x => x > 2); // falsesum(func?)
Returns the sum of all elements, or applies func before summing.
[1, 2, 3].sum(); // 6
[1, 2, 3].sum(x => x * 2); // 12reject(func)
Returns a new array without elements matching func.
1, 2, 3, 4.reject(x => x % 2 === 0); // 1, 3
partition(func)
Splits the array into two: one matching func, one not.
[1, 2, 3, 4].partition(x => x % 2 === 0); // [[2, 4], [1, 3]]count(func?)
Returns the number of elements satisfying func, or the total length.
[1, 2, 3, 4].count(x => x % 2 === 0); // 2
[1, 2, 3].count(); // 3pluck(prop)
Extracts values of the given property from an array of objects.
[{id: 1}, {id: 2}].pluck("id"); // [1, 2]from(n)
Returns a new array starting from index n.
[10, 20, 30, 40].from(2); // [30, 40]combination(n)
Returns all possible combinations of n elements.
[1, 2, 3].combination(2); // [[1,2], [1,3], [2,3]]tally()
Counts occurrences of each unique element.
["a", "b", "a"].tally(); // { a: 2, b: 1 }each_cons(n)
Returns overlapping subarrays of size n.
[1, 2, 3, 4].each_cons(2); // [[1,2], [2,3], [3,4]]rotate(n = 1)
Returns a rotated array by n places.
[1, 2, 3].rotate(); // [2, 3, 1]sample(n = 1)
Returns n random elements.
[1, 2, 3, 4].sample(2); // Random subsetzip(arr)
Zips two arrays together.
[1, 2, 3].zip(["a", "b", "c"]); // [[1, "a"], [2, "b"], [3, "c"]]union(...arrs)
Returns a merged array without duplicates.
[1, 2].union([2, 3], [3, 4]); // [1, 2, 3, 4]Aliases
collect → map
all → every
select → filter
each → forEach
detect → find
inject → reduce
delete_if → reject
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago