Skip to content

Commit 3d55ec4

Browse files
docs: DOC-5743 BITOP examples (#3087)
1 parent dee7955 commit 3d55ec4

File tree

1 file changed

+124
-4
lines changed

1 file changed

+124
-4
lines changed

doctests/dt-bitmap.js

Lines changed: 124 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
// EXAMPLE: bitmap_tutorial
2-
// HIDE_START
2+
// REMOVE_START
33
import assert from 'assert';
4-
import { createClient } from 'redis';
4+
// REMOVE_END
5+
import { createClient, RESP_TYPES } from 'redis';
56

6-
const client = createClient();
7+
const client = createClient({
8+
commandOptions: {
9+
typeMapping: {
10+
[RESP_TYPES.BLOB_STRING]: Buffer
11+
}
12+
}
13+
});
714
await client.connect();
8-
// HIDE_END
915

1016
// REMOVE_START
1117
await client.flushDb();
@@ -35,5 +41,119 @@ console.log(res4) // >>> 1
3541
// STEP_END
3642
// REMOVE_START
3743
assert.equal(res4, 1)
44+
// REMOVE_END
45+
46+
// STEP_START bitop_setup
47+
await client.setBit("A", 0, 1)
48+
await client.setBit("A", 1, 1)
49+
await client.setBit("A", 3, 1)
50+
await client.setBit("A", 4, 1)
51+
52+
const res5 = await client.get("A")
53+
console.log(res5.readUInt8(0).toString(2).padStart(8, '0'))
54+
// >>> 11011000
55+
56+
await client.setBit("B", 3, 1)
57+
await client.setBit("B", 4, 1)
58+
await client.setBit("B", 7, 1)
59+
60+
const res6 = await client.get("B")
61+
console.log(res6.readUInt8(0).toString(2).padStart(8, '0'))
62+
// >>> 00011001
63+
64+
await client.setBit("C", 1, 1)
65+
await client.setBit("C", 2, 1)
66+
await client.setBit("C", 4, 1)
67+
await client.setBit("C", 5, 1)
68+
69+
const res7 = await client.get("C")
70+
console.log(res7.readUInt8(0).toString(2).padStart(8, '0'))
71+
// >>> 01101100
72+
// STEP_END
73+
// REMOVE_START
74+
assert.equal(res5.readUInt8(0), 0b11011000)
75+
assert.equal(res6.readUInt8(0), 0b00011001)
76+
assert.equal(res7.readUInt8(0), 0b01101100)
77+
// REMOVE_END
78+
79+
// STEP_START bitop_and
80+
await client.bitOp("AND", "R", ["A", "B", "C"])
81+
const res8 = await client.get("R")
82+
console.log(res8.readUInt8(0).toString(2).padStart(8, '0'))
83+
// >>> 00001000
84+
// STEP_END
85+
// REMOVE_START
86+
assert.equal(res8.readUInt8(0), 0b00001000)
87+
// REMOVE_END
88+
89+
// STEP_START bitop_or
90+
await client.bitOp("OR", "R", ["A", "B", "C"])
91+
const res9 = await client.get("R")
92+
console.log(res9.readUInt8(0).toString(2).padStart(8, '0'))
93+
// >>> 11111101
94+
// STEP_END
95+
// REMOVE_START
96+
assert.equal(res9.readUInt8(0), 0b11111101)
97+
// REMOVE_END
98+
99+
// STEP_START bitop_xor
100+
await client.bitOp("XOR", "R", ["A", "B"]) // XOR uses two keys here
101+
const res10 = await client.get("R")
102+
console.log(res10.readUInt8(0).toString(2).padStart(8, '0'))
103+
// >>> 11000001
104+
// STEP_END
105+
// REMOVE_START
106+
assert.equal(res10.readUInt8(0), 0b11000001)
107+
// REMOVE_END
108+
109+
// STEP_START bitop_not
110+
await client.bitOp("NOT", "R", "A")
111+
const res11 = await client.get("R")
112+
console.log(res11.readUInt8(0).toString(2).padStart(8, '0'))
113+
// >>> 00100111
114+
// STEP_END
115+
// REMOVE_START
116+
assert.equal(res11.readUInt8(0), 0b00100111)
117+
// REMOVE_END
118+
119+
// STEP_START bitop_diff
120+
await client.bitOp("DIFF", "R", ["A", "B", "C"])
121+
const res12 = await client.get("R")
122+
console.log(res12.readUInt8(0).toString(2).padStart(8, '0'))
123+
// >>> 10000000
124+
// STEP_END
125+
// REMOVE_START
126+
assert.equal(res12.readUInt8(0), 0b10000000)
127+
// REMOVE_END
128+
129+
// STEP_START bitop_diff1
130+
await client.bitOp("DIFF1", "R", ["A", "B", "C"])
131+
const res13 = await client.get("R")
132+
console.log(res13.readUInt8(0).toString(2).padStart(8, '0'))
133+
// >>> 00100101
134+
// STEP_END
135+
// REMOVE_START
136+
assert.equal(res13.readUInt8(0), 0b00100101)
137+
// REMOVE_END
138+
139+
// STEP_START bitop_andor
140+
await client.bitOp("ANDOR", "R", ["A", "B", "C"])
141+
const res14 = await client.get("R")
142+
console.log(res14.readUInt8(0).toString(2).padStart(8, '0'))
143+
// >>> 01011000
144+
// STEP_END
145+
// REMOVE_START
146+
assert.equal(res14.readUInt8(0), 0b01011000)
147+
// REMOVE_END
148+
149+
// STEP_START bitop_one
150+
await client.bitOp("ONE", "R", ["A", "B", "C"])
151+
const res15 = await client.get("R")
152+
console.log(res15.readUInt8(0).toString(2).padStart(8, '0'))
153+
// >>> 10100101
154+
// STEP_END
155+
// REMOVE_START
156+
assert.equal(res15.readUInt8(0), 0b10100101)
157+
38158
await client.close();
39159
// REMOVE_END

0 commit comments

Comments
 (0)