Skip to content

Commit

Permalink
feat(isArrayLike): add support for currying
Browse files Browse the repository at this point in the history
Also make tests consistent with the rest of the codebase
  • Loading branch information
char0n committed Dec 21, 2018
1 parent 4fd6a1d commit a3e843c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 45 deletions.
5 changes: 3 additions & 2 deletions src/isArrayLike.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { has } from 'ramda';
import curry1 from 'ramda/src/internal/_curry1';

import isArray from './isArray';
import isString from './isString';
Expand Down Expand Up @@ -27,7 +28,7 @@ import isString from './isString';
* RA.isArrayLike({0: 'zero', 9: 'nine', length: 10}); //=> true
*/
/* eslint-enable max-len */
const isArrayLike = val => {
const isArrayLike = curry1(val => {
if (isArray(val)) {
return true;
}
Expand All @@ -50,7 +51,7 @@ const isArrayLike = val => {
return has(0, val) && has(val.length - 1, val);
}
return false;
};
});

export default isArrayLike;

Expand Down
75 changes: 32 additions & 43 deletions test/isArrayLike.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import * as R from 'ramda';

import * as RA from '../src/index';
import eq from './shared/eq';

describe('isArrayLike', function() {
it('is true for Arrays', function() {
it('should return true for Arrays', function() {
eq(RA.isArrayLike([]), true);
eq(RA.isArrayLike([1, 2, 3, 4]), true);
eq(RA.isArrayLike([null]), true);
});

it('is true for arguments', function() {
it('should return true for arguments', function() {
function testingFn() {
return RA.isArrayLike(arguments);
}
Expand All @@ -17,58 +19,45 @@ describe('isArrayLike', function() {
eq(testingFn(null), true);
});

it('is false for Strings', function() {
it('should return false for Strings', function() {
eq(RA.isArrayLike(''), false);
eq(RA.isArrayLike('abcdefg'), false);
});

it('is true for arbitrary objects with numeric length, if extreme indices are defined', function() {
const obj1 = { length: 0 };
const obj2 = { 0: 'something', length: 0 };
const obj3 = { 0: void 0, length: 0 };
const obj4 = { 0: 'zero', 1: 'one', length: 2 };
const obj5 = { nodeType: 1, length: 2 };
const obj6 = { 0: 'zero', length: 2 };
const obj7 = { 1: 'one', length: 2 };

eq(RA.isArrayLike(obj1), true);
eq(RA.isArrayLike(obj2), true);
eq(RA.isArrayLike(obj3), true);
eq(RA.isArrayLike(obj4), true);
eq(RA.isArrayLike(obj5), true);
eq(RA.isArrayLike(obj6), false);
eq(RA.isArrayLike(obj7), false);
context('given extreme indices are defined', function() {
specify(
'should return true for arbitrary objects with numeric length',
function() {
const obj1 = { length: 0 };
const obj2 = { 0: 'something', length: 0 };
const obj3 = { 0: void 0, length: 0 };
const obj4 = { 0: 'zero', 1: 'one', length: 2 };
const obj5 = { nodeType: 1, length: 2 };
const obj6 = { 0: 'zero', length: 2 };
const obj7 = { 1: 'one', length: 2 };

eq(RA.isArrayLike(obj1), true);
eq(RA.isArrayLike(obj2), true);
eq(RA.isArrayLike(obj3), true);
eq(RA.isArrayLike(obj4), true);
eq(RA.isArrayLike(obj5), true);
eq(RA.isArrayLike(obj6), false);
eq(RA.isArrayLike(obj7), false);
}
);
});

it('is false for everything else', function() {
it('should return false for everything else', function() {
eq(RA.isArrayLike(undefined), false);
eq(RA.isArrayLike(1), false);
eq(RA.isArrayLike({}), false);
eq(RA.isArrayLike(false), false);
eq(RA.isArrayLike(function() {}), false);
});
});

/**
The MIT License (MIT)

Copyright (c) 2013-2016 Scott Sauyet and Michael Hurley
it('should support placeholder to specify "gaps"', function() {
const isArrayLike = RA.isArrayLike(R.__);

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
eq(isArrayLike([]), true);
});
});

0 comments on commit a3e843c

Please sign in to comment.