/
usage.mjs
49 lines (34 loc) · 1.32 KB
/
usage.mjs
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
import 'usnam-pmb';
import assert from 'assert';
// ¦mjsUsageDemo¦+
import objPop from '../op.js';
// ¦mjsUsageDemo¦- importPkgName
function repeat(n, x) { return Array.from({ length: n + 1 }).join(x); }
// ¦mjsUsageDemo¦+
function makeSandwich(receipe) {
const pop = objPop(receipe);
const sandwich = ('🍞'
+ repeat(pop('lettuce', 0), '🍀') // unicode is weak on salad.
+ repeat(pop('tomato', 0), '🍅')
+ repeat(pop('ham', 0), '🍗') // close enough.
+ repeat(pop('pineapple', 0), '🍍')
+ repeat(pop('cheese', 0), '🧀')
+ repeat(pop('cherry', 0), '🍒')
);
pop.expectEmpty('unknown ingredients');
return sandwich;
}
const hawaii = { ham: 1, pineapple: 1, cheese: 1, cherry: 1 };
assert.strictEqual(makeSandwich(hawaii), '🍞🍗🍍🧀🍒');
// Original object wasn't modified, so it works again:
assert.strictEqual(makeSandwich(hawaii), '🍞🍗🍍🧀🍒');
// Except when you use direct mode (.d):
const directPop = objPop.d(hawaii);
assert.strictEqual(directPop('cherry'), 1);
assert.strictEqual(directPop('cherry'), undefined);
const blt = { bacon: 2, lettuce: 3, tomato: 3 };
assert.throws(() => makeSandwich(blt),
'LeftoverKeys: unknown ingredients: bacon');
// Unicode would be way better with bacon.
// ¦mjsUsageDemo¦-
console.log('+OK usage test passed.');