Skip to content

Commit a14e62a

Browse files
authored
Merge pull request #18 from palashmon/feature/add-ci
2 parents bab73ce + 3e6393f commit a14e62a

File tree

6 files changed

+91
-33
lines changed

6 files changed

+91
-33
lines changed

.github/dependabot.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# To get started with Dependabot version updates, you'll need to specify which
2+
# package ecosystems to update and where the package manifests are located.
3+
# Please see the documentation for all configuration options:
4+
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
5+
6+
version: 2
7+
updates:
8+
- package-ecosystem: "npm" # See documentation for possible values
9+
directory: "/" # Location of package manifests
10+
schedule:
11+
interval: "weekly"

.github/workflows/main.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: CI
2+
on:
3+
- push
4+
- pull_request
5+
jobs:
6+
test:
7+
name: Node.js ${{ matrix.node-version }}
8+
runs-on: ubuntu-latest
9+
strategy:
10+
fail-fast: false
11+
matrix:
12+
node-version:
13+
- 18
14+
- 16
15+
- 14
16+
steps:
17+
- uses: actions/checkout@v3
18+
- uses: actions/setup-node@v3
19+
with:
20+
node-version: ${{ matrix.node-version }}
21+
- run: npm install
22+
- run: npm test

index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
module.exports = (arrayInput = []) => {
1+
function cloneArrayObjects(arrayInput = []) {
22
if (!Array.isArray(arrayInput)) {
33
throw new TypeError(`Expected an array, got ${typeof arrayInput}`);
44
}
5-
return arrayInput.map(obj => ({...obj}));
6-
};
5+
6+
return arrayInput.map(object => {
7+
if (typeof object !== 'object' || object === null) {
8+
throw new TypeError(`Expected an object, got ${typeof object}`);
9+
}
10+
11+
return {...object};
12+
});
13+
}
14+
15+
export default cloneArrayObjects;

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "clone-array-objects",
3-
"version": "1.0.0",
3+
"version": "1.0.2",
44
"description": "Clone an array of objects",
55
"license": "MIT",
66
"repository": "https://github.com/palashmon/clone-array-objects",
@@ -10,7 +10,7 @@
1010
"url": "https://github.com/palashmon"
1111
},
1212
"engines": {
13-
"node": ">=8"
13+
"node": ">=14"
1414
},
1515
"scripts": {
1616
"test": "xo && ava",
@@ -30,8 +30,9 @@
3030
"test"
3131
],
3232
"devDependencies": {
33-
"ava": "^3.0.0",
33+
"ava": "^5.1.0",
3434
"nyc": "^15.0.0",
35-
"xo": "^0.30.0"
36-
}
37-
}
35+
"xo": "^0.54.0"
36+
},
37+
"type": "module"
38+
}

readme.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
# clone-array-objects
1+
# clone-array-objects ![CI](https://github.com/palashmon/clone-array-objects/actions/workflows/main.yaml/badge.svg)
22

3-
[![Build Status](https://travis-ci.org/palashmon/clone-array-objects.svg?branch=master)](https://travis-ci.org/palashmon/clone-array-objects)
4-
[![npm](https://img.shields.io/npm/v/clone-array-objects.svg)](https://www.npmjs.org/package/clone-array-objects)
5-
[![codecov](https://codecov.io/gh/palashmon/clone-array-objects/branch/master/graph/badge.svg)](https://codecov.io/gh/palashmon/clone-array-objects)
6-
[![Gzip Size](https://img.badgesize.io/https://unpkg.com/clone-array-objects?compression=gzip)](https://bundlephobia.com/result?p=clone-array-objects)
7-
8-
Tiny module to clone an array of objects
3+
> Tiny module to clone an array of objects
94
105
## Install
116

test.js

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
import test from 'ava';
2-
import cloneArrayObjects from '.';
2+
import cloneArrayObjects from './index.js';
33

4-
test('Return typeerror when arrayInput is not an array.', t => {
5-
const err = t.throws(() => {
6-
cloneArrayObjects(23);
7-
}, TypeError);
8-
t.is(err.message, 'Expected an array, got number');
4+
test('should return a new array with cloned objects', t => {
5+
const input = [
6+
{name: 'John', age: 30},
7+
{name: 'Jane', age: 25},
8+
];
9+
const output = cloneArrayObjects(input);
10+
11+
t.not(output, input); // Should not modify the original array
12+
t.deepEqual(output, input); // Should have the same values as the input
13+
});
14+
15+
test('should throw a TypeError if the input is not an array', t => {
16+
const input = 'not an array';
17+
18+
const error = t.throws(() => cloneArrayObjects(input));
19+
20+
t.is(error.message, `Expected an array, got ${typeof input}`);
21+
});
22+
23+
test('should throw a TypeError if any object in the array is null', t => {
24+
const input = [{name: 'John', age: 30}, null];
25+
26+
const error = t.throws(() => cloneArrayObjects(input));
27+
28+
t.is(error.message, 'Expected an object, got object');
929
});
1030

11-
test('Return empty array when no valid input passed.', t => {
12-
t.deepEqual(cloneArrayObjects(), []);
13-
t.deepEqual(cloneArrayObjects([]), []);
31+
test('should throw a TypeError if any object in the array is not an object', t => {
32+
const input = [{name: 'John', age: 30}, 'not an object'];
33+
34+
const error = t.throws(() => cloneArrayObjects(input));
35+
36+
t.is(error.message, 'Expected an object, got string');
1437
});
1538

16-
test('Test clone', t => {
17-
const actual = [{a: 1}, {b: 2}];
18-
const expected = [{a: 1}, {b: 2}];
19-
const modified = [{a: 1}, {b: 3}];
20-
t.deepEqual(cloneArrayObjects(actual), expected);
21-
expected[1].b = 3;
22-
t.notDeepEqual(cloneArrayObjects(actual), expected);
23-
t.deepEqual(modified, expected);
39+
test('should return an empty array if the input is an empty array', t => {
40+
const input = [];
41+
const output = cloneArrayObjects(input);
42+
43+
t.deepEqual(output, []);
2444
});

0 commit comments

Comments
 (0)