From 04682cbea790fde874a8488777738d28758012a9 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Mon, 7 Jun 2021 14:50:01 -0300 Subject: experimental AggregateError & Thing class hijinks --- src/thing/structures.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/thing/structures.js (limited to 'src/thing/structures.js') diff --git a/src/thing/structures.js b/src/thing/structures.js new file mode 100644 index 0000000..e6b9fd4 --- /dev/null +++ b/src/thing/structures.js @@ -0,0 +1,17 @@ +// Generic structure utilities common across various Thing types. + +export function validateReference(type = '') { + return ref => { + if (typeof ref !== 'string') + throw new TypeError(`Expected a string, got ${ref}`); + + if (type) { + if (!ref.includes(':')) + throw new TypeError(`Expected ref to begin with "${type}:", but no type specified (ref: ${ref})`); + + const typePart = ref.split(':')[0]; + if (typePart !== type) + throw new TypeError(`Expected ref to begin with "${type}:", got "${typePart}:" (ref: ${ref})`); + } + }; +} -- cgit 1.3.0-6-gf8a5