-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ref #86
- Loading branch information
Showing
5 changed files
with
177 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { has } from 'ramda'; | ||
|
||
import isArray from './isArray'; | ||
import isString from './isString'; | ||
|
||
|
||
/* eslint-disable max-len */ | ||
/** | ||
* Tests whether or not an object is similar to an array. | ||
* | ||
* @func isArrayLike | ||
* @memberOf RA | ||
* @since {@link https://char0n.github.io/ramda-adjunct/1.9.0|v1.9.0} | ||
* @licence https://github.com/ramda/ramda/blob/master/LICENSE.txt | ||
* @category List | ||
* @category Type | ||
* @sig * -> Boolean | ||
* @param {*} val The value to test | ||
* @returns {Boolean} `true` if `val` has a numeric length property and extreme indices defined; `false` otherwise. | ||
* @example | ||
* | ||
* RA.isArrayLike([]); //=> true | ||
* RA.isArrayLike(true); //=> false | ||
* RA.isArrayLike({}); //=> false | ||
* RA.isArrayLike({length: 10}); //=> false | ||
* RA.isArrayLike({0: 'zero', 9: 'nine', length: 10}); //=> true | ||
*/ | ||
/* eslint-enable max-lent */ | ||
const isArrayLike = (val) => { | ||
if (isArray(val)) { return true } | ||
if (!val) { return false } | ||
if (typeof val !== 'object') { return false } | ||
if (isString(val)) { return false } | ||
if (val.nodeType === 1) { return !!val.length } | ||
if (val.length === 0) { return true } | ||
if (val.length > 0) { | ||
return has(0, val) && has(val.length - 1, val); | ||
} | ||
return false; | ||
}; | ||
|
||
export default isArrayLike; | ||
|
||
/** | ||
The MIT License (MIT) | ||
Copyright (c) 2013-2016 Scott Sauyet and Michael Hurley | ||
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. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import RA from '../src/index'; | ||
import eq from './shared/eq'; | ||
|
||
|
||
describe('isArrayLike', function() { | ||
it('is 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() { | ||
function test() { | ||
return RA.isArrayLike(arguments); | ||
} | ||
eq(test(), true); | ||
eq(test(1, 2, 3), true); | ||
eq(test(null), true); | ||
}); | ||
|
||
it('is 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 = { 0: 'zero', length: 2 }; | ||
const obj6 = { 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), false); | ||
eq(RA.isArrayLike(obj6), false); | ||
}); | ||
|
||
it('is 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 | ||
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. | ||
*/ |