Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
feat(length): Add module length
Browse files Browse the repository at this point in the history
Returns the length of an array or arity of a function
  • Loading branch information
adambrgmn committed Jan 16, 2018
1 parent 8ce40aa commit 812aba5
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/__tests__/length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import length from '../length';

test('Core.length', () => {
expect(length([])).toBe(0);
expect(length([1, 2, 3])).toBe(3);
expect(length((a, b) => a + b)).toBe(2);
});
6 changes: 4 additions & 2 deletions src/curry.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import length from './length';

export default fn => {
const totalArgs = fn.length;
const totalArgs = length(fn);

const curried = (...partialArgs) => (...args) => {
if (partialArgs.length + args.length >= totalArgs)
if (length(partialArgs) + length(args) >= totalArgs)
return fn(...partialArgs, ...args);

return curried(...partialArgs, ...args);
Expand Down
4 changes: 3 additions & 1 deletion src/findIndex.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import length from './length';

export default (predicateFn, arr) => {
let i = 0;
const len = arr.length;
const len = length(arr);

while (i < len) {
const val = arr[i];
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import isObject from './isObject';
import isString from './isString';
import join from './join';
import last from './last';
import length from './length';
import map from './map';
import nth from './nth';
import pipe from './pipe';
Expand Down Expand Up @@ -46,6 +47,7 @@ export {
isString,
join,
last,
length,
map,
nth,
pipe,
Expand Down
1 change: 1 addition & 0 deletions src/length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default arr => arr.length;
4 changes: 3 additions & 1 deletion src/nth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import length from './length';

export default (n, arr) => {
const index = n < 0 ? arr.length + n : n;
const index = n < 0 ? length(arr) + n : n;
return typeof arr === 'string' ? arr.charAt(index) : arr[index];
};
4 changes: 3 additions & 1 deletion src/reduce.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import length from './length';

export default (fn, init, arr) => {
let l = init;
for (let i = 0; i < arr.length; i++) l = fn(l, arr[i]);
for (let i = 0; i < length(arr); i++) l = fn(l, arr[i]);
return l;
};
4 changes: 3 additions & 1 deletion src/reduceRight.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import length from './length';

export default (fn, init, arr) => {
let l = init;
for (let i = arr.length - 1; i >= 0; i--) l = fn(l, arr[i]);
for (let i = length(arr) - 1; i >= 0; i--) l = fn(l, arr[i]);
return l;
};

0 comments on commit 812aba5

Please sign in to comment.