source: node_modules/ramda-adjunct/src/isArrayLike.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 2.4 KB
Line 
1import { has, curryN } from 'ramda';
2
3import isArray from './isArray';
4import isString from './isString';
5
6/* eslint-disable max-len */
7/**
8 * Tests whether or not an object is similar to an array.
9 *
10 * @func isArrayLike
11 * @memberOf RA
12 * @since {@link https://char0n.github.io/ramda-adjunct/1.9.0|v1.9.0}
13 * @licence https://github.com/ramda/ramda/blob/master/LICENSE.txt
14 * @category List
15 * @category Type
16 * @sig * -> Boolean
17 * @param {*} val The value to test
18 * @returns {boolean} `true` if `val` has a numeric length property and extreme indices defined; `false` otherwise.
19 * @see {@link RA.isNotArrayLike|isNotArrayLike}
20
21 * @example
22 *
23 * RA.isArrayLike([]); //=> true
24 * RA.isArrayLike(true); //=> false
25 * RA.isArrayLike({}); //=> false
26 * RA.isArrayLike({length: 10}); //=> false
27 * RA.isArrayLike({0: 'zero', 9: 'nine', length: 10}); //=> true
28 */
29/* eslint-enable max-len */
30const isArrayLike = curryN(1, (val) => {
31 if (isArray(val)) {
32 return true;
33 }
34 if (!val) {
35 return false;
36 }
37 if (isString(val)) {
38 return false;
39 }
40 if (typeof val !== 'object') {
41 return false;
42 }
43 if (val.nodeType === 1) {
44 return !!val.length;
45 }
46 if (val.length === 0) {
47 return true;
48 }
49 if (val.length > 0) {
50 return has(0, val) && has(val.length - 1, val);
51 }
52 return false;
53});
54
55export default isArrayLike;
56
57/**
58 The MIT License (MIT)
59
60 Copyright (c) 2013-2016 Scott Sauyet and Michael Hurley
61
62 Permission is hereby granted, free of charge, to any person obtaining a copy
63 of this software and associated documentation files (the "Software"), to deal
64 in the Software without restriction, including without limitation the rights
65 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
66 copies of the Software, and to permit persons to whom the Software is
67 furnished to do so, subject to the following conditions:
68
69 The above copyright notice and this permission notice shall be included in
70 all copies or substantial portions of the Software.
71
72 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
73 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
74 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
75 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
76 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
77 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
78 THE SOFTWARE.
79*/
Note: See TracBrowser for help on using the repository browser.