Repository navigation
Expand file tree
/
Copy pathfactoryPatternRORO.js
More file actions
81 lines (70 loc) · 1.72 KB
/
Copy pathfactoryPatternRORO.js
File metadata and controls
81 lines (70 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function isObject(subject) {
return typeof subject == "object";
}
function isArray(subject) {
return Array.isArray(subject);
}
function deepCopy(subject) {
let copySubject;
const subjectIsObject = isObject(subject);
const subjectIsArray = isArray(subject);
if (subjectIsArray) {
copySubject = [];
} else if (subjectIsObject) {
copySubject = {};
} else {
return subject;
}
for (key in subject) {
const keyIsObject = isObject(subject[key]);
if (keyIsObject) {
copySubject[key] = deepCopy(subject[key]);
} else {
if (subjectIsArray) {
copySubject.push(subject[key]);
} else {
copySubject[key] = subject[key];
}
}
}
return copySubject;
}
// const studentBase = {
// name: undefined,
// email: undefined,
// age: undefined,
// approvedCourses: undefined,
// learningPaths: undefined,
// socialMedia: {
// twitter: undefined,
// instagram: undefined,
// facebook: undefined,
// },
// };
function requiredParam(param) {
throw new Error(param + " es obligatorio");
}
function createStudent({
name = requiredParam("name"),
email = requiredParam("email"),
age,
twitter,
instagram,
facebook,
approvedCourses = [],
learningPaths = [],
} = {}) {
return {
name,
email,
age,
approvedCourses,
learningPaths,
socialMedia: {
twitter,
instagram,
facebook,
},
};
}
const juan = createStudent({ email: "juanito@frijoles.co", name: "Juanito" });