Skip to content

Commit

Permalink
Publishing the first version of the package.
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-maksimov committed Feb 4, 2022
0 parents commit 509406f
Show file tree
Hide file tree
Showing 13 changed files with 453 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .babelrc
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"
}
6 changes: 6 additions & 0 deletions .browserslistrc
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
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# for `yarn lint` development
coverage
node_modules
lib
tests
25 changes: 25 additions & 0 deletions .eslintrc.js
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",
},
}
8 changes: 8 additions & 0 deletions .gitignore
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
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"semi": false,
"printWidth": 100
}
21 changes: 21 additions & 0 deletions LICENSE
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.
103 changes: 103 additions & 0 deletions README.md
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
78 changes: 78 additions & 0 deletions lib/deep-cloning.js
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;
}
});
});
58 changes: 58 additions & 0 deletions package.json
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"
}
}
40 changes: 40 additions & 0 deletions src/deep-cloning.ts
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
}
Loading

0 comments on commit 509406f

Please sign in to comment.