Skip to content

Commit 7a6c607

Browse files
authored
Merge pull request #13 from SecJS/feat/len-deep-copy
chore: copy method to Json Class
2 parents baf4ea6 + bb129dd commit 7a6c607

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ yarn add @secjs/utils
2727

2828
### Json
2929

30-
> Use Json to parse json without errors and more.
30+
> Use Json to parse json without errors, deep copy and more.
3131
3232
```js
3333
import { Json } from '@secjs/utils'
@@ -52,6 +52,22 @@ json.getJson(textWithJsons) // ['{"text":"hello"}', '{"hello":"world"}']
5252
const text = 'a string that is not a valid JSON'
5353

5454
json.parse(text) // null
55+
56+
57+
const object = {
58+
test: 'hello',
59+
hello: () => 'hy',
60+
}
61+
62+
const objectCopy = json.copy(object)
63+
64+
objectCopy.test = 'hello from copy'
65+
objectCopy.hello = () => 'hy from copy'
66+
67+
console.log(object.test) // hello
68+
console.log(object.hello()) // hy
69+
console.log(objectCopy.test) // hello from copy
70+
console.log(objectCopy.hello()) // hy from copy
5571
```
5672

5773
---

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@secjs/utils",
3-
"version": "1.3.3",
3+
"version": "1.3.4",
44
"description": "",
55
"scripts": {
66
"build": "tsc",

src/Classes/Json.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@ export class Json {
1313
return !results.includes(false)
1414
}
1515

16+
/**
17+
* Deep copy any object properties without reference
18+
*
19+
* @param object The object to be copied
20+
* @return A copy from object without any reference
21+
*/
22+
copy<T>(object: T): T {
23+
const copy: any = {}
24+
25+
for (const i in object) {
26+
const item = object[i]
27+
copy[i] =
28+
item != null && typeof item === 'object' ? this.copy(item) : item
29+
}
30+
31+
return copy
32+
}
33+
1634
/**
1735
* Find all JSON's inside string and return it.
1836
* @param text A valid string with one or more JSON's inside.

tests/Classes/json.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@ describe('\n Json Class', () => {
2020
expect(json.isArrayOfObjects([1, 2, 3])).toBe(false)
2121
})
2222

23+
it('should return a deep copy from the object', async () => {
24+
const object = {
25+
test: 'hello',
26+
hello: () => 'hy',
27+
}
28+
29+
const objectCopy = json.copy(object)
30+
31+
objectCopy.test = 'hello from copy'
32+
objectCopy.hello = () => 'hy from copy'
33+
34+
expect(object.test).toBe('hello')
35+
expect(object.hello()).toBe('hy')
36+
expect(objectCopy.test).toBe('hello from copy')
37+
expect(objectCopy.hello()).toBe('hy from copy')
38+
})
39+
2340
it('should return all json found inside of the string', () => {
2441
const text =
2542
'this is a string with a json inside of it {"text":"hello"} and one more json {"hello":"world"}'

0 commit comments

Comments
 (0)