|
| 1 | +// EXAMPLE: cmds_list |
| 2 | +// HIDE_START |
| 3 | +import assert from 'node:assert'; |
| 4 | +import { createClient } from 'redis'; |
| 5 | + |
| 6 | +const client = createClient(); |
| 7 | +await client.connect().catch(console.error); |
| 8 | +// HIDE_END |
| 9 | + |
| 10 | +// STEP_START lpush |
| 11 | +const res1 = await client.lPush('mylist', 'world'); |
| 12 | +console.log(res1); // 1 |
| 13 | + |
| 14 | +const res2 = await client.lPush('mylist', 'hello'); |
| 15 | +console.log(res2); // 2 |
| 16 | + |
| 17 | +const res3 = await client.lRange('mylist', 0, -1); |
| 18 | +console.log(res3); // [ 'hello', 'world' ] |
| 19 | + |
| 20 | +// REMOVE_START |
| 21 | +assert.deepEqual(res3, [ 'hello', 'world' ]); |
| 22 | +await client.del('mylist'); |
| 23 | +// REMOVE_END |
| 24 | +// STEP_END |
| 25 | + |
| 26 | +// STEP_START lrange |
| 27 | +const res4 = await client.rPush('mylist', 'one'); |
| 28 | +console.log(res4); // 1 |
| 29 | + |
| 30 | +const res5 = await client.rPush('mylist', 'two'); |
| 31 | +console.log(res5); // 2 |
| 32 | + |
| 33 | +const res6 = await client.rPush('mylist', 'three'); |
| 34 | +console.log(res6); // 3 |
| 35 | + |
| 36 | +const res7 = await client.lRange('mylist', 0, 0); |
| 37 | +console.log(res7); // [ 'one' ] |
| 38 | + |
| 39 | +const res8 = await client.lRange('mylist', -3, 2); |
| 40 | +console.log(res8); // [ 'one', 'two', 'three' ] |
| 41 | + |
| 42 | +const res9 = await client.lRange('mylist', -100, 100); |
| 43 | +console.log(res9); // [ 'one', 'two', 'three' ] |
| 44 | + |
| 45 | +const res10 = await client.lRange('mylist', 5, 10); |
| 46 | +console.log(res10); // [] |
| 47 | + |
| 48 | +// REMOVE_START |
| 49 | +assert.deepEqual(res7, [ 'one' ]); |
| 50 | +assert.deepEqual(res8, [ 'one', 'two', 'three' ]); |
| 51 | +assert.deepEqual(res9, [ 'one', 'two', 'three' ]); |
| 52 | +assert.deepEqual(res10, []); |
| 53 | +await client.del('mylist'); |
| 54 | +// REMOVE_END |
| 55 | +// STEP_END |
| 56 | + |
| 57 | +// STEP_START llen |
| 58 | +const res11 = await client.lPush('mylist', 'World'); |
| 59 | +console.log(res11); // 1 |
| 60 | + |
| 61 | +const res12 = await client.lPush('mylist', 'Hello'); |
| 62 | +console.log(res12); // 2 |
| 63 | + |
| 64 | +const res13 = await client.lLen('mylist'); |
| 65 | +console.log(res13); // 2 |
| 66 | + |
| 67 | +// REMOVE_START |
| 68 | +assert.equal(res13, 2); |
| 69 | +await client.del('mylist'); |
| 70 | +// REMOVE_END |
| 71 | +// STEP_END |
| 72 | + |
| 73 | +// STEP_START rpush |
| 74 | +const res14 = await client.rPush('mylist', 'hello'); |
| 75 | +console.log(res14); // 1 |
| 76 | + |
| 77 | +const res15 = await client.rPush('mylist', 'world'); |
| 78 | +console.log(res15); // 2 |
| 79 | + |
| 80 | +const res16 = await client.lRange('mylist', 0, -1); |
| 81 | +console.log(res16); // [ 'hello', 'world' ] |
| 82 | + |
| 83 | +// REMOVE_START |
| 84 | +assert.deepEqual(res16, [ 'hello', 'world' ]); |
| 85 | +await client.del('mylist'); |
| 86 | +// REMOVE_END |
| 87 | +// STEP_END |
| 88 | + |
| 89 | +// STEP_START lpop |
| 90 | +const res17 = await client.rPush('mylist', ["one", "two", "three", "four", "five"]); |
| 91 | +console.log(res17); // 5 |
| 92 | + |
| 93 | +const res18 = await client.lPop('mylist'); |
| 94 | +console.log(res18); // 'one' |
| 95 | + |
| 96 | +const res19 = await client.lPopCount('mylist', 2); |
| 97 | +console.log(res19); // [ 'two', 'three' ] |
| 98 | + |
| 99 | +const res20 = await client.lRange('mylist', 0, -1); |
| 100 | +console.log(res20); // [ 'four', 'five' ] |
| 101 | + |
| 102 | +// REMOVE_START |
| 103 | +assert.deepEqual(res20, [ 'four', 'five' ]); |
| 104 | +await client.del('mylist'); |
| 105 | +// REMOVE_END |
| 106 | +// STEP_END |
| 107 | + |
| 108 | +// STEP_START rpop |
| 109 | +const res21 = await client.rPush('mylist', ["one", "two", "three", "four", "five"]); |
| 110 | +console.log(res21); // 5 |
| 111 | + |
| 112 | +const res22 = await client.rPop('mylist'); |
| 113 | +console.log(res22); // 'five' |
| 114 | + |
| 115 | +const res23 = await client.rPopCount('mylist', 2); |
| 116 | +console.log(res23); // [ 'four', 'three' ] |
| 117 | + |
| 118 | +const res24 = await client.lRange('mylist', 0, -1); |
| 119 | +console.log(res24); // [ 'one', 'two' ] |
| 120 | + |
| 121 | +// REMOVE_START |
| 122 | +assert.deepEqual(res24, [ 'one', 'two' ]); |
| 123 | +await client.del('mylist'); |
| 124 | +// REMOVE_END |
| 125 | +// STEP_END |
| 126 | + |
| 127 | +// HIDE_START |
| 128 | +await client.quit(); |
| 129 | +// HIDE_END |
0 commit comments