-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
79 lines (61 loc) · 1.35 KB
/
index.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
'use strict'
exports.nextVal = require('./lib/next-val')
exports.prevVal = require('./lib/prev-val')
exports.first = function first(map) {
if (!(map instanceof Map) && !(map instanceof Set)) {
throw new TypeError('map must be a Map or Set')
}
if (!map.size) {
return null
}
return map.entries().next().value
}
exports.firstKey = function firstKey(map) {
const entry = exports.first(map)
if (entry) {
return entry[0]
}
return null
}
exports.firstVal = function firstVal(map) {
const entry = exports.first(map)
if (entry) {
return entry[1]
}
return null
}
exports.last = function last(map) {
if (!(map instanceof Map) && !(map instanceof Set)) {
throw new TypeError('map must be a Map or Set')
}
if (!map.size) {
return null
}
let out = null
for (const item of map.entries()) {
out = item
}
return out
}
exports.lastKey = function lastKey(map) {
const entry = exports.last(map)
if (entry) {
return entry[0]
}
return null
}
exports.lastVal = function lastVal(map) {
const entry = exports.last(map)
if (entry) {
return entry[1]
}
return null
}
exports.replace = function replace(from, to, map) {
if (!(map instanceof Map) && !(map instanceof Set)) {
throw new TypeError('map must be a Map or Set')
}
const o = map.get(from)
map.delete(from)
map.set(to, o)
}