single-key
v1.0.0
Published
Utilities for key-tagged values (tagged unions).
Downloads
24
Maintainers
Readme
single-key
Utilities for key-tagged values (tagged unions).
API
NOTE: All functions (except isKeyTaggedValue
) throw
NonConformingError
—a subtype of TypeError—when the argument is not
a key-tagged value.
isKeyTaggedValue
isKeyTaggedValue(val: ?): Boolean
Returns true if the value conforms to the key-tagged value protocol.
That is, it returns true if val
:
- is an object (note, an array counts as an object)
- contains exactly one enumerable property name according to
Object.keys()
(note: symbols don't count as names, even if they are enumerable)
Otherwise, returns false.
let obj = { foo: 'bar' };
assert(isKeyTaggedValue(obj));
let obj = { foo: 'bar', 'herp': 'derp'};
assert(isKeyTaggedValue(obj) === false);
let obj = { [Symbol.iterator]: function*() { yield 'foo'; } };
assert(isKeyTaggedValue(obj) === false);
match
match(obj: Object, cases: Object, otherwise: function|undefined): ?
Similar to a switch statement on the key. Given a key-tagged value and
an object of function, calls the function whose name matches the key.
match()
returns the result of the called function. An optional third
parameter is called when no match can be made.
The provided functions (including the third parameter) are called with the value of the key-tagged value, and the key, respectively.
Throws MatchError
when no callback is matched and no third parameter
is given.
/* Standard match. */
let obj = { sypha: 'belnades' };
let value = match(obj, {
trevor: () => 'vampire killer',
sypha: () => 'magic',
grant: () => 'daggers'
});
assert(value === 'magic');
/* No match found for the object. */
try {
match(obj, {
alucard: (val, key) => 'turning into a flippin bat'
});
} catch (err) {
assert(err instanceof MatchError);
}
/* Default provided to suppress MatchError. */
let value = match(obj,
{
alucard: (val, key) => 'turning into a flippin bat'
},
(val, key) => {
return "No one knows.";
}
);
assert(value === "No one knows.");
unpack
unpack(obj: Object): [key: String, value: ?]
Returns an array of two elements: the key and the value from the key-tagged value.
let [key, value] = unpack({ content: 42 });
assert(key === 'content');
assert(value === 42);
unpackObject
unpackObject(obj: Object): {key: String, value: ?}
Same as unpack, but returns an object with keys, key
and
value
:
let {key, value} = unpackObject({ content: 42 });
assert(key === 'content');
assert(value === 42);