Skip to content

Commit bace4c5

Browse files
committed
First commit
0 parents  commit bace4c5

11 files changed

+203
-0
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[*.yml]
11+
indent_style = space
12+
indent_size = 2

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
yarn.lock
3+
.nyc_output
4+
coverage/
5+
.vscode/

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.travis.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
language: node_js
2+
node_js:
3+
- '10'
4+
- '8'
5+
notifications:
6+
email: false
7+
before_install:
8+
- "npm i -g npm@latest"
9+
install:
10+
- npm install
11+
- npm install -g codecov
12+
before_script:
13+
- npm prune
14+
script:
15+
- npm run cover
16+
- codecov

codecov.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
codecov:
2+
notify:
3+
require_ci_to_pass: yes
4+
5+
coverage:
6+
precision: 2
7+
round: down
8+
range: "40...100"
9+
10+
status:
11+
project: yes
12+
patch: yes
13+
changes: no
14+
15+
parsers:
16+
gcov:
17+
branch_detection:
18+
conditional: yes
19+
loop: yes
20+
method: no
21+
macro: no
22+
23+
comment:
24+
layout: "header, diff"
25+
behavior: default
26+
require_changes: no

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = (arrayInput = []) => {
2+
if (!Array.isArray(arrayInput)) {
3+
throw new TypeError(`Expected an array, got ${typeof arrayInput}`);
4+
}
5+
return arrayInput.map(obj => ({...obj}));
6+
};

license

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018-present Palash Mondal
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

package.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "clone-array-objects",
3+
"version": "0.0.0",
4+
"description": "Clone an array of objects",
5+
"license": "MIT",
6+
"repository": "https://github.com/palashmon/clone-array-objects",
7+
"author": {
8+
"name": "Palash Mondal",
9+
"email": "palashm@live.in",
10+
"url": "https://github.com/palashmon"
11+
},
12+
"engines": {
13+
"node": ">=8"
14+
},
15+
"scripts": {
16+
"test": "xo && ava",
17+
"lint": "xo --fix",
18+
"cover": "nyc --reporter=lcov ava"
19+
},
20+
"files": [
21+
"index.js"
22+
],
23+
"keywords": [
24+
"clone",
25+
"array",
26+
"objects",
27+
"multiple",
28+
"array item",
29+
"keys",
30+
"test"
31+
],
32+
"devDependencies": {
33+
"ava": "^0.25.0",
34+
"nyc": "^12.0.2",
35+
"xo": "^0.21.1"
36+
}
37+
}

readme.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# clone-array-objects
2+
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
9+
10+
## Install
11+
12+
```
13+
$ npm i clone-array-objects
14+
```
15+
16+
## Usage
17+
18+
This module helps us to get cloned array of objects
19+
20+
```js
21+
const cloneArrayObjects = require('clone-array-objects');
22+
let users = [{ id: 1, username: 'bret' }, { id: 2, username: 'samantha' }];
23+
24+
// Clone this users array
25+
let clonedUsers = cloneArrayObjects(users);
26+
console.log(clonedUsers);
27+
//=> [{ id: 1, username: 'bret' }, { id: 2, username: 'samantha' }];
28+
29+
// Let us modify one of the object key value in the actual users array
30+
users[1].username = 'ava';
31+
32+
// So, actual users array has been updated now
33+
console.log(users);
34+
//=> [{ id: 1, username: 'bret' }, { id: 2, username: 'ava' }];
35+
36+
// But, the cloned users array is still the same
37+
console.log(clonedUsers);
38+
//=> [{ id: 1, username: 'bret' }, { id: 2, username: 'samantha' }];
39+
```
40+
41+
## API
42+
43+
### cloneArrayObjects(arrayInput)
44+
45+
#### arrayInput
46+
47+
Type: `Array`<br>
48+
Default: `[]`
49+
50+
Must be a JavaScript Array. This is main array of object that we will like to clone.
51+
52+
## License
53+
54+
MIT © [Palash Mondal](https://github.com/palashmon)

test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import test from 'ava';
2+
import cloneArrayObjects from '.';
3+
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');
9+
});
10+
11+
test('Return empty array when no valid input passed.', t => {
12+
t.deepEqual(cloneArrayObjects(), []);
13+
t.deepEqual(cloneArrayObjects([]), []);
14+
});
15+
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);
24+
});

0 commit comments

Comments
 (0)