@@ -15,19 +15,19 @@ Object.assign(scores, initialScores);
1515
1616```
1717const minMax = function (numbers) {
18- const results = [];
19- // variables localResult does not clash,
20- // independent blocks are used
21- {
22- const localResult = Math.min(...numbers);
23- results.push(localResult);
24- }
18+ const results = [];
19+ // variables localResult does not clash,
20+ // independent blocks are used
21+ {
22+ const localResult = Math.min(...numbers);
23+ results.push(localResult);
24+ }
2525
26- {
27- const localResult = Math.max(...numbers);
28- results.push(localResult);
29- }
30- return results;
26+ {
27+ const localResult = Math.max(...numbers);
28+ results.push(localResult);
29+ }
30+ return results;
3131};
3232
3333```
@@ -41,3 +41,73 @@ let b = 2;
4141[a, b] = [b, a];
4242```
4343
44+
45+ Remove duplicates from an array
46+
47+ ```
48+ let a = [5, 4, 4, 4, 3];
49+ a = [...(new Set(a))];
50+ ```
51+
52+ Object.seal and Object.freeze to ensure that an external object does not exceed an Interface
53+
54+ ```
55+ const requestInterface = {
56+ event: undefined,
57+ startDate: undefined,
58+ endDate: undefined
59+ };
60+
61+ const handleRequest = function (request) {
62+ const minimalRequest = Object.seal(Object.assign({}, requestInterface));
63+ Object.assign(minimalRequest, request);
64+ processRequest(minimalRequest);
65+ };
66+
67+ const processRequest = function (request) {
68+ console.log(request);
69+ };
70+
71+ // this will throw
72+ handleRequest({
73+ event: undefined,
74+ startDate: undefined,
75+ endDate: undefined,
76+ tooMuch: "data",
77+ leadsTo: "leaks"
78+ });
79+ ```
80+
81+ default assignement syntax to log for required parameters
82+
83+ ```
84+ const required = function () {
85+ throw `required parameter missing`;
86+ };
87+
88+ const vector3Interface = [0, 0, 0];
89+
90+ const slowVector3 = function (vector3 = required()) {
91+ return vector3.map(u => u / 2);
92+ };
93+
94+ slowVector3() --> Error: required parameter missing
95+ ```
96+
97+ It works really well with destructuring too
98+
99+
100+ ```
101+ const required = function (message = "") {
102+ throw `required parameter ${message} missing`;
103+ };
104+
105+ const vector3Interface = [0, 0, 0];
106+
107+ const slowVector3 = function ([x = required("x"), y = required("y"), z = required("z")] = required("v3")) {
108+ return [x / 2, y / 2, z /2];
109+ };
110+
111+ slowVector3([6, 8]); --> Error: required parameter z missing
112+ ```
113+
0 commit comments