-
Notifications
You must be signed in to change notification settings - Fork 2
/
collections.js
474 lines (397 loc) · 10.2 KB
/
collections.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//'use strict'
let uMap = new Map()
uMap.set('key1','Hello')
uMap.set('key2','Hi')
uMap.set('key1','Hey')
//console.log(uMap)
for(let [key,value] of uMap){
// console.log(key,value)
}
//console.log(uMap.get('key1'))
// console.log(uMap.values())
// console.log(uMap.keys())
for(let key of uMap.keys()){
console.log(`Key : ${key}`)
}
let usrObj1 = {
id: 1,
name: 'Hello'
}
let usrObj2 = {
id: 2,
name: 'Hi'
}
let usrObj3 = {
id: 2,
name: 'Hi'
}
console.log(usrObj2 === usrObj3)
let usrMap = new Map()
usrMap.set(usrObj1,usrObj1.name)
usrMap.set(usrObj2,usrObj2.name)
usrMap.set(usrObj3,usrObj1.name)
// traverse via map entries
for(let [key,value] of usrMap){
console.log(`Key : ${key.id} ,Value : ${value}`)
}
// access keys
for(let k of usrMap.keys()){
console.log(`Key is : ${k.id}`)
}
//console.log(usrMap.get(usrObj1))
const uSet = new Set()
uSet.add('How')
uSet.add('Are')
//uSet.add('You')
for(let s of uSet){
console.log(`Set Value : ${s}`)
}
console.log(uSet.keys())
const usrSet = new Set()
usrSet.add(usrObj1)
usrSet.add(usrObj1)
usrSet.add(usrObj3)
for(let u of usrSet){
console.log(`Users are : ${u.name}`)
}
const usRs = new Map()
let mUsr1 = {
id: 1,
name: 'Bob',
email: '[email protected]'
}
let mUsr2 = {
id: 2,
name: 'John',
email: '[email protected]',
salary: 10
}
// console.log(Object.keys(mUsr2))
usRs.set(Object.keys(mUsr1),mUsr1)
usRs.set(Object.keys(mUsr2),mUsr2)
for(let [key,value] of usRs){
key.forEach(col => {
let columnName = value[col]
console.log(`${col} : ${columnName}`)
});
}
let srtNums = new Map()
srtNums.set('John',{ name: 'John'})
srtNums.set('Bob',{ name: 'Bob' })
srtNums.set('Zack',{ name: 'Zack'})
srtNums.delete('Zack')
// sort array by value
let srtdMap = new Map(Array.from(srtNums.entries()).sort((a,b)=>{
if(a[1].name > b[1].name)
return -1
else
return 1
})).forEach((v,k)=>{
console.log(`${k} ${v.name}`)
})
const weakUsers = new WeakMap()
let oWeak1= {id: 1}
weakUsers.set(oWeak1,'Hey')
weakUsers.set({id: 2},'How r u ')
console.log(weakUsers.get(oWeak1))
weakUsers.delete(oWeak1)
const weakSet = new WeakSet()
weakSet.add(oWeak1)
console.log(weakSet)
//
const _clsrMessage= (_msg)=>{
let detailedMsg = `Hey Folks! ,${_msg}`
return (inMsg)=>{
console.log(detailedMsg +' '+inMsg)
}
}
const innerClsrMessage = _clsrMessage('Closure Message')
innerClsrMessage('Inner')
const _calculator= (_first,_operation)=>{
// outer function
let _msg = ''
switch(_operation){
case 'ADD':
_msg = 'SUM is'
return (_next)=>{
// add closure
return ()=>{
// inner add closure
let _result = _first + _next
return `${_msg} ,${_result}`
}
}
case 'DIFF':
_msg = 'DIFFERENCE is'
return (_next)=>{
// add closure
return ()=>{
// inner diff closure
let _result = _first - _next
return `${_msg} ,${_result}`
}
}
case 'MUL':
_msg = 'PRODUCT is'
return (_next)=>{
// mul closure
return ()=>{
// inner mul closure
let _result = _first * _next
return `${_msg} ,${_result}`
}
}
}
}
const _calcOperator = _calculator(10,'ADD')(3)()
console.log(`${_calcOperator}`)
const _calcOuter = _calculator(11,'MUL')
const _calcInner = _calcOuter(3)
const _calcResult = _calcInner()
console.log(`Calculator : ${_calcResult}`)
let clsrEmps = [
{
id: 1,
name: 'John',
designation: 'Programmer'
},
{
id: 2,
name: 'Bob',
designation: 'Programmer'
},
{
id: 3,
name: 'Adams',
designation: 'Developer'
},
{
id: 4,
name: 'Ram',
designation: 'Manager'
}
]
const EMPActions ={
FETCH_ALL: 'FETCH_ALL',
FETCH_BY_DESIGNATION: 'FETCH_BY_DESIGNATION',
ADD: 'ADD',
DEL_BY_ID: 'DEL_BY_ID'
}
//lock the modifications
Object.freeze(EMPActions)
const EmployeeDesignation={
Programmer: 'Programmer',
Developer: 'Developer',
Manager: 'Manager',
Consultant : 'Consultant'
}
//lock the modifications
Object.freeze(EmployeeDesignation)
const clsrEmp = (_emps,_action)=>{
switch(_action){
case EMPActions.FETCH_ALL:
return ()=>{
return _emps
}
case EMPActions.FETCH_BY_DESIGNATION:
return (_designation)=>{
return _emps.filter(e=>{
return e.designation == _designation
})
}
case EMPActions.ADD:
return (_empObj)=>{
const eExists = _emps.find(e=>{ return e.id == _empObj.id})
if(eExists)
throw new Error('User already exists')
_emps.push(_empObj)
return _emps
}
case EMPActions.DEL_BY_ID:
return (_eId)=>{
clsrEmps = _emps.filter(e=> {return e.id != _eId})
return clsrEmps
}
}
}
const _clsrEmpCaller = clsrEmp(clsrEmps,EMPActions.FETCH_ALL)
const _clsrInnerEmpCaller = _clsrEmpCaller()
console.log(_clsrInnerEmpCaller)
const _clsrEmpCallerByDesig = clsrEmp(clsrEmps,EMPActions.FETCH_BY_DESIGNATION)
const _clsrInnerEmpCallerByDesig = _clsrEmpCallerByDesig(EmployeeDesignation.Programmer)
console.log(_clsrInnerEmpCallerByDesig)
const _clsrEmpCallerByAdd = clsrEmp(clsrEmps,EMPActions.ADD)
const _clsrInnerEmpCallerByAdd = _clsrEmpCallerByAdd({id: 5,name:'Mohsin',designation:EmployeeDesignation.Consultant})
console.log(_clsrInnerEmpCallerByAdd)
const _clsrEmpCallerDelById = clsrEmp(clsrEmps,EMPActions.DEL_BY_ID)
const _clsrInnerEmpCallerDelById = _clsrEmpCallerDelById(1)
console.log(_clsrInnerEmpCallerDelById)
// Proxy
const _target = {
txt: 'hello'
}
let _handler = {
get: (target,key)=>{
return target[key]
},
set: (target,key,value)=>{
target[key] = value + value
return true
}
}
let prxy = new Proxy(_target,_handler)
console.log(prxy.txt)
prxy.txt = 'Hey'
console.log(prxy.txt)
// object
let _actions={
_operation: 'DIFF'
}
// function
function _process(_first,_next){
let _result=0
switch(this._operation){
case 'ADD':
_result = _first + _next
return `${this._operation} ,${_result}`
case 'DIFF':
_result = _first - _next
return `${this._operation} ,${_result}`
}
return 'No operation trigerred'
}
// binding the object with the function
let _res = _process.call(_actions,10,2)
console.log(`${_res}`)
_actions._operation = 'ADD'
_res = _process.apply(_actions,[10,2])
console.log(`${_res}`)
let persons = [
{
name: 'p1'
},
{
name : 'p2'
},
{
name : 'p3'
}
]
function parseName(_by){
// this shall be the persons
if(this instanceof Array){
this.forEach(p=>{
console.log(`${_by} Name : ${p.name}`)
})
}else{
console.log(`${_by} Name is ${this.name}`)
}
}
// passing array of persons
parseName.call(persons,'Call')
// passing only a single person
parseName.apply({name: 'Person'},['Apply'])
const _boundParseName = parseName.bind({name: 'Bob'},['Bind'])
setTimeout(()=>{
console.log('Person has been saved')
// calling the bound function at a chosen time after an interval
_boundParseName()
},2000)
/*let _fields={
_first : 10,
_next: 4
}
const _operations = [
[
'ADD' , '+'
],
[
'DIFF', '-'
]
]
const getOperator = (_action)=>{
return _operations.find(op=>{
return op[0] == _action
})[1]
}
function _calcP(_action){
return _action !=undefined ? parseInt(this._first) + getOperator(_action) + parseInt(this._next):'No Operator Specified'
}
console.log(_calcP.call(_fields,'DIFF'))
*/
const pUser = {
firstName : 'John',
lastName : 'The Baptist',
age : 18
}
/*Object.defineProperty(pUser,'id',{
value : 1,
writable: false
})*/
pUser.id = 11
const prxHandler = {
get: (target,_field)=>{
if(_field == 'firstName'){
return target[_field] = 'Field cannot be fetched'
}
return target[_field]
}, // end :get
set: (target,key,value)=>{
/*if(key == 'email'){
target[key] = value
}*/
if(key == 'firstName'){
if(value.length >= 5 && value.length <= 10 ){
target[key] = value
}else{
throw new Error(`FirstName should be in between 5-10 characters only`)
}
}
if(key == 'lastName'){
target[key] = value
}
if(key == 'age'){
if(value >= 18)
target[key] = value
else
throw new Error(`User seems to be juvenile, please ensure to be an adult i.e. 18+ `)
}
}, // end : set
defineProperty: (target,_field,descriptor)=>{
let propArr = Array.from(_field)
if(propArr[0] == '_'){
throw new Error(`Field cannot begin with _`)
}else{
//return false
return target[_field] = descriptor.value
}
}, // end: define property
deleteProperty :(target,_field)=>{
if(_field == 'firstName'){
throw new Error('First name cannot be deleted')
}else{
// allow field deletion
delete target[_field]
}
}
// end : delete property
}
const prxUser = new Proxy(pUser,prxHandler)
console.log(prxUser.firstName)
prxUser.firstName = 'Adams'
prxUser.lastName = 'Charles'
prxUser.age = 19
pUser.id = 5767
//prxUser.email = '[email protected]'
Object.defineProperty(prxUser,'emailAddress',{
value: '[email protected]',
writable: true
})
//delete prxUser.firstName
console.log(prxUser)
//
console.log('FirstName :' +Reflect.get(prxUser,'firstName'))
Reflect.set(prxUser,'age',25)
Reflect.deleteProperty(prxUser,'age')
// refresh the proxy object
console.log(prxUser)