Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add duplicateObject, deepDuplicateObject, deepDuplicateArray Methods #34

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ let merged2 = helpful.mergeObjects({"a": 1, "b": 2}, {"b": 3, "c": 4}); // {"a":
**Return Type:** object (`{"a": 1, "b": 2}`)

### partitionArray
Partitions an array
Partitions an array
```javascript
let partitionArray1 = helpful.partitionArray([1, 2, 3, 4], n => n > 2); // Array(2) [[3, 4], [1, 2]]
let partitionArray2 = helpful.partitionArray([1, 2, 3, 4, 5], n => true); // Array(2) [[1, 2, 3, 4, 5], []]
Expand All @@ -479,6 +479,41 @@ let partitionArray3 = helpful.partitionArray([1, 2, 3, 4, 5], n => false); // Ar

**Return Type:** Array (`Array(2) [[3, 4, 5], [1, 2]]`)


### duplicateObject
Duplicate an object
``` javascript
let duplicate = helpful.duplicateObject({a: 'a', b: 2, c: true, d: null}) // {a: 'a', b: 2, c: true, d: null}
```

**Parameters:**
- object: object (`{a: 'a', b: 2, c: true, d: null}`)

**Return Type:** object (`{a: 'a', b: 2, c: true, d: null}`)

### deepDuplicateObject
Duplicate an object and all inner objects and arrays
``` javascript
let deepDuplicate = helpful.deepDuplicateObject({a: 1, f: {g: ['h']}}) // {a: 1, f: {g: ['h']}}
```

**Parameters:**
- object: object (`{a: 1, f: {g: ['h']}}`)

**Return Type:** object (`{a: 1, f: {g: ['h']}}`)

### deepDuplicateArray
Duplicate an array and all inner objects and arrays
``` javascript
let deepDuplicate = helpful.deepDuplicateArray([1, {b: [2]}]) // [1, {b: [2]}]
```

**Parameters:**
- array: array (`[1, {b: [2]}]`)

**Return Type:** array (`[1, {b: [2]}]`)


## Hex

### hex.convertFromString
Expand Down
40 changes: 40 additions & 0 deletions helpful.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,46 @@
return [pass, notPass];
}

helpful.duplicateObject = function (object) {
return {...object};
}

const typeCheck = function(oldEle, newEle) {
if (oldEle === null) {
newEle= null;
} else if (oldEle === undefined) {
newEle = undefined;
} else if (Array.isArray(oldEle)) {
newEle = helpful.deepFlattenArray(oldEle);
} else if (typeof oldEle === 'object') {
if (oldEle.constructor.name === 'Date') {
newEle = new Date(oldEle.getTime());
} else {
newEle = helpful.deepDuplicateObject(oldEle);
}
} else {
newEle = oldEle
}
return newEle;
}

helpful.deepDuplicateObject = function (object) {
let keys = Object.keys(object);
let newObject = {};
for (const key of keys) {
newObject[key] = typeCheck(object[key], newObject[key]);
}
return {...newObject};
}

helpful.deepDuplicateArray = function (array) {
let newArray = [];
for (let i = 0; i < array.length; i++) {
newArray[i] = typeCheck(array[i], newArray[i]);
}
return [...newArray];
}

helpful.hex = {};

/* Modified from https://github.com/TogaTech/tEnvoy */
Expand Down
Loading