Skip to content

Commit

Permalink
restructure project
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Davis committed Nov 22, 2015
1 parent 05aa561 commit 68ada80
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 20 deletions.
4 changes: 4 additions & 0 deletions .npmignore
@@ -0,0 +1,4 @@
src
test
.babelrc
.travis.yml
10 changes: 7 additions & 3 deletions README.md
Expand Up @@ -7,14 +7,18 @@ Stand-alone deep cloning of Arrays and Objects
$ npm install --save deep-clone
```

Recursively copy nested objects and arrays.
Recursively clone nested objects and arrays containing primitive data or objects and arrays containing primitive data.

```javascript
import deepClone from 'deep-clone'

const obj = { /* ... */ }
const cloneObj = deepClone(obj)
const cloneOfObj = deepClone(obj)

const arr = [ /* ... */ ]
const cloneArr = deepClone(arr)
const cloneOfArr = deepClone(arr)
```

Other options:
- [clone-deep](https://github.com/jonschlinkert/clone-deep)
- [safe-clone-deep](https://github.com/tracker1/safe-clone-deep)
14 changes: 7 additions & 7 deletions index.js → lib/index.js
Expand Up @@ -10,16 +10,16 @@ function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.const
function deepClone(obj) {
if (!obj || (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) !== 'object') return obj;
if (Array.isArray(obj)) {
var _copy = new Array(obj.length);
var _clone = new Array(obj.length);
for (var i = 0; i < obj.length; i++) {
_copy[i] = deepClone(obj[i]);
_clone[i] = deepClone(obj[i]);
}
return _copy;
return _clone;
}
var copy = {};
var clone = {};
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
copy[keys[i]] = deepClone(obj[keys[i]]);
clone[keys[i]] = deepClone(obj[keys[i]]);
}
return copy;
}
return clone;
}
7 changes: 4 additions & 3 deletions package.json
@@ -1,19 +1,20 @@
{
"name": "deep-clone",
"version": "1.0.0",
"version": "1.0.1",
"description": "Stand-alone deep cloning of Arrays and Objects",
"main": "index.js",
"main": "lib/index.js",
"scripts": {
"test": "mocha --compilers js:babel-core/register",
"dev": "mocha -w --compilers js:babel-core/register",
"precompile": "npm test",
"compile": "babel deep-clone.js -o index.js",
"compile": "babel src --out-dir lib",
"prepublish": "npm run compile"
},
"repository": {
"type": "git",
"url": "git+https://github.com/thebearingedge/deep-clone.git"
},
"keywords": ["clone"],
"author": "",
"license": "MIT",
"bugs": {
Expand Down
12 changes: 6 additions & 6 deletions deep-clone.js → src/index.js
Expand Up @@ -2,16 +2,16 @@
export default function deepClone(obj) {
if (!obj || typeof obj !== 'object') return obj
if (Array.isArray(obj)) {
const copy = new Array(obj.length)
const clone = new Array(obj.length)
for (let i = 0; i < obj.length; i++) {
copy[i] = deepClone(obj[i])
clone[i] = deepClone(obj[i])
}
return copy
return clone
}
const copy = {}
const clone = {}
const keys = Object.keys(obj)
for (let i = 0; i < keys.length; i++) {
copy[keys[i]] = deepClone(obj[keys[i]])
clone[keys[i]] = deepClone(obj[keys[i]])
}
return copy
return clone
}
2 changes: 1 addition & 1 deletion test/deep-clone.test.js → test/index.js
@@ -1,6 +1,6 @@

import { expect } from 'chai'
import deepClone from '../deep-clone'
import deepClone from '../src/index.js'

describe('deepClone', () => {

Expand Down

0 comments on commit 68ada80

Please sign in to comment.