Skip to content

Commit

Permalink
feat: add isIndexed (#1337)
Browse files Browse the repository at this point in the history
Closes #1236
  • Loading branch information
damiangreen committed Mar 12, 2020
1 parent 0ff6ad8 commit c4447db
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.js
Expand Up @@ -81,6 +81,7 @@ export { default as isSet } from './isSet';
export { default as isSparseArray } from './isSparseArray';
export { default as isSymbol } from './isSymbol';
export { default as isSafeInteger } from './isSafeInteger';
export { default as isIndexed } from './isIndexed';
// Function
export { default as stubUndefined } from './stubUndefined';
export { default as stubNull } from './stubNull';
Expand Down
25 changes: 25 additions & 0 deletions src/isIndexed.js
@@ -0,0 +1,25 @@
import { either } from 'ramda';
import curry1 from 'ramda/src/internal/_curry1';

import isArray from './isArray';
import isString from './isString';

/**
* Determine if input value is an indexed data type.
*
* @func isIndexed
* @memberOf RA
* @since {@link https://char0n.github.io/ramda-adjunct/2.26.0|v2.26.0}
* @category Type
* @sig * -> Boolean
* @param {*} val The value to test
* @return {boolean}
* @example
*
* RA.isIndexed([1]) //=> true
* RA.isIndexed('test') //=> true
*/

const isIndexed = curry1(either(isString, isArray));

export default isIndexed;
37 changes: 37 additions & 0 deletions test/isIndexed.js
@@ -0,0 +1,37 @@
import { assert } from 'chai';
import * as R from 'ramda';

import * as RA from '../src';

describe('isIndexed', function() {
context('given a `String` value', function() {
specify('should return true', function() {
assert.isTrue(RA.isIndexed('test'));
assert.isTrue(RA.isIndexed(Object('test')));
assert.isTrue(RA.isIndexed(''));
});
});

context('given array value', function() {
specify('should return true', function() {
assert.isTrue(RA.isIndexed([]));
assert.isTrue(RA.isIndexed([1, 2]));
});
});

context('given a non `String` or `Array` value', function() {
specify('should return false', function() {
assert.isFalse(RA.isIndexed(true));
assert.isFalse(RA.isIndexed(new Date()));
assert.isFalse(RA.isIndexed(new Error()));
assert.isFalse(RA.isIndexed({ 0: 1, length: 1 }));
assert.isFalse(RA.isIndexed(1));
});
});

it('should support placeholder to specify "gaps"', function() {
const isIndexed = RA.isIndexed(R.__);

assert.isTrue(isIndexed([]));
});
});
5 changes: 5 additions & 0 deletions types/index.d.ts
Expand Up @@ -1350,6 +1350,11 @@ declare namespace RamdaAdjunct {
*/
sortByProps(props: string[], list: object[]): object[];
sortByProps(props: string[]): (list: object[]) => object[];

/**
* Determine if input value is an indexed data type.
*/
isIndexed(val: any): val is string | any[];
}
}

Expand Down
18 changes: 18 additions & 0 deletions types/test/isIndexed.ts
@@ -0,0 +1,18 @@
import * as RA from 'ramda-adjunct';

RA.isIndexed(-0); // $ExpectType boolean
RA.isIndexed(+0); // $ExpectType boolean
RA.isIndexed(null); // $ExpectType boolean
RA.isIndexed(true); // $ExpectType boolean
RA.isIndexed(NaN); // $ExpectType boolean
RA.isIndexed({}); // $ExpectType boolean
RA.isIndexed([]); // $ExpectType boolean
RA.isIndexed(() => {}); // $ExpectType boolean
RA.isIndexed('string'); // $ExpectType boolean
RA.isIndexed(1); // $ExpectType boolean
RA.isIndexed(0.1); // $ExpectType boolean
RA.isIndexed(Object(0.1)); // $ExpectType boolean
RA.isIndexed(NaN); // $ExpectType boolean
RA.isIndexed(Infinity); // $ExpectType boolean
RA.isIndexed(Number.MAX_VALUE); // $ExpectType boolean
RA.isIndexed(Number.MIN_VALUE); // $ExpectType boolean

0 comments on commit c4447db

Please sign in to comment.