Skip to content

Commit

Permalink
Exports validation (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
jalal246 committed May 16, 2020
1 parent d10800e commit c708b77
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ const result = moveMultiIndex(input, FromToObj);
// result = ["a", "c", "a"];
```

Validation function is also exported:

`isValid(array, { from, to })` and it returns `false` when:

- input is not array
- input array is empty
- from/to is out of range
- from/to is not numbers

```js
isValid([1, 2, 3], { from: 0, to: 1 });

// true

isValid([1, 2, 3], { from: 10, to: 1 });

// false
```

## Tests

```sh
Expand Down
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
* @param {number} to - targeted index
* @returns
*/
function isNotInRange(arr, from, to) {
function isNotInRange(arr, { from, to } = {}) {
const { length } = arr;

return (
!Array.isArray(arr) ||
typeof from !== "number" ||
typeof to !== "number" ||
from < 0 ||
Expand All @@ -19,6 +20,10 @@ function isNotInRange(arr, from, to) {
);
}

function isValid(arr, form2Obj) {
return !isNotInRange.call(this, arr, form2Obj);
}

/**
* Moves element form/to index.
*
Expand All @@ -29,7 +34,7 @@ function isNotInRange(arr, from, to) {
* @returns {Array}
*/
function move(arr = [], from, to, isMutate = true) {
if (isNotInRange(arr, from, to)) return arr;
if (isNotInRange(arr, { from, to })) return arr;

const modified = isMutate ? arr : arr.slice();

Expand All @@ -48,7 +53,7 @@ function move(arr = [], from, to, isMutate = true) {
* @returns {Array}
*/
function moveMultiArr(multiArr, from, to, isMutate) {
return multiArr.map(arr => move(arr, from, to, isMutate));
return multiArr.map((arr) => move(arr, from, to, isMutate));
}

/**
Expand All @@ -69,7 +74,8 @@ function moveMultiIndex(arr = [], movingMap) {
}

module.exports = {
isValid,
move,
moveMultiArr,
moveMultiIndex
moveMultiIndex,
};
42 changes: 41 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// eslint-disable-next-line import/no-extraneous-dependencies
const { expect } = require("chai");

const { move, moveMultiArr, moveMultiIndex } = require("./index");
const { move, moveMultiArr, moveMultiIndex, isValid } = require("./index");

describe("move-position", () => {
it("returns the same input when params is invalid", () => {
Expand Down Expand Up @@ -92,3 +92,43 @@ describe("move-position", () => {
expect(result).to.have.deep.members(expected);
});
});

describe("tests validation", () => {
describe("detects invalid returns false:", () => {
it("when not array", () => {
const result = isValid("foo");
expect(result).to.be.equal(false);
});

it("when array is empty", () => {
const result = isValid([]);
expect(result).to.be.equal(false);
});

it("when from/to is out of range", () => {
let result = isValid([1, 2, 3], { from: -4, to: 0 });
expect(result).to.be.equal(false);

result = isValid([1, 2, 3], { from: 4, to: 0 });
expect(result).to.be.equal(false);

result = isValid([1, 2, 3], { from: 1, to: 6 });
expect(result).to.be.equal(false);

result = isValid([1, 2, 3], { from: 1, to: -6 });
expect(result).to.be.equal(false);
});

it("when from/to is not numbers", () => {
const result = isValid([1, 2, 3], { from: "1", to: "0" });
expect(result).to.be.equal(false);
});
});

describe("detects valid returns true:", () => {
it("when valid array", () => {
const result = isValid([1, 2, 3], { from: 0, to: 1 });
expect(result).to.be.equal(true);
});
});
});

0 comments on commit c708b77

Please sign in to comment.