-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Publishing the first version of the package.
- Loading branch information
0 parents
commit 509406f
Showing
13 changed files
with
453 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"presets": ["@babel/preset-typescript"], | ||
"plugins": [ | ||
"@babel/plugin-transform-modules-umd", | ||
"@babel/plugin-proposal-class-properties", | ||
"@babel/plugin-proposal-object-rest-spread" | ||
], | ||
"moduleRoot": "src" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
last 10 version | ||
not dead | ||
IE 10 | ||
IE 9 | ||
IE 8 | ||
IE 7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# for `yarn lint` development | ||
coverage | ||
node_modules | ||
lib | ||
tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module.exports = { | ||
root: true, | ||
env: { | ||
browser: true, | ||
node: true, | ||
es6: true, | ||
}, | ||
parser: "@typescript-eslint/parser", | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: "module", | ||
}, | ||
plugins: ["@typescript-eslint/eslint-plugin"], | ||
extends: [ | ||
"plugin:prettier/recommended", | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/recommended", | ||
], | ||
rules: { | ||
"no-console": "off", | ||
"no-unused-vars": "off", | ||
"spaced-comment": "off", | ||
camelcase: "off", | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
node_modules | ||
yarn.lock | ||
package-lock.json | ||
coverage | ||
yarn-error.log | ||
|
||
# MAC generated files | ||
**/.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"semi": false, | ||
"printWidth": 100 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) [year] [fullname] | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# Javascript Deep Cloning | ||
|
||
Javascript deep cloning, so pointers are lost. | ||
|
||
<br /> | ||
|
||
<a href="https://www.npmjs.com/package/js-deep-cloning"> | ||
<img src="https://camo.githubusercontent.com/890acbdcb87868b382af9a4b1fac507b9659d9bf/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667" /> | ||
</a> | ||
|
||
<a href="https://www.npmjs.com/package/js-deep-cloning"> | ||
<img src="https://camo.githubusercontent.com/cf80a63dc680fa3e0d2f6b729fea1a0a14ae5a1e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f436f7665726167652d3130302532352d627269676874677265656e2e737667" /> | ||
</a> | ||
|
||
<a href="https://www.npmjs.com/package/js-deep-cloning"> | ||
<img src="https://camo.githubusercontent.com/d4e0f63e9613ee474a7dfdc23c240b9795712c96/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5052732d77656c636f6d652d627269676874677265656e2e737667" /> | ||
</a> | ||
|
||
## Usage | ||
|
||
### Install the package | ||
|
||
package.json | ||
``` | ||
{ | ||
"dependencies": { | ||
"js-deep-cloning": "latest" | ||
} | ||
} | ||
``` | ||
|
||
After that (with Yarn): | ||
``` | ||
yarn install | ||
``` | ||
|
||
Or with NPM: | ||
``` | ||
npm install | ||
``` | ||
|
||
And finally you can use it in your project: | ||
``` | ||
import { deepClone } from "js-deep-cloning" | ||
const student = { | ||
id: 1, | ||
name: "Jack", | ||
items: ["pen", "rubber", "textbook", "tablet"], | ||
examDate: new Date(), | ||
classmates: [ | ||
{ | ||
id: 2, | ||
name: "John", | ||
items: null, | ||
examDate: new Date() | ||
}, | ||
{ | ||
id: 2, | ||
name: "James", | ||
items: ["pencil", "laptop"], | ||
exams: [new Date(), new Date()] | ||
} | ||
], | ||
university: null | ||
} | ||
const studentData = deepClone(student) | ||
``` | ||
|
||
## Contribution | ||
|
||
Everybody can contribute | ||
|
||
### Useful commands (examples with yarn) | ||
|
||
Build your code: | ||
``` | ||
yarn build | ||
``` | ||
|
||
Run tests: | ||
``` | ||
yarn test | ||
``` | ||
|
||
Generate test coverage: | ||
``` | ||
yarn test:coverage | ||
``` | ||
|
||
Run ESLint: | ||
``` | ||
yarn lint | ||
``` | ||
|
||
### Important | ||
|
||
Please before opening a PR for this package - run tests and eslint and fix the errors in your code. | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
(function (global, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define([], factory); | ||
} else if (typeof exports !== "undefined") { | ||
factory(); | ||
} else { | ||
var mod = { | ||
exports: {} | ||
}; | ||
factory(); | ||
global.deepCloning = mod.exports; | ||
} | ||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function () { | ||
"use strict"; | ||
|
||
(function (global, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define(["exports"], factory); | ||
} else if (typeof exports !== "undefined") { | ||
factory(exports); | ||
} else { | ||
var mod = { | ||
exports: {} | ||
}; | ||
factory(mod.exports); | ||
global.deepCloning = mod.exports; | ||
} | ||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : void 0, function (_exports) { | ||
"use strict"; | ||
|
||
Object.defineProperty(_exports, "__esModule", { | ||
value: true | ||
}); | ||
_exports.deepClone = deepClone; | ||
|
||
function deepClone(src) { | ||
let target; | ||
|
||
if (src === null) { | ||
target = src; | ||
} else if (src instanceof Date) { | ||
target = new Date(src); | ||
} else if (typeof src === "object") { | ||
if (Array.isArray(src)) { | ||
target = []; | ||
|
||
for (let i = 0; i < src.length; i++) { | ||
if (src[i] instanceof Date) { | ||
target.push(new Date(src[i])); | ||
} else if (typeof src[i] === "object") { | ||
target.push(deepClone(src[i])); | ||
} else { | ||
target.push(src[i]); | ||
} | ||
} | ||
} else { | ||
target = {}; | ||
|
||
for (const prop in src) { | ||
if (Object.prototype.hasOwnProperty.call(src, prop)) { | ||
if (src[prop] instanceof Date) { | ||
target[prop] = new Date(src[prop]); | ||
} else if (typeof src[prop] === "object") { | ||
target[prop] = deepClone(src[prop]); | ||
} else { | ||
target[prop] = src[prop]; | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
target = src; | ||
} | ||
|
||
return target; | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{ | ||
"name": "js-deep-cloning", | ||
"version": "1.0.0", | ||
"description": "Javascript deep cloning, so pointers are lost.", | ||
"main": "lib/deep-cloning.js", | ||
"repository": "git@github.com:viktor-maksimov/js-deep-cloning.git", | ||
"author": "viktor-maksimov", | ||
"license": "MIT", | ||
"private": false, | ||
"keywords": [ | ||
"object", | ||
"clone", | ||
"deep", | ||
"javascript", | ||
"typescript", | ||
"pointers" | ||
], | ||
"homepage": "https://github.com/viktor-maksimov/js-deep-cloning", | ||
"bugs": "https://github.com/viktor-maksimov/js-deep-cloning/issues", | ||
"contributors": [ | ||
"Andras Serfozo <subztep@gmail.com>" | ||
], | ||
"scripts": { | ||
"build": "babel --presets @babel/preset-typescript src/deep-cloning.ts --extensions '.ts' -d ts-compiled-temp; babel --plugins @babel/plugin-transform-modules-umd ts-compiled-temp -d lib; rm -r ts-compiled-temp", | ||
"check-types": "tsc", | ||
"test": "yarn check-types; jest . --passWithNoTests", | ||
"test:coverage": "jest . --coverage", | ||
"lint": "eslint --ext .js,.ts .", | ||
"lint:debug": "eslint --debug --ext .js,.ts .", | ||
"lint:fix": "eslint --fix --ext .js,.ts ." | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.16.8", | ||
"@babel/core": "^7.16.12", | ||
"@babel/plugin-proposal-class-properties": "^7.16.7", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.16.7", | ||
"@babel/plugin-transform-modules-umd": "^7.16.7", | ||
"@babel/preset-typescript": "^7.16.7", | ||
"@types/jest": "^27.4.0", | ||
"@typescript-eslint/eslint-plugin": "^5.10.1", | ||
"@typescript-eslint/parser": "^5.10.1", | ||
"babel-jest": "^27.4.6", | ||
"eslint": "^8.8.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-config-standard": "^16.0.3", | ||
"eslint-plugin-import": "^2.25.4", | ||
"eslint-plugin-jest": "^26.0.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"eslint-plugin-promise": "^6.0.0", | ||
"eslint-plugin-standard": "^4.1.0", | ||
"jest": "^27.4.7", | ||
"prettier": "^2.5.1", | ||
"ts-jest": "^27.1.3", | ||
"typescript": "^4.5.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
export function deepClone(src: any): any { | ||
let target: any | ||
if (src === null) { | ||
target = src | ||
} else if (src instanceof Date) { | ||
target = new Date(src) | ||
} else if (typeof src === "object") { | ||
if (Array.isArray(src)) { | ||
target = [] | ||
|
||
for (let i = 0; i < src.length; i++) { | ||
if (src[i] instanceof Date) { | ||
target.push(new Date(src[i])) | ||
} else if (typeof src[i] === "object") { | ||
target.push(deepClone(src[i])) | ||
} else { | ||
target.push(src[i]) | ||
} | ||
} | ||
} else { | ||
target = {} | ||
|
||
for (const prop in src) { | ||
if (Object.prototype.hasOwnProperty.call(src, prop)) { | ||
if (src[prop] instanceof Date) { | ||
target[prop] = new Date(src[prop]) | ||
} else if (typeof src[prop] === "object") { | ||
target[prop] = deepClone(src[prop]) | ||
} else { | ||
target[prop] = src[prop] | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
target = src | ||
} | ||
|
||
return target | ||
} |
Oops, something went wrong.