-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom.js
38 lines (31 loc) · 1 KB
/
custom.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
const CUSTOM_OBJ_METHODS_NAMES = ["listValues", "listKeys", "listEntries", "stringify"]
const CUSTOM_ARR_METHODS_NAMES = ["compact", "stringify"]
function loadCustomMethods() {
Array.prototype.compact = function () {
return Array.from(new Set(this))
}
Array.prototype.stringify = function (...args) {
return JSON.stringify(this, ...args)
}
Object.prototype.listKeys = function () {
return Object.entries(this)
.filter(([k, v]) => !(k === "constructor" && v === undefined))
.map(([key, _]) => key)
}
Object.prototype.listEntries = function () {
return Object.entries(this).filter(([k, v]) => !(k === "constructor" && v === undefined))
}
Object.prototype.listValues = function () {
return Object.entries(this)
.filter(([k, v]) => !(k === "constructor" && v === undefined))
.map(([_, val]) => val)
}
Object.prototype.stringify = function (...args) {
return JSON.stringify(this, ...args)
}
}
module.exports = {
CUSTOM_OBJ_METHODS_NAMES,
CUSTOM_ARR_METHODS_NAMES,
loadCustomMethods
}